Expose unannounce activity so that it can be tested
[akkoma] / test / user_test.exs
1 defmodule Pleroma.UserTest do
2 alias Pleroma.Builders.UserBuilder
3 alias Pleroma.{User, Repo, Activity}
4 alias Pleroma.Web.OStatus
5 alias Pleroma.Web.Websub.WebsubClientSubscription
6 alias Pleroma.Web.CommonAPI
7 use Pleroma.DataCase
8
9 import Pleroma.Factory
10 import Ecto.Query
11
12 test "ap_id returns the activity pub id for the user" do
13 user = UserBuilder.build()
14
15 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
16
17 assert expected_ap_id == User.ap_id(user)
18 end
19
20 test "ap_followers returns the followers collection for the user" do
21 user = UserBuilder.build()
22
23 expected_followers_collection = "#{User.ap_id(user)}/followers"
24
25 assert expected_followers_collection == User.ap_followers(user)
26 end
27
28 test "follow takes a user and another user" do
29 user = insert(:user)
30 followed = insert(:user)
31
32 {:ok, user} = User.follow(user, followed)
33
34 user = Repo.get(User, user.id)
35
36 followed = User.get_by_ap_id(followed.ap_id)
37 assert followed.info["follower_count"] == 1
38
39 assert User.ap_followers(followed) in user.following
40 end
41
42 test "can't follow a deactivated users" do
43 user = insert(:user)
44 followed = insert(:user, info: %{"deactivated" => true})
45
46 {:error, _} = User.follow(user, followed)
47 end
48
49 # This is a somewhat useless test.
50 # test "following a remote user will ensure a websub subscription is present" do
51 # user = insert(:user)
52 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
53
54 # assert followed.local == false
55
56 # {:ok, user} = User.follow(user, followed)
57 # assert User.ap_followers(followed) in user.following
58
59 # query = from w in WebsubClientSubscription,
60 # where: w.topic == ^followed.info["topic"]
61 # websub = Repo.one(query)
62
63 # assert websub
64 # end
65
66 test "unfollow takes a user and another user" do
67 followed = insert(:user)
68 user = insert(:user, %{following: [User.ap_followers(followed)]})
69
70 {:ok, user, _activity} = User.unfollow(user, followed)
71
72 user = Repo.get(User, user.id)
73
74 assert user.following == []
75 end
76
77 test "unfollow doesn't unfollow yourself" do
78 user = insert(:user)
79
80 {:error, _} = User.unfollow(user, user)
81
82 user = Repo.get(User, user.id)
83 assert user.following == [user.ap_id]
84 end
85
86 test "test if a user is following another user" do
87 followed = insert(:user)
88 user = insert(:user, %{following: [User.ap_followers(followed)]})
89
90 assert User.following?(user, followed)
91 refute User.following?(followed, user)
92 end
93
94 describe "user registration" do
95 @full_user_data %{
96 bio: "A guy",
97 name: "my name",
98 nickname: "nick",
99 password: "test",
100 password_confirmation: "test",
101 email: "email@example.com"
102 }
103
104 test "it requires an email, name, nickname and password, bio is optional" do
105 @full_user_data
106 |> Map.keys()
107 |> Enum.each(fn key ->
108 params = Map.delete(@full_user_data, key)
109 changeset = User.register_changeset(%User{}, params)
110
111 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
112 end)
113 end
114
115 test "it sets the password_hash, ap_id and following fields" do
116 changeset = User.register_changeset(%User{}, @full_user_data)
117
118 assert changeset.valid?
119
120 assert is_binary(changeset.changes[:password_hash])
121 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
122
123 assert changeset.changes[:following] == [
124 User.ap_followers(%User{nickname: @full_user_data.nickname})
125 ]
126
127 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
128 end
129 end
130
131 describe "fetching a user from nickname or trying to build one" do
132 test "gets an existing user" do
133 user = insert(:user)
134 fetched_user = User.get_or_fetch_by_nickname(user.nickname)
135
136 assert user == fetched_user
137 end
138
139 test "gets an existing user, case insensitive" do
140 user = insert(:user, nickname: "nick")
141 fetched_user = User.get_or_fetch_by_nickname("NICK")
142
143 assert user == fetched_user
144 end
145
146 test "fetches an external user via ostatus if no user exists" do
147 fetched_user = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
148 assert fetched_user.nickname == "shp@social.heldscal.la"
149 end
150
151 test "returns nil if no user could be fetched" do
152 fetched_user = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
153 assert fetched_user == nil
154 end
155
156 test "returns nil for nonexistant local user" do
157 fetched_user = User.get_or_fetch_by_nickname("nonexistant")
158 assert fetched_user == nil
159 end
160 end
161
162 test "returns an ap_id for a user" do
163 user = insert(:user)
164
165 assert User.ap_id(user) ==
166 Pleroma.Web.Router.Helpers.o_status_url(
167 Pleroma.Web.Endpoint,
168 :feed_redirect,
169 user.nickname
170 )
171 end
172
173 test "returns an ap_followers link for a user" do
174 user = insert(:user)
175
176 assert User.ap_followers(user) ==
177 Pleroma.Web.Router.Helpers.o_status_url(
178 Pleroma.Web.Endpoint,
179 :feed_redirect,
180 user.nickname
181 ) <> "/followers"
182 end
183
184 describe "remote user creation changeset" do
185 @valid_remote %{
186 bio: "hello",
187 name: "Someone",
188 nickname: "a@b.de",
189 ap_id: "http...",
190 info: %{some: "info"},
191 avatar: %{some: "avatar"}
192 }
193
194 test "it confirms validity" do
195 cs = User.remote_user_creation(@valid_remote)
196 assert cs.valid?
197 end
198
199 test "it sets the follower_adress" do
200 cs = User.remote_user_creation(@valid_remote)
201 # remote users get a fake local follower address
202 assert cs.changes.follower_address ==
203 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
204 end
205
206 test "it enforces the fqn format for nicknames" do
207 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
208 assert cs.changes.local == false
209 assert cs.changes.avatar
210 refute cs.valid?
211 end
212
213 test "it has required fields" do
214 [:name, :nickname, :ap_id]
215 |> Enum.each(fn field ->
216 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
217 refute cs.valid?
218 end)
219 end
220
221 test "it restricts some sizes" do
222 [bio: 5000, name: 100]
223 |> Enum.each(fn {field, size} ->
224 string = String.pad_leading(".", size)
225 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
226 assert cs.valid?
227
228 string = String.pad_leading(".", size + 1)
229 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
230 refute cs.valid?
231 end)
232 end
233 end
234
235 describe "followers and friends" do
236 test "gets all followers for a given user" do
237 user = insert(:user)
238 follower_one = insert(:user)
239 follower_two = insert(:user)
240 not_follower = insert(:user)
241
242 {:ok, follower_one} = User.follow(follower_one, user)
243 {:ok, follower_two} = User.follow(follower_two, user)
244
245 {:ok, res} = User.get_followers(user)
246
247 assert Enum.member?(res, follower_one)
248 assert Enum.member?(res, follower_two)
249 refute Enum.member?(res, not_follower)
250 end
251
252 test "gets all friends (followed users) for a given user" do
253 user = insert(:user)
254 followed_one = insert(:user)
255 followed_two = insert(:user)
256 not_followed = insert(:user)
257
258 {:ok, user} = User.follow(user, followed_one)
259 {:ok, user} = User.follow(user, followed_two)
260
261 {:ok, res} = User.get_friends(user)
262
263 followed_one = User.get_by_ap_id(followed_one.ap_id)
264 followed_two = User.get_by_ap_id(followed_two.ap_id)
265 assert Enum.member?(res, followed_one)
266 assert Enum.member?(res, followed_two)
267 refute Enum.member?(res, not_followed)
268 end
269 end
270
271 describe "updating note and follower count" do
272 test "it sets the info->note_count property" do
273 note = insert(:note)
274
275 user = User.get_by_ap_id(note.data["actor"])
276
277 assert user.info["note_count"] == nil
278
279 {:ok, user} = User.update_note_count(user)
280
281 assert user.info["note_count"] == 1
282 end
283
284 test "it increases the info->note_count property" do
285 note = insert(:note)
286 user = User.get_by_ap_id(note.data["actor"])
287
288 assert user.info["note_count"] == nil
289
290 {:ok, user} = User.increase_note_count(user)
291
292 assert user.info["note_count"] == 1
293
294 {:ok, user} = User.increase_note_count(user)
295
296 assert user.info["note_count"] == 2
297 end
298
299 test "it sets the info->follower_count property" do
300 user = insert(:user)
301 follower = insert(:user)
302
303 User.follow(follower, user)
304
305 assert user.info["follower_count"] == nil
306
307 {:ok, user} = User.update_follower_count(user)
308
309 assert user.info["follower_count"] == 1
310 end
311 end
312
313 describe "blocks" do
314 test "it blocks people" do
315 user = insert(:user)
316 blocked_user = insert(:user)
317
318 refute User.blocks?(user, blocked_user)
319
320 {:ok, user} = User.block(user, blocked_user)
321
322 assert User.blocks?(user, blocked_user)
323 end
324
325 test "it unblocks users" do
326 user = insert(:user)
327 blocked_user = insert(:user)
328
329 {:ok, user} = User.block(user, blocked_user)
330 {:ok, user} = User.unblock(user, blocked_user)
331
332 refute User.blocks?(user, blocked_user)
333 end
334 end
335
336 test "get recipients from activity" do
337 actor = insert(:user)
338 user = insert(:user, local: true)
339 user_two = insert(:user, local: false)
340 addressed = insert(:user, local: true)
341 addressed_remote = insert(:user, local: false)
342
343 {:ok, activity} =
344 CommonAPI.post(actor, %{
345 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
346 })
347
348 assert [addressed] == User.get_recipients_from_activity(activity)
349
350 {:ok, user} = User.follow(user, actor)
351 {:ok, _user_two} = User.follow(user_two, actor)
352 recipients = User.get_recipients_from_activity(activity)
353 assert length(recipients) == 2
354 assert user in recipients
355 assert addressed in recipients
356 end
357
358 test ".deactivate deactivates a user" do
359 user = insert(:user)
360 assert false == !!user.info["deactivated"]
361 {:ok, user} = User.deactivate(user)
362 assert true == user.info["deactivated"]
363 end
364
365 test ".delete deactivates a user, all follow relationships and all create activities" do
366 user = insert(:user)
367 followed = insert(:user)
368 follower = insert(:user)
369
370 {:ok, user} = User.follow(user, followed)
371 {:ok, follower} = User.follow(follower, user)
372
373 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
374 {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
375
376 {:ok, _, _} = CommonAPI.favorite(activity_two.id, user)
377 {:ok, _, _} = CommonAPI.favorite(activity.id, follower)
378 {:ok, _, _} = CommonAPI.repeat(activity.id, follower)
379
380 :ok = User.delete(user)
381
382 followed = Repo.get(User, followed.id)
383 follower = Repo.get(User, follower.id)
384 user = Repo.get(User, user.id)
385
386 assert user.info["deactivated"]
387
388 refute User.following?(user, followed)
389 refute User.following?(followed, follower)
390
391 # TODO: Remove favorites, repeats, delete activities.
392
393 refute Repo.get(Activity, activity.id)
394 end
395
396 test "get_public_key_for_ap_id fetches a user that's not in the db" do
397 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
398 end
399
400 test "insert or update a user from given data" do
401 user = insert(:user, %{nickname: "nick@name.de"})
402 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
403
404 assert {:ok, %User{}} = User.insert_or_update_user(data)
405 end
406 end