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