1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 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 describe "gather_webfinger_links/1" do
27 test "it returns links" do
31 %{"href" => user.ap_id, "rel" => "self", "type" => "application/activity+json"},
35 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
38 "rel" => "http://ostatus.org/schema/1.0/subscribe",
39 "template" => "#{Pleroma.Web.base_url()}/ostatus_subscribe?acct={uri}"
43 assert expected_links == Publisher.gather_webfinger_links(user)
47 describe "determine_inbox/2" do
48 test "it returns sharedInbox for messages involving as:Public in to" do
51 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
55 data: %{"to" => [@as_public], "cc" => [user.follower_address]}
58 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
61 test "it returns sharedInbox for messages involving as:Public in cc" do
64 source_data: %{"endpoints" => %{"sharedInbox" => "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
77 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
80 user_two = insert(:user)
81 user_three = insert(:user)
84 data: %{"cc" => [], "to" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
87 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
90 test "it returns sharedInbox for messages involving multiple recipients in cc" do
93 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
96 user_two = insert(:user)
97 user_three = insert(:user)
100 data: %{"to" => [], "cc" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
103 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
106 test "it returns sharedInbox for messages involving multiple recipients in total" do
110 "inbox" => "http://example.com/personal-inbox",
111 "endpoints" => %{"sharedInbox" => "http://example.com/inbox"}
115 user_two = insert(:user)
117 activity = %Activity{
118 data: %{"to" => [user_two.ap_id], "cc" => [user.ap_id]}
121 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
124 test "it returns inbox for messages involving single recipients in total" do
128 "inbox" => "http://example.com/personal-inbox",
129 "endpoints" => %{"sharedInbox" => "http://example.com/inbox"}
133 activity = %Activity{
134 data: %{"to" => [user.ap_id], "cc" => []}
137 assert Publisher.determine_inbox(activity, user) == "http://example.com/personal-inbox"
141 describe "publish_one/1" do
142 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
146 actor = insert(:user)
147 inbox = "http://200.site/users/nick1/inbox"
149 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
151 assert called(Instances.set_reachable(inbox))
154 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
158 actor = insert(:user)
159 inbox = "http://200.site/users/nick1/inbox"
162 Publisher.publish_one(%{
167 unreachable_since: NaiveDateTime.utc_now()
170 assert called(Instances.set_reachable(inbox))
173 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
177 actor = insert(:user)
178 inbox = "http://200.site/users/nick1/inbox"
181 Publisher.publish_one(%{
186 unreachable_since: nil
189 refute called(Instances.set_reachable(inbox))
192 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
196 actor = insert(:user)
197 inbox = "http://404.site/users/nick1/inbox"
199 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
201 assert called(Instances.set_unreachable(inbox))
204 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
208 actor = insert(:user)
209 inbox = "http://connrefused.site/users/nick1/inbox"
211 assert capture_log(fn ->
213 Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
214 end) =~ "connrefused"
216 assert called(Instances.set_unreachable(inbox))
219 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
223 actor = insert(:user)
224 inbox = "http://200.site/users/nick1/inbox"
226 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
228 refute called(Instances.set_unreachable(inbox))
231 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
235 actor = insert(:user)
236 inbox = "http://connrefused.site/users/nick1/inbox"
238 assert capture_log(fn ->
240 Publisher.publish_one(%{
245 unreachable_since: NaiveDateTime.utc_now()
247 end) =~ "connrefused"
249 refute called(Instances.set_unreachable(inbox))
253 describe "publish/2" do
254 test_with_mock "publishes an activity with BCC to all relevant peers.",
255 Pleroma.Web.Federator.Publisher,
261 source_data: %{"inbox" => "https://domain.com/users/nick1/inbox"},
265 actor = insert(:user, follower_address: follower.ap_id)
268 {:ok, _follower_one} = Pleroma.User.follow(follower, actor)
269 actor = refresh_record(actor)
272 insert(:note_activity,
273 recipients: [follower.ap_id],
274 data_attrs: %{"bcc" => [user.ap_id]}
277 res = Publisher.publish(actor, note_activity)
281 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
282 inbox: "https://domain.com/users/nick1/inbox",
284 id: note_activity.data["id"]
289 test_with_mock "publishes a delete activity to peers who signed fetch requests to the create acitvity/object.",
290 Pleroma.Web.Federator.Publisher,
296 source_data: %{"inbox" => "https://domain.com/users/nick1/inbox"},
303 source_data: %{"inbox" => "https://domain2.com/users/nick1/inbox"},
307 actor = insert(:user)
309 note_activity = insert(:note_activity, user: actor)
310 object = Object.normalize(note_activity)
312 activity_path = String.trim_leading(note_activity.data["id"], Pleroma.Web.Endpoint.url())
313 object_path = String.trim_leading(object.data["id"], Pleroma.Web.Endpoint.url())
316 |> put_req_header("accept", "application/activity+json")
317 |> assign(:user, fetcher)
319 |> json_response(200)
322 |> put_req_header("accept", "application/activity+json")
323 |> assign(:user, another_fetcher)
324 |> get(activity_path)
325 |> json_response(200)
327 {:ok, delete} = CommonAPI.delete(note_activity.id, actor)
329 res = Publisher.publish(actor, delete)
333 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
334 inbox: "https://domain.com/users/nick1/inbox",
336 id: delete.data["id"]
341 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
342 inbox: "https://domain2.com/users/nick1/inbox",
344 id: delete.data["id"]