summaryrefslogtreecommitdiff
path: root/test/pleroma/web/activity_pub/mrf/no_placeholder_text_policy_test.exs
blob: 3533c2bc8c8c09ba7a6855b4a125a7dfe503251d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ActivityPub.MRF.NoPlaceholderTextPolicyTest do
  use Pleroma.DataCase, async: true
  alias Pleroma.Web.ActivityPub.MRF
  alias Pleroma.Web.ActivityPub.MRF.NoPlaceholderTextPolicy

  test "it clears content object" do
    message = %{
      "type" => "Create",
      "object" => %{"content" => ".", "attachment" => "image"}
    }

    assert {:ok, res} = NoPlaceholderTextPolicy.filter(message)
    assert res["object"]["content"] == ""

    message = put_in(message, ["object", "content"], "<p>.</p>")
    assert {:ok, res} = NoPlaceholderTextPolicy.filter(message)
    assert res["object"]["content"] == ""
  end

  test "history-aware" do
    message = %{
      "type" => "Create",
      "object" => %{
        "content" => ".",
        "attachment" => "image",
        "formerRepresentations" => %{
          "orderedItems" => [%{"content" => ".", "attachment" => "image"}]
        }
      }
    }

    assert {:ok, res} = MRF.filter_one(NoPlaceholderTextPolicy, message)

    assert %{
             "content" => "",
             "formerRepresentations" => %{"orderedItems" => [%{"content" => ""}]}
           } = res["object"]
  end

  test "works with Updates" do
    message = %{
      "type" => "Update",
      "object" => %{
        "content" => ".",
        "attachment" => "image",
        "formerRepresentations" => %{
          "orderedItems" => [%{"content" => ".", "attachment" => "image"}]
        }
      }
    }

    assert {:ok, res} = MRF.filter_one(NoPlaceholderTextPolicy, message)

    assert %{
             "content" => "",
             "formerRepresentations" => %{"orderedItems" => [%{"content" => ""}]}
           } = res["object"]
  end

  @messages [
    %{
      "type" => "Create",
      "object" => %{"content" => "test", "attachment" => "image"}
    },
    %{"type" => "Create", "object" => %{"content" => "."}},
    %{"type" => "Create", "object" => %{"content" => "<p>.</p>"}}
  ]
  test "it skips filter" do
    Enum.each(@messages, fn message ->
      assert {:ok, res} = NoPlaceholderTextPolicy.filter(message)
      assert res == message
    end)
  end
end