tests: add legal boilerplate
[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_mock User,
51 reset_password: fn user, %{password: password, password_confirmation: password} ->
52 send(self(), :reset_password)
53 {:ok, user}
54 end do
55 conn
56 |> LegacyAuthenticationPlug.call(%{})
57 end
58
59 assert_received :reset_password
60 assert conn.assigns.user == user
61 end
62
63 test "it does nothing if the password is wrong", %{
64 conn: conn,
65 user: user
66 } do
67 conn =
68 conn
69 |> assign(:auth_credentials, %{username: "dude", password: "wrong_password"})
70 |> assign(:auth_user, user)
71
72 ret_conn =
73 conn
74 |> LegacyAuthenticationPlug.call(%{})
75
76 assert conn == ret_conn
77 end
78
79 test "with no credentials or user it does nothing", %{conn: conn} do
80 ret_conn =
81 conn
82 |> LegacyAuthenticationPlug.call(%{})
83
84 assert ret_conn == conn
85 end
86 end