make bulk user creation from admin works as a transaction
[akkoma] / test / plugs / legacy_authentication_plug_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Plugs.LegacyAuthenticationPlugTest do
6 use Pleroma.Web.ConnCase, async: true
7
8 alias Pleroma.Plugs.LegacyAuthenticationPlug
9 alias Pleroma.User
10
11 import Mock
12
13 setup do
14 # password is "password"
15 user = %User{
16 id: 1,
17 name: "dude",
18 password_hash:
19 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
20 }
21
22 %{user: user}
23 end
24
25 test "it does nothing if a user is assigned", %{conn: conn, user: user} do
26 conn =
27 conn
28 |> assign(:auth_credentials, %{username: "dude", password: "password"})
29 |> assign(:auth_user, user)
30 |> assign(:user, %User{})
31
32 ret_conn =
33 conn
34 |> LegacyAuthenticationPlug.call(%{})
35
36 assert ret_conn == conn
37 end
38
39 test "it authenticates the auth_user if present and password is correct and resets the password",
40 %{
41 conn: conn,
42 user: user
43 } do
44 conn =
45 conn
46 |> assign(:auth_credentials, %{username: "dude", password: "password"})
47 |> assign(:auth_user, user)
48
49 conn =
50 with_mocks([
51 {:crypt, [], [crypt: fn _password, password_hash -> password_hash end]},
52 {User, [],
53 [
54 reset_password: fn user, %{password: password, password_confirmation: password} ->
55 {:ok, user}
56 end
57 ]}
58 ]) do
59 LegacyAuthenticationPlug.call(conn, %{})
60 end
61
62 assert conn.assigns.user == user
63 end
64
65 test "it does nothing if the password is wrong", %{
66 conn: conn,
67 user: user
68 } do
69 conn =
70 conn
71 |> assign(:auth_credentials, %{username: "dude", password: "wrong_password"})
72 |> assign(:auth_user, user)
73
74 ret_conn =
75 conn
76 |> LegacyAuthenticationPlug.call(%{})
77
78 assert conn == ret_conn
79 end
80
81 test "with no credentials or user it does nothing", %{conn: conn} do
82 ret_conn =
83 conn
84 |> LegacyAuthenticationPlug.call(%{})
85
86 assert ret_conn == conn
87 end
88 end