1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier.FollowHandlingTest do
8 alias Pleroma.Notification
11 alias Pleroma.Web.ActivityPub.Transmogrifier
12 alias Pleroma.Web.ActivityPub.Utils
14 import Pleroma.Factory
19 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 describe "handle_incoming" do
24 setup do: clear_config([:user, :deny_follow_blocked])
26 test "it works for osada follow request" do
30 File.read!("test/fixtures/osada-follow-activity.json")
32 |> Map.put("object", user.ap_id)
34 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
36 assert data["actor"] == "https://apfed.club/channel/indio"
37 assert data["type"] == "Follow"
38 assert data["id"] == "https://apfed.club/follow/9"
40 activity = Repo.get(Activity, activity.id)
41 assert activity.data["state"] == "accept"
42 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
45 test "it works for incoming follow requests" do
49 File.read!("test/fixtures/mastodon-follow-activity.json")
51 |> Map.put("object", user.ap_id)
53 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
55 assert data["actor"] == "http://mastodon.example.org/users/admin"
56 assert data["type"] == "Follow"
57 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
59 activity = Repo.get(Activity, activity.id)
60 assert activity.data["state"] == "accept"
61 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
63 [notification] = Notification.for_user(user)
64 assert notification.type == "follow"
67 test "with locked accounts, it does create a Follow, but not an Accept" do
68 user = insert(:user, is_locked: true)
71 File.read!("test/fixtures/mastodon-follow-activity.json")
73 |> Map.put("object", user.ap_id)
75 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
77 assert data["state"] == "pending"
79 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
84 where: fragment("?->>'type' = ?", a.data, "Accept")
88 assert Enum.empty?(accepts)
90 [notification] = Notification.for_user(user)
91 assert notification.type == "follow_request"
94 test "it works for follow requests when you are already followed, creating a new accept activity" do
95 # This is important because the remote might have the wrong idea about the
96 # current follow status. This can lead to instance A thinking that x@A is
97 # followed by y@B, but B thinks they are not. In this case, the follow can
98 # never go through again because it will never get an Accept.
102 File.read!("test/fixtures/mastodon-follow-activity.json")
104 |> Map.put("object", user.ap_id)
106 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
111 where: fragment("?->>'type' = ?", a.data, "Accept")
115 assert length(accepts) == 1
118 File.read!("test/fixtures/mastodon-follow-activity.json")
120 |> Map.put("id", String.replace(data["id"], "2", "3"))
121 |> Map.put("object", user.ap_id)
123 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
128 where: fragment("?->>'type' = ?", a.data, "Accept")
132 assert length(accepts) == 2
135 test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
136 clear_config([:user, :deny_follow_blocked], true)
139 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
141 {:ok, _user_relationship} = User.block(user, target)
144 File.read!("test/fixtures/mastodon-follow-activity.json")
146 |> Map.put("object", user.ap_id)
148 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
150 %Activity{} = activity = Activity.get_by_ap_id(id)
152 assert activity.data["state"] == "reject"
155 test "it rejects incoming follow requests if the following errors for some reason" do
159 File.read!("test/fixtures/mastodon-follow-activity.json")
161 |> Map.put("object", user.ap_id)
163 with_mock Pleroma.User, [:passthrough], follow: fn _, _, _ -> {:error, :testing} end do
164 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
166 %Activity{} = activity = Activity.get_by_ap_id(id)
168 assert activity.data["state"] == "reject"
172 test "it works for incoming follow requests from hubzilla" do
176 File.read!("test/fixtures/hubzilla-follow-activity.json")
178 |> Map.put("object", user.ap_id)
179 |> Utils.normalize_params()
181 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
183 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
184 assert data["type"] == "Follow"
185 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
186 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
189 test "it works for incoming follows to locked account" do
190 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
191 user = insert(:user, is_locked: true)
194 File.read!("test/fixtures/mastodon-follow-activity.json")
196 |> Map.put("object", user.ap_id)
198 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
200 assert data["type"] == "Follow"
201 assert data["object"] == user.ap_id
202 assert data["state"] == "pending"
203 assert data["actor"] == "http://mastodon.example.org/users/admin"
205 assert [^pending_follower] = User.get_follow_requests(user)