2018-12-23 13:11:29 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 15:46:45 -07:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:11:29 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-23 09:16:47 -06:00
|
|
|
defmodule Pleroma.Web.Plugs.EnsureAuthenticatedPlugTest do
|
2018-09-05 09:59:19 -06:00
|
|
|
use Pleroma.Web.ConnCase, async: true
|
|
|
|
|
|
|
|
alias Pleroma.User
|
2020-06-24 04:07:47 -06:00
|
|
|
alias Pleroma.Web.Plugs.EnsureAuthenticatedPlug
|
2018-09-05 09:59:19 -06:00
|
|
|
|
2020-03-09 11:51:44 -06:00
|
|
|
describe "without :if_func / :unless_func options" do
|
|
|
|
test "it halts if user is NOT assigned", %{conn: conn} do
|
|
|
|
conn = EnsureAuthenticatedPlug.call(conn, %{})
|
2018-09-05 09:59:19 -06:00
|
|
|
|
2020-03-09 11:51:44 -06:00
|
|
|
assert conn.status == 403
|
|
|
|
assert conn.halted == true
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it continues if a user is assigned", %{conn: conn} do
|
|
|
|
conn = assign(conn, :user, %User{})
|
|
|
|
ret_conn = EnsureAuthenticatedPlug.call(conn, %{})
|
|
|
|
|
2020-04-21 07:29:19 -06:00
|
|
|
refute ret_conn.halted
|
2020-03-09 11:51:44 -06:00
|
|
|
end
|
2018-09-05 09:59:19 -06:00
|
|
|
end
|
|
|
|
|
2020-05-07 02:14:54 -06:00
|
|
|
test "it halts if user is assigned and MFA enabled", %{conn: conn} do
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, %User{multi_factor_authentication_settings: %{enabled: true}})
|
|
|
|
|> assign(:auth_credentials, %{password: "xd-42"})
|
|
|
|
|> EnsureAuthenticatedPlug.call(%{})
|
|
|
|
|
|
|
|
assert conn.status == 403
|
|
|
|
assert conn.halted == true
|
|
|
|
|
|
|
|
assert conn.resp_body ==
|
|
|
|
"{\"error\":\"Two-factor authentication enabled, you must use a access token.\"}"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it continues if user is assigned and MFA disabled", %{conn: conn} do
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, %User{multi_factor_authentication_settings: %{enabled: false}})
|
|
|
|
|> assign(:auth_credentials, %{password: "xd-42"})
|
|
|
|
|> EnsureAuthenticatedPlug.call(%{})
|
|
|
|
|
|
|
|
refute conn.status == 403
|
|
|
|
refute conn.halted
|
|
|
|
end
|
|
|
|
|
2020-03-09 11:51:44 -06:00
|
|
|
describe "with :if_func / :unless_func options" do
|
|
|
|
setup do
|
|
|
|
%{
|
2020-04-30 09:19:51 -06:00
|
|
|
true_fn: fn _conn -> true end,
|
|
|
|
false_fn: fn _conn -> false end
|
2020-03-09 11:51:44 -06:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it continues if a user is assigned", %{conn: conn, true_fn: true_fn, false_fn: false_fn} do
|
|
|
|
conn = assign(conn, :user, %User{})
|
2020-04-21 07:29:19 -06:00
|
|
|
refute EnsureAuthenticatedPlug.call(conn, if_func: true_fn).halted
|
|
|
|
refute EnsureAuthenticatedPlug.call(conn, if_func: false_fn).halted
|
|
|
|
refute EnsureAuthenticatedPlug.call(conn, unless_func: true_fn).halted
|
|
|
|
refute EnsureAuthenticatedPlug.call(conn, unless_func: false_fn).halted
|
2020-03-09 11:51:44 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it continues if a user is NOT assigned but :if_func evaluates to `false`",
|
|
|
|
%{conn: conn, false_fn: false_fn} do
|
2020-04-21 07:29:19 -06:00
|
|
|
ret_conn = EnsureAuthenticatedPlug.call(conn, if_func: false_fn)
|
|
|
|
refute ret_conn.halted
|
2020-03-09 11:51:44 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it continues if a user is NOT assigned but :unless_func evaluates to `true`",
|
|
|
|
%{conn: conn, true_fn: true_fn} do
|
2020-04-21 07:29:19 -06:00
|
|
|
ret_conn = EnsureAuthenticatedPlug.call(conn, unless_func: true_fn)
|
|
|
|
refute ret_conn.halted
|
2020-03-09 11:51:44 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it halts if a user is NOT assigned and :if_func evaluates to `true`",
|
|
|
|
%{conn: conn, true_fn: true_fn} do
|
|
|
|
conn = EnsureAuthenticatedPlug.call(conn, if_func: true_fn)
|
|
|
|
|
|
|
|
assert conn.status == 403
|
|
|
|
assert conn.halted == true
|
|
|
|
end
|
2018-09-05 09:59:19 -06:00
|
|
|
|
2020-03-09 11:51:44 -06:00
|
|
|
test "it halts if a user is NOT assigned and :unless_func evaluates to `false`",
|
|
|
|
%{conn: conn, false_fn: false_fn} do
|
|
|
|
conn = EnsureAuthenticatedPlug.call(conn, unless_func: false_fn)
|
2018-09-05 09:59:19 -06:00
|
|
|
|
2020-03-09 11:51:44 -06:00
|
|
|
assert conn.status == 403
|
|
|
|
assert conn.halted == true
|
|
|
|
end
|
2018-09-05 09:59:19 -06:00
|
|
|
end
|
|
|
|
end
|