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)
84 describe "delisting from ftl based on keywords" do
85 test "delists if string matches in content" do
86 clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"])
89 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
92 "content" => "just a daily reminder that compLAINer is a good pun",
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"]
102 test "delists if string matches in summary" do
103 clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"])
106 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
109 "summary" => "just a daily reminder that compLAINer is a good pun",
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"]
119 test "delists if regex matches in content" do
120 clear_config([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
123 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
126 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
128 "content" => "just a daily reminder that #{content} is a good pun",
133 {:ok, result} = KeywordPolicy.filter(message)
135 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
136 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
140 test "delists if regex matches in summary" do
141 clear_config([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
144 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
147 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
149 "summary" => "just a daily reminder that #{content} is a good pun",
154 {:ok, result} = KeywordPolicy.filter(message)
156 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
157 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
162 describe "replacing keywords" do
163 test "replaces keyword if string matches in content" do
164 clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
168 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
169 "object" => %{"content" => "ZFS is opensource", "summary" => ""}
172 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
173 assert result == "ZFS is free software"
176 test "replaces keyword if string matches in summary" do
177 clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
181 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
182 "object" => %{"summary" => "ZFS is opensource", "content" => ""}
185 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
186 assert result == "ZFS is free software"
189 test "replaces keyword if regex matches in content" do
190 clear_config([:mrf_keyword, :replace], [
191 {~r/open(-|\s)?source\s?(software)?/, "free software"}
195 Enum.all?(["opensource", "open-source", "open source"], fn content ->
198 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
199 "object" => %{"content" => "ZFS is #{content}", "summary" => ""}
202 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
203 result == "ZFS is free software"
207 test "replaces keyword if regex matches in summary" do
208 clear_config([:mrf_keyword, :replace], [
209 {~r/open(-|\s)?source\s?(software)?/, "free software"}
213 Enum.all?(["opensource", "open-source", "open source"], fn content ->
216 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
217 "object" => %{"summary" => "ZFS is #{content}", "content" => ""}
220 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
221 result == "ZFS is free software"