tests: add legal boilerplate
[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 end