Merge branch 'exclude-reblogs-from-admin-api-by-default' into 'develop'
[akkoma] / test / plugs / legacy_authentication_plug_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
7
8 import Pleroma.Factory
9
10 alias Pleroma.Plugs.LegacyAuthenticationPlug
11 alias Pleroma.User
12
13 setup do
14 user =
15 insert(:user,
16 password: "password",
17 password_hash:
18 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
19 )
20
21 %{user: user}
22 end
23
24 test "it does nothing if a user is assigned", %{conn: conn, user: user} do
25 conn =
26 conn
27 |> assign(:auth_credentials, %{username: "dude", password: "password"})
28 |> assign(:auth_user, user)
29 |> assign(:user, %User{})
30
31 ret_conn =
32 conn
33 |> LegacyAuthenticationPlug.call(%{})
34
35 assert ret_conn == conn
36 end
37
38 @tag :skip_on_mac
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 = LegacyAuthenticationPlug.call(conn, %{})
50
51 assert conn.assigns.user.id == user.id
52 end
53
54 @tag :skip_on_mac
55 test "it does nothing if the password is wrong", %{
56 conn: conn,
57 user: user
58 } do
59 conn =
60 conn
61 |> assign(:auth_credentials, %{username: "dude", password: "wrong_password"})
62 |> assign(:auth_user, user)
63
64 ret_conn =
65 conn
66 |> LegacyAuthenticationPlug.call(%{})
67
68 assert conn == ret_conn
69 end
70
71 test "with no credentials or user it does nothing", %{conn: conn} do
72 ret_conn =
73 conn
74 |> LegacyAuthenticationPlug.call(%{})
75
76 assert ret_conn == conn
77 end
78 end