Merge branch 'develop' into feature/compat/push-subscriptions
[akkoma] / test / web / federator_test.exs
1 defmodule Pleroma.Web.FederatorTest do
2 alias Pleroma.Web.Federator
3 alias Pleroma.Web.CommonAPI
4 use Pleroma.DataCase
5 import Pleroma.Factory
6 import Mock
7
8 test "enqueues an element according to priority" do
9 queue = [%{item: 1, priority: 2}]
10
11 new_queue = Federator.enqueue_sorted(queue, 2, 1)
12 assert new_queue == [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
13
14 new_queue = Federator.enqueue_sorted(queue, 2, 3)
15 assert new_queue == [%{item: 1, priority: 2}, %{item: 2, priority: 3}]
16 end
17
18 test "pop first item" do
19 queue = [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
20
21 assert {2, [%{item: 1, priority: 2}]} = Federator.queue_pop(queue)
22 end
23
24 describe "Publish an activity" do
25 setup do
26 user = insert(:user)
27 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})
28
29 relay_mock = {
30 Pleroma.Web.ActivityPub.Relay,
31 [],
32 [publish: fn _activity -> send(self(), :relay_publish) end]
33 }
34
35 %{activity: activity, relay_mock: relay_mock}
36 end
37
38 test "with relays active, it publishes to the relay", %{
39 activity: activity,
40 relay_mock: relay_mock
41 } do
42 with_mocks([relay_mock]) do
43 Federator.handle(:publish, activity)
44 end
45
46 assert_received :relay_publish
47 end
48
49 test "with relays deactivated, it does not publish to the relay", %{
50 activity: activity,
51 relay_mock: relay_mock
52 } do
53 Pleroma.Config.put([:instance, :allow_relay], false)
54
55 with_mocks([relay_mock]) do
56 Federator.handle(:publish, activity)
57 end
58
59 refute_received :relay_publish
60
61 Pleroma.Config.put([:instance, :allow_relay], true)
62 end
63 end
64
65 describe "Receive an activity" do
66 test "successfully processes incoming AP docs with correct origin" do
67 params = %{
68 "@context" => "https://www.w3.org/ns/activitystreams",
69 "actor" => "http://mastodon.example.org/users/admin",
70 "type" => "Create",
71 "id" => "http://mastodon.example.org/users/admin/activities/1",
72 "object" => %{
73 "type" => "Note",
74 "content" => "hi world!",
75 "id" => "http://mastodon.example.org/users/admin/objects/1",
76 "attributedTo" => "http://mastodon.example.org/users/admin"
77 },
78 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
79 }
80
81 {:ok, _activity} = Federator.handle(:incoming_ap_doc, params)
82 end
83
84 test "rejects incoming AP docs with incorrect origin" do
85 params = %{
86 "@context" => "https://www.w3.org/ns/activitystreams",
87 "actor" => "https://niu.moe/users/rye",
88 "type" => "Create",
89 "id" => "http://mastodon.example.org/users/admin/activities/1",
90 "object" => %{
91 "type" => "Note",
92 "content" => "hi world!",
93 "id" => "http://mastodon.example.org/users/admin/objects/1",
94 "attributedTo" => "http://mastodon.example.org/users/admin"
95 },
96 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
97 }
98
99 :error = Federator.handle(:incoming_ap_doc, params)
100 end
101 end
102 end