activity_pub_controller: Add authentication to object & activity endpoints
[akkoma] / test / pleroma / web / activity_pub / relay_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.User
10 alias Pleroma.Web.ActivityPub.Relay
11 alias Pleroma.Web.CommonAPI
12
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
15 import Mock
16
17 test "gets an actor for the relay" do
18 user = Relay.get_actor()
19 assert user.ap_id == "#{Pleroma.Web.Endpoint.url()}/relay"
20 end
21
22 test "relay actor is invisible" do
23 user = Relay.get_actor()
24 assert User.invisible?(user)
25 end
26
27 describe "follow/1" do
28 test "returns errors when user not found" do
29 assert capture_log(fn ->
30 {:error, _} = Relay.follow("test-ap-id")
31 end) =~ "Could not decode user at fetch"
32 end
33
34 test "returns activity" do
35 user = insert(:user)
36 service_actor = Relay.get_actor()
37 assert {:ok, %Activity{} = activity} = Relay.follow(user.ap_id)
38 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
39 assert user.ap_id in activity.recipients
40 assert activity.data["type"] == "Follow"
41 assert activity.data["actor"] == service_actor.ap_id
42 assert activity.data["object"] == user.ap_id
43 end
44 end
45
46 describe "unfollow/1" do
47 test "returns errors when user not found" do
48 assert capture_log(fn ->
49 {:error, _} = Relay.unfollow("test-ap-id")
50 end) =~ "Could not decode user at fetch"
51 end
52
53 test "returns activity" do
54 user = insert(:user)
55 service_actor = Relay.get_actor()
56 CommonAPI.follow(service_actor, user)
57 assert "#{user.ap_id}/followers" in User.following(service_actor)
58 assert {:ok, %Activity{} = activity} = Relay.unfollow(user.ap_id)
59 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
60 assert user.ap_id in activity.recipients
61 assert activity.data["type"] == "Undo"
62 assert activity.data["actor"] == service_actor.ap_id
63 assert activity.data["to"] == [user.ap_id]
64 refute "#{user.ap_id}/followers" in User.following(service_actor)
65 end
66
67 test "force unfollow when target service is dead" do
68 user = insert(:user)
69 user_ap_id = user.ap_id
70 user_id = user.id
71
72 Tesla.Mock.mock(fn %{method: :get, url: ^user_ap_id} ->
73 %Tesla.Env{status: 404}
74 end)
75
76 service_actor = Relay.get_actor()
77 CommonAPI.follow(service_actor, user)
78 assert "#{user.ap_id}/followers" in User.following(service_actor)
79
80 assert Pleroma.Repo.get_by(
81 Pleroma.FollowingRelationship,
82 follower_id: service_actor.id,
83 following_id: user_id
84 )
85
86 Pleroma.Repo.delete(user)
87 User.invalidate_cache(user)
88
89 assert {:ok, %Activity{} = activity} = Relay.unfollow(user_ap_id, %{force: true})
90
91 assert refresh_record(service_actor).following_count == 0
92
93 refute Pleroma.Repo.get_by(
94 Pleroma.FollowingRelationship,
95 follower_id: service_actor.id,
96 following_id: user_id
97 )
98
99 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
100 assert user.ap_id in activity.recipients
101 assert activity.data["type"] == "Undo"
102 assert activity.data["actor"] == service_actor.ap_id
103 assert activity.data["to"] == [user_ap_id]
104 refute "#{user.ap_id}/followers" in User.following(service_actor)
105 end
106 end
107
108 describe "publish/1" do
109 setup do: clear_config([:instance, :federating])
110
111 test "returns error when activity not `Create` type" do
112 activity = insert(:like_activity)
113 assert Relay.publish(activity) == {:error, "Not implemented"}
114 end
115
116 @tag capture_log: true
117 test "returns error when activity not public" do
118 activity = insert(:direct_note_activity)
119 assert Relay.publish(activity) == {:error, false}
120 end
121
122 test "returns error when object is unknown" do
123 activity =
124 insert(:note_activity,
125 data: %{
126 "type" => "Create",
127 "object" => "http://mastodon.example.org/eee/99541947525187367"
128 }
129 )
130
131 Tesla.Mock.mock(fn
132 %{method: :get, url: "http://mastodon.example.org/eee/99541947525187367"} ->
133 %Tesla.Env{status: 500, body: ""}
134 end)
135
136 assert capture_log(fn ->
137 assert Relay.publish(activity) == {:error, false}
138 end) =~ "[error] error: false"
139 end
140
141 test_with_mock "returns announce activity and publish to federate",
142 Pleroma.Web.Federator,
143 [:passthrough],
144 [] do
145 clear_config([:instance, :federating], true)
146 service_actor = Relay.get_actor()
147 note = insert(:note_activity)
148 assert {:ok, %Activity{} = activity} = Relay.publish(note)
149 assert activity.data["type"] == "Announce"
150 assert activity.data["actor"] == service_actor.ap_id
151 assert activity.data["to"] == [service_actor.follower_address]
152 assert called(Pleroma.Web.Federator.publish(activity))
153 end
154
155 test_with_mock "returns announce activity and not publish to federate",
156 Pleroma.Web.Federator,
157 [:passthrough],
158 [] do
159 clear_config([:instance, :federating], false)
160 service_actor = Relay.get_actor()
161 note = insert(:note_activity)
162 assert {:ok, %Activity{} = activity} = Relay.publish(note)
163 assert activity.data["type"] == "Announce"
164 assert activity.data["actor"] == service_actor.ap_id
165 refute called(Pleroma.Web.Federator.publish(activity))
166 end
167 end
168 end