logo

pleroma

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

admin_secret_authentication_plug_test.exs (2147B)


  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.AdminSecretAuthenticationPlugTest do
  5. use Pleroma.Web.ConnCase
  6. import Mock
  7. import Pleroma.Factory
  8. alias Pleroma.Web.Plugs.AdminSecretAuthenticationPlug
  9. alias Pleroma.Web.Plugs.OAuthScopesPlug
  10. alias Pleroma.Web.Plugs.PlugHelper
  11. alias Pleroma.Web.Plugs.RateLimiter
  12. test "does nothing if a user is assigned", %{conn: conn} do
  13. user = insert(:user)
  14. conn =
  15. conn
  16. |> assign(:user, user)
  17. ret_conn =
  18. conn
  19. |> AdminSecretAuthenticationPlug.call(%{})
  20. assert conn == ret_conn
  21. end
  22. describe "when secret set it assigns an admin user" do
  23. setup do: clear_config([:admin_token])
  24. setup_with_mocks([{RateLimiter, [:passthrough], []}]) do
  25. :ok
  26. end
  27. test "with `admin_token` query parameter", %{conn: conn} do
  28. clear_config(:admin_token, "password123")
  29. conn =
  30. %{conn | params: %{"admin_token" => "wrong_password"}}
  31. |> AdminSecretAuthenticationPlug.call(%{})
  32. refute conn.assigns[:user]
  33. assert called(RateLimiter.call(conn, name: :authentication))
  34. conn =
  35. %{conn | params: %{"admin_token" => "password123"}}
  36. |> AdminSecretAuthenticationPlug.call(%{})
  37. assert conn.assigns[:user].is_admin
  38. assert conn.assigns[:token] == nil
  39. assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
  40. end
  41. test "with `x-admin-token` HTTP header", %{conn: conn} do
  42. clear_config(:admin_token, "☕️")
  43. conn =
  44. conn
  45. |> put_req_header("x-admin-token", "🥛")
  46. |> AdminSecretAuthenticationPlug.call(%{})
  47. refute conn.assigns[:user]
  48. assert called(RateLimiter.call(conn, name: :authentication))
  49. conn =
  50. conn
  51. |> put_req_header("x-admin-token", "☕️")
  52. |> AdminSecretAuthenticationPlug.call(%{})
  53. assert conn.assigns[:user].is_admin
  54. assert conn.assigns[:token] == nil
  55. assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
  56. end
  57. end
  58. end