b55e746f8d8b82819b35bf23b9decee3d232e892
[akkoma] / test / plugs / 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.AuthenticationPlugTest do
6 use Pleroma.Web.ConnCase, async: true
7
8 alias Pleroma.Plugs.AuthenticationPlug
9 alias Pleroma.User
10
11 setup %{conn: conn} do
12 user = %User{
13 id: 1,
14 name: "dude",
15 password_hash: Comeonin.Pbkdf2.hashpwsalt("guy")
16 }
17
18 conn =
19 conn
20 |> assign(:auth_user, user)
21
22 %{user: user, conn: conn}
23 end
24
25 test "it does nothing if a user is assigned", %{conn: conn} do
26 conn =
27 conn
28 |> assign(:user, %User{})
29
30 ret_conn =
31 conn
32 |> AuthenticationPlug.call(%{})
33
34 assert ret_conn == conn
35 end
36
37 test "with a correct password in the credentials, it assigns the auth_user", %{conn: conn} do
38 conn =
39 conn
40 |> assign(:auth_credentials, %{password: "guy"})
41 |> AuthenticationPlug.call(%{})
42
43 assert conn.assigns.user == conn.assigns.auth_user
44 end
45
46 test "with a wrong password in the credentials, it does nothing", %{conn: conn} do
47 conn =
48 conn
49 |> assign(:auth_credentials, %{password: "wrong"})
50
51 ret_conn =
52 conn
53 |> AuthenticationPlug.call(%{})
54
55 assert conn == ret_conn
56 end
57
58 describe "checkpw/2" do
59 test "check pbkdf2 hash" do
60 hash =
61 "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
62
63 assert AuthenticationPlug.checkpw("test-password", hash)
64 refute AuthenticationPlug.checkpw("test-password1", hash)
65 end
66
67 test "check sha512-crypt hash" do
68 hash =
69 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
70
71 assert AuthenticationPlug.checkpw("password", hash)
72 refute AuthenticationPlug.checkpw("password1", hash)
73 end
74
75 test "it returns false when hash invalid" do
76 hash =
77 "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
78
79 refute Pleroma.Plugs.AuthenticationPlug.checkpw("password", hash)
80 end
81 end
82 end