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