1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do
8 alias Pleroma.Web.ActivityPub.MRF.KeywordPolicy
10 setup do: clear_config(:mrf_keyword)
13 clear_config([:mrf_keyword], %{reject: [], federated_timeline_removal: [], replace: []})
16 describe "rejecting based on keywords" do
17 test "rejects if string matches in content" do
18 clear_config([:mrf_keyword, :reject], ["pun"])
23 "content" => "just a daily reminder that compLAINer is a good pun",
28 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
29 KeywordPolicy.filter(message)
32 test "rejects if string matches in summary" do
33 clear_config([:mrf_keyword, :reject], ["pun"])
38 "summary" => "just a daily reminder that compLAINer is a good pun",
43 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
44 KeywordPolicy.filter(message)
47 test "rejects if regex matches in content" do
48 clear_config([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
51 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
55 "content" => "just a daily reminder that #{content} is a good pun",
60 {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
61 KeywordPolicy.filter(message)
65 test "rejects if regex matches in summary" do
66 clear_config([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
69 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
73 "summary" => "just a daily reminder that #{content} is a good pun",
78 {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
79 KeywordPolicy.filter(message)
83 test "rejects if string matches in history" do
84 clear_config([:mrf_keyword, :reject], ["pun"])
89 "content" => "just a daily reminder that compLAINer is a good",
91 "formerRepresentations" => %{
92 "type" => "OrderedCollection",
95 "content" => "just a daily reminder that compLAINer is a good pun",
103 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
104 KeywordPolicy.filter(message)
107 test "rejects Updates" do
108 clear_config([:mrf_keyword, :reject], ["pun"])
113 "content" => "just a daily reminder that compLAINer is a good",
115 "formerRepresentations" => %{
116 "type" => "OrderedCollection",
119 "content" => "just a daily reminder that compLAINer is a good pun",
127 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
128 KeywordPolicy.filter(message)
132 describe "delisting from ftl based on keywords" do
133 test "delists if string matches in content" do
134 clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"])
137 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
140 "content" => "just a daily reminder that compLAINer is a good pun",
145 {:ok, result} = KeywordPolicy.filter(message)
146 assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
147 refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
150 test "delists if string matches in summary" do
151 clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"])
154 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
157 "summary" => "just a daily reminder that compLAINer is a good pun",
162 {:ok, result} = KeywordPolicy.filter(message)
163 assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
164 refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
167 test "delists if regex matches in content" do
168 clear_config([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
171 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
174 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
176 "content" => "just a daily reminder that #{content} is a good pun",
181 {:ok, result} = KeywordPolicy.filter(message)
183 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
184 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
188 test "delists if regex matches in summary" do
189 clear_config([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
192 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
195 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
197 "summary" => "just a daily reminder that #{content} is a good pun",
202 {:ok, result} = KeywordPolicy.filter(message)
204 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
205 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
209 test "delists if string matches in history" do
210 clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"])
213 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
216 "content" => "just a daily reminder that compLAINer is a good",
218 "formerRepresentations" => %{
221 "content" => "just a daily reminder that compLAINer is a good pun",
229 {:ok, result} = KeywordPolicy.filter(message)
230 assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
231 refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
235 describe "replacing keywords" do
236 test "replaces keyword if string matches in content" do
237 clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
241 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
242 "object" => %{"content" => "ZFS is opensource", "summary" => ""}
245 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
246 assert result == "ZFS is free software"
249 test "replaces keyword if string matches in summary" do
250 clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
254 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
255 "object" => %{"summary" => "ZFS is opensource", "content" => ""}
258 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
259 assert result == "ZFS is free software"
262 test "replaces keyword if regex matches in content" do
263 clear_config([:mrf_keyword, :replace], [
264 {~r/open(-|\s)?source\s?(software)?/, "free software"}
268 Enum.all?(["opensource", "open-source", "open source"], fn content ->
271 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
272 "object" => %{"content" => "ZFS is #{content}", "summary" => ""}
275 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
276 result == "ZFS is free software"
280 test "replaces keyword if regex matches in summary" do
281 clear_config([:mrf_keyword, :replace], [
282 {~r/open(-|\s)?source\s?(software)?/, "free software"}
286 Enum.all?(["opensource", "open-source", "open source"], fn content ->
289 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
290 "object" => %{"summary" => "ZFS is #{content}", "content" => ""}
293 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
294 result == "ZFS is free software"
298 test "replaces keyword if string matches in history" do
299 clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
303 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
305 "content" => "ZFS is opensource",
307 "formerRepresentations" => %{
308 "type" => "OrderedCollection",
310 %{"content" => "ZFS is opensource mew mew", "summary" => ""}
319 "content" => "ZFS is free software",
320 "formerRepresentations" => %{
321 "orderedItems" => [%{"content" => "ZFS is free software mew mew"}]
324 }} = KeywordPolicy.filter(message)
327 test "replaces keyword in Updates" do
328 clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
332 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
334 "content" => "ZFS is opensource",
336 "formerRepresentations" => %{
337 "type" => "OrderedCollection",
339 %{"content" => "ZFS is opensource mew mew", "summary" => ""}
348 "content" => "ZFS is free software",
349 "formerRepresentations" => %{
350 "orderedItems" => [%{"content" => "ZFS is free software mew mew"}]
353 }} = KeywordPolicy.filter(message)