fe
[akkoma] / test / web / activity_pub / mrf / keyword_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 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
11 Pleroma.Config.put([:mrf_keyword], %{reject: [], federated_timeline_removal: [], replace: []})
12 end
13
14 describe "rejecting based on keywords" do
15 test "rejects if string matches in content" do
16 Pleroma.Config.put([:mrf_keyword, :reject], ["pun"])
17
18 message = %{
19 "type" => "Create",
20 "object" => %{
21 "content" => "just a daily reminder that compLAINer is a good pun",
22 "summary" => ""
23 }
24 }
25
26 assert {:reject, nil} == KeywordPolicy.filter(message)
27 end
28
29 test "rejects if string matches in summary" do
30 Pleroma.Config.put([:mrf_keyword, :reject], ["pun"])
31
32 message = %{
33 "type" => "Create",
34 "object" => %{
35 "summary" => "just a daily reminder that compLAINer is a good pun",
36 "content" => ""
37 }
38 }
39
40 assert {:reject, nil} == KeywordPolicy.filter(message)
41 end
42
43 test "rejects if regex matches in content" do
44 Pleroma.Config.put([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
45
46 assert true ==
47 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
48 message = %{
49 "type" => "Create",
50 "object" => %{
51 "content" => "just a daily reminder that #{content} is a good pun",
52 "summary" => ""
53 }
54 }
55
56 {:reject, nil} == KeywordPolicy.filter(message)
57 end)
58 end
59
60 test "rejects if regex matches in summary" do
61 Pleroma.Config.put([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
62
63 assert true ==
64 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
65 message = %{
66 "type" => "Create",
67 "object" => %{
68 "summary" => "just a daily reminder that #{content} is a good pun",
69 "content" => ""
70 }
71 }
72
73 {:reject, nil} == KeywordPolicy.filter(message)
74 end)
75 end
76 end
77
78 describe "delisting from ftl based on keywords" do
79 test "delists if string matches in content" do
80 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"])
81
82 message = %{
83 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
84 "type" => "Create",
85 "object" => %{
86 "content" => "just a daily reminder that compLAINer is a good pun",
87 "summary" => ""
88 }
89 }
90
91 {:ok, result} = KeywordPolicy.filter(message)
92 assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
93 refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
94 end
95
96 test "delists if string matches in summary" do
97 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"])
98
99 message = %{
100 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
101 "type" => "Create",
102 "object" => %{
103 "summary" => "just a daily reminder that compLAINer is a good pun",
104 "content" => ""
105 }
106 }
107
108 {:ok, result} = KeywordPolicy.filter(message)
109 assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
110 refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
111 end
112
113 test "delists if regex matches in content" do
114 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
115
116 assert true ==
117 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
118 message = %{
119 "type" => "Create",
120 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
121 "object" => %{
122 "content" => "just a daily reminder that #{content} is a good pun",
123 "summary" => ""
124 }
125 }
126
127 {:ok, result} = KeywordPolicy.filter(message)
128
129 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
130 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
131 end)
132 end
133
134 test "delists if regex matches in summary" do
135 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
136
137 assert true ==
138 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
139 message = %{
140 "type" => "Create",
141 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
142 "object" => %{
143 "summary" => "just a daily reminder that #{content} is a good pun",
144 "content" => ""
145 }
146 }
147
148 {:ok, result} = KeywordPolicy.filter(message)
149
150 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
151 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
152 end)
153 end
154 end
155
156 describe "replacing keywords" do
157 test "replaces keyword if string matches in content" do
158 Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}])
159
160 message = %{
161 "type" => "Create",
162 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
163 "object" => %{"content" => "ZFS is opensource", "summary" => ""}
164 }
165
166 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
167 assert result == "ZFS is free software"
168 end
169
170 test "replaces keyword if string matches in summary" do
171 Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}])
172
173 message = %{
174 "type" => "Create",
175 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
176 "object" => %{"summary" => "ZFS is opensource", "content" => ""}
177 }
178
179 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
180 assert result == "ZFS is free software"
181 end
182
183 test "replaces keyword if regex matches in content" do
184 Pleroma.Config.put([:mrf_keyword, :replace], [
185 {~r/open(-|\s)?source\s?(software)?/, "free software"}
186 ])
187
188 assert true ==
189 Enum.all?(["opensource", "open-source", "open source"], fn content ->
190 message = %{
191 "type" => "Create",
192 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
193 "object" => %{"content" => "ZFS is #{content}", "summary" => ""}
194 }
195
196 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
197 result == "ZFS is free software"
198 end)
199 end
200
201 test "replaces keyword if regex matches in summary" do
202 Pleroma.Config.put([:mrf_keyword, :replace], [
203 {~r/open(-|\s)?source\s?(software)?/, "free software"}
204 ])
205
206 assert true ==
207 Enum.all?(["opensource", "open-source", "open source"], fn content ->
208 message = %{
209 "type" => "Create",
210 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
211 "object" => %{"summary" => "ZFS is #{content}", "content" => ""}
212 }
213
214 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
215 result == "ZFS is free software"
216 end)
217 end
218 end
219 end