Merge branch 'follow-enhancements' into 'develop'
[akkoma] / test / web / activity_pub / mrf / keyword_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 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
11 Pleroma.Config.put([:mrf_keyword], %{reject: [], federated_timeline_removal: [], replace: []})
12 end
13
14 describe "rejecting based on keywords" do
15 test "rejects if string matches" do
16 Pleroma.Config.put([:mrf_keyword, :reject], ["pun"])
17
18 message = %{
19 "type" => "Create",
20 "object" => %{"content" => "just a daily reminder that compLAINer is a good pun"}
21 }
22
23 assert {:reject, nil} == KeywordPolicy.filter(message)
24 end
25
26 test "rejects if regex matches" do
27 Pleroma.Config.put([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
28
29 assert true ==
30 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
31 message = %{
32 "type" => "Create",
33 "object" => %{
34 "content" => "just a daily reminder that #{content} is a good pun"
35 }
36 }
37
38 {:reject, nil} == KeywordPolicy.filter(message)
39 end)
40 end
41 end
42
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"])
46
47 message = %{
48 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
49 "type" => "Create",
50 "object" => %{"content" => "just a daily reminder that compLAINer is a good pun"}
51 }
52
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"]
56 end
57
58 test "delists if regex matches" do
59 Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
60
61 assert true ==
62 Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
63 message = %{
64 "type" => "Create",
65 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
66 "object" => %{
67 "content" => "just a daily reminder that #{content} is a good pun"
68 }
69 }
70
71 {:ok, result} = KeywordPolicy.filter(message)
72
73 ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
74 not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
75 end)
76 end
77 end
78
79 describe "replacing keywords" do
80 test "replaces keyword if string matches" do
81 Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}])
82
83 message = %{
84 "type" => "Create",
85 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
86 "object" => %{"content" => "ZFS is opensource"}
87 }
88
89 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
90 assert result == "ZFS is free software"
91 end
92
93 test "replaces keyword if regex matches" do
94 Pleroma.Config.put([:mrf_keyword, :replace], [
95 {~r/open(-|\s)?source\s?(software)?/, "free software"}
96 ])
97
98 assert true ==
99 Enum.all?(["opensource", "open-source", "open source"], fn content ->
100 message = %{
101 "type" => "Create",
102 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
103 "object" => %{"content" => "ZFS is #{content}"}
104 }
105
106 {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
107 result == "ZFS is free software"
108 end)
109 end
110 end
111 end