Merge branch 'exclude-reblogs-from-admin-api-by-default' into 'develop'
[akkoma] / test / web / activity_pub / relay_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.RelayTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Activity
9 alias Pleroma.Object
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Relay
13
14 import ExUnit.CaptureLog
15 import Pleroma.Factory
16 import Mock
17
18 test "gets an actor for the relay" do
19 user = Relay.get_actor()
20 assert user.ap_id == "#{Pleroma.Web.Endpoint.url()}/relay"
21 end
22
23 test "relay actor is invisible" do
24 user = Relay.get_actor()
25 assert User.invisible?(user)
26 end
27
28 describe "follow/1" do
29 test "returns errors when user not found" do
30 assert capture_log(fn ->
31 {:error, _} = Relay.follow("test-ap-id")
32 end) =~ "Could not decode user at fetch"
33 end
34
35 test "returns activity" do
36 user = insert(:user)
37 service_actor = Relay.get_actor()
38 assert {:ok, %Activity{} = activity} = Relay.follow(user.ap_id)
39 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
40 assert user.ap_id in activity.recipients
41 assert activity.data["type"] == "Follow"
42 assert activity.data["actor"] == service_actor.ap_id
43 assert activity.data["object"] == user.ap_id
44 end
45 end
46
47 describe "unfollow/1" do
48 test "returns errors when user not found" do
49 assert capture_log(fn ->
50 {:error, _} = Relay.unfollow("test-ap-id")
51 end) =~ "Could not decode user at fetch"
52 end
53
54 test "returns activity" do
55 user = insert(:user)
56 service_actor = Relay.get_actor()
57 ActivityPub.follow(service_actor, user)
58 Pleroma.User.follow(service_actor, user)
59 assert "#{user.ap_id}/followers" in User.following(service_actor)
60 assert {:ok, %Activity{} = activity} = Relay.unfollow(user.ap_id)
61 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
62 assert user.ap_id in activity.recipients
63 assert activity.data["type"] == "Undo"
64 assert activity.data["actor"] == service_actor.ap_id
65 assert activity.data["to"] == [user.ap_id]
66 refute "#{user.ap_id}/followers" in User.following(service_actor)
67 end
68 end
69
70 describe "publish/1" do
71 clear_config([:instance, :federating])
72
73 test "returns error when activity not `Create` type" do
74 activity = insert(:like_activity)
75 assert Relay.publish(activity) == {:error, "Not implemented"}
76 end
77
78 test "returns error when activity not public" do
79 activity = insert(:direct_note_activity)
80 assert Relay.publish(activity) == {:error, false}
81 end
82
83 test "returns error when object is unknown" do
84 activity =
85 insert(:note_activity,
86 data: %{
87 "type" => "Create",
88 "object" => "http://mastodon.example.org/eee/99541947525187367"
89 }
90 )
91
92 assert capture_log(fn ->
93 assert Relay.publish(activity) == {:error, nil}
94 end) =~ "[error] error: nil"
95 end
96
97 test_with_mock "returns announce activity and publish to federate",
98 Pleroma.Web.Federator,
99 [:passthrough],
100 [] do
101 Pleroma.Config.put([:instance, :federating], true)
102 service_actor = Relay.get_actor()
103 note = insert(:note_activity)
104 assert {:ok, %Activity{} = activity, %Object{} = obj} = Relay.publish(note)
105 assert activity.data["type"] == "Announce"
106 assert activity.data["actor"] == service_actor.ap_id
107 assert activity.data["object"] == obj.data["id"]
108 assert called(Pleroma.Web.Federator.publish(activity))
109 end
110
111 test_with_mock "returns announce activity and not publish to federate",
112 Pleroma.Web.Federator,
113 [:passthrough],
114 [] do
115 Pleroma.Config.put([:instance, :federating], false)
116 service_actor = Relay.get_actor()
117 note = insert(:note_activity)
118 assert {:ok, %Activity{} = activity, %Object{} = obj} = Relay.publish(note)
119 assert activity.data["type"] == "Announce"
120 assert activity.data["actor"] == service_actor.ap_id
121 assert activity.data["object"] == obj.data["id"]
122 refute called(Pleroma.Web.Federator.publish(activity))
123 end
124 end
125 end