1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.base_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
53 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
57 data: %{"to" => [@as_public], "cc" => [user.follower_address]}
60 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
63 test "it returns sharedInbox for messages involving as:Public in cc" do
66 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
70 data: %{"cc" => [@as_public], "to" => [user.follower_address]}
73 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
76 test "it returns sharedInbox for messages involving multiple recipients in to" do
79 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
82 user_two = insert(:user)
83 user_three = insert(:user)
86 data: %{"cc" => [], "to" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
89 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
92 test "it returns sharedInbox for messages involving multiple recipients in cc" do
95 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
98 user_two = insert(:user)
99 user_three = insert(:user)
101 activity = %Activity{
102 data: %{"to" => [], "cc" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
105 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
108 test "it returns sharedInbox for messages involving multiple recipients in total" do
112 "inbox" => "http://example.com/personal-inbox",
113 "endpoints" => %{"sharedInbox" => "http://example.com/inbox"}
117 user_two = insert(:user)
119 activity = %Activity{
120 data: %{"to" => [user_two.ap_id], "cc" => [user.ap_id]}
123 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
126 test "it returns inbox for messages involving single recipients in total" do
130 "inbox" => "http://example.com/personal-inbox",
131 "endpoints" => %{"sharedInbox" => "http://example.com/inbox"}
135 activity = %Activity{
136 data: %{"to" => [user.ap_id], "cc" => []}
139 assert Publisher.determine_inbox(activity, user) == "http://example.com/personal-inbox"
143 describe "publish_one/1" do
144 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
148 actor = insert(:user)
149 inbox = "http://200.site/users/nick1/inbox"
151 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
153 assert called(Instances.set_reachable(inbox))
156 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
160 actor = insert(:user)
161 inbox = "http://200.site/users/nick1/inbox"
164 Publisher.publish_one(%{
169 unreachable_since: NaiveDateTime.utc_now()
172 assert called(Instances.set_reachable(inbox))
175 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
179 actor = insert(:user)
180 inbox = "http://200.site/users/nick1/inbox"
183 Publisher.publish_one(%{
188 unreachable_since: nil
191 refute called(Instances.set_reachable(inbox))
194 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
198 actor = insert(:user)
199 inbox = "http://404.site/users/nick1/inbox"
201 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
203 assert called(Instances.set_unreachable(inbox))
206 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
210 actor = insert(:user)
211 inbox = "http://connrefused.site/users/nick1/inbox"
213 assert capture_log(fn ->
215 Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
216 end) =~ "connrefused"
218 assert called(Instances.set_unreachable(inbox))
221 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
225 actor = insert(:user)
226 inbox = "http://200.site/users/nick1/inbox"
228 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
230 refute called(Instances.set_unreachable(inbox))
233 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
237 actor = insert(:user)
238 inbox = "http://connrefused.site/users/nick1/inbox"
240 assert capture_log(fn ->
242 Publisher.publish_one(%{
247 unreachable_since: NaiveDateTime.utc_now()
249 end) =~ "connrefused"
251 refute called(Instances.set_unreachable(inbox))
255 describe "publish/2" do
256 test_with_mock "publishes an activity with BCC to all relevant peers.",
257 Pleroma.Web.Federator.Publisher,
263 source_data: %{"inbox" => "https://domain.com/users/nick1/inbox"},
267 actor = insert(:user, follower_address: follower.ap_id)
270 {:ok, _follower_one} = Pleroma.User.follow(follower, actor)
271 actor = refresh_record(actor)
274 insert(:note_activity,
275 recipients: [follower.ap_id],
276 data_attrs: %{"bcc" => [user.ap_id]}
279 res = Publisher.publish(actor, note_activity)
283 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
284 inbox: "https://domain.com/users/nick1/inbox",
286 id: note_activity.data["id"]
291 test_with_mock "publishes a delete activity to peers who signed fetch requests to the create acitvity/object.",
292 Pleroma.Web.Federator.Publisher,
298 source_data: %{"inbox" => "https://domain.com/users/nick1/inbox"},
305 source_data: %{"inbox" => "https://domain2.com/users/nick1/inbox"},
309 actor = insert(:user)
311 note_activity = insert(:note_activity, user: actor)
312 object = Object.normalize(note_activity)
314 activity_path = String.trim_leading(note_activity.data["id"], Pleroma.Web.Endpoint.url())
315 object_path = String.trim_leading(object.data["id"], Pleroma.Web.Endpoint.url())
318 |> put_req_header("accept", "application/activity+json")
319 |> assign(:user, fetcher)
321 |> json_response(200)
324 |> put_req_header("accept", "application/activity+json")
325 |> assign(:user, another_fetcher)
326 |> get(activity_path)
327 |> json_response(200)
329 {:ok, delete} = CommonAPI.delete(note_activity.id, actor)
331 res = Publisher.publish(actor, delete)
335 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
336 inbox: "https://domain.com/users/nick1/inbox",
338 id: delete.data["id"]
343 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
344 inbox: "https://domain2.com/users/nick1/inbox",
346 id: delete.data["id"]