[#1335] User: refactored :blocks field into :blocked_users relation.
[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} = User.mute(user, muted_user)
677
678 assert User.mutes?(user, muted_user)
679 assert User.muted_notifications?(user, muted_user)
680 end
681
682 test "it unmutes users" do
683 user = insert(:user)
684 muted_user = insert(:user)
685
686 {:ok, user} = User.mute(user, muted_user)
687 {:ok, user} = User.unmute(user, muted_user)
688
689 refute User.mutes?(user, muted_user)
690 refute User.muted_notifications?(user, muted_user)
691 end
692
693 test "it mutes user without notifications" do
694 user = insert(:user)
695 muted_user = insert(:user)
696
697 refute User.mutes?(user, muted_user)
698 refute User.muted_notifications?(user, muted_user)
699
700 {:ok, user} = User.mute(user, muted_user, false)
701
702 assert User.mutes?(user, muted_user)
703 refute User.muted_notifications?(user, muted_user)
704 end
705 end
706
707 describe "blocks" do
708 test "it blocks people" do
709 user = insert(:user)
710 blocked_user = insert(:user)
711
712 refute User.blocks?(user, blocked_user)
713
714 {:ok, _user_block} = User.block(user, blocked_user)
715
716 assert User.blocks?(user, blocked_user)
717 end
718
719 test "it unblocks users" do
720 user = insert(:user)
721 blocked_user = insert(:user)
722
723 {:ok, _user_block} = User.block(user, blocked_user)
724 {:ok, _user_block} = User.unblock(user, blocked_user)
725
726 refute User.blocks?(user, blocked_user)
727 end
728
729 test "blocks tear down cyclical follow relationships" do
730 blocker = insert(:user)
731 blocked = insert(:user)
732
733 {:ok, blocker} = User.follow(blocker, blocked)
734 {:ok, blocked} = User.follow(blocked, blocker)
735
736 assert User.following?(blocker, blocked)
737 assert User.following?(blocked, blocker)
738
739 {:ok, _user_block} = User.block(blocker, blocked)
740 blocked = User.get_cached_by_id(blocked.id)
741
742 assert User.blocks?(blocker, blocked)
743
744 refute User.following?(blocker, blocked)
745 refute User.following?(blocked, blocker)
746 end
747
748 test "blocks tear down blocker->blocked follow relationships" do
749 blocker = insert(:user)
750 blocked = insert(:user)
751
752 {:ok, blocker} = User.follow(blocker, blocked)
753
754 assert User.following?(blocker, blocked)
755 refute User.following?(blocked, blocker)
756
757 {:ok, _user_block} = User.block(blocker, blocked)
758 blocked = User.get_cached_by_id(blocked.id)
759
760 assert User.blocks?(blocker, blocked)
761
762 refute User.following?(blocker, blocked)
763 refute User.following?(blocked, blocker)
764 end
765
766 test "blocks tear down blocked->blocker follow relationships" do
767 blocker = insert(:user)
768 blocked = insert(:user)
769
770 {:ok, blocked} = User.follow(blocked, blocker)
771
772 refute User.following?(blocker, blocked)
773 assert User.following?(blocked, blocker)
774
775 {:ok, _user_block} = User.block(blocker, blocked)
776 blocked = User.get_cached_by_id(blocked.id)
777
778 assert User.blocks?(blocker, blocked)
779
780 refute User.following?(blocker, blocked)
781 refute User.following?(blocked, blocker)
782 end
783
784 test "blocks tear down blocked->blocker subscription relationships" do
785 blocker = insert(:user)
786 blocked = insert(:user)
787
788 {:ok, blocker} = User.subscribe(blocked, blocker)
789
790 assert User.subscribed_to?(blocked, blocker)
791 refute User.subscribed_to?(blocker, blocked)
792
793 {:ok, _user_block} = User.block(blocker, blocked)
794
795 assert User.blocks?(blocker, blocked)
796 refute User.subscribed_to?(blocker, blocked)
797 refute User.subscribed_to?(blocked, blocker)
798 end
799 end
800
801 describe "domain blocking" do
802 test "blocks domains" do
803 user = insert(:user)
804 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
805
806 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
807
808 assert User.blocks?(user, collateral_user)
809 end
810
811 test "does not block domain with same end" do
812 user = insert(:user)
813
814 collateral_user =
815 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
816
817 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
818
819 refute User.blocks?(user, collateral_user)
820 end
821
822 test "does not block domain with same end if wildcard added" do
823 user = insert(:user)
824
825 collateral_user =
826 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
827
828 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
829
830 refute User.blocks?(user, collateral_user)
831 end
832
833 test "blocks domain with wildcard for subdomain" do
834 user = insert(:user)
835
836 user_from_subdomain =
837 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
838
839 user_with_two_subdomains =
840 insert(:user, %{
841 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
842 })
843
844 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
845
846 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
847
848 assert User.blocks?(user, user_from_subdomain)
849 assert User.blocks?(user, user_with_two_subdomains)
850 assert User.blocks?(user, user_domain)
851 end
852
853 test "unblocks domains" do
854 user = insert(:user)
855 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
856
857 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
858 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
859
860 refute User.blocks?(user, collateral_user)
861 end
862 end
863
864 describe "blocks_import" do
865 test "it imports user blocks from list" do
866 [user1, user2, user3] = insert_list(3, :user)
867
868 identifiers = [
869 user2.ap_id,
870 user3.nickname
871 ]
872
873 {:ok, job} = User.blocks_import(user1, identifiers)
874 result = ObanHelpers.perform(job)
875
876 assert is_list(result)
877 assert result == [user2, user3]
878 end
879 end
880
881 describe "get_recipients_from_activity" do
882 test "get recipients" do
883 actor = insert(:user)
884 user = insert(:user, local: true)
885 user_two = insert(:user, local: false)
886 addressed = insert(:user, local: true)
887 addressed_remote = insert(:user, local: false)
888
889 {:ok, activity} =
890 CommonAPI.post(actor, %{
891 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
892 })
893
894 assert Enum.map([actor, addressed], & &1.ap_id) --
895 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
896
897 {:ok, user} = User.follow(user, actor)
898 {:ok, _user_two} = User.follow(user_two, actor)
899 recipients = User.get_recipients_from_activity(activity)
900 assert length(recipients) == 3
901 assert user in recipients
902 assert addressed in recipients
903 end
904
905 test "has following" do
906 actor = insert(:user)
907 user = insert(:user)
908 user_two = insert(:user)
909 addressed = insert(:user, local: true)
910
911 {:ok, activity} =
912 CommonAPI.post(actor, %{
913 "status" => "hey @#{addressed.nickname}"
914 })
915
916 assert Enum.map([actor, addressed], & &1.ap_id) --
917 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
918
919 {:ok, _actor} = User.follow(actor, user)
920 {:ok, _actor} = User.follow(actor, user_two)
921 recipients = User.get_recipients_from_activity(activity)
922 assert length(recipients) == 2
923 assert addressed in recipients
924 end
925 end
926
927 describe ".deactivate" do
928 test "can de-activate then re-activate a user" do
929 user = insert(:user)
930 assert false == user.deactivated
931 {:ok, user} = User.deactivate(user)
932 assert true == user.deactivated
933 {:ok, user} = User.deactivate(user, false)
934 assert false == user.deactivated
935 end
936
937 test "hide a user from followers" do
938 user = insert(:user)
939 user2 = insert(:user)
940
941 {:ok, user} = User.follow(user, user2)
942 {:ok, _user} = User.deactivate(user)
943
944 info = User.get_cached_user_info(user2)
945
946 assert info.follower_count == 0
947 assert [] = User.get_followers(user2)
948 end
949
950 test "hide a user from friends" do
951 user = insert(:user)
952 user2 = insert(:user)
953
954 {:ok, user2} = User.follow(user2, user)
955 assert User.following_count(user2) == 1
956
957 {:ok, _user} = User.deactivate(user)
958
959 info = User.get_cached_user_info(user2)
960
961 assert info.following_count == 0
962 assert User.following_count(user2) == 0
963 assert [] = User.get_friends(user2)
964 end
965
966 test "hide a user's statuses from timelines and notifications" do
967 user = insert(:user)
968 user2 = insert(:user)
969
970 {:ok, user2} = User.follow(user2, user)
971
972 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
973
974 activity = Repo.preload(activity, :bookmark)
975
976 [notification] = Pleroma.Notification.for_user(user2)
977 assert notification.activity.id == activity.id
978
979 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
980
981 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
982 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
983 "user" => user2
984 })
985
986 {:ok, _user} = User.deactivate(user)
987
988 assert [] == ActivityPub.fetch_public_activities(%{})
989 assert [] == Pleroma.Notification.for_user(user2)
990
991 assert [] ==
992 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
993 "user" => user2
994 })
995 end
996 end
997
998 describe "delete" do
999 setup do
1000 {:ok, user} = insert(:user) |> User.set_cache()
1001
1002 [user: user]
1003 end
1004
1005 clear_config([:instance, :federating])
1006
1007 test ".delete_user_activities deletes all create activities", %{user: user} do
1008 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
1009
1010 User.delete_user_activities(user)
1011
1012 # TODO: Remove favorites, repeats, delete activities.
1013 refute Activity.get_by_id(activity.id)
1014 end
1015
1016 test "it deletes deactivated user" do
1017 {:ok, user} = insert(:user, deactivated: true) |> User.set_cache()
1018
1019 {:ok, job} = User.delete(user)
1020 {:ok, _user} = ObanHelpers.perform(job)
1021
1022 refute User.get_by_id(user.id)
1023 end
1024
1025 test "it deletes a user, all follow relationships and all activities", %{user: user} do
1026 follower = insert(:user)
1027 {:ok, follower} = User.follow(follower, user)
1028
1029 object = insert(:note, user: user)
1030 activity = insert(:note_activity, user: user, note: object)
1031
1032 object_two = insert(:note, user: follower)
1033 activity_two = insert(:note_activity, user: follower, note: object_two)
1034
1035 {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
1036 {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
1037 {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
1038
1039 {:ok, job} = User.delete(user)
1040 {:ok, _user} = ObanHelpers.perform(job)
1041
1042 follower = User.get_cached_by_id(follower.id)
1043
1044 refute User.following?(follower, user)
1045 refute User.get_by_id(user.id)
1046 assert {:ok, nil} == Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1047
1048 user_activities =
1049 user.ap_id
1050 |> Activity.Queries.by_actor()
1051 |> Repo.all()
1052 |> Enum.map(fn act -> act.data["type"] end)
1053
1054 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1055
1056 refute Activity.get_by_id(activity.id)
1057 refute Activity.get_by_id(like.id)
1058 refute Activity.get_by_id(like_two.id)
1059 refute Activity.get_by_id(repeat.id)
1060 end
1061
1062 test_with_mock "it sends out User Delete activity",
1063 %{user: user},
1064 Pleroma.Web.ActivityPub.Publisher,
1065 [:passthrough],
1066 [] do
1067 Pleroma.Config.put([:instance, :federating], true)
1068
1069 {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
1070 {:ok, _} = User.follow(follower, user)
1071
1072 {:ok, job} = User.delete(user)
1073 {:ok, _user} = ObanHelpers.perform(job)
1074
1075 assert ObanHelpers.member?(
1076 %{
1077 "op" => "publish_one",
1078 "params" => %{
1079 "inbox" => "http://mastodon.example.org/inbox",
1080 "id" => "pleroma:fakeid"
1081 }
1082 },
1083 all_enqueued(worker: Pleroma.Workers.PublisherWorker)
1084 )
1085 end
1086 end
1087
1088 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1089 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1090 end
1091
1092 describe "insert or update a user from given data" do
1093 test "with normal data" do
1094 user = insert(:user, %{nickname: "nick@name.de"})
1095 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
1096
1097 assert {:ok, %User{}} = User.insert_or_update_user(data)
1098 end
1099
1100 test "with overly long fields" do
1101 current_max_length = Pleroma.Config.get([:instance, :account_field_value_length], 255)
1102 user = insert(:user, nickname: "nickname@supergood.domain")
1103
1104 data = %{
1105 ap_id: user.ap_id,
1106 name: user.name,
1107 nickname: user.nickname,
1108 fields: [
1109 %{"name" => "myfield", "value" => String.duplicate("h", current_max_length + 1)}
1110 ]
1111 }
1112
1113 assert {:ok, %User{}} = User.insert_or_update_user(data)
1114 end
1115
1116 test "with an overly long bio" do
1117 current_max_length = Pleroma.Config.get([:instance, :user_bio_length], 5000)
1118 user = insert(:user, nickname: "nickname@supergood.domain")
1119
1120 data = %{
1121 ap_id: user.ap_id,
1122 name: user.name,
1123 nickname: user.nickname,
1124 bio: String.duplicate("h", current_max_length + 1),
1125 info: %{}
1126 }
1127
1128 assert {:ok, %User{}} = User.insert_or_update_user(data)
1129 end
1130
1131 test "with an overly long display name" do
1132 current_max_length = Pleroma.Config.get([:instance, :user_name_length], 100)
1133 user = insert(:user, nickname: "nickname@supergood.domain")
1134
1135 data = %{
1136 ap_id: user.ap_id,
1137 name: String.duplicate("h", current_max_length + 1),
1138 nickname: user.nickname,
1139 info: %{}
1140 }
1141
1142 assert {:ok, %User{}} = User.insert_or_update_user(data)
1143 end
1144 end
1145
1146 describe "per-user rich-text filtering" do
1147 test "html_filter_policy returns default policies, when rich-text is enabled" do
1148 user = insert(:user)
1149
1150 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1151 end
1152
1153 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1154 user = insert(:user, no_rich_text: true)
1155
1156 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1157 end
1158 end
1159
1160 describe "caching" do
1161 test "invalidate_cache works" do
1162 user = insert(:user)
1163 _user_info = User.get_cached_user_info(user)
1164
1165 User.invalidate_cache(user)
1166
1167 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1168 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1169 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
1170 end
1171
1172 test "User.delete() plugs any possible zombie objects" do
1173 user = insert(:user)
1174
1175 {:ok, job} = User.delete(user)
1176 {:ok, _} = ObanHelpers.perform(job)
1177
1178 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1179
1180 assert cached_user != user
1181
1182 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1183
1184 assert cached_user != user
1185 end
1186 end
1187
1188 test "auth_active?/1 works correctly" do
1189 Pleroma.Config.put([:instance, :account_activation_required], true)
1190
1191 local_user = insert(:user, local: true, confirmation_pending: true)
1192 confirmed_user = insert(:user, local: true, confirmation_pending: false)
1193 remote_user = insert(:user, local: false)
1194
1195 refute User.auth_active?(local_user)
1196 assert User.auth_active?(confirmed_user)
1197 assert User.auth_active?(remote_user)
1198 end
1199
1200 describe "superuser?/1" do
1201 test "returns false for unprivileged users" do
1202 user = insert(:user, local: true)
1203
1204 refute User.superuser?(user)
1205 end
1206
1207 test "returns false for remote users" do
1208 user = insert(:user, local: false)
1209 remote_admin_user = insert(:user, local: false, is_admin: true)
1210
1211 refute User.superuser?(user)
1212 refute User.superuser?(remote_admin_user)
1213 end
1214
1215 test "returns true for local moderators" do
1216 user = insert(:user, local: true, is_moderator: true)
1217
1218 assert User.superuser?(user)
1219 end
1220
1221 test "returns true for local admins" do
1222 user = insert(:user, local: true, is_admin: true)
1223
1224 assert User.superuser?(user)
1225 end
1226 end
1227
1228 describe "invisible?/1" do
1229 test "returns true for an invisible user" do
1230 user = insert(:user, local: true, invisible: true)
1231
1232 assert User.invisible?(user)
1233 end
1234
1235 test "returns false for a non-invisible user" do
1236 user = insert(:user, local: true)
1237
1238 refute User.invisible?(user)
1239 end
1240 end
1241
1242 describe "visible_for?/2" do
1243 test "returns true when the account is itself" do
1244 user = insert(:user, local: true)
1245
1246 assert User.visible_for?(user, user)
1247 end
1248
1249 test "returns false when the account is unauthenticated and auth is required" do
1250 Pleroma.Config.put([:instance, :account_activation_required], true)
1251
1252 user = insert(:user, local: true, confirmation_pending: true)
1253 other_user = insert(:user, local: true)
1254
1255 refute User.visible_for?(user, other_user)
1256 end
1257
1258 test "returns true when the account is unauthenticated and auth is not required" do
1259 user = insert(:user, local: true, confirmation_pending: true)
1260 other_user = insert(:user, local: true)
1261
1262 assert User.visible_for?(user, other_user)
1263 end
1264
1265 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1266 Pleroma.Config.put([:instance, :account_activation_required], true)
1267
1268 user = insert(:user, local: true, confirmation_pending: true)
1269 other_user = insert(:user, local: true, is_admin: true)
1270
1271 assert User.visible_for?(user, other_user)
1272 end
1273 end
1274
1275 describe "parse_bio/2" do
1276 test "preserves hosts in user links text" do
1277 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1278 user = insert(:user)
1279 bio = "A.k.a. @nick@domain.com"
1280
1281 expected_text =
1282 ~s(A.k.a. <span class="h-card"><a data-user="#{remote_user.id}" class="u-url mention" href="#{
1283 remote_user.ap_id
1284 }" rel="ugc">@<span>nick@domain.com</span></a></span>)
1285
1286 assert expected_text == User.parse_bio(bio, user)
1287 end
1288
1289 test "Adds rel=me on linkbacked urls" do
1290 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1291
1292 bio = "http://example.com/rel_me/null"
1293 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1294 assert expected_text == User.parse_bio(bio, user)
1295
1296 bio = "http://example.com/rel_me/link"
1297 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1298 assert expected_text == User.parse_bio(bio, user)
1299
1300 bio = "http://example.com/rel_me/anchor"
1301 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1302 assert expected_text == User.parse_bio(bio, user)
1303 end
1304 end
1305
1306 test "follower count is updated when a follower is blocked" do
1307 user = insert(:user)
1308 follower = insert(:user)
1309 follower2 = insert(:user)
1310 follower3 = insert(:user)
1311
1312 {:ok, follower} = User.follow(follower, user)
1313 {:ok, _follower2} = User.follow(follower2, user)
1314 {:ok, _follower3} = User.follow(follower3, user)
1315
1316 {:ok, _user_block} = User.block(user, follower)
1317 user = refresh_record(user)
1318
1319 assert User.user_info(user).follower_count == 2
1320 end
1321
1322 describe "list_inactive_users_query/1" do
1323 defp days_ago(days) do
1324 NaiveDateTime.add(
1325 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1326 -days * 60 * 60 * 24,
1327 :second
1328 )
1329 end
1330
1331 test "Users are inactive by default" do
1332 total = 10
1333
1334 users =
1335 Enum.map(1..total, fn _ ->
1336 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1337 end)
1338
1339 inactive_users_ids =
1340 Pleroma.User.list_inactive_users_query()
1341 |> Pleroma.Repo.all()
1342 |> Enum.map(& &1.id)
1343
1344 Enum.each(users, fn user ->
1345 assert user.id in inactive_users_ids
1346 end)
1347 end
1348
1349 test "Only includes users who has no recent activity" do
1350 total = 10
1351
1352 users =
1353 Enum.map(1..total, fn _ ->
1354 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1355 end)
1356
1357 {inactive, active} = Enum.split(users, trunc(total / 2))
1358
1359 Enum.map(active, fn user ->
1360 to = Enum.random(users -- [user])
1361
1362 {:ok, _} =
1363 CommonAPI.post(user, %{
1364 "status" => "hey @#{to.nickname}"
1365 })
1366 end)
1367
1368 inactive_users_ids =
1369 Pleroma.User.list_inactive_users_query()
1370 |> Pleroma.Repo.all()
1371 |> Enum.map(& &1.id)
1372
1373 Enum.each(active, fn user ->
1374 refute user.id in inactive_users_ids
1375 end)
1376
1377 Enum.each(inactive, fn user ->
1378 assert user.id in inactive_users_ids
1379 end)
1380 end
1381
1382 test "Only includes users with no read notifications" do
1383 total = 10
1384
1385 users =
1386 Enum.map(1..total, fn _ ->
1387 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1388 end)
1389
1390 [sender | recipients] = users
1391 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1392
1393 Enum.each(recipients, fn to ->
1394 {:ok, _} =
1395 CommonAPI.post(sender, %{
1396 "status" => "hey @#{to.nickname}"
1397 })
1398
1399 {:ok, _} =
1400 CommonAPI.post(sender, %{
1401 "status" => "hey again @#{to.nickname}"
1402 })
1403 end)
1404
1405 Enum.each(active, fn user ->
1406 [n1, _n2] = Pleroma.Notification.for_user(user)
1407 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1408 end)
1409
1410 inactive_users_ids =
1411 Pleroma.User.list_inactive_users_query()
1412 |> Pleroma.Repo.all()
1413 |> Enum.map(& &1.id)
1414
1415 Enum.each(active, fn user ->
1416 refute user.id in inactive_users_ids
1417 end)
1418
1419 Enum.each(inactive, fn user ->
1420 assert user.id in inactive_users_ids
1421 end)
1422 end
1423 end
1424
1425 describe "toggle_confirmation/1" do
1426 test "if user is confirmed" do
1427 user = insert(:user, confirmation_pending: false)
1428 {:ok, user} = User.toggle_confirmation(user)
1429
1430 assert user.confirmation_pending
1431 assert user.confirmation_token
1432 end
1433
1434 test "if user is unconfirmed" do
1435 user = insert(:user, confirmation_pending: true, confirmation_token: "some token")
1436 {:ok, user} = User.toggle_confirmation(user)
1437
1438 refute user.confirmation_pending
1439 refute user.confirmation_token
1440 end
1441 end
1442
1443 describe "ensure_keys_present" do
1444 test "it creates keys for a user and stores them in info" do
1445 user = insert(:user)
1446 refute is_binary(user.keys)
1447 {:ok, user} = User.ensure_keys_present(user)
1448 assert is_binary(user.keys)
1449 end
1450
1451 test "it doesn't create keys if there already are some" do
1452 user = insert(:user, keys: "xxx")
1453 {:ok, user} = User.ensure_keys_present(user)
1454 assert user.keys == "xxx"
1455 end
1456 end
1457
1458 describe "get_ap_ids_by_nicknames" do
1459 test "it returns a list of AP ids for a given set of nicknames" do
1460 user = insert(:user)
1461 user_two = insert(:user)
1462
1463 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1464 assert length(ap_ids) == 2
1465 assert user.ap_id in ap_ids
1466 assert user_two.ap_id in ap_ids
1467 end
1468 end
1469
1470 describe "sync followers count" do
1471 setup do
1472 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1473 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1474 insert(:user, local: true)
1475 insert(:user, local: false, deactivated: true)
1476 {:ok, user1: user1, user2: user2}
1477 end
1478
1479 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1480 [fdb_user1] = User.external_users(limit: 1)
1481
1482 assert fdb_user1.ap_id
1483 assert fdb_user1.ap_id == user1.ap_id
1484 assert fdb_user1.id == user1.id
1485
1486 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1487
1488 assert fdb_user2.ap_id
1489 assert fdb_user2.ap_id == user2.ap_id
1490 assert fdb_user2.id == user2.id
1491
1492 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1493 end
1494 end
1495
1496 describe "set_info_cache/2" do
1497 setup do
1498 user = insert(:user)
1499 {:ok, user: user}
1500 end
1501
1502 test "update from args", %{user: user} do
1503 User.set_info_cache(user, %{following_count: 15, follower_count: 18})
1504
1505 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1506 assert followers == 18
1507 assert following == 15
1508 end
1509
1510 test "without args", %{user: user} do
1511 User.set_info_cache(user, %{})
1512
1513 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1514 assert followers == 0
1515 assert following == 0
1516 end
1517 end
1518
1519 describe "user_info/2" do
1520 setup do
1521 user = insert(:user)
1522 {:ok, user: user}
1523 end
1524
1525 test "update from args", %{user: user} do
1526 %{follower_count: followers, following_count: following} =
1527 User.user_info(user, %{following_count: 15, follower_count: 18})
1528
1529 assert followers == 18
1530 assert following == 15
1531 end
1532
1533 test "without args", %{user: user} do
1534 %{follower_count: followers, following_count: following} = User.user_info(user)
1535
1536 assert followers == 0
1537 assert following == 0
1538 end
1539 end
1540
1541 describe "is_internal_user?/1" do
1542 test "non-internal user returns false" do
1543 user = insert(:user)
1544 refute User.is_internal_user?(user)
1545 end
1546
1547 test "user with no nickname returns true" do
1548 user = insert(:user, %{nickname: nil})
1549 assert User.is_internal_user?(user)
1550 end
1551
1552 test "user with internal-prefixed nickname returns true" do
1553 user = insert(:user, %{nickname: "internal.test"})
1554 assert User.is_internal_user?(user)
1555 end
1556 end
1557
1558 describe "update_and_set_cache/1" do
1559 test "returns error when user is stale instead Ecto.StaleEntryError" do
1560 user = insert(:user)
1561
1562 changeset = Ecto.Changeset.change(user, bio: "test")
1563
1564 Repo.delete(user)
1565
1566 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1567 User.update_and_set_cache(changeset)
1568 end
1569
1570 test "performs update cache if user updated" do
1571 user = insert(:user)
1572 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1573
1574 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1575
1576 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1577 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1578 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1579 end
1580 end
1581
1582 describe "following/followers synchronization" do
1583 clear_config([:instance, :external_user_synchronization])
1584
1585 test "updates the counters normally on following/getting a follow when disabled" do
1586 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1587 user = insert(:user)
1588
1589 other_user =
1590 insert(:user,
1591 local: false,
1592 follower_address: "http://localhost:4001/users/masto_closed/followers",
1593 following_address: "http://localhost:4001/users/masto_closed/following",
1594 ap_enabled: true
1595 )
1596
1597 assert User.user_info(other_user).following_count == 0
1598 assert User.user_info(other_user).follower_count == 0
1599
1600 {:ok, user} = Pleroma.User.follow(user, other_user)
1601 other_user = Pleroma.User.get_by_id(other_user.id)
1602
1603 assert User.user_info(user).following_count == 1
1604 assert User.user_info(other_user).follower_count == 1
1605 end
1606
1607 test "syncronizes the counters with the remote instance for the followed when enabled" do
1608 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1609
1610 user = insert(:user)
1611
1612 other_user =
1613 insert(:user,
1614 local: false,
1615 follower_address: "http://localhost:4001/users/masto_closed/followers",
1616 following_address: "http://localhost:4001/users/masto_closed/following",
1617 ap_enabled: true
1618 )
1619
1620 assert User.user_info(other_user).following_count == 0
1621 assert User.user_info(other_user).follower_count == 0
1622
1623 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1624 {:ok, _user} = User.follow(user, other_user)
1625 other_user = User.get_by_id(other_user.id)
1626
1627 assert User.user_info(other_user).follower_count == 437
1628 end
1629
1630 test "syncronizes the counters with the remote instance for the follower when enabled" do
1631 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1632
1633 user = insert(:user)
1634
1635 other_user =
1636 insert(:user,
1637 local: false,
1638 follower_address: "http://localhost:4001/users/masto_closed/followers",
1639 following_address: "http://localhost:4001/users/masto_closed/following",
1640 ap_enabled: true
1641 )
1642
1643 assert User.user_info(other_user).following_count == 0
1644 assert User.user_info(other_user).follower_count == 0
1645
1646 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1647 {:ok, other_user} = User.follow(other_user, user)
1648
1649 assert User.user_info(other_user).following_count == 152
1650 end
1651 end
1652
1653 describe "change_email/2" do
1654 setup do
1655 [user: insert(:user)]
1656 end
1657
1658 test "blank email returns error", %{user: user} do
1659 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1660 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1661 end
1662
1663 test "non unique email returns error", %{user: user} do
1664 %{email: email} = insert(:user)
1665
1666 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1667 User.change_email(user, email)
1668 end
1669
1670 test "invalid email returns error", %{user: user} do
1671 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1672 User.change_email(user, "cofe")
1673 end
1674
1675 test "changes email", %{user: user} do
1676 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1677 end
1678 end
1679
1680 describe "get_cached_by_nickname_or_id" do
1681 setup do
1682 limit_to_local_content = Pleroma.Config.get([:instance, :limit_to_local_content])
1683 local_user = insert(:user)
1684 remote_user = insert(:user, nickname: "nickname@example.com", local: false)
1685
1686 on_exit(fn ->
1687 Pleroma.Config.put([:instance, :limit_to_local_content], limit_to_local_content)
1688 end)
1689
1690 [local_user: local_user, remote_user: remote_user]
1691 end
1692
1693 test "allows getting remote users by id no matter what :limit_to_local_content is set to", %{
1694 remote_user: remote_user
1695 } do
1696 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1697 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1698
1699 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1700 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1701
1702 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1703 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1704 end
1705
1706 test "disallows getting remote users by nickname without authentication when :limit_to_local_content is set to :unauthenticated",
1707 %{remote_user: remote_user} do
1708 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1709 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1710 end
1711
1712 test "allows getting remote users by nickname with authentication when :limit_to_local_content is set to :unauthenticated",
1713 %{remote_user: remote_user, local_user: local_user} do
1714 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1715 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.nickname, for: local_user)
1716 end
1717
1718 test "disallows getting remote users by nickname when :limit_to_local_content is set to true",
1719 %{remote_user: remote_user} do
1720 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1721 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1722 end
1723
1724 test "allows getting local users by nickname no matter what :limit_to_local_content is set to",
1725 %{local_user: local_user} do
1726 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1727 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1728
1729 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1730 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1731
1732 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1733 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1734 end
1735 end
1736
1737 describe "update_email_notifications/2" do
1738 setup do
1739 user = insert(:user, email_notifications: %{"digest" => true})
1740
1741 {:ok, user: user}
1742 end
1743
1744 test "Notifications are updated", %{user: user} do
1745 true = user.email_notifications["digest"]
1746 assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false})
1747 assert result.email_notifications["digest"] == false
1748 end
1749 end
1750 end