1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
18 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 describe "handle_incoming" do
23 setup do: clear_config([:user, :deny_follow_blocked])
25 test "it works for osada follow request" do
29 File.read!("test/fixtures/osada-follow-activity.json")
31 |> Map.put("object", user.ap_id)
33 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
35 assert data["actor"] == "https://apfed.club/channel/indio"
36 assert data["type"] == "Follow"
37 assert data["id"] == "https://apfed.club/follow/9"
39 activity = Repo.get(Activity, activity.id)
40 assert activity.data["state"] == "accept"
41 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
44 test "it works for incoming follow requests" do
48 File.read!("test/fixtures/mastodon-follow-activity.json")
50 |> Map.put("object", user.ap_id)
52 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
54 assert data["actor"] == "http://mastodon.example.org/users/admin"
55 assert data["type"] == "Follow"
56 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
58 activity = Repo.get(Activity, activity.id)
59 assert activity.data["state"] == "accept"
60 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
62 [notification] = Notification.for_user(user)
63 assert notification.type == "follow"
66 test "with locked accounts, it does create a Follow, but not an Accept" do
67 user = insert(:user, locked: true)
70 File.read!("test/fixtures/mastodon-follow-activity.json")
72 |> Map.put("object", user.ap_id)
74 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
76 assert data["state"] == "pending"
78 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
83 where: fragment("?->>'type' = ?", a.data, "Accept")
87 assert Enum.empty?(accepts)
89 [notification] = Notification.for_user(user)
90 assert notification.type == "follow_request"
93 test "it works for follow requests when you are already followed, creating a new accept activity" do
94 # This is important because the remote might have the wrong idea about the
95 # current follow status. This can lead to instance A thinking that x@A is
96 # followed by y@B, but B thinks they are not. In this case, the follow can
97 # never go through again because it will never get an Accept.
101 File.read!("test/fixtures/mastodon-follow-activity.json")
103 |> Map.put("object", user.ap_id)
105 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
110 where: fragment("?->>'type' = ?", a.data, "Accept")
114 assert length(accepts) == 1
117 File.read!("test/fixtures/mastodon-follow-activity.json")
119 |> Map.put("id", String.replace(data["id"], "2", "3"))
120 |> Map.put("object", user.ap_id)
122 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
127 where: fragment("?->>'type' = ?", a.data, "Accept")
131 assert length(accepts) == 2
134 test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
135 Pleroma.Config.put([:user, :deny_follow_blocked], true)
138 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
140 {:ok, _user_relationship} = User.block(user, target)
143 File.read!("test/fixtures/mastodon-follow-activity.json")
145 |> Map.put("object", user.ap_id)
147 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
149 %Activity{} = activity = Activity.get_by_ap_id(id)
151 assert activity.data["state"] == "reject"
154 test "it works for incoming follow requests from hubzilla" do
158 File.read!("test/fixtures/hubzilla-follow-activity.json")
160 |> Map.put("object", user.ap_id)
161 |> Utils.normalize_params()
163 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
165 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
166 assert data["type"] == "Follow"
167 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
168 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)