1 defmodule Pleroma.Plugs.AuthenticationPlugTest do
2 use Pleroma.Web.ConnCase, async: true
4 alias Pleroma.Plugs.AuthenticationPlug
6 defp fetch_nil(_name) do
13 password_hash: Comeonin.Pbkdf2.hashpwsalt("guy")
16 defp fetch_user(_name) do
20 defp basic_auth_enc(username, password) do
21 "Basic " <> Base.encode64("#{username}:#{password}")
24 describe "without an authorization header" do
25 test "it halts the application" do
26 conn = build_conn() |> AuthenticationPlug.call(%{})
28 assert conn.status == 403
29 assert conn.halted == true
32 test "it assigns a nil user if the 'optional' option is used" do
33 conn = build_conn() |> AuthenticationPlug.call(%{optional: true})
35 assert %{ user: nil } == conn.assigns
39 describe "with an authorization header for a nonexisting user" do
40 test "it halts the application" do
43 |> AuthenticationPlug.call(%{fetcher: &fetch_nil/1})
45 assert conn.status == 403
46 assert conn.halted == true
49 test "it assigns a nil user if the 'optional' option is used" do
52 |> AuthenticationPlug.call(%{optional: true, fetcher: &fetch_nil/1 })
54 assert %{ user: nil } == conn.assigns
58 describe "with an incorrect authorization header for a enxisting user" do
59 test "it halts the application" do
61 fetcher: &fetch_user/1
64 header = basic_auth_enc("dude", "man")
68 |> put_req_header("authorization", header)
69 |> AuthenticationPlug.call(opts)
71 assert conn.status == 403
72 assert conn.halted == true
75 test "it assigns a nil user if the 'optional' option is used" do
78 fetcher: &fetch_user/1
81 header = basic_auth_enc("dude", "man")
85 |> put_req_header("authorization", header)
86 |> AuthenticationPlug.call(opts)
88 assert %{ user: nil } == conn.assigns
92 describe "with a correct authorization header for an existing user" do
93 test "it assigns the user" do
96 fetcher: &fetch_user/1
99 header = basic_auth_enc("dude", "guy")
103 |> put_req_header("authorization", header)
104 |> AuthenticationPlug.call(opts)
106 assert %{ user: @user } == conn.assigns
107 assert conn.halted == false