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" do
16 Pleroma.Config.put([:mrf_keyword, :reject], ["pun"])
20 "object" => %{"content" => "just a daily reminder that compLAINer is a good pun"}
23 assert {:reject, nil} == KeywordPolicy.filter(message)
26 test "rejects if regex matches" do
27 Pleroma.Config.put([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
30 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
34 "content" => "just a daily reminder that #{content} is a good pun"
38 {:reject, nil} == KeywordPolicy.filter(message)
43 describe "delisting from ftl based on keywords" do
44 test "delists if string matches" do
45 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"])
48 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
50 "object" => %{"content" => "just a daily reminder that compLAINer is a good pun"}
53 {:ok, result} = KeywordPolicy.filter(message)
54 assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
55 refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
58 test "delists if regex matches" do
59 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
62 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
65 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
67 "content" => "just a daily reminder that #{content} is a good pun"
71 {:ok, result} = KeywordPolicy.filter(message)
73 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
74 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
79 describe "replacing keywords" do
80 test "replaces keyword if string matches" do
81 Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}])
85 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
86 "object" => %{"content" => "ZFS is opensource"}
89 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
90 assert result == "ZFS is free software"
93 test "replaces keyword if regex matches" do
94 Pleroma.Config.put([:mrf_keyword, :replace], [
95 {~r/open(-|\s)?source\s?(software)?/, "free software"}
99 Enum.all?(["opensource", "open-source", "open source"], fn content ->
102 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
103 "object" => %{"content" => "ZFS is #{content}"}
106 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
107 result == "ZFS is free software"