move to 2.5.5
[anni] / test / pleroma / web / activity_pub / mrf / anti_link_spam_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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   import ExUnit.CaptureLog
9
10   alias Pleroma.Web.ActivityPub.MRF
11   alias Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicy
12
13   @linkless_message %{
14     "type" => "Create",
15     "object" => %{
16       "content" => "hi world!"
17     }
18   }
19
20   @linkful_message %{
21     "type" => "Create",
22     "object" => %{
23       "content" => "<a href='https://example.com'>hi world!</a>"
24     }
25   }
26
27   @response_message %{
28     "type" => "Create",
29     "object" => %{
30       "name" => "yes",
31       "type" => "Answer"
32     }
33   }
34
35   describe "with new user" do
36     test "it allows posts without links" do
37       user = insert(:user, local: false)
38
39       assert user.note_count == 0
40
41       message =
42         @linkless_message
43         |> Map.put("actor", user.ap_id)
44
45       {:ok, _message} = AntiLinkSpamPolicy.filter(message)
46     end
47
48     test "it disallows posts with links" do
49       user = insert(:user, local: false)
50
51       assert user.note_count == 0
52
53       message = %{
54         "type" => "Create",
55         "actor" => user.ap_id,
56         "object" => %{
57           "formerRepresentations" => %{
58             "type" => "OrderedCollection",
59             "orderedItems" => [
60               %{
61                 "content" => "<a href='https://example.com'>hi world!</a>"
62               }
63             ]
64           },
65           "content" => "mew"
66         }
67       }
68
69       {:reject, _} = MRF.filter_one(AntiLinkSpamPolicy, message)
70     end
71
72     test "it allows posts with links for local users" do
73       user = insert(:user)
74
75       assert user.note_count == 0
76
77       message =
78         @linkful_message
79         |> Map.put("actor", user.ap_id)
80
81       {:ok, _message} = AntiLinkSpamPolicy.filter(message)
82     end
83
84     test "it disallows posts with links in history" do
85       user = insert(:user, local: false)
86
87       assert user.note_count == 0
88
89       message =
90         @linkful_message
91         |> Map.put("actor", user.ap_id)
92
93       {:reject, _} = AntiLinkSpamPolicy.filter(message)
94     end
95   end
96
97   describe "with old user" do
98     test "it allows posts without links" do
99       user = insert(:user, note_count: 1)
100
101       assert user.note_count == 1
102
103       message =
104         @linkless_message
105         |> Map.put("actor", user.ap_id)
106
107       {:ok, _message} = AntiLinkSpamPolicy.filter(message)
108     end
109
110     test "it allows posts with links" do
111       user = insert(:user, note_count: 1)
112
113       assert user.note_count == 1
114
115       message =
116         @linkful_message
117         |> Map.put("actor", user.ap_id)
118
119       {:ok, _message} = AntiLinkSpamPolicy.filter(message)
120     end
121   end
122
123   describe "with followed new user" do
124     test "it allows posts without links" do
125       user = insert(:user, follower_count: 1)
126
127       assert user.follower_count == 1
128
129       message =
130         @linkless_message
131         |> Map.put("actor", user.ap_id)
132
133       {:ok, _message} = AntiLinkSpamPolicy.filter(message)
134     end
135
136     test "it allows posts with links" do
137       user = insert(:user, follower_count: 1)
138
139       assert user.follower_count == 1
140
141       message =
142         @linkful_message
143         |> Map.put("actor", user.ap_id)
144
145       {:ok, _message} = AntiLinkSpamPolicy.filter(message)
146     end
147   end
148
149   describe "with unknown actors" do
150     setup do
151       Tesla.Mock.mock(fn
152         %{method: :get, url: "http://invalid.actor"} ->
153           %Tesla.Env{status: 500, body: ""}
154       end)
155
156       :ok
157     end
158
159     test "it rejects posts without links" do
160       message =
161         @linkless_message
162         |> Map.put("actor", "http://invalid.actor")
163
164       assert capture_log(fn ->
165                {:reject, _} = AntiLinkSpamPolicy.filter(message)
166              end) =~ "[error] Could not decode user at fetch http://invalid.actor"
167     end
168
169     test "it rejects posts with links" do
170       message =
171         @linkful_message
172         |> Map.put("actor", "http://invalid.actor")
173
174       assert capture_log(fn ->
175                {:reject, _} = AntiLinkSpamPolicy.filter(message)
176              end) =~ "[error] Could not decode user at fetch http://invalid.actor"
177     end
178   end
179
180   describe "with contentless-objects" do
181     test "it does not reject them or error out" do
182       user = insert(:user, note_count: 1)
183
184       message =
185         @response_message
186         |> Map.put("actor", user.ap_id)
187
188       {:ok, _message} = AntiLinkSpamPolicy.filter(message)
189     end
190   end
191 end