Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / web / activity_pub / relay_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.ActivityPub.Relay
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 describe "follow/1" do
23 test "returns errors when user not found" do
24 assert capture_log(fn ->
25 assert Relay.follow("test-ap-id") == {:error, "Could not fetch by AP id"}
26 end) =~ "Could not fetch by AP id"
27 end
28
29 test "returns activity" do
30 user = insert(:user)
31 service_actor = Relay.get_actor()
32 assert {:ok, %Activity{} = activity} = Relay.follow(user.ap_id)
33 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
34 assert user.ap_id in activity.recipients
35 assert activity.data["type"] == "Follow"
36 assert activity.data["actor"] == service_actor.ap_id
37 assert activity.data["object"] == user.ap_id
38 end
39 end
40
41 describe "unfollow/1" do
42 test "returns errors when user not found" do
43 assert capture_log(fn ->
44 assert Relay.unfollow("test-ap-id") == {:error, "Could not fetch by AP id"}
45 end) =~ "Could not fetch by AP id"
46 end
47
48 test "returns activity" do
49 user = insert(:user)
50 service_actor = Relay.get_actor()
51 ActivityPub.follow(service_actor, user)
52 Pleroma.User.follow(service_actor, user)
53 assert "#{user.ap_id}/followers" in refresh_record(service_actor).following
54 assert {:ok, %Activity{} = activity} = Relay.unfollow(user.ap_id)
55 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
56 assert user.ap_id in activity.recipients
57 assert activity.data["type"] == "Undo"
58 assert activity.data["actor"] == service_actor.ap_id
59 assert activity.data["to"] == [user.ap_id]
60 refute "#{user.ap_id}/followers" in refresh_record(service_actor).following
61 end
62 end
63
64 describe "publish/1" do
65 clear_config([:instance, :federating])
66
67 test "returns error when activity not `Create` type" do
68 activity = insert(:like_activity)
69 assert Relay.publish(activity) == {:error, "Not implemented"}
70 end
71
72 test "returns error when activity not public" do
73 activity = insert(:direct_note_activity)
74 assert Relay.publish(activity) == {:error, false}
75 end
76
77 test "returns error when object is unknown" do
78 activity =
79 insert(:note_activity,
80 data: %{
81 "type" => "Create",
82 "object" => "http://mastodon.example.org/eee/99541947525187367"
83 }
84 )
85
86 assert capture_log(fn ->
87 assert Relay.publish(activity) == {:error, nil}
88 end) =~ "[error] error: nil"
89 end
90
91 test_with_mock "returns announce activity and publish to federate",
92 Pleroma.Web.Federator,
93 [:passthrough],
94 [] do
95 Pleroma.Config.put([:instance, :federating], true)
96 service_actor = Relay.get_actor()
97 note = insert(:note_activity)
98 assert {:ok, %Activity{} = activity, %Object{} = obj} = Relay.publish(note)
99 assert activity.data["type"] == "Announce"
100 assert activity.data["actor"] == service_actor.ap_id
101 assert activity.data["object"] == obj.data["id"]
102 assert called(Pleroma.Web.Federator.publish(activity))
103 end
104
105 test_with_mock "returns announce activity and not publish to federate",
106 Pleroma.Web.Federator,
107 [:passthrough],
108 [] do
109 Pleroma.Config.put([:instance, :federating], false)
110 service_actor = Relay.get_actor()
111 note = insert(:note_activity)
112 assert {:ok, %Activity{} = activity, %Object{} = obj} = Relay.publish(note)
113 assert activity.data["type"] == "Announce"
114 assert activity.data["actor"] == service_actor.ap_id
115 assert activity.data["object"] == obj.data["id"]
116 refute called(Pleroma.Web.Federator.publish(activity))
117 end
118 end
119 end