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