Merge branch 'bugfix/apc2s_upload_activity' into 'develop'
[akkoma] / test / web / activity_pub / mrf / anti_link_spam_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 setup do
114 Tesla.Mock.mock(fn
115 %{method: :get, url: "http://invalid.actor"} ->
116 %Tesla.Env{status: 500, body: ""}
117 end)
118
119 :ok
120 end
121
122 test "it rejects posts without links" do
123 message =
124 @linkless_message
125 |> Map.put("actor", "http://invalid.actor")
126
127 assert capture_log(fn ->
128 {:reject, _} = AntiLinkSpamPolicy.filter(message)
129 end) =~ "[error] Could not decode user at fetch http://invalid.actor"
130 end
131
132 test "it rejects posts with links" do
133 message =
134 @linkful_message
135 |> Map.put("actor", "http://invalid.actor")
136
137 assert capture_log(fn ->
138 {:reject, _} = AntiLinkSpamPolicy.filter(message)
139 end) =~ "[error] Could not decode user at fetch http://invalid.actor"
140 end
141 end
142
143 describe "with contentless-objects" do
144 test "it does not reject them or error out" do
145 user = insert(:user, note_count: 1)
146
147 message =
148 @response_message
149 |> Map.put("actor", user.ap_id)
150
151 {:ok, _message} = AntiLinkSpamPolicy.filter(message)
152 end
153 end
154 end