1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicyTest do
8 import ExUnit.CaptureLog
10 alias Pleroma.Web.ActivityPub.MRF
11 alias Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicy
16 "content" => "hi world!"
23 "content" => "<a href='https://example.com'>hi world!</a>"
35 @linkless_update_message %{
38 "content" => "hi world!"
42 @linkful_update_message %{
45 "content" => "<a href='https://example.com'>hi world!</a>"
49 @response_update_message %{
57 describe "with new user" do
58 test "it allows posts without links" do
59 user = insert(:user, local: false)
61 assert user.note_count == 0
65 |> Map.put("actor", user.ap_id)
68 @linkless_update_message
69 |> Map.put("actor", user.ap_id)
71 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
72 {:ok, _update_message} = AntiLinkSpamPolicy.filter(update_message)
75 test "it disallows posts with links" do
76 user = insert(:user, local: false)
78 assert user.note_count == 0
82 "actor" => user.ap_id,
84 "formerRepresentations" => %{
85 "type" => "OrderedCollection",
88 "content" => "<a href='https://example.com'>hi world!</a>"
98 "actor" => user.ap_id,
100 "formerRepresentations" => %{
101 "type" => "OrderedCollection",
104 "content" => "<a href='https://example.com'>hi world!</a>"
112 {:reject, _} = MRF.filter_one(AntiLinkSpamPolicy, message)
113 {:reject, _} = MRF.filter_one(AntiLinkSpamPolicy, update_message)
116 test "it allows posts with links for local users" do
119 assert user.note_count == 0
123 |> Map.put("actor", user.ap_id)
126 @linkful_update_message
127 |> Map.put("actor", user.ap_id)
129 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
130 {:ok, _update_message} = AntiLinkSpamPolicy.filter(update_message)
133 test "it disallows posts with links in history" do
134 user = insert(:user, local: false)
136 assert user.note_count == 0
140 |> Map.put("actor", user.ap_id)
143 @linkful_update_message
144 |> Map.put("actor", user.ap_id)
146 {:reject, _} = AntiLinkSpamPolicy.filter(message)
147 {:reject, _} = AntiLinkSpamPolicy.filter(update_message)
151 describe "with old user" do
152 test "it allows posts without links" do
153 user = insert(:user, note_count: 1)
155 assert user.note_count == 1
159 |> Map.put("actor", user.ap_id)
162 @linkless_update_message
163 |> Map.put("actor", user.ap_id)
165 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
166 {:ok, _update_message} = AntiLinkSpamPolicy.filter(update_message)
169 test "it allows posts with links" do
170 user = insert(:user, note_count: 1)
172 assert user.note_count == 1
176 |> Map.put("actor", user.ap_id)
179 @linkful_update_message
180 |> Map.put("actor", user.ap_id)
182 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
183 {:ok, _update_message} = AntiLinkSpamPolicy.filter(update_message)
187 describe "with followed new user" do
188 test "it allows posts without links" do
189 user = insert(:user, follower_count: 1)
191 assert user.follower_count == 1
195 |> Map.put("actor", user.ap_id)
198 @linkless_update_message
199 |> Map.put("actor", user.ap_id)
201 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
202 {:ok, _update_message} = AntiLinkSpamPolicy.filter(update_message)
205 test "it allows posts with links" do
206 user = insert(:user, follower_count: 1)
208 assert user.follower_count == 1
212 |> Map.put("actor", user.ap_id)
215 @linkful_update_message
216 |> Map.put("actor", user.ap_id)
218 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
219 {:ok, _update_message} = AntiLinkSpamPolicy.filter(update_message)
223 describe "with unknown actors" do
226 %{method: :get, url: "http://invalid.actor"} ->
227 %Tesla.Env{status: 500, body: ""}
233 test "it rejects posts without links" do
236 |> Map.put("actor", "http://invalid.actor")
239 @linkless_update_message
240 |> Map.put("actor", "http://invalid.actor")
242 assert capture_log(fn ->
243 {:reject, _} = AntiLinkSpamPolicy.filter(message)
244 end) =~ "[error] Could not decode user at fetch http://invalid.actor"
246 assert capture_log(fn ->
247 {:reject, _} = AntiLinkSpamPolicy.filter(update_message)
248 end) =~ "[error] Could not decode user at fetch http://invalid.actor"
251 test "it rejects posts with links" do
254 |> Map.put("actor", "http://invalid.actor")
257 @linkful_update_message
258 |> Map.put("actor", "http://invalid.actor")
260 assert capture_log(fn ->
261 {:reject, _} = AntiLinkSpamPolicy.filter(message)
262 end) =~ "[error] Could not decode user at fetch http://invalid.actor"
264 assert capture_log(fn ->
265 {:reject, _} = AntiLinkSpamPolicy.filter(update_message)
266 end) =~ "[error] Could not decode user at fetch http://invalid.actor"
270 describe "with contentless-objects" do
271 test "it does not reject them or error out" do
272 user = insert(:user, note_count: 1)
276 |> Map.put("actor", user.ap_id)
279 @response_update_message
280 |> Map.put("actor", user.ap_id)
282 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
283 {:ok, _update_message} = AntiLinkSpamPolicy.filter(update_message)