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