1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.RelayTest do
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.ActivityPub.Relay
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
17 test "gets an actor for the relay" do
18 user = Relay.get_actor()
19 assert user.ap_id == "#{Pleroma.Web.Endpoint.url()}/relay"
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"
29 test "returns activity" do
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
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"
48 test "returns activity" do
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
64 describe "publish/1" do
65 clear_config([:instance, :federating])
67 test "returns error when activity not `Create` type" do
68 activity = insert(:like_activity)
69 assert Relay.publish(activity) == {:error, "Not implemented"}
72 test "returns error when activity not public" do
73 activity = insert(:direct_note_activity)
74 assert Relay.publish(activity) == {:error, false}
77 test "returns error when object is unknown" do
79 insert(:note_activity,
82 "object" => "http://mastodon.example.org/eee/99541947525187367"
86 assert capture_log(fn ->
87 assert Relay.publish(activity) == {:error, nil}
88 end) =~ "[error] error: nil"
91 test_with_mock "returns announce activity and publish to federate",
92 Pleroma.Web.Federator,
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))
105 test_with_mock "returns announce activity and not publish to federate",
106 Pleroma.Web.Federator,
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))