1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 Pleroma.Config.put([:mrf_keyword], %{reject: [], federated_timeline_removal: [], replace: []})
16 describe "rejecting based on keywords" do
17 test "rejects if string matches in content" do
18 Pleroma.Config.put([:mrf_keyword, :reject], ["pun"])
23 "content" => "just a daily reminder that compLAINer is a good pun",
28 assert {:reject, nil} == KeywordPolicy.filter(message)
31 test "rejects if string matches in summary" do
32 Pleroma.Config.put([:mrf_keyword, :reject], ["pun"])
37 "summary" => "just a daily reminder that compLAINer is a good pun",
42 assert {:reject, nil} == KeywordPolicy.filter(message)
45 test "rejects if regex matches in content" do
46 Pleroma.Config.put([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
49 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
53 "content" => "just a daily reminder that #{content} is a good pun",
58 {:reject, nil} == KeywordPolicy.filter(message)
62 test "rejects if regex matches in summary" do
63 Pleroma.Config.put([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
66 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
70 "summary" => "just a daily reminder that #{content} is a good pun",
75 {:reject, nil} == KeywordPolicy.filter(message)
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"])
85 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
88 "content" => "just a daily reminder that compLAINer is a good pun",
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"]
98 test "delists if string matches in summary" do
99 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"])
102 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
105 "summary" => "just a daily reminder that compLAINer is a good pun",
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"]
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/])
119 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
122 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
124 "content" => "just a daily reminder that #{content} is a good pun",
129 {:ok, result} = KeywordPolicy.filter(message)
131 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
132 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
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/])
140 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
143 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
145 "summary" => "just a daily reminder that #{content} is a good pun",
150 {:ok, result} = KeywordPolicy.filter(message)
152 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
153 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
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"}])
164 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
165 "object" => %{"content" => "ZFS is opensource", "summary" => ""}
168 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
169 assert result == "ZFS is free software"
172 test "replaces keyword if string matches in summary" do
173 Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}])
177 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
178 "object" => %{"summary" => "ZFS is opensource", "content" => ""}
181 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
182 assert result == "ZFS is free software"
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"}
191 Enum.all?(["opensource", "open-source", "open source"], fn content ->
194 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
195 "object" => %{"content" => "ZFS is #{content}", "summary" => ""}
198 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
199 result == "ZFS is free software"
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"}
209 Enum.all?(["opensource", "open-source", "open source"], fn content ->
212 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
213 "object" => %{"summary" => "ZFS is #{content}", "content" => ""}
216 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
217 result == "ZFS is free software"