summaryrefslogtreecommitdiff
path: root/test/pleroma/web/activity_pub/mrf/hashtag_policy_test.exs
blob: 32991c9660a80797c0a9f3e6c6c080bf2bbbb8c5 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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.HashtagPolicyTest do
  use Oban.Testing, repo: Pleroma.Repo
  use Pleroma.DataCase

  alias Pleroma.Web.ActivityPub.Transmogrifier
  alias Pleroma.Web.CommonAPI

  import Pleroma.Factory

  test "it sets the sensitive property with relevant hashtags" do
    user = insert(:user)

    {:ok, activity} = CommonAPI.post(user, %{status: "#nsfw hey"})
    {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)

    assert modified["object"]["sensitive"]
  end

  test "it is history-aware" do
    activity = %{
      "type" => "Create",
      "object" => %{
        "content" => "hey",
        "tag" => []
      }
    }

    activity_data =
      activity
      |> put_in(
        ["object", "formerRepresentations"],
        %{
          "type" => "OrderedCollection",
          "orderedItems" => [
            Map.put(
              activity["object"],
              "tag",
              [%{"type" => "Hashtag", "name" => "#nsfw"}]
            )
          ]
        }
      )

    {:ok, modified} =
      Pleroma.Web.ActivityPub.MRF.filter_one(
        Pleroma.Web.ActivityPub.MRF.HashtagPolicy,
        activity_data
      )

    refute modified["object"]["sensitive"]
    assert Enum.at(modified["object"]["formerRepresentations"]["orderedItems"], 0)["sensitive"]
  end

  test "it works with Update" do
    activity = %{
      "type" => "Update",
      "object" => %{
        "content" => "hey",
        "tag" => []
      }
    }

    activity_data =
      activity
      |> put_in(
        ["object", "formerRepresentations"],
        %{
          "type" => "OrderedCollection",
          "orderedItems" => [
            Map.put(
              activity["object"],
              "tag",
              [%{"type" => "Hashtag", "name" => "#nsfw"}]
            )
          ]
        }
      )

    {:ok, modified} =
      Pleroma.Web.ActivityPub.MRF.filter_one(
        Pleroma.Web.ActivityPub.MRF.HashtagPolicy,
        activity_data
      )

    refute modified["object"]["sensitive"]
    assert Enum.at(modified["object"]["formerRepresentations"]["orderedItems"], 0)["sensitive"]
  end

  test "it doesn't sets the sensitive property with irrelevant hashtags" do
    user = insert(:user)

    {:ok, activity} = CommonAPI.post(user, %{status: "#cofe hey"})
    {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)

    refute modified["object"]["sensitive"]
  end
end