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
12 alias Pleroma.Activity
13 alias Pleroma.Instances
14 alias Pleroma.Web.ActivityPub.Publisher
16 @as_public "https://www.w3.org/ns/activitystreams#Public"
19 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 describe "determine_inbox/2" do
24 test "it returns sharedInbox for messages involving as:Public in to" do
27 info: %{source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}}
31 data: %{"to" => [@as_public], "cc" => [user.follower_address]}
34 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
37 test "it returns sharedInbox for messages involving as:Public in cc" do
40 info: %{source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}}
44 data: %{"cc" => [@as_public], "to" => [user.follower_address]}
47 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
50 test "it returns sharedInbox for messages involving multiple recipients in to" do
53 info: %{source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}}
56 user_two = insert(:user)
57 user_three = insert(:user)
60 data: %{"cc" => [], "to" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
63 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
66 test "it returns sharedInbox for messages involving multiple recipients in cc" do
69 info: %{source_data: %{"endpoints" => %{"sharedInbox" => "http://example.com/inbox"}}}
72 user_two = insert(:user)
73 user_three = insert(:user)
76 data: %{"to" => [], "cc" => [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 total" do
87 "inbox" => "http://example.com/personal-inbox",
88 "endpoints" => %{"sharedInbox" => "http://example.com/inbox"}
93 user_two = insert(:user)
96 data: %{"to" => [user_two.ap_id], "cc" => [user.ap_id]}
99 assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
102 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"}
113 activity = %Activity{
114 data: %{"to" => [user.ap_id], "cc" => []}
117 assert Publisher.determine_inbox(activity, user) == "http://example.com/personal-inbox"
121 describe "publish_one/1" do
122 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
126 actor = insert(:user)
127 inbox = "http://200.site/users/nick1/inbox"
129 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
131 assert called(Instances.set_reachable(inbox))
134 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
138 actor = insert(:user)
139 inbox = "http://200.site/users/nick1/inbox"
142 Publisher.publish_one(%{
147 unreachable_since: NaiveDateTime.utc_now()
150 assert called(Instances.set_reachable(inbox))
153 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
157 actor = insert(:user)
158 inbox = "http://200.site/users/nick1/inbox"
161 Publisher.publish_one(%{
166 unreachable_since: nil
169 refute called(Instances.set_reachable(inbox))
172 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
176 actor = insert(:user)
177 inbox = "http://404.site/users/nick1/inbox"
179 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
181 assert called(Instances.set_unreachable(inbox))
184 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
188 actor = insert(:user)
189 inbox = "http://connrefused.site/users/nick1/inbox"
191 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
193 assert called(Instances.set_unreachable(inbox))
196 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
200 actor = insert(:user)
201 inbox = "http://200.site/users/nick1/inbox"
203 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
205 refute called(Instances.set_unreachable(inbox))
208 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
212 actor = insert(:user)
213 inbox = "http://connrefused.site/users/nick1/inbox"
216 Publisher.publish_one(%{
221 unreachable_since: NaiveDateTime.utc_now()
224 refute called(Instances.set_unreachable(inbox))
228 describe "publish/2" do
229 test_with_mock "publishes an activity with BCC to all relevant peers.",
230 Pleroma.Web.Federator.Publisher,
238 source_data: %{"inbox" => "https://domain.com/users/nick1/inbox"}
242 actor = insert(:user, follower_address: follower.ap_id)
245 {:ok, _follower_one} = Pleroma.User.follow(follower, actor)
246 actor = refresh_record(actor)
249 insert(:note_activity,
250 recipients: [follower.ap_id],
251 data_attrs: %{"bcc" => [user.ap_id]}
254 res = Publisher.publish(actor, note_activity)
258 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
259 inbox: "https://domain.com/users/nick1/inbox",
261 id: note_activity.data["id"]