1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.FederatorTest do
6 alias Pleroma.Web.Federator
7 alias Pleroma.Web.CommonAPI
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 test "enqueues an element according to priority" do
18 queue = [%{item: 1, priority: 2}]
20 new_queue = Federator.enqueue_sorted(queue, 2, 1)
21 assert new_queue == [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
23 new_queue = Federator.enqueue_sorted(queue, 2, 3)
24 assert new_queue == [%{item: 1, priority: 2}, %{item: 2, priority: 3}]
27 test "pop first item" do
28 queue = [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
30 assert {2, [%{item: 1, priority: 2}]} = Federator.queue_pop(queue)
33 describe "Publish an activity" do
36 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})
39 Pleroma.Web.ActivityPub.Relay,
41 [publish: fn _activity -> send(self(), :relay_publish) end]
44 %{activity: activity, relay_mock: relay_mock}
47 test "with relays active, it publishes to the relay", %{
49 relay_mock: relay_mock
51 with_mocks([relay_mock]) do
52 Federator.handle(:publish, activity)
55 assert_received :relay_publish
58 test "with relays deactivated, it does not publish to the relay", %{
60 relay_mock: relay_mock
62 Pleroma.Config.put([:instance, :allow_relay], false)
64 with_mocks([relay_mock]) do
65 Federator.handle(:publish, activity)
68 refute_received :relay_publish
70 Pleroma.Config.put([:instance, :allow_relay], true)
74 describe "Receive an activity" do
75 test "successfully processes incoming AP docs with correct origin" do
77 "@context" => "https://www.w3.org/ns/activitystreams",
78 "actor" => "http://mastodon.example.org/users/admin",
80 "id" => "http://mastodon.example.org/users/admin/activities/1",
83 "content" => "hi world!",
84 "id" => "http://mastodon.example.org/users/admin/objects/1",
85 "attributedTo" => "http://mastodon.example.org/users/admin"
87 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
90 {:ok, _activity} = Federator.handle(:incoming_ap_doc, params)
93 test "rejects incoming AP docs with incorrect origin" do
95 "@context" => "https://www.w3.org/ns/activitystreams",
96 "actor" => "https://niu.moe/users/rye",
98 "id" => "http://mastodon.example.org/users/admin/activities/1",
101 "content" => "hi world!",
102 "id" => "http://mastodon.example.org/users/admin/objects/1",
103 "attributedTo" => "http://mastodon.example.org/users/admin"
105 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
108 :error = Federator.handle(:incoming_ap_doc, params)