Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / web / activity_pub / transmogrifier / follow_handling_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 osada follow request" do
23 user = insert(:user)
24
25 data =
26 File.read!("test/fixtures/osada-follow-activity.json")
27 |> Poison.decode!()
28 |> Map.put("object", user.ap_id)
29
30 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
31
32 assert data["actor"] == "https://apfed.club/channel/indio"
33 assert data["type"] == "Follow"
34 assert data["id"] == "https://apfed.club/follow/9"
35
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)
39 end
40
41 test "it works for incoming follow requests" do
42 user = insert(:user)
43
44 data =
45 File.read!("test/fixtures/mastodon-follow-activity.json")
46 |> Poison.decode!()
47 |> Map.put("object", user.ap_id)
48
49 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
50
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"
54
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)
58 end
59
60 test "with locked accounts, it does not create a follow or an accept" do
61 user = insert(:user, info: %{locked: true})
62
63 data =
64 File.read!("test/fixtures/mastodon-follow-activity.json")
65 |> Poison.decode!()
66 |> Map.put("object", user.ap_id)
67
68 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
69
70 assert data["state"] == "pending"
71
72 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
73
74 accepts =
75 from(
76 a in Activity,
77 where: fragment("?->>'type' = ?", a.data, "Accept")
78 )
79 |> Repo.all()
80
81 assert length(accepts) == 0
82 end
83
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.
89 user = insert(:user)
90
91 data =
92 File.read!("test/fixtures/mastodon-follow-activity.json")
93 |> Poison.decode!()
94 |> Map.put("object", user.ap_id)
95
96 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
97
98 accepts =
99 from(
100 a in Activity,
101 where: fragment("?->>'type' = ?", a.data, "Accept")
102 )
103 |> Repo.all()
104
105 assert length(accepts) == 1
106
107 data =
108 File.read!("test/fixtures/mastodon-follow-activity.json")
109 |> Poison.decode!()
110 |> Map.put("id", String.replace(data["id"], "2", "3"))
111 |> Map.put("object", user.ap_id)
112
113 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
114
115 accepts =
116 from(
117 a in Activity,
118 where: fragment("?->>'type' = ?", a.data, "Accept")
119 )
120 |> Repo.all()
121
122 assert length(accepts) == 2
123 end
124
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)
127
128 user = insert(:user)
129 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
130
131 {:ok, user} = User.block(user, target)
132
133 data =
134 File.read!("test/fixtures/mastodon-follow-activity.json")
135 |> Poison.decode!()
136 |> Map.put("object", user.ap_id)
137
138 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
139
140 %Activity{} = activity = Activity.get_by_ap_id(id)
141
142 assert activity.data["state"] == "reject"
143 end
144
145 test "it works for incoming follow requests from hubzilla" do
146 user = insert(:user)
147
148 data =
149 File.read!("test/fixtures/hubzilla-follow-activity.json")
150 |> Poison.decode!()
151 |> Map.put("object", user.ap_id)
152 |> Utils.normalize_params()
153
154 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
155
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)
160 end
161 end
162 end