logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

plug_helper_test.exs (3503B)


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