1 defmodule Pleroma.Web.FederatorTest do
2 alias Pleroma.Web.Federator
3 alias Pleroma.Web.CommonAPI
9 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
13 test "enqueues an element according to priority" do
14 queue = [%{item: 1, priority: 2}]
16 new_queue = Federator.enqueue_sorted(queue, 2, 1)
17 assert new_queue == [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
19 new_queue = Federator.enqueue_sorted(queue, 2, 3)
20 assert new_queue == [%{item: 1, priority: 2}, %{item: 2, priority: 3}]
23 test "pop first item" do
24 queue = [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
26 assert {2, [%{item: 1, priority: 2}]} = Federator.queue_pop(queue)
29 describe "Publish an activity" do
32 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})
35 Pleroma.Web.ActivityPub.Relay,
37 [publish: fn _activity -> send(self(), :relay_publish) end]
40 %{activity: activity, relay_mock: relay_mock}
43 test "with relays active, it publishes to the relay", %{
45 relay_mock: relay_mock
47 with_mocks([relay_mock]) do
48 Federator.handle(:publish, activity)
51 assert_received :relay_publish
54 test "with relays deactivated, it does not publish to the relay", %{
56 relay_mock: relay_mock
58 Pleroma.Config.put([:instance, :allow_relay], false)
60 with_mocks([relay_mock]) do
61 Federator.handle(:publish, activity)
64 refute_received :relay_publish
66 Pleroma.Config.put([:instance, :allow_relay], true)
70 describe "Receive an activity" do
71 test "successfully processes incoming AP docs with correct origin" do
73 "@context" => "https://www.w3.org/ns/activitystreams",
74 "actor" => "http://mastodon.example.org/users/admin",
76 "id" => "http://mastodon.example.org/users/admin/activities/1",
79 "content" => "hi world!",
80 "id" => "http://mastodon.example.org/users/admin/objects/1",
81 "attributedTo" => "http://mastodon.example.org/users/admin"
83 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
86 {:ok, _activity} = Federator.handle(:incoming_ap_doc, params)
89 test "rejects incoming AP docs with incorrect origin" do
91 "@context" => "https://www.w3.org/ns/activitystreams",
92 "actor" => "https://niu.moe/users/rye",
94 "id" => "http://mastodon.example.org/users/admin/activities/1",
97 "content" => "hi world!",
98 "id" => "http://mastodon.example.org/users/admin/objects/1",
99 "attributedTo" => "http://mastodon.example.org/users/admin"
101 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
104 :error = Federator.handle(:incoming_ap_doc, params)