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.PublisherTest do
6 use Pleroma.Web.ConnCase
8 import ExUnit.CaptureLog
13 alias Pleroma.Activity
14 alias Pleroma.Instances
16 alias Pleroma.Web.ActivityPub.Publisher
17 alias Pleroma.Web.CommonAPI
19 @as_public "https://www.w3.org/ns/activitystreams#Public"
22 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 clear_config([:instance, :federating], true)
28 clear_config([:instance, :quarantined_instances], [])
29 clear_config([:mrf_simple, :reject], [])
32 describe "gather_webfinger_links/1" do
33 test "it returns links" do
37 %{"href" => user.ap_id, "rel" => "self", "type" => "application/activity+json"},
41 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
44 "rel" => "http://ostatus.org/schema/1.0/subscribe",
45 "template" => "#{Pleroma.Web.Endpoint.url()}/ostatus_subscribe?acct={uri}"
49 assert expected_links == Publisher.gather_webfinger_links(user)
53 describe "determine_inbox/2" do
54 test "it returns sharedInbox for messages involving as:Public in to" do
55 user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
58 data: %{"to" => [@as_public], "cc" => [user.follower_address]}
61 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
64 test "it returns sharedInbox for messages involving as:Public in cc" do
65 user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
68 data: %{"cc" => [@as_public], "to" => [user.follower_address]}
71 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
74 test "it returns sharedInbox for messages involving multiple recipients in to" do
75 user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
76 user_two = insert(:user)
77 user_three = insert(:user)
80 data: %{"cc" => [], "to" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
83 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
86 test "it returns sharedInbox for messages involving multiple recipients in cc" do
87 user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
88 user_two = insert(:user)
89 user_three = insert(:user)
92 data: %{"to" => [], "cc" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
95 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
98 test "it returns sharedInbox for messages involving multiple recipients in total" do
101 shared_inbox: "http://example.com/inbox",
102 inbox: "http://example.com/personal-inbox"
105 user_two = insert(:user)
107 activity = %Activity{
108 data: %{"to" => [user_two.ap_id], "cc" => [user.ap_id]}
111 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
114 test "it returns inbox for messages involving single recipients in total" do
117 shared_inbox: "http://example.com/inbox",
118 inbox: "http://example.com/personal-inbox"
121 activity = %Activity{
122 data: %{"to" => [user.ap_id], "cc" => []}
125 assert Publisher.determine_inbox(activity, user) == "http://example.com/personal-inbox"
129 describe "publish_one/1" do
130 test "publish to url with with different ports" do
131 inbox80 = "http://42.site/users/nick1/inbox"
132 inbox42 = "http://42.site:42/users/nick1/inbox"
135 %{method: :post, url: "http://42.site:42/users/nick1/inbox"} ->
136 {:ok, %Tesla.Env{status: 200, body: "port 42"}}
138 %{method: :post, url: "http://42.site/users/nick1/inbox"} ->
139 {:ok, %Tesla.Env{status: 200, body: "port 80"}}
142 actor = insert(:user)
144 assert {:ok, %{body: "port 42"}} =
145 Publisher.publish_one(%{
150 unreachable_since: true
153 assert {:ok, %{body: "port 80"}} =
154 Publisher.publish_one(%{
159 unreachable_since: true
163 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
167 actor = insert(:user)
168 inbox = "http://200.site/users/nick1/inbox"
170 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
171 assert called(Instances.set_reachable(inbox))
174 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
178 actor = insert(:user)
179 inbox = "http://200.site/users/nick1/inbox"
182 Publisher.publish_one(%{
187 unreachable_since: NaiveDateTime.utc_now()
190 assert called(Instances.set_reachable(inbox))
193 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
197 actor = insert(:user)
198 inbox = "http://200.site/users/nick1/inbox"
201 Publisher.publish_one(%{
206 unreachable_since: nil
209 refute called(Instances.set_reachable(inbox))
212 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
216 actor = insert(:user)
217 inbox = "http://404.site/users/nick1/inbox"
219 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
221 assert called(Instances.set_unreachable(inbox))
224 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
228 actor = insert(:user)
229 inbox = "http://connrefused.site/users/nick1/inbox"
231 assert capture_log(fn ->
233 Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
234 end) =~ "connrefused"
236 assert called(Instances.set_unreachable(inbox))
239 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
243 actor = insert(:user)
244 inbox = "http://200.site/users/nick1/inbox"
246 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
248 refute called(Instances.set_unreachable(inbox))
251 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
255 actor = insert(:user)
256 inbox = "http://connrefused.site/users/nick1/inbox"
258 assert capture_log(fn ->
260 Publisher.publish_one(%{
265 unreachable_since: NaiveDateTime.utc_now()
267 end) =~ "connrefused"
269 refute called(Instances.set_unreachable(inbox))
273 describe "publish/2" do
274 test_with_mock "doesn't publish any activity to quarantined or rejected instances.",
275 Pleroma.Web.Federator.Publisher,
278 Config.put([:instance, :quarantined_instances], [{"domain.com", "some reason"}])
280 Config.put([:mrf_simple, :reject], [{"rejected.com", "some reason"}])
285 inbox: "https://domain.com/users/nick1/inbox",
292 inbox: "https://rejected.com/users/nick2/inbox",
296 actor = insert(:user, follower_address: follower.ap_id)
298 {:ok, follower, actor} = Pleroma.User.follow(follower, actor)
299 {:ok, _another_follower, actor} = Pleroma.User.follow(another_follower, actor)
301 actor = refresh_record(actor)
304 insert(:followers_only_note_activity,
306 recipients: [follower.ap_id]
309 public_note_activity =
310 insert(:note_activity,
312 recipients: [follower.ap_id, @as_public]
315 res = Publisher.publish(actor, note_activity)
319 :ok = Publisher.publish(actor, public_note_activity)
322 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
323 inbox: "https://domain.com/users/nick1/inbox",
325 id: note_activity.data["id"]
330 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
331 inbox: "https://domain.com/users/nick1/inbox",
333 id: public_note_activity.data["id"]
338 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
339 inbox: "https://rejected.com/users/nick2/inbox",
341 id: note_activity.data["id"]
346 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
347 inbox: "https://rejected.com/users/nick2/inbox",
349 id: public_note_activity.data["id"]
354 test_with_mock "Publishes a non-public activity to non-quarantined instances.",
355 Pleroma.Web.Federator.Publisher,
358 Config.put([:instance, :quarantined_instances], [{"somedomain.com", "some reason"}])
363 inbox: "https://domain.com/users/nick1/inbox",
367 actor = insert(:user, follower_address: follower.ap_id)
369 {:ok, follower, actor} = Pleroma.User.follow(follower, actor)
370 actor = refresh_record(actor)
373 insert(:followers_only_note_activity,
375 recipients: [follower.ap_id]
378 res = Publisher.publish(actor, note_activity)
383 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
384 inbox: "https://domain.com/users/nick1/inbox",
386 id: note_activity.data["id"]
391 test_with_mock "publishes an activity with BCC to all relevant peers.",
392 Pleroma.Web.Federator.Publisher,
395 Config.put([:instance, :quarantined_instances], [])
400 inbox: "https://domain.com/users/nick1/inbox",
404 actor = insert(:user, follower_address: follower.ap_id)
407 {:ok, follower, actor} = Pleroma.User.follow(follower, actor)
410 insert(:note_activity,
411 recipients: [follower.ap_id],
412 data_attrs: %{"bcc" => [user.ap_id]}
415 res = Publisher.publish(actor, note_activity)
419 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
420 inbox: "https://domain.com/users/nick1/inbox",
422 id: note_activity.data["id"]
427 test_with_mock "publishes a delete activity to peers who signed fetch requests to the create acitvity/object.",
428 Pleroma.Web.Federator.Publisher,
431 clear_config([:instance, :quarantined_instances], [])
436 inbox: "https://domain.com/users/nick1/inbox",
443 inbox: "https://domain2.com/users/nick1/inbox",
447 actor = insert(:user)
449 note_activity = insert(:note_activity, user: actor)
450 object = Object.normalize(note_activity, fetch: false)
452 activity_path = String.trim_leading(note_activity.data["id"], Pleroma.Web.Endpoint.url())
453 object_path = String.trim_leading(object.data["id"], Pleroma.Web.Endpoint.url())
456 |> put_req_header("accept", "application/activity+json")
457 |> assign(:user, fetcher)
459 |> json_response(200)
462 |> put_req_header("accept", "application/activity+json")
463 |> assign(:user, another_fetcher)
464 |> get(activity_path)
465 |> json_response(200)
467 {:ok, delete} = CommonAPI.delete(note_activity.id, actor)
469 res = Publisher.publish(actor, delete)
473 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
474 inbox: "https://domain.com/users/nick1/inbox",
476 id: delete.data["id"]
481 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
482 inbox: "https://domain2.com/users/nick1/inbox",
484 id: delete.data["id"]