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