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