a0db8df5471ce16493a58f83b62721110925a174
[anni] / test / pleroma / web / activity_pub / mrf / tag_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.TagPolicyTest do
6   use Pleroma.DataCase, async: true
7   import Pleroma.Factory
8
9   alias Pleroma.Web.ActivityPub.MRF.TagPolicy
10   @public "https://www.w3.org/ns/activitystreams#Public"
11
12   describe "mrf_tag:disable-any-subscription" do
13     test "rejects message" do
14       actor = insert(:user, tags: ["mrf_tag:disable-any-subscription"])
15       message = %{"object" => actor.ap_id, "type" => "Follow", "actor" => actor.ap_id}
16       assert {:reject, _} = TagPolicy.filter(message)
17     end
18   end
19
20   describe "mrf_tag:disable-remote-subscription" do
21     test "rejects non-local follow requests" do
22       actor = insert(:user, tags: ["mrf_tag:disable-remote-subscription"])
23       follower = insert(:user, tags: ["mrf_tag:disable-remote-subscription"], local: false)
24       message = %{"object" => actor.ap_id, "type" => "Follow", "actor" => follower.ap_id}
25       assert {:reject, _} = TagPolicy.filter(message)
26     end
27
28     test "allows non-local follow requests" do
29       actor = insert(:user, tags: ["mrf_tag:disable-remote-subscription"])
30       follower = insert(:user, tags: ["mrf_tag:disable-remote-subscription"], local: true)
31       message = %{"object" => actor.ap_id, "type" => "Follow", "actor" => follower.ap_id}
32       assert {:ok, _message} = TagPolicy.filter(message)
33     end
34   end
35
36   describe "mrf_tag:sandbox" do
37     test "removes from public timelines" do
38       actor = insert(:user, tags: ["mrf_tag:sandbox"])
39
40       message = %{
41         "actor" => actor.ap_id,
42         "type" => "Create",
43         "object" => %{},
44         "to" => [@public, "f"],
45         "cc" => [@public, "d"]
46       }
47
48       except_message = %{
49         "actor" => actor.ap_id,
50         "type" => "Create",
51         "object" => %{"to" => ["f", actor.follower_address], "cc" => ["d"]},
52         "to" => ["f", actor.follower_address],
53         "cc" => ["d"]
54       }
55
56       assert TagPolicy.filter(message) == {:ok, except_message}
57     end
58   end
59
60   describe "mrf_tag:force-unlisted" do
61     test "removes from the federated timeline" do
62       actor = insert(:user, tags: ["mrf_tag:force-unlisted"])
63
64       message = %{
65         "actor" => actor.ap_id,
66         "type" => "Create",
67         "object" => %{},
68         "to" => [@public, "f"],
69         "cc" => [actor.follower_address, "d"]
70       }
71
72       except_message = %{
73         "actor" => actor.ap_id,
74         "type" => "Create",
75         "object" => %{"to" => ["f", actor.follower_address], "cc" => ["d", @public]},
76         "to" => ["f", actor.follower_address],
77         "cc" => ["d", @public]
78       }
79
80       assert TagPolicy.filter(message) == {:ok, except_message}
81     end
82   end
83
84   describe "mrf_tag:media-strip" do
85     test "removes attachments" do
86       actor = insert(:user, tags: ["mrf_tag:media-strip"])
87
88       message = %{
89         "actor" => actor.ap_id,
90         "type" => "Create",
91         "object" => %{"attachment" => ["file1"]}
92       }
93
94       except_message = %{
95         "actor" => actor.ap_id,
96         "type" => "Create",
97         "object" => %{}
98       }
99
100       assert TagPolicy.filter(message) == {:ok, except_message}
101     end
102
103     test "removes attachments in Updates" do
104       actor = insert(:user, tags: ["mrf_tag:media-strip"])
105
106       message = %{
107         "actor" => actor.ap_id,
108         "type" => "Update",
109         "object" => %{"attachment" => ["file1"]}
110       }
111
112       except_message = %{
113         "actor" => actor.ap_id,
114         "type" => "Update",
115         "object" => %{}
116       }
117
118       assert TagPolicy.filter(message) == {:ok, except_message}
119     end
120   end
121
122   describe "mrf_tag:media-force-nsfw" do
123     test "Mark as sensitive on presence of attachments" do
124       actor = insert(:user, tags: ["mrf_tag:media-force-nsfw"])
125
126       message = %{
127         "actor" => actor.ap_id,
128         "type" => "Create",
129         "object" => %{"tag" => ["test"], "attachment" => ["file1"]}
130       }
131
132       except_message = %{
133         "actor" => actor.ap_id,
134         "type" => "Create",
135         "object" => %{"tag" => ["test"], "attachment" => ["file1"], "sensitive" => true}
136       }
137
138       assert TagPolicy.filter(message) == {:ok, except_message}
139     end
140
141     test "Mark as sensitive on presence of attachments in Updates" do
142       actor = insert(:user, tags: ["mrf_tag:media-force-nsfw"])
143
144       message = %{
145         "actor" => actor.ap_id,
146         "type" => "Update",
147         "object" => %{"tag" => ["test"], "attachment" => ["file1"]}
148       }
149
150       except_message = %{
151         "actor" => actor.ap_id,
152         "type" => "Update",
153         "object" => %{"tag" => ["test"], "attachment" => ["file1"], "sensitive" => true}
154       }
155
156       assert TagPolicy.filter(message) == {:ok, except_message}
157     end
158   end
159 end