logo

pleroma

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

mapped_identity_to_signature_plug_test.exs (2021B)


  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.Web.Plugs.MappedSignatureToIdentityPlugTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Web.Plugs.MappedSignatureToIdentityPlug
  7. import Tesla.Mock
  8. import Plug.Conn
  9. setup do
  10. mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
  11. :ok
  12. end
  13. defp set_signature(conn, key_id) do
  14. conn
  15. |> put_req_header("signature", "keyId=\"#{key_id}\"")
  16. |> assign(:valid_signature, true)
  17. end
  18. test "it successfully maps a valid identity with a valid signature" do
  19. conn =
  20. build_conn(:get, "/doesntmattter")
  21. |> set_signature("http://mastodon.example.org/users/admin")
  22. |> MappedSignatureToIdentityPlug.call(%{})
  23. refute is_nil(conn.assigns.user)
  24. end
  25. test "it successfully maps a valid identity with a valid signature with payload" do
  26. conn =
  27. build_conn(:post, "/doesntmattter", %{"actor" => "http://mastodon.example.org/users/admin"})
  28. |> set_signature("http://mastodon.example.org/users/admin")
  29. |> MappedSignatureToIdentityPlug.call(%{})
  30. refute is_nil(conn.assigns.user)
  31. end
  32. test "it considers a mapped identity to be invalid when it mismatches a payload" do
  33. conn =
  34. build_conn(:post, "/doesntmattter", %{"actor" => "http://mastodon.example.org/users/admin"})
  35. |> set_signature("https://niu.moe/users/rye")
  36. |> MappedSignatureToIdentityPlug.call(%{})
  37. assert %{valid_signature: false} == conn.assigns
  38. end
  39. @tag skip: "known breakage; the testsuite presently depends on it"
  40. test "it considers a mapped identity to be invalid when the identity cannot be found" do
  41. conn =
  42. build_conn(:post, "/doesntmattter", %{"actor" => "http://mastodon.example.org/users/admin"})
  43. |> set_signature("http://niu.moe/users/rye")
  44. |> MappedSignatureToIdentityPlug.call(%{})
  45. assert %{valid_signature: false} == conn.assigns
  46. end
  47. end