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
41 |> Plug.Session.call(Plug.Session.init(@session_opts))
43 |> AuthenticationPlug.call(%{})
45 assert conn.status == 403
46 assert conn.halted == true
49 test "it assigns a nil user if the 'optional' option is used" do
51 |> Plug.Session.call(Plug.Session.init(@session_opts))
53 |> AuthenticationPlug.call(%{optional: true})
55 assert %{ user: nil } == conn.assigns
59 describe "with an authorization header for a nonexisting user" do
60 test "it halts the application" do
63 |> Plug.Session.call(Plug.Session.init(@session_opts))
65 |> AuthenticationPlug.call(%{fetcher: &fetch_nil/1})
67 assert conn.status == 403
68 assert conn.halted == true
71 test "it assigns a nil user if the 'optional' option is used" do
74 |> Plug.Session.call(Plug.Session.init(@session_opts))
76 |> AuthenticationPlug.call(%{optional: true, fetcher: &fetch_nil/1 })
78 assert %{ user: nil } == conn.assigns
82 describe "with an incorrect authorization header for a enxisting user" do
83 test "it halts the application" do
85 fetcher: &fetch_user/1
88 header = basic_auth_enc("dude", "man")
92 |> Plug.Session.call(Plug.Session.init(@session_opts))
94 |> put_req_header("authorization", header)
95 |> AuthenticationPlug.call(opts)
97 assert conn.status == 403
98 assert conn.halted == true
101 test "it assigns a nil user if the 'optional' option is used" do
104 fetcher: &fetch_user/1
107 header = basic_auth_enc("dude", "man")
111 |> Plug.Session.call(Plug.Session.init(@session_opts))
113 |> put_req_header("authorization", header)
114 |> AuthenticationPlug.call(opts)
116 assert %{ user: nil } == conn.assigns
120 describe "with a correct authorization header for an existing user" do
121 test "it assigns the user", %{conn: conn} do
124 fetcher: &fetch_user/1
127 header = basic_auth_enc("dude", "guy")
130 |> Plug.Session.call(Plug.Session.init(@session_opts))
132 |> put_req_header("authorization", header)
133 |> AuthenticationPlug.call(opts)
135 assert %{ user: @user } == conn.assigns
136 assert get_session(conn, :user_id) == @user.id
137 assert conn.halted == false
141 describe "with a correct authorization header for an deactiviated user" do
142 test "it halts the appication", %{conn: conn} do
145 fetcher: fn _ -> @deactivated end
148 header = basic_auth_enc("dude", "guy")
151 |> Plug.Session.call(Plug.Session.init(@session_opts))
153 |> put_req_header("authorization", header)
154 |> AuthenticationPlug.call(opts)
156 assert conn.status == 403
157 assert conn.halted == true
161 describe "with a user_id in the session for an existing user" do
162 test "it assigns the user", %{conn: conn} do
165 fetcher: &fetch_user/1
168 header = basic_auth_enc("dude", "THIS IS WRONG")
171 |> Plug.Session.call(Plug.Session.init(@session_opts))
173 |> put_session(:user_id, @user.id)
174 |> put_req_header("authorization", header)
175 |> AuthenticationPlug.call(opts)
177 assert %{ user: @user } == conn.assigns
178 assert get_session(conn, :user_id) == @user.id
179 assert conn.halted == false
183 describe "with an assigned user" do
184 test "it does nothing, returning the incoming conn", %{conn: conn} do
186 |> assign(:user, @user)
188 conn_result = AuthenticationPlug.call(conn, %{})
190 assert conn == conn_result