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