Merge branch 'ecto-rollback-in-test-env' into 'develop'
[akkoma] / test / pleroma / web / activity_pub / mrf / anti_link_spam_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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, local: false)
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, local: false)
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
59 test "it allows posts with links for local users" do
60 user = insert(:user)
61
62 assert user.note_count == 0
63
64 message =
65 @linkful_message
66 |> Map.put("actor", user.ap_id)
67
68 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
69 end
70 end
71
72 describe "with old user" do
73 test "it allows posts without links" do
74 user = insert(:user, note_count: 1)
75
76 assert user.note_count == 1
77
78 message =
79 @linkless_message
80 |> Map.put("actor", user.ap_id)
81
82 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
83 end
84
85 test "it allows posts with links" do
86 user = insert(:user, note_count: 1)
87
88 assert user.note_count == 1
89
90 message =
91 @linkful_message
92 |> Map.put("actor", user.ap_id)
93
94 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
95 end
96 end
97
98 describe "with followed new user" do
99 test "it allows posts without links" do
100 user = insert(:user, follower_count: 1)
101
102 assert user.follower_count == 1
103
104 message =
105 @linkless_message
106 |> Map.put("actor", user.ap_id)
107
108 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
109 end
110
111 test "it allows posts with links" do
112 user = insert(:user, follower_count: 1)
113
114 assert user.follower_count == 1
115
116 message =
117 @linkful_message
118 |> Map.put("actor", user.ap_id)
119
120 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
121 end
122 end
123
124 describe "with unknown actors" do
125 setup do
126 Tesla.Mock.mock(fn
127 %{method: :get, url: "http://invalid.actor"} ->
128 %Tesla.Env{status: 500, body: ""}
129 end)
130
131 :ok
132 end
133
134 test "it rejects posts without links" do
135 message =
136 @linkless_message
137 |> Map.put("actor", "http://invalid.actor")
138
139 assert capture_log(fn ->
140 {:reject, _} = AntiLinkSpamPolicy.filter(message)
141 end) =~ "[error] Could not decode user at fetch http://invalid.actor"
142 end
143
144 test "it rejects posts with links" do
145 message =
146 @linkful_message
147 |> Map.put("actor", "http://invalid.actor")
148
149 assert capture_log(fn ->
150 {:reject, _} = AntiLinkSpamPolicy.filter(message)
151 end) =~ "[error] Could not decode user at fetch http://invalid.actor"
152 end
153 end
154
155 describe "with contentless-objects" do
156 test "it does not reject them or error out" do
157 user = insert(:user, note_count: 1)
158
159 message =
160 @response_message
161 |> Map.put("actor", user.ap_id)
162
163 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
164 end
165 end
166 end