MRF Policies: Return a {:reject, reason} instead of {:reject, nil}
[akkoma] / test / web / activity_pub / mrf / hellthread_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicyTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8
9 import Pleroma.Web.ActivityPub.MRF.HellthreadPolicy
10
11 alias Pleroma.Web.CommonAPI
12
13 setup do
14 user = insert(:user)
15
16 message = %{
17 "actor" => user.ap_id,
18 "cc" => [user.follower_address],
19 "type" => "Create",
20 "to" => [
21 "https://www.w3.org/ns/activitystreams#Public",
22 "https://instance.tld/users/user1",
23 "https://instance.tld/users/user2",
24 "https://instance.tld/users/user3"
25 ],
26 "object" => %{
27 "type" => "Note"
28 }
29 }
30
31 [user: user, message: message]
32 end
33
34 setup do: clear_config(:mrf_hellthread)
35
36 test "doesn't die on chat messages" do
37 Pleroma.Config.put([:mrf_hellthread], %{delist_threshold: 2, reject_threshold: 0})
38
39 user = insert(:user)
40 other_user = insert(:user)
41
42 {:ok, activity} = CommonAPI.post_chat_message(user, other_user, "moin")
43
44 assert {:ok, _} = filter(activity.data)
45 end
46
47 describe "reject" do
48 test "rejects the message if the recipient count is above reject_threshold", %{
49 message: message
50 } do
51 Pleroma.Config.put([:mrf_hellthread], %{delist_threshold: 0, reject_threshold: 2})
52
53 assert {:reject, "[HellthreadPolicy] 3 recipients is over the limit of 2"} ==
54 filter(message)
55 end
56
57 test "does not reject the message if the recipient count is below reject_threshold", %{
58 message: message
59 } do
60 Pleroma.Config.put([:mrf_hellthread], %{delist_threshold: 0, reject_threshold: 3})
61
62 assert {:ok, ^message} = filter(message)
63 end
64 end
65
66 describe "delist" do
67 test "delists the message if the recipient count is above delist_threshold", %{
68 user: user,
69 message: message
70 } do
71 Pleroma.Config.put([:mrf_hellthread], %{delist_threshold: 2, reject_threshold: 0})
72
73 {:ok, message} = filter(message)
74 assert user.follower_address in message["to"]
75 assert "https://www.w3.org/ns/activitystreams#Public" in message["cc"]
76 end
77
78 test "does not delist the message if the recipient count is below delist_threshold", %{
79 message: message
80 } do
81 Pleroma.Config.put([:mrf_hellthread], %{delist_threshold: 4, reject_threshold: 0})
82
83 assert {:ok, ^message} = filter(message)
84 end
85 end
86
87 test "excludes follower collection and public URI from threshold count", %{message: message} do
88 Pleroma.Config.put([:mrf_hellthread], %{delist_threshold: 0, reject_threshold: 3})
89
90 assert {:ok, ^message} = filter(message)
91 end
92 end