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} = activity} = 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"
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 "with locked accounts, it does not create a follow or an accept" do
42 user = insert(:user, info: %{locked: true})
45 File.read!("test/fixtures/mastodon-follow-activity.json")
47 |> Map.put("object", user.ap_id)
49 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
51 assert data["state"] == "pending"
53 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
58 where: fragment("?->>'type' = ?", a.data, "Accept")
62 assert length(accepts) == 0
65 test "it works for follow requests when you are already followed, creating a new accept activity" do
66 # This is important because the remote might have the wrong idea about the
67 # current follow status. This can lead to instance A thinking that x@A is
68 # followed by y@B, but B thinks they are not. In this case, the follow can
69 # never go through again because it will never get an Accept.
73 File.read!("test/fixtures/mastodon-follow-activity.json")
75 |> Map.put("object", user.ap_id)
77 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
82 where: fragment("?->>'type' = ?", a.data, "Accept")
86 assert length(accepts) == 1
89 File.read!("test/fixtures/mastodon-follow-activity.json")
91 |> Map.put("id", String.replace(data["id"], "2", "3"))
92 |> Map.put("object", user.ap_id)
94 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
99 where: fragment("?->>'type' = ?", a.data, "Accept")
103 assert length(accepts) == 2
106 test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
107 Pleroma.Config.put([:user, :deny_follow_blocked], true)
110 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
112 {:ok, user} = User.block(user, target)
115 File.read!("test/fixtures/mastodon-follow-activity.json")
117 |> Map.put("object", user.ap_id)
119 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
121 %Activity{} = activity = Activity.get_by_ap_id(id)
123 assert activity.data["state"] == "reject"
126 test "it works for incoming follow requests from hubzilla" do
130 File.read!("test/fixtures/hubzilla-follow-activity.json")
132 |> Map.put("object", user.ap_id)
133 |> Utils.normalize_params()
135 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
137 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
138 assert data["type"] == "Follow"
139 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
140 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)