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