1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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 describe "with new user" do
36 test "it allows posts without links" do
37 user = insert(:user, local: false)
39 assert user.note_count == 0
43 |> Map.put("actor", user.ap_id)
45 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
48 test "it disallows posts with links" do
49 user = insert(:user, local: false)
51 assert user.note_count == 0
55 "actor" => user.ap_id,
57 "formerRepresentations" => %{
58 "type" => "OrderedCollection",
61 "content" => "<a href='https://example.com'>hi world!</a>"
69 {:reject, _} = MRF.filter_one(AntiLinkSpamPolicy, message)
72 test "it allows posts with links for local users" do
75 assert user.note_count == 0
79 |> Map.put("actor", user.ap_id)
81 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
84 test "it disallows posts with links in history" do
85 user = insert(:user, local: false)
87 assert user.note_count == 0
91 |> Map.put("actor", user.ap_id)
93 {:reject, _} = AntiLinkSpamPolicy.filter(message)
97 describe "with old user" do
98 test "it allows posts without links" do
99 user = insert(:user, note_count: 1)
101 assert user.note_count == 1
105 |> Map.put("actor", user.ap_id)
107 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
110 test "it allows posts with links" do
111 user = insert(:user, note_count: 1)
113 assert user.note_count == 1
117 |> Map.put("actor", user.ap_id)
119 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
123 describe "with followed new user" do
124 test "it allows posts without links" do
125 user = insert(:user, follower_count: 1)
127 assert user.follower_count == 1
131 |> Map.put("actor", user.ap_id)
133 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
136 test "it allows posts with links" do
137 user = insert(:user, follower_count: 1)
139 assert user.follower_count == 1
143 |> Map.put("actor", user.ap_id)
145 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
149 describe "with unknown actors" do
152 %{method: :get, url: "http://invalid.actor"} ->
153 %Tesla.Env{status: 500, body: ""}
159 test "it rejects posts without links" do
162 |> Map.put("actor", "http://invalid.actor")
164 assert capture_log(fn ->
165 {:reject, _} = AntiLinkSpamPolicy.filter(message)
166 end) =~ "[error] Could not decode user at fetch http://invalid.actor"
169 test "it rejects posts with links" do
172 |> Map.put("actor", "http://invalid.actor")
174 assert capture_log(fn ->
175 {:reject, _} = AntiLinkSpamPolicy.filter(message)
176 end) =~ "[error] Could not decode user at fetch http://invalid.actor"
180 describe "with contentless-objects" do
181 test "it does not reject them or error out" do
182 user = insert(:user, note_count: 1)
186 |> Map.put("actor", user.ap_id)
188 {:ok, _message} = AntiLinkSpamPolicy.filter(message)