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 "determine_inbox/2" do
27 test "it returns sharedInbox for messages involving as:Public in to" do
30 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
34 data: %{"to" => [@as_public], "cc" => [user.follower_address]}
37 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
40 test "it returns sharedInbox for messages involving as:Public in cc" do
43 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
47 data: %{"cc" => [@as_public], "to" => [user.follower_address]}
50 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
53 test "it returns sharedInbox for messages involving multiple recipients in to" do
56 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
59 user_two = insert(:user)
60 user_three = insert(:user)
63 data: %{"cc" => [], "to" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
66 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
69 test "it returns sharedInbox for messages involving multiple recipients in cc" do
72 source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}
75 user_two = insert(:user)
76 user_three = insert(:user)
79 data: %{"to" => [], "cc" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
82 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
85 test "it returns sharedInbox for messages involving multiple recipients in total" do
89 "inbox" => "http://example.com/personal-inbox",
90 "endpoints" => %{"sharedInbox" => "http://example.com/inbox"}
94 user_two = insert(:user)
97 data: %{"to" => [user_two.ap_id], "cc" => [user.ap_id]}
100 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
103 test "it returns inbox for messages involving single recipients in total" do
107 "inbox" => "http://example.com/personal-inbox",
108 "endpoints" => %{"sharedInbox" => "http://example.com/inbox"}
112 activity = %Activity{
113 data: %{"to" => [user.ap_id], "cc" => []}
116 assert Publisher.determine_inbox(activity, user) == "http://example.com/personal-inbox"
120 describe "publish_one/1" do
121 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
125 actor = insert(:user)
126 inbox = "http://200.site/users/nick1/inbox"
128 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
130 assert called(Instances.set_reachable(inbox))
133 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
137 actor = insert(:user)
138 inbox = "http://200.site/users/nick1/inbox"
141 Publisher.publish_one(%{
146 unreachable_since: NaiveDateTime.utc_now()
149 assert called(Instances.set_reachable(inbox))
152 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
156 actor = insert(:user)
157 inbox = "http://200.site/users/nick1/inbox"
160 Publisher.publish_one(%{
165 unreachable_since: nil
168 refute called(Instances.set_reachable(inbox))
171 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
175 actor = insert(:user)
176 inbox = "http://404.site/users/nick1/inbox"
178 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
180 assert called(Instances.set_unreachable(inbox))
183 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
187 actor = insert(:user)
188 inbox = "http://connrefused.site/users/nick1/inbox"
190 assert capture_log(fn ->
192 Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
193 end) =~ "connrefused"
195 assert called(Instances.set_unreachable(inbox))
198 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
202 actor = insert(:user)
203 inbox = "http://200.site/users/nick1/inbox"
205 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
207 refute called(Instances.set_unreachable(inbox))
210 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
214 actor = insert(:user)
215 inbox = "http://connrefused.site/users/nick1/inbox"
217 assert capture_log(fn ->
219 Publisher.publish_one(%{
224 unreachable_since: NaiveDateTime.utc_now()
226 end) =~ "connrefused"
228 refute called(Instances.set_unreachable(inbox))
232 describe "publish/2" do
233 test_with_mock "publishes an activity with BCC to all relevant peers.",
234 Pleroma.Web.Federator.Publisher,
240 source_data: %{"inbox" => "https://domain.com/users/nick1/inbox"},
244 actor = insert(:user, follower_address: follower.ap_id)
247 {:ok, _follower_one} = Pleroma.User.follow(follower, actor)
248 actor = refresh_record(actor)
251 insert(:note_activity,
252 recipients: [follower.ap_id],
253 data_attrs: %{"bcc" => [user.ap_id]}
256 res = Publisher.publish(actor, note_activity)
260 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
261 inbox: "https://domain.com/users/nick1/inbox",
263 id: note_activity.data["id"]
268 test_with_mock "publishes a delete activity to peers who signed fetch requests to the create acitvity/object.",
269 Pleroma.Web.Federator.Publisher,
275 source_data: %{"inbox" => "https://domain.com/users/nick1/inbox"},
282 source_data: %{"inbox" => "https://domain2.com/users/nick1/inbox"},
286 actor = insert(:user)
288 note_activity = insert(:note_activity, user: actor)
289 object = Object.normalize(note_activity)
291 activity_path = String.trim_leading(note_activity.data["id"], Pleroma.Web.Endpoint.url())
292 object_path = String.trim_leading(object.data["id"], Pleroma.Web.Endpoint.url())
295 |> put_req_header("accept", "application/activity+json")
296 |> assign(:user, fetcher)
298 |> json_response(200)
301 |> put_req_header("accept", "application/activity+json")
302 |> assign(:user, another_fetcher)
303 |> get(activity_path)
304 |> json_response(200)
306 {:ok, delete} = CommonAPI.delete(note_activity.id, actor)
308 res = Publisher.publish(actor, delete)
312 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
313 inbox: "https://domain.com/users/nick1/inbox",
315 id: delete.data["id"]
320 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
321 inbox: "https://domain2.com/users/nick1/inbox",
323 id: delete.data["id"]