move to 2.5.5
[anni] / test / pleroma / web / activity_pub / mrf / keyword_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.KeywordPolicyTest do
6   use Pleroma.DataCase
7
8   alias Pleroma.Web.ActivityPub.MRF.KeywordPolicy
9
10   setup do: clear_config(:mrf_keyword)
11
12   setup do
13     clear_config([:mrf_keyword], %{reject: [], federated_timeline_removal: [], replace: []})
14   end
15
16   describe "rejecting based on keywords" do
17     test "rejects if string matches in content" do
18       clear_config([:mrf_keyword, :reject], ["pun"])
19
20       message = %{
21         "type" => "Create",
22         "object" => %{
23           "content" => "just a daily reminder that compLAINer is a good pun",
24           "summary" => ""
25         }
26       }
27
28       assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
29                KeywordPolicy.filter(message)
30     end
31
32     test "rejects if string matches in summary" do
33       clear_config([:mrf_keyword, :reject], ["pun"])
34
35       message = %{
36         "type" => "Create",
37         "object" => %{
38           "summary" => "just a daily reminder that compLAINer is a good pun",
39           "content" => ""
40         }
41       }
42
43       assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
44                KeywordPolicy.filter(message)
45     end
46
47     test "rejects if regex matches in content" do
48       clear_config([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
49
50       assert true ==
51                Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
52                  message = %{
53                    "type" => "Create",
54                    "object" => %{
55                      "content" => "just a daily reminder that #{content} is a good pun",
56                      "summary" => ""
57                    }
58                  }
59
60                  {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
61                    KeywordPolicy.filter(message)
62                end)
63     end
64
65     test "rejects if regex matches in summary" do
66       clear_config([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
67
68       assert true ==
69                Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
70                  message = %{
71                    "type" => "Create",
72                    "object" => %{
73                      "summary" => "just a daily reminder that #{content} is a good pun",
74                      "content" => ""
75                    }
76                  }
77
78                  {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
79                    KeywordPolicy.filter(message)
80                end)
81     end
82
83     test "rejects if string matches in history" do
84       clear_config([:mrf_keyword, :reject], ["pun"])
85
86       message = %{
87         "type" => "Create",
88         "object" => %{
89           "content" => "just a daily reminder that compLAINer is a good",
90           "summary" => "",
91           "formerRepresentations" => %{
92             "type" => "OrderedCollection",
93             "orderedItems" => [
94               %{
95                 "content" => "just a daily reminder that compLAINer is a good pun",
96                 "summary" => ""
97               }
98             ]
99           }
100         }
101       }
102
103       assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
104                KeywordPolicy.filter(message)
105     end
106
107     test "rejects Updates" do
108       clear_config([:mrf_keyword, :reject], ["pun"])
109
110       message = %{
111         "type" => "Update",
112         "object" => %{
113           "content" => "just a daily reminder that compLAINer is a good",
114           "summary" => "",
115           "formerRepresentations" => %{
116             "type" => "OrderedCollection",
117             "orderedItems" => [
118               %{
119                 "content" => "just a daily reminder that compLAINer is a good pun",
120                 "summary" => ""
121               }
122             ]
123           }
124         }
125       }
126
127       assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
128                KeywordPolicy.filter(message)
129     end
130   end
131
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"])
135
136       message = %{
137         "to" => ["https://www.w3.org/ns/activitystreams#Public"],
138         "type" => "Create",
139         "object" => %{
140           "content" => "just a daily reminder that compLAINer is a good pun",
141           "summary" => ""
142         }
143       }
144
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"]
148     end
149
150     test "delists if string matches in summary" do
151       clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"])
152
153       message = %{
154         "to" => ["https://www.w3.org/ns/activitystreams#Public"],
155         "type" => "Create",
156         "object" => %{
157           "summary" => "just a daily reminder that compLAINer is a good pun",
158           "content" => ""
159         }
160       }
161
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"]
165     end
166
167     test "delists if regex matches in content" do
168       clear_config([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
169
170       assert true ==
171                Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
172                  message = %{
173                    "type" => "Create",
174                    "to" => ["https://www.w3.org/ns/activitystreams#Public"],
175                    "object" => %{
176                      "content" => "just a daily reminder that #{content} is a good pun",
177                      "summary" => ""
178                    }
179                  }
180
181                  {:ok, result} = KeywordPolicy.filter(message)
182
183                  ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
184                    not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
185                end)
186     end
187
188     test "delists if regex matches in summary" do
189       clear_config([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
190
191       assert true ==
192                Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
193                  message = %{
194                    "type" => "Create",
195                    "to" => ["https://www.w3.org/ns/activitystreams#Public"],
196                    "object" => %{
197                      "summary" => "just a daily reminder that #{content} is a good pun",
198                      "content" => ""
199                    }
200                  }
201
202                  {:ok, result} = KeywordPolicy.filter(message)
203
204                  ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
205                    not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
206                end)
207     end
208
209     test "delists if string matches in history" do
210       clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"])
211
212       message = %{
213         "to" => ["https://www.w3.org/ns/activitystreams#Public"],
214         "type" => "Create",
215         "object" => %{
216           "content" => "just a daily reminder that compLAINer is a good",
217           "summary" => "",
218           "formerRepresentations" => %{
219             "orderedItems" => [
220               %{
221                 "content" => "just a daily reminder that compLAINer is a good pun",
222                 "summary" => ""
223               }
224             ]
225           }
226         }
227       }
228
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"]
232     end
233   end
234
235   describe "replacing keywords" do
236     test "replaces keyword if string matches in content" do
237       clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
238
239       message = %{
240         "type" => "Create",
241         "to" => ["https://www.w3.org/ns/activitystreams#Public"],
242         "object" => %{"content" => "ZFS is opensource", "summary" => ""}
243       }
244
245       {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
246       assert result == "ZFS is free software"
247     end
248
249     test "replaces keyword if string matches in summary" do
250       clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
251
252       message = %{
253         "type" => "Create",
254         "to" => ["https://www.w3.org/ns/activitystreams#Public"],
255         "object" => %{"summary" => "ZFS is opensource", "content" => ""}
256       }
257
258       {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
259       assert result == "ZFS is free software"
260     end
261
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"}
265       ])
266
267       assert true ==
268                Enum.all?(["opensource", "open-source", "open source"], fn content ->
269                  message = %{
270                    "type" => "Create",
271                    "to" => ["https://www.w3.org/ns/activitystreams#Public"],
272                    "object" => %{"content" => "ZFS is #{content}", "summary" => ""}
273                  }
274
275                  {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
276                  result == "ZFS is free software"
277                end)
278     end
279
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"}
283       ])
284
285       assert true ==
286                Enum.all?(["opensource", "open-source", "open source"], fn content ->
287                  message = %{
288                    "type" => "Create",
289                    "to" => ["https://www.w3.org/ns/activitystreams#Public"],
290                    "object" => %{"summary" => "ZFS is #{content}", "content" => ""}
291                  }
292
293                  {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
294                  result == "ZFS is free software"
295                end)
296     end
297
298     test "replaces keyword if string matches in history" do
299       clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
300
301       message = %{
302         "type" => "Create",
303         "to" => ["https://www.w3.org/ns/activitystreams#Public"],
304         "object" => %{
305           "content" => "ZFS is opensource",
306           "summary" => "",
307           "formerRepresentations" => %{
308             "type" => "OrderedCollection",
309             "orderedItems" => [
310               %{"content" => "ZFS is opensource mew mew", "summary" => ""}
311             ]
312           }
313         }
314       }
315
316       {:ok,
317        %{
318          "object" => %{
319            "content" => "ZFS is free software",
320            "formerRepresentations" => %{
321              "orderedItems" => [%{"content" => "ZFS is free software mew mew"}]
322            }
323          }
324        }} = KeywordPolicy.filter(message)
325     end
326
327     test "replaces keyword in Updates" do
328       clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
329
330       message = %{
331         "type" => "Update",
332         "to" => ["https://www.w3.org/ns/activitystreams#Public"],
333         "object" => %{
334           "content" => "ZFS is opensource",
335           "summary" => "",
336           "formerRepresentations" => %{
337             "type" => "OrderedCollection",
338             "orderedItems" => [
339               %{"content" => "ZFS is opensource mew mew", "summary" => ""}
340             ]
341           }
342         }
343       }
344
345       {:ok,
346        %{
347          "object" => %{
348            "content" => "ZFS is free software",
349            "formerRepresentations" => %{
350              "orderedItems" => [%{"content" => "ZFS is free software mew mew"}]
351            }
352          }
353        }} = KeywordPolicy.filter(message)
354     end
355   end
356 end