1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do
9 alias Pleroma.Web.ActivityPub.MRF.SimplePolicy
12 orig = Config.get!(:mrf_simple)
14 Config.put(:mrf_simple,
17 federated_timeline_removal: [],
26 Config.put(:mrf_simple, orig)
30 describe "when :media_removal" do
32 Config.put([:mrf_simple, :media_removal], [])
33 media_message = build_media_message()
34 local_message = build_local_message()
36 assert SimplePolicy.filter(media_message) == {:ok, media_message}
37 assert SimplePolicy.filter(local_message) == {:ok, local_message}
40 test "has a matching host" do
41 Config.put([:mrf_simple, :media_removal], ["remote.instance"])
42 media_message = build_media_message()
43 local_message = build_local_message()
45 assert SimplePolicy.filter(media_message) ==
48 |> Map.put("object", Map.delete(media_message["object"], "attachment"))}
50 assert SimplePolicy.filter(local_message) == {:ok, local_message}
54 describe "when :media_nsfw" do
56 Config.put([:mrf_simple, :media_nsfw], [])
57 media_message = build_media_message()
58 local_message = build_local_message()
60 assert SimplePolicy.filter(media_message) == {:ok, media_message}
61 assert SimplePolicy.filter(local_message) == {:ok, local_message}
64 test "has a matching host" do
65 Config.put([:mrf_simple, :media_nsfw], ["remote.instance"])
66 media_message = build_media_message()
67 local_message = build_local_message()
69 assert SimplePolicy.filter(media_message) ==
72 |> put_in(["object", "tag"], ["foo", "nsfw"])
73 |> put_in(["object", "sensitive"], true)}
75 assert SimplePolicy.filter(local_message) == {:ok, local_message}
79 defp build_media_message do
81 "actor" => "https://remote.instance/users/bob",
84 "attachment" => [%{}],
91 describe "when :report_removal" do
93 Config.put([:mrf_simple, :report_removal], [])
94 report_message = build_report_message()
95 local_message = build_local_message()
97 assert SimplePolicy.filter(report_message) == {:ok, report_message}
98 assert SimplePolicy.filter(local_message) == {:ok, local_message}
101 test "has a matching host" do
102 Config.put([:mrf_simple, :report_removal], ["remote.instance"])
103 report_message = build_report_message()
104 local_message = build_local_message()
106 assert SimplePolicy.filter(report_message) == {:reject, nil}
107 assert SimplePolicy.filter(local_message) == {:ok, local_message}
111 defp build_report_message do
113 "actor" => "https://remote.instance/users/bob",
118 describe "when :federated_timeline_removal" do
120 Config.put([:mrf_simple, :federated_timeline_removal], [])
121 {_, ftl_message} = build_ftl_actor_and_message()
122 local_message = build_local_message()
124 assert SimplePolicy.filter(ftl_message) == {:ok, ftl_message}
125 assert SimplePolicy.filter(local_message) == {:ok, local_message}
128 test "has a matching host" do
129 {actor, ftl_message} = build_ftl_actor_and_message()
131 ftl_message_actor_host =
133 |> Map.fetch!("actor")
137 Config.put([:mrf_simple, :federated_timeline_removal], [ftl_message_actor_host])
138 local_message = build_local_message()
140 assert {:ok, ftl_message} = SimplePolicy.filter(ftl_message)
141 assert actor.follower_address in ftl_message["to"]
142 refute actor.follower_address in ftl_message["cc"]
143 refute "https://www.w3.org/ns/activitystreams#Public" in ftl_message["to"]
144 assert "https://www.w3.org/ns/activitystreams#Public" in ftl_message["cc"]
146 assert SimplePolicy.filter(local_message) == {:ok, local_message}
150 defp build_ftl_actor_and_message do
151 actor = insert(:user)
155 "actor" => actor.ap_id,
156 "to" => ["https://www.w3.org/ns/activitystreams#Public", "http://foo.bar/baz"],
157 "cc" => [actor.follower_address, "http://foo.bar/qux"]
161 describe "when :reject" do
163 Config.put([:mrf_simple, :reject], [])
165 remote_message = build_remote_message()
167 assert SimplePolicy.filter(remote_message) == {:ok, remote_message}
170 test "has a matching host" do
171 Config.put([:mrf_simple, :reject], ["remote.instance"])
173 remote_message = build_remote_message()
175 assert SimplePolicy.filter(remote_message) == {:reject, nil}
179 describe "when :accept" do
181 Config.put([:mrf_simple, :accept], [])
183 local_message = build_local_message()
184 remote_message = build_remote_message()
186 assert SimplePolicy.filter(local_message) == {:ok, local_message}
187 assert SimplePolicy.filter(remote_message) == {:ok, remote_message}
190 test "is not empty but it doesn't have a matching host" do
191 Config.put([:mrf_simple, :accept], ["non.matching.remote"])
193 local_message = build_local_message()
194 remote_message = build_remote_message()
196 assert SimplePolicy.filter(local_message) == {:ok, local_message}
197 assert SimplePolicy.filter(remote_message) == {:reject, nil}
200 test "has a matching host" do
201 Config.put([:mrf_simple, :accept], ["remote.instance"])
203 local_message = build_local_message()
204 remote_message = build_remote_message()
206 assert SimplePolicy.filter(local_message) == {:ok, local_message}
207 assert SimplePolicy.filter(remote_message) == {:ok, remote_message}
211 describe "when :avatar_removal" do
213 Config.put([:mrf_simple, :avatar_removal], [])
215 remote_user = build_remote_user()
217 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
220 test "is not empty but it doesn't have a matching host" do
221 Config.put([:mrf_simple, :avatar_removal], ["non.matching.remote"])
223 remote_user = build_remote_user()
225 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
228 test "has a matching host" do
229 Config.put([:mrf_simple, :avatar_removal], ["remote.instance"])
231 remote_user = build_remote_user()
232 {:ok, filtered} = SimplePolicy.filter(remote_user)
234 refute filtered["icon"]
238 describe "when :banner_removal" do
240 Config.put([:mrf_simple, :banner_removal], [])
242 remote_user = build_remote_user()
244 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
247 test "is not empty but it doesn't have a matching host" do
248 Config.put([:mrf_simple, :banner_removal], ["non.matching.remote"])
250 remote_user = build_remote_user()
252 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
255 test "has a matching host" do
256 Config.put([:mrf_simple, :banner_removal], ["remote.instance"])
258 remote_user = build_remote_user()
259 {:ok, filtered} = SimplePolicy.filter(remote_user)
261 refute filtered["image"]
265 defp build_local_message do
267 "actor" => "#{Pleroma.Web.base_url()}/users/alice",
273 defp build_remote_message do
274 %{"actor" => "https://remote.instance/users/bob"}
277 defp build_remote_user do
279 "id" => "https://remote.instance/users/bob",
281 "url" => "http://example.com/image.jpg",
285 "url" => "http://example.com/image.jpg",