1 defmodule Pleroma.Plugs.AuthenticationPlugTest do
2 use Pleroma.Web.ConnCase, async: true
4 alias Pleroma.Plugs.AuthenticationPlug
7 defp fetch_nil(_name) do
14 password_hash: Comeonin.Pbkdf2.hashpwsalt("guy")
20 password_hash: Comeonin.Pbkdf2.hashpwsalt("guy"),
21 info: %{"deactivated" => true}
27 signing_salt: "cooldude"
30 defp fetch_user(_name) do
34 defp basic_auth_enc(username, password) do
35 "Basic " <> Base.encode64("#{username}:#{password}")
38 describe "without an authorization header" do
39 test "it halts the application" do
42 |> Plug.Session.call(Plug.Session.init(@session_opts))
44 |> AuthenticationPlug.call(%{})
46 assert conn.status == 403
47 assert conn.halted == true
50 test "it assigns a nil user if the 'optional' option is used" do
53 |> Plug.Session.call(Plug.Session.init(@session_opts))
55 |> AuthenticationPlug.call(%{optional: true})
57 assert %{user: nil} == conn.assigns
61 describe "with an authorization header for a nonexisting user" do
62 test "it halts the application" do
65 |> Plug.Session.call(Plug.Session.init(@session_opts))
67 |> AuthenticationPlug.call(%{fetcher: &fetch_nil/1})
69 assert conn.status == 403
70 assert conn.halted == true
73 test "it assigns a nil user if the 'optional' option is used" do
76 |> Plug.Session.call(Plug.Session.init(@session_opts))
78 |> AuthenticationPlug.call(%{optional: true, fetcher: &fetch_nil/1})
80 assert %{user: nil} == conn.assigns
84 describe "with an incorrect authorization header for a enxisting user" do
85 test "it halts the application" do
87 fetcher: &fetch_user/1
90 header = basic_auth_enc("dude", "man")
94 |> Plug.Session.call(Plug.Session.init(@session_opts))
96 |> put_req_header("authorization", header)
97 |> AuthenticationPlug.call(opts)
99 assert conn.status == 403
100 assert conn.halted == true
103 test "it assigns a nil user if the 'optional' option is used" do
106 fetcher: &fetch_user/1
109 header = basic_auth_enc("dude", "man")
113 |> Plug.Session.call(Plug.Session.init(@session_opts))
115 |> put_req_header("authorization", header)
116 |> AuthenticationPlug.call(opts)
118 assert %{user: nil} == conn.assigns
122 describe "with a correct authorization header for an existing user" do
123 test "it assigns the user", %{conn: conn} do
126 fetcher: &fetch_user/1
129 header = basic_auth_enc("dude", "guy")
133 |> Plug.Session.call(Plug.Session.init(@session_opts))
135 |> put_req_header("authorization", header)
136 |> AuthenticationPlug.call(opts)
138 assert %{user: @user} == conn.assigns
139 assert get_session(conn, :user_id) == @user.id
140 assert conn.halted == false
144 describe "with a correct authorization header for an deactiviated user" do
145 test "it halts the appication", %{conn: conn} do
148 fetcher: fn _ -> @deactivated end
151 header = basic_auth_enc("dude", "guy")
155 |> Plug.Session.call(Plug.Session.init(@session_opts))
157 |> put_req_header("authorization", header)
158 |> AuthenticationPlug.call(opts)
160 assert conn.status == 403
161 assert conn.halted == true
165 describe "with a user_id in the session for an existing user" do
166 test "it assigns the user", %{conn: conn} do
169 fetcher: &fetch_user/1
172 header = basic_auth_enc("dude", "THIS IS WRONG")
176 |> Plug.Session.call(Plug.Session.init(@session_opts))
178 |> put_session(:user_id, @user.id)
179 |> put_req_header("authorization", header)
180 |> AuthenticationPlug.call(opts)
182 assert %{user: @user} == conn.assigns
183 assert get_session(conn, :user_id) == @user.id
184 assert conn.halted == false
188 describe "with an assigned user" do
189 test "it does nothing, returning the incoming conn", %{conn: conn} do
192 |> assign(:user, @user)
194 conn_result = AuthenticationPlug.call(conn, %{})
196 assert conn == conn_result