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