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.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 osada follow request" do
26 File.read!("test/fixtures/osada-follow-activity.json")
28 |> Map.put("object", user.ap_id)
30 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
32 assert data["actor"] == "https://apfed.club/channel/indio"
33 assert data["type"] == "Follow"
34 assert data["id"] == "https://apfed.club/follow/9"
36 activity = Repo.get(Activity, activity.id)
37 assert activity.data["state"] == "accept"
38 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
41 test "it works for incoming follow requests" do
45 File.read!("test/fixtures/mastodon-follow-activity.json")
47 |> Map.put("object", user.ap_id)
49 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
51 assert data["actor"] == "http://mastodon.example.org/users/admin"
52 assert data["type"] == "Follow"
53 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
55 activity = Repo.get(Activity, activity.id)
56 assert activity.data["state"] == "accept"
57 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
60 test "with locked accounts, it does not create a follow or an accept" do
61 user = insert(:user, info: %{locked: true})
64 File.read!("test/fixtures/mastodon-follow-activity.json")
66 |> Map.put("object", user.ap_id)
68 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
70 assert data["state"] == "pending"
72 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
77 where: fragment("?->>'type' = ?", a.data, "Accept")
81 assert length(accepts) == 0
84 test "it works for follow requests when you are already followed, creating a new accept activity" do
85 # This is important because the remote might have the wrong idea about the
86 # current follow status. This can lead to instance A thinking that x@A is
87 # followed by y@B, but B thinks they are not. In this case, the follow can
88 # never go through again because it will never get an Accept.
92 File.read!("test/fixtures/mastodon-follow-activity.json")
94 |> Map.put("object", user.ap_id)
96 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
101 where: fragment("?->>'type' = ?", a.data, "Accept")
105 assert length(accepts) == 1
108 File.read!("test/fixtures/mastodon-follow-activity.json")
110 |> Map.put("id", String.replace(data["id"], "2", "3"))
111 |> Map.put("object", user.ap_id)
113 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
118 where: fragment("?->>'type' = ?", a.data, "Accept")
122 assert length(accepts) == 2
125 test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
126 Pleroma.Config.put([:user, :deny_follow_blocked], true)
129 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
131 {:ok, user} = User.block(user, target)
134 File.read!("test/fixtures/mastodon-follow-activity.json")
136 |> Map.put("object", user.ap_id)
138 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
140 %Activity{} = activity = Activity.get_by_ap_id(id)
142 assert activity.data["state"] == "reject"
145 test "it works for incoming follow requests from hubzilla" do
149 File.read!("test/fixtures/hubzilla-follow-activity.json")
151 |> Map.put("object", user.ap_id)
152 |> Utils.normalize_params()
154 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
156 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
157 assert data["type"] == "Follow"
158 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
159 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)