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