Fix MRF policies to also work with Update
[akkoma] / test / pleroma / web / activity_pub / mrf / hellthread_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 setup do
12 user = insert(:user)
13
14 message = %{
15 "actor" => user.ap_id,
16 "cc" => [user.follower_address],
17 "type" => "Create",
18 "to" => [
19 "https://www.w3.org/ns/activitystreams#Public",
20 "https://instance.tld/users/user1",
21 "https://instance.tld/users/user2",
22 "https://instance.tld/users/user3"
23 ],
24 "object" => %{
25 "type" => "Note"
26 }
27 }
28
29 update_message = %{
30 "actor" => user.ap_id,
31 "cc" => [user.follower_address],
32 "type" => "Update",
33 "to" => [
34 "https://www.w3.org/ns/activitystreams#Public",
35 "https://instance.tld/users/user1",
36 "https://instance.tld/users/user2",
37 "https://instance.tld/users/user3"
38 ],
39 "object" => %{
40 "type" => "Note"
41 }
42 }
43
44 [user: user, message: message, update_message: update_message]
45 end
46
47 setup do: clear_config(:mrf_hellthread)
48
49 describe "reject" do
50 test "rejects the message if the recipient count is above reject_threshold", %{
51 message: message,
52 update_message: update_message
53 } do
54 clear_config([:mrf_hellthread], %{delist_threshold: 0, reject_threshold: 2})
55
56 assert {:reject, "[HellthreadPolicy] 3 recipients is over the limit of 2"} ==
57 filter(message)
58
59 assert {:reject, "[HellthreadPolicy] 3 recipients is over the limit of 2"} ==
60 filter(update_message)
61 end
62
63 test "does not reject the message if the recipient count is below reject_threshold", %{
64 message: message,
65 update_message: update_message
66 } do
67 clear_config([:mrf_hellthread], %{delist_threshold: 0, reject_threshold: 3})
68
69 assert {:ok, ^message} = filter(message)
70 assert {:ok, ^update_message} = filter(update_message)
71 end
72 end
73
74 describe "delist" do
75 test "delists the message if the recipient count is above delist_threshold", %{
76 user: user,
77 message: message,
78 update_message: update_message
79 } do
80 clear_config([:mrf_hellthread], %{delist_threshold: 2, reject_threshold: 0})
81
82 {:ok, message} = filter(message)
83 assert user.follower_address in message["to"]
84 assert "https://www.w3.org/ns/activitystreams#Public" in message["cc"]
85
86 {:ok, update_message} = filter(update_message)
87 assert user.follower_address in update_message["to"]
88 assert "https://www.w3.org/ns/activitystreams#Public" in update_message["cc"]
89 end
90
91 test "does not delist the message if the recipient count is below delist_threshold", %{
92 message: message,
93 update_message: update_message
94 } do
95 clear_config([:mrf_hellthread], %{delist_threshold: 4, reject_threshold: 0})
96
97 assert {:ok, ^message} = filter(message)
98 assert {:ok, ^update_message} = filter(update_message)
99 end
100 end
101
102 test "excludes follower collection and public URI from threshold count", %{
103 message: message,
104 update_message: update_message
105 } do
106 clear_config([:mrf_hellthread], %{delist_threshold: 0, reject_threshold: 3})
107
108 assert {:ok, ^message} = filter(message)
109 assert {:ok, ^update_message} = filter(update_message)
110 end
111 end