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