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.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)
26 setup_all do: clear_config([:instance, :federating], true)
28 describe "gather_webfinger_links/1" do
29 test "it returns links" do
33 %{"href" => user.ap_id, "rel" => "self", "type" => "application/activity+json"},
37 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
40 "rel" => "http://ostatus.org/schema/1.0/subscribe",
41 "template" => "#{Pleroma.Web.Endpoint.url()}/ostatus_subscribe?acct={uri}"
45 assert expected_links == Publisher.gather_webfinger_links(user)
49 describe "determine_inbox/2" do
50 test "it returns sharedInbox for messages involving as:Public in to" do
51 user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
54 data: %{"to" => [@as_public], "cc" => [user.follower_address]}
57 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
60 test "it returns sharedInbox for messages involving as:Public in cc" do
61 user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
64 data: %{"cc" => [@as_public], "to" => [user.follower_address]}
67 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
70 test "it returns sharedInbox for messages involving multiple recipients in to" do
71 user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
72 user_two = insert(:user)
73 user_three = insert(:user)
76 data: %{"cc" => [], "to" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
79 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
82 test "it returns sharedInbox for messages involving multiple recipients in cc" do
83 user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
84 user_two = insert(:user)
85 user_three = insert(:user)
88 data: %{"to" => [], "cc" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
91 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
94 test "it returns sharedInbox for messages involving multiple recipients in total" do
97 shared_inbox: "http://example.com/inbox",
98 inbox: "http://example.com/personal-inbox"
101 user_two = insert(:user)
103 activity = %Activity{
104 data: %{"to" => [user_two.ap_id], "cc" => [user.ap_id]}
107 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
110 test "it returns inbox for messages involving single recipients in total" do
113 shared_inbox: "http://example.com/inbox",
114 inbox: "http://example.com/personal-inbox"
117 activity = %Activity{
118 data: %{"to" => [user.ap_id], "cc" => []}
121 assert Publisher.determine_inbox(activity, user) == "http://example.com/personal-inbox"
125 describe "publish_one/1" do
126 test "publish to url with with different ports" do
127 inbox80 = "http://42.site/users/nick1/inbox"
128 inbox42 = "http://42.site:42/users/nick1/inbox"
131 %{method: :post, url: "http://42.site:42/users/nick1/inbox"} ->
132 {:ok, %Tesla.Env{status: 200, body: "port 42"}}
134 %{method: :post, url: "http://42.site/users/nick1/inbox"} ->
135 {:ok, %Tesla.Env{status: 200, body: "port 80"}}
138 actor = insert(:user)
140 assert {:ok, %{body: "port 42"}} =
141 Publisher.publish_one(%{
146 unreachable_since: true
149 assert {:ok, %{body: "port 80"}} =
150 Publisher.publish_one(%{
155 unreachable_since: true
159 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
163 actor = insert(:user)
164 inbox = "http://200.site/users/nick1/inbox"
166 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
167 assert called(Instances.set_reachable(inbox))
170 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
174 actor = insert(:user)
175 inbox = "http://200.site/users/nick1/inbox"
178 Publisher.publish_one(%{
183 unreachable_since: NaiveDateTime.utc_now()
186 assert called(Instances.set_reachable(inbox))
189 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
193 actor = insert(:user)
194 inbox = "http://200.site/users/nick1/inbox"
197 Publisher.publish_one(%{
202 unreachable_since: nil
205 refute called(Instances.set_reachable(inbox))
208 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
212 actor = insert(:user)
213 inbox = "http://404.site/users/nick1/inbox"
215 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
217 assert called(Instances.set_unreachable(inbox))
220 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
224 actor = insert(:user)
225 inbox = "http://connrefused.site/users/nick1/inbox"
227 assert capture_log(fn ->
229 Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
230 end) =~ "connrefused"
232 assert called(Instances.set_unreachable(inbox))
235 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
239 actor = insert(:user)
240 inbox = "http://200.site/users/nick1/inbox"
242 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
244 refute called(Instances.set_unreachable(inbox))
247 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
251 actor = insert(:user)
252 inbox = "http://connrefused.site/users/nick1/inbox"
254 assert capture_log(fn ->
256 Publisher.publish_one(%{
261 unreachable_since: NaiveDateTime.utc_now()
263 end) =~ "connrefused"
265 refute called(Instances.set_unreachable(inbox))
269 describe "publish/2" do
270 test_with_mock "doesn't publish a non-public activity to quarantined instances.",
271 Pleroma.Web.Federator.Publisher,
274 Config.put([:instance, :quarantined_instances], [{"domain.com", "some reason"}])
279 inbox: "https://domain.com/users/nick1/inbox",
283 actor = insert(:user, follower_address: follower.ap_id)
285 {:ok, follower, actor} = Pleroma.User.follow(follower, actor)
286 actor = refresh_record(actor)
289 insert(:followers_only_note_activity,
291 recipients: [follower.ap_id]
294 res = Publisher.publish(actor, note_activity)
299 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
300 inbox: "https://domain.com/users/nick1/inbox",
302 id: note_activity.data["id"]
307 test_with_mock "Publishes a non-public activity to non-quarantined instances.",
308 Pleroma.Web.Federator.Publisher,
311 Config.put([:instance, :quarantined_instances], [{"somedomain.com", "some reason"}])
316 inbox: "https://domain.com/users/nick1/inbox",
320 actor = insert(:user, follower_address: follower.ap_id)
322 {:ok, follower, actor} = Pleroma.User.follow(follower, actor)
323 actor = refresh_record(actor)
326 insert(:followers_only_note_activity,
328 recipients: [follower.ap_id]
331 res = Publisher.publish(actor, note_activity)
336 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
337 inbox: "https://domain.com/users/nick1/inbox",
339 id: note_activity.data["id"]
344 test_with_mock "publishes an activity with BCC to all relevant peers.",
345 Pleroma.Web.Federator.Publisher,
351 inbox: "https://domain.com/users/nick1/inbox",
355 actor = insert(:user, follower_address: follower.ap_id)
358 {:ok, follower, actor} = Pleroma.User.follow(follower, actor)
361 insert(:note_activity,
362 recipients: [follower.ap_id],
363 data_attrs: %{"bcc" => [user.ap_id]}
366 res = Publisher.publish(actor, note_activity)
370 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
371 inbox: "https://domain.com/users/nick1/inbox",
373 id: note_activity.data["id"]
378 test_with_mock "publishes a delete activity to peers who signed fetch requests to the create acitvity/object.",
379 Pleroma.Web.Federator.Publisher,
385 inbox: "https://domain.com/users/nick1/inbox",
392 inbox: "https://domain2.com/users/nick1/inbox",
396 actor = insert(:user)
398 note_activity = insert(:note_activity, user: actor)
399 object = Object.normalize(note_activity, fetch: false)
401 activity_path = String.trim_leading(note_activity.data["id"], Pleroma.Web.Endpoint.url())
402 object_path = String.trim_leading(object.data["id"], Pleroma.Web.Endpoint.url())
405 |> put_req_header("accept", "application/activity+json")
406 |> assign(:user, fetcher)
408 |> json_response(200)
411 |> put_req_header("accept", "application/activity+json")
412 |> assign(:user, another_fetcher)
413 |> get(activity_path)
414 |> json_response(200)
416 {:ok, delete} = CommonAPI.delete(note_activity.id, actor)
418 res = Publisher.publish(actor, delete)
422 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
423 inbox: "https://domain.com/users/nick1/inbox",
425 id: delete.data["id"]
430 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
431 inbox: "https://domain2.com/users/nick1/inbox",
433 id: delete.data["id"]