1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.PlugHelperTest do
6 @moduledoc "Tests for the functionality added via `use Pleroma.Web, :plug`"
8 alias Pleroma.Web.Plugs.ExpectAuthenticatedCheckPlug
9 alias Pleroma.Web.Plugs.ExpectPublicOrAuthenticatedCheckPlug
10 alias Pleroma.Web.Plugs.PlugHelper
14 use Pleroma.Web.ConnCase
16 describe "when plug is skipped, " do
19 {ExpectPublicOrAuthenticatedCheckPlug, [:passthrough], []}
23 conn = ExpectPublicOrAuthenticatedCheckPlug.skip_plug(conn)
27 test "it neither adds plug to called plugs list nor calls `perform/2`, " <>
28 "regardless of :if_func / :unless_func options",
30 for opts <- [%{}, %{if_func: fn _ -> true end}, %{unless_func: fn _ -> false end}] do
31 ret_conn = ExpectPublicOrAuthenticatedCheckPlug.call(conn, opts)
33 refute called(ExpectPublicOrAuthenticatedCheckPlug.perform(:_, :_))
34 refute PlugHelper.plug_called?(ret_conn, ExpectPublicOrAuthenticatedCheckPlug)
39 describe "when plug is NOT skipped, " do
40 setup_with_mocks([{ExpectAuthenticatedCheckPlug, [:passthrough], []}]) do
44 test "with no pre-run checks, adds plug to called plugs list and calls `perform/2`", %{
47 ret_conn = ExpectAuthenticatedCheckPlug.call(conn, %{})
49 assert called(ExpectAuthenticatedCheckPlug.perform(ret_conn, :_))
50 assert PlugHelper.plug_called?(ret_conn, ExpectAuthenticatedCheckPlug)
53 test "when :if_func option is given, calls the plug only if provided function evals tru-ish",
55 ret_conn = ExpectAuthenticatedCheckPlug.call(conn, %{if_func: fn _ -> false end})
57 refute called(ExpectAuthenticatedCheckPlug.perform(:_, :_))
58 refute PlugHelper.plug_called?(ret_conn, ExpectAuthenticatedCheckPlug)
60 ret_conn = ExpectAuthenticatedCheckPlug.call(conn, %{if_func: fn _ -> true end})
62 assert called(ExpectAuthenticatedCheckPlug.perform(ret_conn, :_))
63 assert PlugHelper.plug_called?(ret_conn, ExpectAuthenticatedCheckPlug)
66 test "if :unless_func option is given, calls the plug only if provided function evals falsy",
68 ret_conn = ExpectAuthenticatedCheckPlug.call(conn, %{unless_func: fn _ -> true end})
70 refute called(ExpectAuthenticatedCheckPlug.perform(:_, :_))
71 refute PlugHelper.plug_called?(ret_conn, ExpectAuthenticatedCheckPlug)
73 ret_conn = ExpectAuthenticatedCheckPlug.call(conn, %{unless_func: fn _ -> false end})
75 assert called(ExpectAuthenticatedCheckPlug.perform(ret_conn, :_))
76 assert PlugHelper.plug_called?(ret_conn, ExpectAuthenticatedCheckPlug)
79 test "allows a plug to be called multiple times (even if it's in called plugs list)", %{
82 conn = ExpectAuthenticatedCheckPlug.call(conn, %{an_option: :value1})
83 assert called(ExpectAuthenticatedCheckPlug.perform(conn, %{an_option: :value1}))
85 assert PlugHelper.plug_called?(conn, ExpectAuthenticatedCheckPlug)
87 conn = ExpectAuthenticatedCheckPlug.call(conn, %{an_option: :value2})
88 assert called(ExpectAuthenticatedCheckPlug.perform(conn, %{an_option: :value2}))