67266cb7a7ce7e27f659e7693ea4121f2986d673
[akkoma] / test / user_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.UserTest do
6 alias Pleroma.Activity
7 alias Pleroma.Builders.UserBuilder
8 alias Pleroma.Object
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.Web.CommonAPI
12
13 use Pleroma.DataCase
14
15 import Pleroma.Factory
16
17 setup_all do
18 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 :ok
20 end
21
22 describe "when tags are nil" do
23 test "tagging a user" do
24 user = insert(:user, %{tags: nil})
25 user = User.tag(user, ["cool", "dude"])
26
27 assert "cool" in user.tags
28 assert "dude" in user.tags
29 end
30
31 test "untagging a user" do
32 user = insert(:user, %{tags: nil})
33 user = User.untag(user, ["cool", "dude"])
34
35 assert user.tags == []
36 end
37 end
38
39 test "ap_id returns the activity pub id for the user" do
40 user = UserBuilder.build()
41
42 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
43
44 assert expected_ap_id == User.ap_id(user)
45 end
46
47 test "ap_followers returns the followers collection for the user" do
48 user = UserBuilder.build()
49
50 expected_followers_collection = "#{User.ap_id(user)}/followers"
51
52 assert expected_followers_collection == User.ap_followers(user)
53 end
54
55 test "returns all pending follow requests" do
56 unlocked = insert(:user)
57 locked = insert(:user, %{info: %{locked: true}})
58 follower = insert(:user)
59
60 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => unlocked.id})
61 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => locked.id})
62
63 assert {:ok, []} = User.get_follow_requests(unlocked)
64 assert {:ok, [activity]} = User.get_follow_requests(locked)
65
66 assert activity
67 end
68
69 test "doesn't return already accepted or duplicate follow requests" do
70 locked = insert(:user, %{info: %{locked: true}})
71 pending_follower = insert(:user)
72 accepted_follower = insert(:user)
73
74 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
75 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
76 Pleroma.Web.TwitterAPI.TwitterAPI.follow(accepted_follower, %{"user_id" => locked.id})
77 User.maybe_follow(accepted_follower, locked)
78
79 assert {:ok, [activity]} = User.get_follow_requests(locked)
80 assert activity
81 end
82
83 test "follow_all follows mutliple users" do
84 user = insert(:user)
85 followed_zero = insert(:user)
86 followed_one = insert(:user)
87 followed_two = insert(:user)
88 blocked = insert(:user)
89 not_followed = insert(:user)
90 reverse_blocked = insert(:user)
91
92 {:ok, user} = User.block(user, blocked)
93 {:ok, reverse_blocked} = User.block(reverse_blocked, user)
94
95 {:ok, user} = User.follow(user, followed_zero)
96
97 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
98
99 assert User.following?(user, followed_one)
100 assert User.following?(user, followed_two)
101 assert User.following?(user, followed_zero)
102 refute User.following?(user, not_followed)
103 refute User.following?(user, blocked)
104 refute User.following?(user, reverse_blocked)
105 end
106
107 test "follow_all follows mutliple users without duplicating" do
108 user = insert(:user)
109 followed_zero = insert(:user)
110 followed_one = insert(:user)
111 followed_two = insert(:user)
112
113 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
114 assert length(user.following) == 3
115
116 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
117 assert length(user.following) == 4
118 end
119
120 test "follow takes a user and another user" do
121 user = insert(:user)
122 followed = insert(:user)
123
124 {:ok, user} = User.follow(user, followed)
125
126 user = User.get_cached_by_id(user.id)
127
128 followed = User.get_cached_by_ap_id(followed.ap_id)
129 assert followed.info.follower_count == 1
130
131 assert User.ap_followers(followed) in user.following
132 end
133
134 test "can't follow a deactivated users" do
135 user = insert(:user)
136 followed = insert(:user, info: %{deactivated: true})
137
138 {:error, _} = User.follow(user, followed)
139 end
140
141 test "can't follow a user who blocked us" do
142 blocker = insert(:user)
143 blockee = insert(:user)
144
145 {:ok, blocker} = User.block(blocker, blockee)
146
147 {:error, _} = User.follow(blockee, blocker)
148 end
149
150 test "can't subscribe to a user who blocked us" do
151 blocker = insert(:user)
152 blocked = insert(:user)
153
154 {:ok, blocker} = User.block(blocker, blocked)
155
156 {:error, _} = User.subscribe(blocked, blocker)
157 end
158
159 test "local users do not automatically follow local locked accounts" do
160 follower = insert(:user, info: %{locked: true})
161 followed = insert(:user, info: %{locked: true})
162
163 {:ok, follower} = User.maybe_direct_follow(follower, followed)
164
165 refute User.following?(follower, followed)
166 end
167
168 # This is a somewhat useless test.
169 # test "following a remote user will ensure a websub subscription is present" do
170 # user = insert(:user)
171 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
172
173 # assert followed.local == false
174
175 # {:ok, user} = User.follow(user, followed)
176 # assert User.ap_followers(followed) in user.following
177
178 # query = from w in WebsubClientSubscription,
179 # where: w.topic == ^followed.info["topic"]
180 # websub = Repo.one(query)
181
182 # assert websub
183 # end
184
185 test "unfollow takes a user and another user" do
186 followed = insert(:user)
187 user = insert(:user, %{following: [User.ap_followers(followed)]})
188
189 {:ok, user, _activity} = User.unfollow(user, followed)
190
191 user = User.get_cached_by_id(user.id)
192
193 assert user.following == []
194 end
195
196 test "unfollow doesn't unfollow yourself" do
197 user = insert(:user)
198
199 {:error, _} = User.unfollow(user, user)
200
201 user = User.get_cached_by_id(user.id)
202 assert user.following == [user.ap_id]
203 end
204
205 test "test if a user is following another user" do
206 followed = insert(:user)
207 user = insert(:user, %{following: [User.ap_followers(followed)]})
208
209 assert User.following?(user, followed)
210 refute User.following?(followed, user)
211 end
212
213 test "fetches correct profile for nickname beginning with number" do
214 # Use old-style integer ID to try to reproduce the problem
215 user = insert(:user, %{id: 1080})
216 userwithnumbers = insert(:user, %{nickname: "#{user.id}garbage"})
217 assert userwithnumbers == User.get_cached_by_nickname_or_id(userwithnumbers.nickname)
218 end
219
220 describe "user registration" do
221 @full_user_data %{
222 bio: "A guy",
223 name: "my name",
224 nickname: "nick",
225 password: "test",
226 password_confirmation: "test",
227 email: "email@example.com"
228 }
229
230 test "it autofollows accounts that are set for it" do
231 user = insert(:user)
232 remote_user = insert(:user, %{local: false})
233
234 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
235 user.nickname,
236 remote_user.nickname
237 ])
238
239 cng = User.register_changeset(%User{}, @full_user_data)
240
241 {:ok, registered_user} = User.register(cng)
242
243 assert User.following?(registered_user, user)
244 refute User.following?(registered_user, remote_user)
245
246 Pleroma.Config.put([:instance, :autofollowed_nicknames], [])
247 end
248
249 test "it sends a welcome message if it is set" do
250 welcome_user = insert(:user)
251
252 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
253 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
254
255 cng = User.register_changeset(%User{}, @full_user_data)
256 {:ok, registered_user} = User.register(cng)
257
258 activity = Repo.one(Pleroma.Activity)
259 assert registered_user.ap_id in activity.recipients
260 assert Object.normalize(activity).data["content"] =~ "cool site"
261 assert activity.actor == welcome_user.ap_id
262
263 Pleroma.Config.put([:instance, :welcome_user_nickname], nil)
264 Pleroma.Config.put([:instance, :welcome_message], nil)
265 end
266
267 test "it requires an email, name, nickname and password, bio is optional" do
268 @full_user_data
269 |> Map.keys()
270 |> Enum.each(fn key ->
271 params = Map.delete(@full_user_data, key)
272 changeset = User.register_changeset(%User{}, params)
273
274 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
275 end)
276 end
277
278 test "it restricts certain nicknames" do
279 [restricted_name | _] = Pleroma.Config.get([Pleroma.User, :restricted_nicknames])
280
281 assert is_bitstring(restricted_name)
282
283 params =
284 @full_user_data
285 |> Map.put(:nickname, restricted_name)
286
287 changeset = User.register_changeset(%User{}, params)
288
289 refute changeset.valid?
290 end
291
292 test "it sets the password_hash, ap_id and following fields" do
293 changeset = User.register_changeset(%User{}, @full_user_data)
294
295 assert changeset.valid?
296
297 assert is_binary(changeset.changes[:password_hash])
298 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
299
300 assert changeset.changes[:following] == [
301 User.ap_followers(%User{nickname: @full_user_data.nickname})
302 ]
303
304 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
305 end
306
307 test "it ensures info is not nil" do
308 changeset = User.register_changeset(%User{}, @full_user_data)
309
310 assert changeset.valid?
311
312 {:ok, user} =
313 changeset
314 |> Repo.insert()
315
316 refute is_nil(user.info)
317 end
318 end
319
320 describe "user registration, with :account_activation_required" do
321 @full_user_data %{
322 bio: "A guy",
323 name: "my name",
324 nickname: "nick",
325 password: "test",
326 password_confirmation: "test",
327 email: "email@example.com"
328 }
329
330 setup do
331 setting = Pleroma.Config.get([:instance, :account_activation_required])
332
333 unless setting do
334 Pleroma.Config.put([:instance, :account_activation_required], true)
335 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
336 end
337
338 :ok
339 end
340
341 test "it creates unconfirmed user" do
342 changeset = User.register_changeset(%User{}, @full_user_data)
343 assert changeset.valid?
344
345 {:ok, user} = Repo.insert(changeset)
346
347 assert user.info.confirmation_pending
348 assert user.info.confirmation_token
349 end
350
351 test "it creates confirmed user if :confirmed option is given" do
352 changeset = User.register_changeset(%User{}, @full_user_data, confirmed: true)
353 assert changeset.valid?
354
355 {:ok, user} = Repo.insert(changeset)
356
357 refute user.info.confirmation_pending
358 refute user.info.confirmation_token
359 end
360 end
361
362 describe "get_or_fetch/1" do
363 test "gets an existing user by nickname" do
364 user = insert(:user)
365 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
366
367 assert user == fetched_user
368 end
369
370 test "gets an existing user by ap_id" do
371 ap_id = "http://mastodon.example.org/users/admin"
372
373 user =
374 insert(
375 :user,
376 local: false,
377 nickname: "admin@mastodon.example.org",
378 ap_id: ap_id,
379 info: %{}
380 )
381
382 {:ok, fetched_user} = User.get_or_fetch(ap_id)
383 freshed_user = refresh_record(user)
384 assert freshed_user == fetched_user
385 end
386 end
387
388 describe "fetching a user from nickname or trying to build one" do
389 test "gets an existing user" do
390 user = insert(:user)
391 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
392
393 assert user == fetched_user
394 end
395
396 test "gets an existing user, case insensitive" do
397 user = insert(:user, nickname: "nick")
398 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
399
400 assert user == fetched_user
401 end
402
403 test "gets an existing user by fully qualified nickname" do
404 user = insert(:user)
405
406 {:ok, fetched_user} =
407 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
408
409 assert user == fetched_user
410 end
411
412 test "gets an existing user by fully qualified nickname, case insensitive" do
413 user = insert(:user, nickname: "nick")
414 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
415
416 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
417
418 assert user == fetched_user
419 end
420
421 test "fetches an external user via ostatus if no user exists" do
422 {:ok, fetched_user} = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
423 assert fetched_user.nickname == "shp@social.heldscal.la"
424 end
425
426 test "returns nil if no user could be fetched" do
427 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
428 assert fetched_user == "not found nonexistant@social.heldscal.la"
429 end
430
431 test "returns nil for nonexistant local user" do
432 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
433 assert fetched_user == "not found nonexistant"
434 end
435
436 test "updates an existing user, if stale" do
437 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
438
439 orig_user =
440 insert(
441 :user,
442 local: false,
443 nickname: "admin@mastodon.example.org",
444 ap_id: "http://mastodon.example.org/users/admin",
445 last_refreshed_at: a_week_ago,
446 info: %{}
447 )
448
449 assert orig_user.last_refreshed_at == a_week_ago
450
451 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
452 assert user.info.source_data["endpoints"]
453
454 refute user.last_refreshed_at == orig_user.last_refreshed_at
455 end
456 end
457
458 test "returns an ap_id for a user" do
459 user = insert(:user)
460
461 assert User.ap_id(user) ==
462 Pleroma.Web.Router.Helpers.o_status_url(
463 Pleroma.Web.Endpoint,
464 :feed_redirect,
465 user.nickname
466 )
467 end
468
469 test "returns an ap_followers link for a user" do
470 user = insert(:user)
471
472 assert User.ap_followers(user) ==
473 Pleroma.Web.Router.Helpers.o_status_url(
474 Pleroma.Web.Endpoint,
475 :feed_redirect,
476 user.nickname
477 ) <> "/followers"
478 end
479
480 describe "remote user creation changeset" do
481 @valid_remote %{
482 bio: "hello",
483 name: "Someone",
484 nickname: "a@b.de",
485 ap_id: "http...",
486 info: %{some: "info"},
487 avatar: %{some: "avatar"}
488 }
489
490 test "it confirms validity" do
491 cs = User.remote_user_creation(@valid_remote)
492 assert cs.valid?
493 end
494
495 test "it sets the follower_adress" do
496 cs = User.remote_user_creation(@valid_remote)
497 # remote users get a fake local follower address
498 assert cs.changes.follower_address ==
499 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
500 end
501
502 test "it enforces the fqn format for nicknames" do
503 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
504 assert cs.changes.local == false
505 assert cs.changes.avatar
506 refute cs.valid?
507 end
508
509 test "it has required fields" do
510 [:name, :ap_id]
511 |> Enum.each(fn field ->
512 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
513 refute cs.valid?
514 end)
515 end
516
517 test "it restricts some sizes" do
518 [bio: 5000, name: 100]
519 |> Enum.each(fn {field, size} ->
520 string = String.pad_leading(".", size)
521 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
522 assert cs.valid?
523
524 string = String.pad_leading(".", size + 1)
525 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
526 refute cs.valid?
527 end)
528 end
529 end
530
531 describe "followers and friends" do
532 test "gets all followers for a given user" do
533 user = insert(:user)
534 follower_one = insert(:user)
535 follower_two = insert(:user)
536 not_follower = insert(:user)
537
538 {:ok, follower_one} = User.follow(follower_one, user)
539 {:ok, follower_two} = User.follow(follower_two, user)
540
541 {:ok, res} = User.get_followers(user)
542
543 assert Enum.member?(res, follower_one)
544 assert Enum.member?(res, follower_two)
545 refute Enum.member?(res, not_follower)
546 end
547
548 test "gets all friends (followed users) for a given user" do
549 user = insert(:user)
550 followed_one = insert(:user)
551 followed_two = insert(:user)
552 not_followed = insert(:user)
553
554 {:ok, user} = User.follow(user, followed_one)
555 {:ok, user} = User.follow(user, followed_two)
556
557 {:ok, res} = User.get_friends(user)
558
559 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
560 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
561 assert Enum.member?(res, followed_one)
562 assert Enum.member?(res, followed_two)
563 refute Enum.member?(res, not_followed)
564 end
565 end
566
567 describe "updating note and follower count" do
568 test "it sets the info->note_count property" do
569 note = insert(:note)
570
571 user = User.get_cached_by_ap_id(note.data["actor"])
572
573 assert user.info.note_count == 0
574
575 {:ok, user} = User.update_note_count(user)
576
577 assert user.info.note_count == 1
578 end
579
580 test "it increases the info->note_count property" do
581 note = insert(:note)
582 user = User.get_cached_by_ap_id(note.data["actor"])
583
584 assert user.info.note_count == 0
585
586 {:ok, user} = User.increase_note_count(user)
587
588 assert user.info.note_count == 1
589
590 {:ok, user} = User.increase_note_count(user)
591
592 assert user.info.note_count == 2
593 end
594
595 test "it decreases the info->note_count property" do
596 note = insert(:note)
597 user = User.get_cached_by_ap_id(note.data["actor"])
598
599 assert user.info.note_count == 0
600
601 {:ok, user} = User.increase_note_count(user)
602
603 assert user.info.note_count == 1
604
605 {:ok, user} = User.decrease_note_count(user)
606
607 assert user.info.note_count == 0
608
609 {:ok, user} = User.decrease_note_count(user)
610
611 assert user.info.note_count == 0
612 end
613
614 test "it sets the info->follower_count property" do
615 user = insert(:user)
616 follower = insert(:user)
617
618 User.follow(follower, user)
619
620 assert user.info.follower_count == 0
621
622 {:ok, user} = User.update_follower_count(user)
623
624 assert user.info.follower_count == 1
625 end
626 end
627
628 describe "follow_import" do
629 test "it imports user followings from list" do
630 [user1, user2, user3] = insert_list(3, :user)
631
632 identifiers = [
633 user2.ap_id,
634 user3.nickname
635 ]
636
637 result = User.follow_import(user1, identifiers)
638 assert is_list(result)
639 assert result == [user2, user3]
640 end
641 end
642
643 describe "mutes" do
644 test "it mutes people" do
645 user = insert(:user)
646 muted_user = insert(:user)
647
648 refute User.mutes?(user, muted_user)
649
650 {:ok, user} = User.mute(user, muted_user)
651
652 assert User.mutes?(user, muted_user)
653 end
654
655 test "it unmutes users" do
656 user = insert(:user)
657 muted_user = insert(:user)
658
659 {:ok, user} = User.mute(user, muted_user)
660 {:ok, user} = User.unmute(user, muted_user)
661
662 refute User.mutes?(user, muted_user)
663 end
664 end
665
666 describe "blocks" do
667 test "it blocks people" do
668 user = insert(:user)
669 blocked_user = insert(:user)
670
671 refute User.blocks?(user, blocked_user)
672
673 {:ok, user} = User.block(user, blocked_user)
674
675 assert User.blocks?(user, blocked_user)
676 end
677
678 test "it unblocks users" do
679 user = insert(:user)
680 blocked_user = insert(:user)
681
682 {:ok, user} = User.block(user, blocked_user)
683 {:ok, user} = User.unblock(user, blocked_user)
684
685 refute User.blocks?(user, blocked_user)
686 end
687
688 test "blocks tear down cyclical follow relationships" do
689 blocker = insert(:user)
690 blocked = insert(:user)
691
692 {:ok, blocker} = User.follow(blocker, blocked)
693 {:ok, blocked} = User.follow(blocked, blocker)
694
695 assert User.following?(blocker, blocked)
696 assert User.following?(blocked, blocker)
697
698 {:ok, blocker} = User.block(blocker, blocked)
699 blocked = User.get_cached_by_id(blocked.id)
700
701 assert User.blocks?(blocker, blocked)
702
703 refute User.following?(blocker, blocked)
704 refute User.following?(blocked, blocker)
705 end
706
707 test "blocks tear down blocker->blocked follow relationships" do
708 blocker = insert(:user)
709 blocked = insert(:user)
710
711 {:ok, blocker} = User.follow(blocker, blocked)
712
713 assert User.following?(blocker, blocked)
714 refute User.following?(blocked, blocker)
715
716 {:ok, blocker} = User.block(blocker, blocked)
717 blocked = User.get_cached_by_id(blocked.id)
718
719 assert User.blocks?(blocker, blocked)
720
721 refute User.following?(blocker, blocked)
722 refute User.following?(blocked, blocker)
723 end
724
725 test "blocks tear down blocked->blocker follow relationships" do
726 blocker = insert(:user)
727 blocked = insert(:user)
728
729 {:ok, blocked} = User.follow(blocked, blocker)
730
731 refute User.following?(blocker, blocked)
732 assert User.following?(blocked, blocker)
733
734 {:ok, blocker} = User.block(blocker, blocked)
735 blocked = User.get_cached_by_id(blocked.id)
736
737 assert User.blocks?(blocker, blocked)
738
739 refute User.following?(blocker, blocked)
740 refute User.following?(blocked, blocker)
741 end
742
743 test "blocks tear down blocked->blocker subscription relationships" do
744 blocker = insert(:user)
745 blocked = insert(:user)
746
747 {:ok, blocker} = User.subscribe(blocked, blocker)
748
749 assert User.subscribed_to?(blocked, blocker)
750 refute User.subscribed_to?(blocker, blocked)
751
752 {:ok, blocker} = User.block(blocker, blocked)
753
754 assert User.blocks?(blocker, blocked)
755 refute User.subscribed_to?(blocker, blocked)
756 refute User.subscribed_to?(blocked, blocker)
757 end
758 end
759
760 describe "domain blocking" do
761 test "blocks domains" do
762 user = insert(:user)
763 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
764
765 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
766
767 assert User.blocks?(user, collateral_user)
768 end
769
770 test "unblocks domains" do
771 user = insert(:user)
772 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
773
774 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
775 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
776
777 refute User.blocks?(user, collateral_user)
778 end
779 end
780
781 describe "blocks_import" do
782 test "it imports user blocks from list" do
783 [user1, user2, user3] = insert_list(3, :user)
784
785 identifiers = [
786 user2.ap_id,
787 user3.nickname
788 ]
789
790 result = User.blocks_import(user1, identifiers)
791 assert is_list(result)
792 assert result == [user2, user3]
793 end
794 end
795
796 test "get recipients from activity" do
797 actor = insert(:user)
798 user = insert(:user, local: true)
799 user_two = insert(:user, local: false)
800 addressed = insert(:user, local: true)
801 addressed_remote = insert(:user, local: false)
802
803 {:ok, activity} =
804 CommonAPI.post(actor, %{
805 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
806 })
807
808 assert Enum.map([actor, addressed], & &1.ap_id) --
809 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
810
811 {:ok, user} = User.follow(user, actor)
812 {:ok, _user_two} = User.follow(user_two, actor)
813 recipients = User.get_recipients_from_activity(activity)
814 assert length(recipients) == 3
815 assert user in recipients
816 assert addressed in recipients
817 end
818
819 test ".deactivate can de-activate then re-activate a user" do
820 user = insert(:user)
821 assert false == user.info.deactivated
822 {:ok, user} = User.deactivate(user)
823 assert true == user.info.deactivated
824 {:ok, user} = User.deactivate(user, false)
825 assert false == user.info.deactivated
826 end
827
828 test ".delete_user_activities deletes all create activities" do
829 user = insert(:user)
830
831 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
832 {:ok, _} = User.delete_user_activities(user)
833
834 # TODO: Remove favorites, repeats, delete activities.
835 refute Activity.get_by_id(activity.id)
836 end
837
838 test ".delete deactivates a user, all follow relationships and all create activities" do
839 user = insert(:user)
840 followed = insert(:user)
841 follower = insert(:user)
842
843 {:ok, user} = User.follow(user, followed)
844 {:ok, follower} = User.follow(follower, user)
845
846 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
847 {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
848
849 {:ok, _, _} = CommonAPI.favorite(activity_two.id, user)
850 {:ok, _, _} = CommonAPI.favorite(activity.id, follower)
851 {:ok, _, _} = CommonAPI.repeat(activity.id, follower)
852
853 {:ok, _} = User.delete(user)
854
855 followed = User.get_cached_by_id(followed.id)
856 follower = User.get_cached_by_id(follower.id)
857 user = User.get_cached_by_id(user.id)
858
859 assert user.info.deactivated
860
861 refute User.following?(user, followed)
862 refute User.following?(followed, follower)
863
864 # TODO: Remove favorites, repeats, delete activities.
865
866 refute Activity.get_by_id(activity.id)
867 end
868
869 test "get_public_key_for_ap_id fetches a user that's not in the db" do
870 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
871 end
872
873 test "insert or update a user from given data" do
874 user = insert(:user, %{nickname: "nick@name.de"})
875 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
876
877 assert {:ok, %User{}} = User.insert_or_update_user(data)
878 end
879
880 describe "per-user rich-text filtering" do
881 test "html_filter_policy returns default policies, when rich-text is enabled" do
882 user = insert(:user)
883
884 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
885 end
886
887 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
888 user = insert(:user, %{info: %{no_rich_text: true}})
889
890 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
891 end
892 end
893
894 describe "caching" do
895 test "invalidate_cache works" do
896 user = insert(:user)
897 _user_info = User.get_cached_user_info(user)
898
899 User.invalidate_cache(user)
900
901 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
902 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
903 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
904 end
905
906 test "User.delete() plugs any possible zombie objects" do
907 user = insert(:user)
908
909 {:ok, _} = User.delete(user)
910
911 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
912
913 assert cached_user != user
914
915 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
916
917 assert cached_user != user
918 end
919 end
920
921 describe "User.search" do
922 test "finds a user by full or partial nickname" do
923 user = insert(:user, %{nickname: "john"})
924
925 Enum.each(["john", "jo", "j"], fn query ->
926 assert user ==
927 User.search(query)
928 |> List.first()
929 |> Map.put(:search_rank, nil)
930 |> Map.put(:search_type, nil)
931 end)
932 end
933
934 test "finds a user by full or partial name" do
935 user = insert(:user, %{name: "John Doe"})
936
937 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
938 assert user ==
939 User.search(query)
940 |> List.first()
941 |> Map.put(:search_rank, nil)
942 |> Map.put(:search_type, nil)
943 end)
944 end
945
946 test "finds users, preferring nickname matches over name matches" do
947 u1 = insert(:user, %{name: "lain", nickname: "nick1"})
948 u2 = insert(:user, %{nickname: "lain", name: "nick1"})
949
950 assert [u2.id, u1.id] == Enum.map(User.search("lain"), & &1.id)
951 end
952
953 test "finds users, considering density of matched tokens" do
954 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
955 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
956
957 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
958 end
959
960 test "finds users, ranking by similarity" do
961 u1 = insert(:user, %{name: "lain"})
962 _u2 = insert(:user, %{name: "ean"})
963 u3 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
964 u4 = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
965
966 assert [u4.id, u3.id, u1.id] == Enum.map(User.search("lain@ple"), & &1.id)
967 end
968
969 test "finds users, handling misspelled requests" do
970 u1 = insert(:user, %{name: "lain"})
971
972 assert [u1.id] == Enum.map(User.search("laiin"), & &1.id)
973 end
974
975 test "finds users, boosting ranks of friends and followers" do
976 u1 = insert(:user)
977 u2 = insert(:user, %{name: "Doe"})
978 follower = insert(:user, %{name: "Doe"})
979 friend = insert(:user, %{name: "Doe"})
980
981 {:ok, follower} = User.follow(follower, u1)
982 {:ok, u1} = User.follow(u1, friend)
983
984 assert [friend.id, follower.id, u2.id] --
985 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
986 end
987
988 test "finds a user whose name is nil" do
989 _user = insert(:user, %{name: "notamatch", nickname: "testuser@pleroma.amplifie.red"})
990 user_two = insert(:user, %{name: nil, nickname: "lain@pleroma.soykaf.com"})
991
992 assert user_two ==
993 User.search("lain@pleroma.soykaf.com")
994 |> List.first()
995 |> Map.put(:search_rank, nil)
996 |> Map.put(:search_type, nil)
997 end
998
999 test "does not yield false-positive matches" do
1000 insert(:user, %{name: "John Doe"})
1001
1002 Enum.each(["mary", "a", ""], fn query ->
1003 assert [] == User.search(query)
1004 end)
1005 end
1006
1007 test "works with URIs" do
1008 results = User.search("http://mastodon.example.org/users/admin", resolve: true)
1009 result = results |> List.first()
1010
1011 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
1012
1013 assert length(results) == 1
1014 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
1015 end
1016 end
1017
1018 test "auth_active?/1 works correctly" do
1019 Pleroma.Config.put([:instance, :account_activation_required], true)
1020
1021 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
1022 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
1023 remote_user = insert(:user, local: false)
1024
1025 refute User.auth_active?(local_user)
1026 assert User.auth_active?(confirmed_user)
1027 assert User.auth_active?(remote_user)
1028
1029 Pleroma.Config.put([:instance, :account_activation_required], false)
1030 end
1031
1032 describe "superuser?/1" do
1033 test "returns false for unprivileged users" do
1034 user = insert(:user, local: true)
1035
1036 refute User.superuser?(user)
1037 end
1038
1039 test "returns false for remote users" do
1040 user = insert(:user, local: false)
1041 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1042
1043 refute User.superuser?(user)
1044 refute User.superuser?(remote_admin_user)
1045 end
1046
1047 test "returns true for local moderators" do
1048 user = insert(:user, local: true, info: %{is_moderator: true})
1049
1050 assert User.superuser?(user)
1051 end
1052
1053 test "returns true for local admins" do
1054 user = insert(:user, local: true, info: %{is_admin: true})
1055
1056 assert User.superuser?(user)
1057 end
1058 end
1059
1060 describe "visible_for?/2" do
1061 test "returns true when the account is itself" do
1062 user = insert(:user, local: true)
1063
1064 assert User.visible_for?(user, user)
1065 end
1066
1067 test "returns false when the account is unauthenticated and auth is required" do
1068 Pleroma.Config.put([:instance, :account_activation_required], true)
1069
1070 user = insert(:user, local: true, info: %{confirmation_pending: true})
1071 other_user = insert(:user, local: true)
1072
1073 refute User.visible_for?(user, other_user)
1074
1075 Pleroma.Config.put([:instance, :account_activation_required], false)
1076 end
1077
1078 test "returns true when the account is unauthenticated and auth is not required" do
1079 user = insert(:user, local: true, info: %{confirmation_pending: true})
1080 other_user = insert(:user, local: true)
1081
1082 assert User.visible_for?(user, other_user)
1083 end
1084
1085 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1086 Pleroma.Config.put([:instance, :account_activation_required], true)
1087
1088 user = insert(:user, local: true, info: %{confirmation_pending: true})
1089 other_user = insert(:user, local: true, info: %{is_admin: true})
1090
1091 assert User.visible_for?(user, other_user)
1092
1093 Pleroma.Config.put([:instance, :account_activation_required], false)
1094 end
1095 end
1096
1097 describe "parse_bio/2" do
1098 test "preserves hosts in user links text" do
1099 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1100 user = insert(:user)
1101 bio = "A.k.a. @nick@domain.com"
1102
1103 expected_text =
1104 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1105 remote_user.ap_id
1106 }'>" <> "@<span>nick@domain.com</span></a></span>"
1107
1108 assert expected_text == User.parse_bio(bio, user)
1109 end
1110
1111 test "Adds rel=me on linkbacked urls" do
1112 user = insert(:user, ap_id: "http://social.example.org/users/lain")
1113
1114 bio = "http://example.org/rel_me/null"
1115 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1116 assert expected_text == User.parse_bio(bio, user)
1117
1118 bio = "http://example.org/rel_me/link"
1119 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1120 assert expected_text == User.parse_bio(bio, user)
1121
1122 bio = "http://example.org/rel_me/anchor"
1123 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1124 assert expected_text == User.parse_bio(bio, user)
1125 end
1126 end
1127
1128 test "follower count is updated when a follower is blocked" do
1129 user = insert(:user)
1130 follower = insert(:user)
1131 follower2 = insert(:user)
1132 follower3 = insert(:user)
1133
1134 {:ok, follower} = Pleroma.User.follow(follower, user)
1135 {:ok, _follower2} = Pleroma.User.follow(follower2, user)
1136 {:ok, _follower3} = Pleroma.User.follow(follower3, user)
1137
1138 {:ok, _} = Pleroma.User.block(user, follower)
1139
1140 user_show = Pleroma.Web.TwitterAPI.UserView.render("show.json", %{user: user})
1141
1142 assert Map.get(user_show, "followers_count") == 2
1143 end
1144 end