Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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 import ExUnit.CaptureLog
12 import Mock
13
14 setup %{conn: conn} do
15 user = %User{
16 id: 1,
17 name: "dude",
18 password_hash: Comeonin.Pbkdf2.hashpwsalt("guy")
19 }
20
21 conn =
22 conn
23 |> assign(:auth_user, user)
24
25 %{user: user, conn: conn}
26 end
27
28 test "it does nothing if a user is assigned", %{conn: conn} do
29 conn =
30 conn
31 |> assign(:user, %User{})
32
33 ret_conn =
34 conn
35 |> AuthenticationPlug.call(%{})
36
37 assert ret_conn == conn
38 end
39
40 test "with a correct password in the credentials, it assigns the auth_user", %{conn: conn} do
41 conn =
42 conn
43 |> assign(:auth_credentials, %{password: "guy"})
44 |> AuthenticationPlug.call(%{})
45
46 assert conn.assigns.user == conn.assigns.auth_user
47 end
48
49 test "with a wrong password in the credentials, it does nothing", %{conn: conn} do
50 conn =
51 conn
52 |> assign(:auth_credentials, %{password: "wrong"})
53
54 ret_conn =
55 conn
56 |> AuthenticationPlug.call(%{})
57
58 assert conn == ret_conn
59 end
60
61 describe "checkpw/2" do
62 test "check pbkdf2 hash" do
63 hash =
64 "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
65
66 assert AuthenticationPlug.checkpw("test-password", hash)
67 refute AuthenticationPlug.checkpw("test-password1", hash)
68 end
69
70 test "check sha512-crypt hash" do
71 hash =
72 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
73
74 with_mock :crypt, crypt: fn _password, password_hash -> password_hash end do
75 assert AuthenticationPlug.checkpw("password", hash)
76 end
77 end
78
79 test "it returns false when hash invalid" do
80 hash =
81 "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
82
83 assert capture_log(fn ->
84 refute Pleroma.Plugs.AuthenticationPlug.checkpw("password", hash)
85 end) =~ "[error] Password hash not recognized"
86 end
87 end
88 end