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}
28 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
34 signing_salt: "cooldude"
37 defp fetch_user(_name) do
41 defp basic_auth_enc(username, password) do
42 "Basic " <> Base.encode64("#{username}:#{password}")
45 describe "without an authorization header" do
46 test "it halts the application" do
49 |> Plug.Session.call(Plug.Session.init(@session_opts))
51 |> AuthenticationPlug.call(%{})
53 assert conn.status == 403
54 assert conn.halted == true
57 test "it assigns a nil user if the 'optional' option is used" do
60 |> Plug.Session.call(Plug.Session.init(@session_opts))
62 |> AuthenticationPlug.call(%{optional: true})
64 assert %{user: nil} == conn.assigns
68 describe "with an authorization header for a nonexisting user" do
69 test "it halts the application" do
72 |> Plug.Session.call(Plug.Session.init(@session_opts))
74 |> AuthenticationPlug.call(%{fetcher: &fetch_nil/1})
76 assert conn.status == 403
77 assert conn.halted == true
80 test "it assigns a nil user if the 'optional' option is used" do
83 |> Plug.Session.call(Plug.Session.init(@session_opts))
85 |> AuthenticationPlug.call(%{optional: true, fetcher: &fetch_nil/1})
87 assert %{user: nil} == conn.assigns
91 describe "with an incorrect authorization header for a enxisting user" do
92 test "it halts the application" do
94 fetcher: &fetch_user/1
97 header = basic_auth_enc("dude", "man")
101 |> Plug.Session.call(Plug.Session.init(@session_opts))
103 |> put_req_header("authorization", header)
104 |> AuthenticationPlug.call(opts)
106 assert conn.status == 403
107 assert conn.halted == true
110 test "it assigns a nil user if the 'optional' option is used" do
113 fetcher: &fetch_user/1
116 header = basic_auth_enc("dude", "man")
120 |> Plug.Session.call(Plug.Session.init(@session_opts))
122 |> put_req_header("authorization", header)
123 |> AuthenticationPlug.call(opts)
125 assert %{user: nil} == conn.assigns
129 describe "with a correct authorization header for an existing user" do
130 test "it assigns the user", %{conn: conn} do
133 fetcher: &fetch_user/1
136 header = basic_auth_enc("dude", "guy")
140 |> Plug.Session.call(Plug.Session.init(@session_opts))
142 |> put_req_header("authorization", header)
143 |> AuthenticationPlug.call(opts)
145 assert %{user: @user} == conn.assigns
146 assert get_session(conn, :user_id) == @user.id
147 assert conn.halted == false
150 test "it assigns legacy user", %{conn: conn} do
153 fetcher: fn _ -> {:ok, @legacy} end,
154 update_legacy_password: false
157 header = basic_auth_enc("dude", "password")
161 |> Plug.Session.call(Plug.Session.init(@session_opts))
163 |> put_req_header("authorization", header)
164 |> AuthenticationPlug.call(opts)
166 assert %{user: @legacy} == conn.assigns
167 assert get_session(conn, :user_id) == @legacy.id
168 assert conn.halted == false
172 describe "with a correct authorization header for an deactiviated user" do
173 test "it halts the appication", %{conn: conn} do
176 fetcher: fn _ -> @deactivated end
179 header = basic_auth_enc("dude", "guy")
183 |> Plug.Session.call(Plug.Session.init(@session_opts))
185 |> put_req_header("authorization", header)
186 |> AuthenticationPlug.call(opts)
188 assert conn.status == 403
189 assert conn.halted == true
193 describe "with a user_id in the session for an existing user" do
194 test "it assigns the user", %{conn: conn} do
197 fetcher: &fetch_user/1
200 header = basic_auth_enc("dude", "THIS IS WRONG")
204 |> Plug.Session.call(Plug.Session.init(@session_opts))
206 |> put_session(:user_id, @user.id)
207 |> put_req_header("authorization", header)
208 |> AuthenticationPlug.call(opts)
210 assert %{user: @user} == conn.assigns
211 assert get_session(conn, :user_id) == @user.id
212 assert conn.halted == false
216 describe "with an assigned user" do
217 test "it does nothing, returning the incoming conn", %{conn: conn} do
220 |> assign(:user, @user)
222 conn_result = AuthenticationPlug.call(conn, %{})
224 assert conn == conn_result