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