MRF Policies: Return a {:reject, reason} instead of {:reject, nil}
[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 setup do: 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, "[KeywordPolicy] Matches with rejected keyword"} =
29 KeywordPolicy.filter(message)
30 end
31
32 test "rejects if string matches in summary" do
33 Pleroma.Config.put([: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 Pleroma.Config.put([: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 Pleroma.Config.put([: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 end
83
84 describe "delisting from ftl based on keywords" do
85 test "delists if string matches in content" do
86 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"])
87
88 message = %{
89 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
90 "type" => "Create",
91 "object" => %{
92 "content" => "just a daily reminder that compLAINer is a good pun",
93 "summary" => ""
94 }
95 }
96
97 {:ok, result} = KeywordPolicy.filter(message)
98 assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
99 refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
100 end
101
102 test "delists if string matches in summary" do
103 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"])
104
105 message = %{
106 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
107 "type" => "Create",
108 "object" => %{
109 "summary" => "just a daily reminder that compLAINer is a good pun",
110 "content" => ""
111 }
112 }
113
114 {:ok, result} = KeywordPolicy.filter(message)
115 assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
116 refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
117 end
118
119 test "delists if regex matches in content" do
120 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
121
122 assert true ==
123 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
124 message = %{
125 "type" => "Create",
126 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
127 "object" => %{
128 "content" => "just a daily reminder that #{content} is a good pun",
129 "summary" => ""
130 }
131 }
132
133 {:ok, result} = KeywordPolicy.filter(message)
134
135 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
136 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
137 end)
138 end
139
140 test "delists if regex matches in summary" do
141 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
142
143 assert true ==
144 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
145 message = %{
146 "type" => "Create",
147 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
148 "object" => %{
149 "summary" => "just a daily reminder that #{content} is a good pun",
150 "content" => ""
151 }
152 }
153
154 {:ok, result} = KeywordPolicy.filter(message)
155
156 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
157 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
158 end)
159 end
160 end
161
162 describe "replacing keywords" do
163 test "replaces keyword if string matches in content" do
164 Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}])
165
166 message = %{
167 "type" => "Create",
168 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
169 "object" => %{"content" => "ZFS is opensource", "summary" => ""}
170 }
171
172 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
173 assert result == "ZFS is free software"
174 end
175
176 test "replaces keyword if string matches in summary" do
177 Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}])
178
179 message = %{
180 "type" => "Create",
181 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
182 "object" => %{"summary" => "ZFS is opensource", "content" => ""}
183 }
184
185 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
186 assert result == "ZFS is free software"
187 end
188
189 test "replaces keyword if regex matches in content" do
190 Pleroma.Config.put([:mrf_keyword, :replace], [
191 {~r/open(-|\s)?source\s?(software)?/, "free software"}
192 ])
193
194 assert true ==
195 Enum.all?(["opensource", "open-source", "open source"], fn content ->
196 message = %{
197 "type" => "Create",
198 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
199 "object" => %{"content" => "ZFS is #{content}", "summary" => ""}
200 }
201
202 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
203 result == "ZFS is free software"
204 end)
205 end
206
207 test "replaces keyword if regex matches in summary" do
208 Pleroma.Config.put([:mrf_keyword, :replace], [
209 {~r/open(-|\s)?source\s?(software)?/, "free software"}
210 ])
211
212 assert true ==
213 Enum.all?(["opensource", "open-source", "open source"], fn content ->
214 message = %{
215 "type" => "Create",
216 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
217 "object" => %{"summary" => "ZFS is #{content}", "content" => ""}
218 }
219
220 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
221 result == "ZFS is free software"
222 end)
223 end
224 end
225 end