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