Merge remote-tracking branch 'remotes/origin/develop' into ostatus-controller-no...
[akkoma] / test / pleroma / web / 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.Web.Plugs.LegacyAuthenticationPlugTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.User
11 alias Pleroma.Web.Plugs.LegacyAuthenticationPlug
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
13 alias Pleroma.Web.Plugs.PlugHelper
14
15 setup do
16 user =
17 insert(:user,
18 password: "password",
19 password_hash:
20 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
21 )
22
23 %{user: user}
24 end
25
26 test "it does nothing if a user is assigned", %{conn: conn, user: user} do
27 conn =
28 conn
29 |> assign(:auth_credentials, %{username: "dude", password: "password"})
30 |> assign(:auth_user, user)
31 |> assign(:user, %User{})
32
33 ret_conn =
34 conn
35 |> LegacyAuthenticationPlug.call(%{})
36
37 assert ret_conn == conn
38 end
39
40 @tag :skip_on_mac
41 test "if `auth_user` is present and password is correct, " <>
42 "it authenticates the user, resets the password, marks OAuthScopesPlug as skipped",
43 %{
44 conn: conn,
45 user: user
46 } do
47 conn =
48 conn
49 |> assign(:auth_credentials, %{username: "dude", password: "password"})
50 |> assign(:auth_user, user)
51
52 conn = LegacyAuthenticationPlug.call(conn, %{})
53
54 assert conn.assigns.user.id == user.id
55 assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
56 end
57
58 @tag :skip_on_mac
59 test "it does nothing if the password is wrong", %{
60 conn: conn,
61 user: user
62 } do
63 conn =
64 conn
65 |> assign(:auth_credentials, %{username: "dude", password: "wrong_password"})
66 |> assign(:auth_user, user)
67
68 ret_conn =
69 conn
70 |> LegacyAuthenticationPlug.call(%{})
71
72 assert conn == ret_conn
73 end
74
75 test "with no credentials or user it does nothing", %{conn: conn} do
76 ret_conn =
77 conn
78 |> LegacyAuthenticationPlug.call(%{})
79
80 assert ret_conn == conn
81 end
82 end