[#2497] Specified SHELL in .gitlab-ci.yml as required for `exexec`.
[akkoma] / test / web / activity_pub / transmogrifier / follow_handling_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 setup do: clear_config([:user, :deny_follow_blocked])
23
24 test "it works for osada follow request" do
25 user = insert(:user)
26
27 data =
28 File.read!("test/fixtures/osada-follow-activity.json")
29 |> Poison.decode!()
30 |> Map.put("object", user.ap_id)
31
32 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
33
34 assert data["actor"] == "https://apfed.club/channel/indio"
35 assert data["type"] == "Follow"
36 assert data["id"] == "https://apfed.club/follow/9"
37
38 activity = Repo.get(Activity, activity.id)
39 assert activity.data["state"] == "accept"
40 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
41 end
42
43 test "it works for incoming follow requests" do
44 user = insert(:user)
45
46 data =
47 File.read!("test/fixtures/mastodon-follow-activity.json")
48 |> Poison.decode!()
49 |> Map.put("object", user.ap_id)
50
51 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
52
53 assert data["actor"] == "http://mastodon.example.org/users/admin"
54 assert data["type"] == "Follow"
55 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
56
57 activity = Repo.get(Activity, activity.id)
58 assert activity.data["state"] == "accept"
59 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
60 end
61
62 test "with locked accounts, it does not create a follow or an accept" do
63 user = insert(:user, locked: true)
64
65 data =
66 File.read!("test/fixtures/mastodon-follow-activity.json")
67 |> Poison.decode!()
68 |> Map.put("object", user.ap_id)
69
70 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
71
72 assert data["state"] == "pending"
73
74 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
75
76 accepts =
77 from(
78 a in Activity,
79 where: fragment("?->>'type' = ?", a.data, "Accept")
80 )
81 |> Repo.all()
82
83 assert Enum.empty?(accepts)
84 end
85
86 test "it works for follow requests when you are already followed, creating a new accept activity" do
87 # This is important because the remote might have the wrong idea about the
88 # current follow status. This can lead to instance A thinking that x@A is
89 # followed by y@B, but B thinks they are not. In this case, the follow can
90 # never go through again because it will never get an Accept.
91 user = insert(:user)
92
93 data =
94 File.read!("test/fixtures/mastodon-follow-activity.json")
95 |> Poison.decode!()
96 |> Map.put("object", user.ap_id)
97
98 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
99
100 accepts =
101 from(
102 a in Activity,
103 where: fragment("?->>'type' = ?", a.data, "Accept")
104 )
105 |> Repo.all()
106
107 assert length(accepts) == 1
108
109 data =
110 File.read!("test/fixtures/mastodon-follow-activity.json")
111 |> Poison.decode!()
112 |> Map.put("id", String.replace(data["id"], "2", "3"))
113 |> Map.put("object", user.ap_id)
114
115 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
116
117 accepts =
118 from(
119 a in Activity,
120 where: fragment("?->>'type' = ?", a.data, "Accept")
121 )
122 |> Repo.all()
123
124 assert length(accepts) == 2
125 end
126
127 test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
128 Pleroma.Config.put([:user, :deny_follow_blocked], true)
129
130 user = insert(:user)
131 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
132
133 {:ok, _user_relationship} = User.block(user, target)
134
135 data =
136 File.read!("test/fixtures/mastodon-follow-activity.json")
137 |> Poison.decode!()
138 |> Map.put("object", user.ap_id)
139
140 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
141
142 %Activity{} = activity = Activity.get_by_ap_id(id)
143
144 assert activity.data["state"] == "reject"
145 end
146
147 test "it works for incoming follow requests from hubzilla" do
148 user = insert(:user)
149
150 data =
151 File.read!("test/fixtures/hubzilla-follow-activity.json")
152 |> Poison.decode!()
153 |> Map.put("object", user.ap_id)
154 |> Utils.normalize_params()
155
156 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
157
158 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
159 assert data["type"] == "Follow"
160 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
161 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
162 end
163 end
164 end