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