1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 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
11 Pleroma.Config.put([:mrf_keyword], %{reject: [], federated_timeline_removal: [], replace: []})
14 describe "rejecting based on keywords" do
15 test "rejects if string matches in content" do
16 Pleroma.Config.put([:mrf_keyword, :reject], ["pun"])
21 "content" => "just a daily reminder that compLAINer is a good pun",
26 assert {:reject, nil} == KeywordPolicy.filter(message)
29 test "rejects if string matches in summary" do
30 Pleroma.Config.put([:mrf_keyword, :reject], ["pun"])
35 "summary" => "just a daily reminder that compLAINer is a good pun",
40 assert {:reject, nil} == KeywordPolicy.filter(message)
43 test "rejects if regex matches in content" do
44 Pleroma.Config.put([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
47 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
51 "content" => "just a daily reminder that #{content} is a good pun",
56 {:reject, nil} == KeywordPolicy.filter(message)
60 test "rejects if regex matches in summary" do
61 Pleroma.Config.put([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
64 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
68 "summary" => "just a daily reminder that #{content} is a good pun",
73 {:reject, nil} == KeywordPolicy.filter(message)
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"])
83 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
86 "content" => "just a daily reminder that compLAINer is a good pun",
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"]
96 test "delists if string matches in summary" do
97 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"])
100 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
103 "summary" => "just a daily reminder that compLAINer is a good pun",
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"]
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/])
117 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
120 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
122 "content" => "just a daily reminder that #{content} is a good pun",
127 {:ok, result} = KeywordPolicy.filter(message)
129 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
130 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
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/])
138 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
141 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
143 "summary" => "just a daily reminder that #{content} is a good pun",
148 {:ok, result} = KeywordPolicy.filter(message)
150 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
151 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
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"}])
162 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
163 "object" => %{"content" => "ZFS is opensource", "summary" => ""}
166 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
167 assert result == "ZFS is free software"
170 test "replaces keyword if string matches in summary" do
171 Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}])
175 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
176 "object" => %{"summary" => "ZFS is opensource", "content" => ""}
179 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
180 assert result == "ZFS is free software"
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"}
189 Enum.all?(["opensource", "open-source", "open source"], fn content ->
192 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
193 "object" => %{"content" => "ZFS is #{content}", "summary" => ""}
196 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
197 result == "ZFS is free software"
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"}
207 Enum.all?(["opensource", "open-source", "open source"], fn content ->
210 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
211 "object" => %{"summary" => "ZFS is #{content}", "content" => ""}
214 {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
215 result == "ZFS is free software"