logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

admin_secret_authentication_plug_test.exs (2057B)


  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. defmodule Pleroma.Plugs.AdminSecretAuthenticationPlugTest do
  5. use Pleroma.Web.ConnCase
  6. import Mock
  7. import Pleroma.Factory
  8. alias Pleroma.Plugs.AdminSecretAuthenticationPlug
  9. alias Pleroma.Plugs.OAuthScopesPlug
  10. alias Pleroma.Plugs.PlugHelper
  11. alias Pleroma.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. Pleroma.Config.put(: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 PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
  39. end
  40. test "with `x-admin-token` HTTP header", %{conn: conn} do
  41. Pleroma.Config.put(:admin_token, "☕️")
  42. conn =
  43. conn
  44. |> put_req_header("x-admin-token", "🥛")
  45. |> AdminSecretAuthenticationPlug.call(%{})
  46. refute conn.assigns[:user]
  47. assert called(RateLimiter.call(conn, name: :authentication))
  48. conn =
  49. conn
  50. |> put_req_header("x-admin-token", "☕️")
  51. |> AdminSecretAuthenticationPlug.call(%{})
  52. assert conn.assigns[:user].is_admin
  53. assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
  54. end
  55. end
  56. end