5ddf6cd52c2bb3020e0ed77395da92fc30263f72
[akkoma] / test / web / activity_pub / transmogrifier / follow_handling_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier.FollowHandlingTest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Repo
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 alias Pleroma.Web.ActivityPub.Utils
12
13 import Pleroma.Factory
14 import Ecto.Query
15
16 setup_all do
17 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 :ok
19 end
20
21 describe "handle_incoming" do
22 test "it works for incoming follow requests" do
23 user = insert(:user)
24
25 data =
26 File.read!("test/fixtures/mastodon-follow-activity.json")
27 |> Poison.decode!()
28 |> Map.put("object", user.ap_id)
29
30 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
31
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)
36 end
37
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.
43 user = insert(:user)
44
45 data =
46 File.read!("test/fixtures/mastodon-follow-activity.json")
47 |> Poison.decode!()
48 |> Map.put("object", user.ap_id)
49
50 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
51
52 accepts =
53 from(
54 a in Activity,
55 where: fragment("?->>'type' = ?", a.data, "Accept")
56 )
57 |> Repo.all()
58
59 assert length(accepts) == 1
60
61 data =
62 File.read!("test/fixtures/mastodon-follow-activity.json")
63 |> Poison.decode!()
64 |> Map.put("id", String.replace(data["id"], "2", "3"))
65 |> Map.put("object", user.ap_id)
66
67 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
68
69 accepts =
70 from(
71 a in Activity,
72 where: fragment("?->>'type' = ?", a.data, "Accept")
73 )
74 |> Repo.all()
75
76 assert length(accepts) == 2
77 end
78
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)
81
82 user = insert(:user)
83 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
84
85 {:ok, user} = User.block(user, target)
86
87 data =
88 File.read!("test/fixtures/mastodon-follow-activity.json")
89 |> Poison.decode!()
90 |> Map.put("object", user.ap_id)
91
92 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
93
94 %Activity{} = activity = Activity.get_by_ap_id(id)
95
96 assert activity.data["state"] == "reject"
97 end
98
99 test "it works for incoming follow requests from hubzilla" do
100 user = insert(:user)
101
102 data =
103 File.read!("test/fixtures/hubzilla-follow-activity.json")
104 |> Poison.decode!()
105 |> Map.put("object", user.ap_id)
106 |> Utils.normalize_params()
107
108 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
109
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)
114 end
115 end
116 end