move to 2.5.5
[anni] / test / pleroma / web / activity_pub / mrf / hashtag_policy_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.ActivityPub.MRF.HashtagPolicyTest do
6   use Oban.Testing, repo: Pleroma.Repo
7   use Pleroma.DataCase
8
9   alias Pleroma.Web.ActivityPub.Transmogrifier
10   alias Pleroma.Web.CommonAPI
11
12   import Pleroma.Factory
13
14   test "it sets the sensitive property with relevant hashtags" do
15     user = insert(:user)
16
17     {:ok, activity} = CommonAPI.post(user, %{status: "#nsfw hey"})
18     {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
19
20     assert modified["object"]["sensitive"]
21   end
22
23   test "it is history-aware" do
24     activity = %{
25       "type" => "Create",
26       "object" => %{
27         "content" => "hey",
28         "tag" => []
29       }
30     }
31
32     activity_data =
33       activity
34       |> put_in(
35         ["object", "formerRepresentations"],
36         %{
37           "type" => "OrderedCollection",
38           "orderedItems" => [
39             Map.put(
40               activity["object"],
41               "tag",
42               [%{"type" => "Hashtag", "name" => "#nsfw"}]
43             )
44           ]
45         }
46       )
47
48     {:ok, modified} =
49       Pleroma.Web.ActivityPub.MRF.filter_one(
50         Pleroma.Web.ActivityPub.MRF.HashtagPolicy,
51         activity_data
52       )
53
54     refute modified["object"]["sensitive"]
55     assert Enum.at(modified["object"]["formerRepresentations"]["orderedItems"], 0)["sensitive"]
56   end
57
58   test "it works with Update" do
59     activity = %{
60       "type" => "Update",
61       "object" => %{
62         "content" => "hey",
63         "tag" => []
64       }
65     }
66
67     activity_data =
68       activity
69       |> put_in(
70         ["object", "formerRepresentations"],
71         %{
72           "type" => "OrderedCollection",
73           "orderedItems" => [
74             Map.put(
75               activity["object"],
76               "tag",
77               [%{"type" => "Hashtag", "name" => "#nsfw"}]
78             )
79           ]
80         }
81       )
82
83     {:ok, modified} =
84       Pleroma.Web.ActivityPub.MRF.filter_one(
85         Pleroma.Web.ActivityPub.MRF.HashtagPolicy,
86         activity_data
87       )
88
89     refute modified["object"]["sensitive"]
90     assert Enum.at(modified["object"]["formerRepresentations"]["orderedItems"], 0)["sensitive"]
91   end
92
93   test "it doesn't sets the sensitive property with irrelevant hashtags" do
94     user = insert(:user)
95
96     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe hey"})
97     {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
98
99     refute modified["object"]["sensitive"]
100   end
101 end