1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier.FollowHandlingTest do
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 alias Pleroma.Web.ActivityPub.Utils
13 import Pleroma.Factory
17 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 describe "handle_incoming" do
22 test "it works for incoming follow requests" do
26 File.read!("test/fixtures/mastodon-follow-activity.json")
28 |> Map.put("object", user.ap_id)
30 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
32 assert data["actor"] == "http://mastodon.example.org/users/admin"
33 assert data["type"] == "Follow"
34 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
35 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
38 test "it works for follow requests when you are already followed, creating a new accept activity" do
39 # This is important because the remote might have the wrong idea about the
40 # current follow status. This can lead to instance A thinking that x@A is
41 # followed by y@B, but B thinks they are not. In this case, the follow can
42 # never go through again because it will never get an Accept.
46 File.read!("test/fixtures/mastodon-follow-activity.json")
48 |> Map.put("object", user.ap_id)
50 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
55 where: fragment("?->>'type' = ?", a.data, "Accept")
59 assert length(accepts) == 1
62 File.read!("test/fixtures/mastodon-follow-activity.json")
64 |> Map.put("id", String.replace(data["id"], "2", "3"))
65 |> Map.put("object", user.ap_id)
67 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
72 where: fragment("?->>'type' = ?", a.data, "Accept")
76 assert length(accepts) == 2
79 test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
80 Pleroma.Config.put([:user, :deny_follow_blocked], true)
83 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
85 {:ok, user} = User.block(user, target)
88 File.read!("test/fixtures/mastodon-follow-activity.json")
90 |> Map.put("object", user.ap_id)
92 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
94 %Activity{} = activity = Activity.get_by_ap_id(id)
96 assert activity.data["state"] == "reject"
99 test "it works for incoming follow requests from hubzilla" do
103 File.read!("test/fixtures/hubzilla-follow-activity.json")
105 |> Map.put("object", user.ap_id)
106 |> Utils.normalize_params()
108 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
110 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
111 assert data["type"] == "Follow"
112 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
113 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)