Fix tagpolicy to also work with Update
[akkoma] / test / pleroma / web / activity_pub / mrf / tag_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 edit_message = %{
57 "actor" => actor.ap_id,
58 "type" => "Update",
59 "object" => %{},
60 "to" => [@public, "f"],
61 "cc" => [@public, "d"]
62 }
63
64 edit_expect_message = %{
65 "actor" => actor.ap_id,
66 "type" => "Update",
67 "object" => %{"to" => ["f", actor.follower_address], "cc" => ["d"]},
68 "to" => ["f", actor.follower_address],
69 "cc" => ["d"]
70 }
71
72 assert TagPolicy.filter(message) == {:ok, except_message}
73 assert TagPolicy.filter(edit_message) == {:ok, edit_expect_message}
74 end
75 end
76
77 describe "mrf_tag:force-unlisted" do
78 test "removes from the federated timeline" do
79 actor = insert(:user, tags: ["mrf_tag:force-unlisted"])
80
81 message = %{
82 "actor" => actor.ap_id,
83 "type" => "Create",
84 "object" => %{},
85 "to" => [@public, "f"],
86 "cc" => [actor.follower_address, "d"]
87 }
88
89 except_message = %{
90 "actor" => actor.ap_id,
91 "type" => "Create",
92 "object" => %{"to" => ["f", actor.follower_address], "cc" => ["d", @public]},
93 "to" => ["f", actor.follower_address],
94 "cc" => ["d", @public]
95 }
96
97 edit_message = %{
98 "actor" => actor.ap_id,
99 "type" => "Update",
100 "object" => %{},
101 "to" => [@public, "f"],
102 "cc" => [actor.follower_address, "d"]
103 }
104
105 edit_expect_message = %{
106 "actor" => actor.ap_id,
107 "type" => "Update",
108 "object" => %{"to" => ["f", actor.follower_address], "cc" => ["d", @public]},
109 "to" => ["f", actor.follower_address],
110 "cc" => ["d", @public]
111 }
112
113 assert TagPolicy.filter(message) == {:ok, except_message}
114 assert TagPolicy.filter(edit_message) == {:ok, edit_expect_message}
115 end
116 end
117
118 describe "mrf_tag:media-strip" do
119 test "removes attachments" do
120 actor = insert(:user, tags: ["mrf_tag:media-strip"])
121
122 message = %{
123 "actor" => actor.ap_id,
124 "type" => "Create",
125 "object" => %{"attachment" => ["file1"]}
126 }
127
128 except_message = %{
129 "actor" => actor.ap_id,
130 "type" => "Create",
131 "object" => %{}
132 }
133
134 edit_message = %{
135 "actor" => actor.ap_id,
136 "type" => "Update",
137 "object" => %{"attachment" => ["file1"]}
138 }
139
140 edit_expect_message = %{
141 "actor" => actor.ap_id,
142 "type" => "Update",
143 "object" => %{}
144 }
145
146 assert TagPolicy.filter(message) == {:ok, except_message}
147 assert TagPolicy.filter(edit_message) == {:ok, edit_expect_message}
148 end
149 end
150
151 describe "mrf_tag:media-force-nsfw" do
152 test "Mark as sensitive on presence of attachments" do
153 actor = insert(:user, tags: ["mrf_tag:media-force-nsfw"])
154
155 message = %{
156 "actor" => actor.ap_id,
157 "type" => "Create",
158 "object" => %{"tag" => ["test"], "attachment" => ["file1"]}
159 }
160
161 except_message = %{
162 "actor" => actor.ap_id,
163 "type" => "Create",
164 "object" => %{"tag" => ["test"], "attachment" => ["file1"], "sensitive" => true}
165 }
166
167 edit_message = %{
168 "actor" => actor.ap_id,
169 "type" => "Update",
170 "object" => %{"tag" => ["test"], "attachment" => ["file1"]}
171 }
172
173 edit_expect_message = %{
174 "actor" => actor.ap_id,
175 "type" => "Update",
176 "object" => %{"tag" => ["test"], "attachment" => ["file1"], "sensitive" => true}
177 }
178
179 assert TagPolicy.filter(message) == {:ok, except_message}
180 assert TagPolicy.filter(edit_message) == {:ok, edit_expect_message}
181 end
182 end
183 end