tests consistency
[akkoma] / test / pleroma / web / plugs / plug_helper_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.PlugHelperTest do
6 @moduledoc "Tests for the functionality added via `use Pleroma.Web, :plug`"
7
8 alias Pleroma.Plugs.ExpectAuthenticatedCheckPlug
9 alias Pleroma.Plugs.ExpectPublicOrAuthenticatedCheckPlug
10 alias Pleroma.Plugs.PlugHelper
11
12 import Mock
13
14 use Pleroma.Web.ConnCase
15
16 describe "when plug is skipped, " do
17 setup_with_mocks(
18 [
19 {ExpectPublicOrAuthenticatedCheckPlug, [:passthrough], []}
20 ],
21 %{conn: conn}
22 ) do
23 conn = ExpectPublicOrAuthenticatedCheckPlug.skip_plug(conn)
24 %{conn: conn}
25 end
26
27 test "it neither adds plug to called plugs list nor calls `perform/2`, " <>
28 "regardless of :if_func / :unless_func options",
29 %{conn: conn} do
30 for opts <- [%{}, %{if_func: fn _ -> true end}, %{unless_func: fn _ -> false end}] do
31 ret_conn = ExpectPublicOrAuthenticatedCheckPlug.call(conn, opts)
32
33 refute called(ExpectPublicOrAuthenticatedCheckPlug.perform(:_, :_))
34 refute PlugHelper.plug_called?(ret_conn, ExpectPublicOrAuthenticatedCheckPlug)
35 end
36 end
37 end
38
39 describe "when plug is NOT skipped, " do
40 setup_with_mocks([{ExpectAuthenticatedCheckPlug, [:passthrough], []}]) do
41 :ok
42 end
43
44 test "with no pre-run checks, adds plug to called plugs list and calls `perform/2`", %{
45 conn: conn
46 } do
47 ret_conn = ExpectAuthenticatedCheckPlug.call(conn, %{})
48
49 assert called(ExpectAuthenticatedCheckPlug.perform(ret_conn, :_))
50 assert PlugHelper.plug_called?(ret_conn, ExpectAuthenticatedCheckPlug)
51 end
52
53 test "when :if_func option is given, calls the plug only if provided function evals tru-ish",
54 %{conn: conn} do
55 ret_conn = ExpectAuthenticatedCheckPlug.call(conn, %{if_func: fn _ -> false end})
56
57 refute called(ExpectAuthenticatedCheckPlug.perform(:_, :_))
58 refute PlugHelper.plug_called?(ret_conn, ExpectAuthenticatedCheckPlug)
59
60 ret_conn = ExpectAuthenticatedCheckPlug.call(conn, %{if_func: fn _ -> true end})
61
62 assert called(ExpectAuthenticatedCheckPlug.perform(ret_conn, :_))
63 assert PlugHelper.plug_called?(ret_conn, ExpectAuthenticatedCheckPlug)
64 end
65
66 test "if :unless_func option is given, calls the plug only if provided function evals falsy",
67 %{conn: conn} do
68 ret_conn = ExpectAuthenticatedCheckPlug.call(conn, %{unless_func: fn _ -> true end})
69
70 refute called(ExpectAuthenticatedCheckPlug.perform(:_, :_))
71 refute PlugHelper.plug_called?(ret_conn, ExpectAuthenticatedCheckPlug)
72
73 ret_conn = ExpectAuthenticatedCheckPlug.call(conn, %{unless_func: fn _ -> false end})
74
75 assert called(ExpectAuthenticatedCheckPlug.perform(ret_conn, :_))
76 assert PlugHelper.plug_called?(ret_conn, ExpectAuthenticatedCheckPlug)
77 end
78
79 test "allows a plug to be called multiple times (even if it's in called plugs list)", %{
80 conn: conn
81 } do
82 conn = ExpectAuthenticatedCheckPlug.call(conn, %{an_option: :value1})
83 assert called(ExpectAuthenticatedCheckPlug.perform(conn, %{an_option: :value1}))
84
85 assert PlugHelper.plug_called?(conn, ExpectAuthenticatedCheckPlug)
86
87 conn = ExpectAuthenticatedCheckPlug.call(conn, %{an_option: :value2})
88 assert called(ExpectAuthenticatedCheckPlug.perform(conn, %{an_option: :value2}))
89 end
90 end
91 end