284c13336af60b6c3ba0877fc220ac74332b22e0
[akkoma] / test / web / activity_pub / mrf / anti_link_spam_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.AntiLinkSpamPolicyTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8
9 alias Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicy
10
11 @linkless_message %{
12 "type" => "Create",
13 "object" => %{
14 "content" => "hi world!"
15 }
16 }
17
18 @linkful_message %{
19 "type" => "Create",
20 "object" => %{
21 "content" => "<a href='https://example.com'>hi world!</a>"
22 }
23 }
24
25 @response_message %{
26 "type" => "Create",
27 "object" => %{
28 "name" => "yes",
29 "type" => "Answer"
30 }
31 }
32
33 describe "with new user" do
34 test "it allows posts without links" do
35 user = insert(:user)
36
37 assert user.info.note_count == 0
38
39 message =
40 @linkless_message
41 |> Map.put("actor", user.ap_id)
42
43 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
44 end
45
46 test "it disallows posts with links" do
47 user = insert(:user)
48
49 assert user.info.note_count == 0
50
51 message =
52 @linkful_message
53 |> Map.put("actor", user.ap_id)
54
55 {:reject, _} = AntiLinkSpamPolicy.filter(message)
56 end
57 end
58
59 describe "with old user" do
60 test "it allows posts without links" do
61 user = insert(:user, info: %{note_count: 1})
62
63 assert user.info.note_count == 1
64
65 message =
66 @linkless_message
67 |> Map.put("actor", user.ap_id)
68
69 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
70 end
71
72 test "it allows posts with links" do
73 user = insert(:user, info: %{note_count: 1})
74
75 assert user.info.note_count == 1
76
77 message =
78 @linkful_message
79 |> Map.put("actor", user.ap_id)
80
81 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
82 end
83 end
84
85 describe "with followed new user" do
86 test "it allows posts without links" do
87 user = insert(:user, info: %{follower_count: 1})
88
89 assert user.info.follower_count == 1
90
91 message =
92 @linkless_message
93 |> Map.put("actor", user.ap_id)
94
95 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
96 end
97
98 test "it allows posts with links" do
99 user = insert(:user, info: %{follower_count: 1})
100
101 assert user.info.follower_count == 1
102
103 message =
104 @linkful_message
105 |> Map.put("actor", user.ap_id)
106
107 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
108 end
109 end
110
111 describe "with unknown actors" do
112 test "it rejects posts without links" do
113 message =
114 @linkless_message
115 |> Map.put("actor", "http://invalid.actor")
116
117 {:reject, _} = AntiLinkSpamPolicy.filter(message)
118 end
119
120 test "it rejects posts with links" do
121 message =
122 @linkful_message
123 |> Map.put("actor", "http://invalid.actor")
124
125 {:reject, _} = AntiLinkSpamPolicy.filter(message)
126 end
127 end
128
129 describe "with contentless-objects" do
130 test "it does not reject them or error out" do
131 user = insert(:user, info: %{note_count: 1})
132
133 message =
134 @response_message
135 |> Map.put("actor", user.ap_id)
136
137 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
138 end
139 end
140 end