1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UserTest do
7 alias Pleroma.Builders.UserBuilder
10 alias Pleroma.Tests.ObanHelpers
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.CommonAPI
16 use Oban.Testing, repo: Pleroma.Repo
19 import Pleroma.Factory
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 clear_config([:instance, :account_activation_required])
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"])
33 assert "cool" in user.tags
34 assert "dude" in user.tags
37 test "untagging a user" do
38 user = insert(:user, %{tags: nil})
39 user = User.untag(user, ["cool", "dude"])
41 assert user.tags == []
45 test "ap_id returns the activity pub id for the user" do
46 user = UserBuilder.build()
48 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
50 assert expected_ap_id == User.ap_id(user)
53 test "ap_followers returns the followers collection for the user" do
54 user = UserBuilder.build()
56 expected_followers_collection = "#{User.ap_id(user)}/followers"
58 assert expected_followers_collection == User.ap_followers(user)
61 test "ap_following returns the following collection for the user" do
62 user = UserBuilder.build()
64 expected_followers_collection = "#{User.ap_id(user)}/following"
66 assert expected_followers_collection == User.ap_following(user)
69 test "returns all pending follow requests" do
70 unlocked = insert(:user)
71 locked = insert(:user, locked: true)
72 follower = insert(:user)
74 CommonAPI.follow(follower, unlocked)
75 CommonAPI.follow(follower, locked)
77 assert [] = User.get_follow_requests(unlocked)
78 assert [activity] = User.get_follow_requests(locked)
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)
88 CommonAPI.follow(pending_follower, locked)
89 CommonAPI.follow(pending_follower, locked)
90 CommonAPI.follow(accepted_follower, locked)
91 User.follow(accepted_follower, locked)
93 assert [activity] = User.get_follow_requests(locked)
97 test "clears follow requests when requester is blocked" do
98 followed = insert(:user, locked: true)
99 follower = insert(:user)
101 CommonAPI.follow(follower, followed)
102 assert [_activity] = User.get_follow_requests(followed)
104 {:ok, _follower} = User.block(followed, follower)
105 assert [] = User.get_follow_requests(followed)
108 test "follow_all follows mutliple users" do
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)
117 {:ok, user} = User.block(user, blocked)
118 {:ok, reverse_blocked} = User.block(reverse_blocked, user)
120 {:ok, user} = User.follow(user, followed_zero)
122 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
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)
132 test "follow_all follows mutliple users without duplicating" do
134 followed_zero = insert(:user)
135 followed_one = insert(:user)
136 followed_two = insert(:user)
138 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
139 assert length(user.following) == 3
141 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
142 assert length(user.following) == 4
145 test "follow takes a user and another user" do
147 followed = insert(:user)
149 {:ok, user} = User.follow(user, followed)
151 user = User.get_cached_by_id(user.id)
153 followed = User.get_cached_by_ap_id(followed.ap_id)
154 assert followed.follower_count == 1
156 assert User.ap_followers(followed) in user.following
159 test "can't follow a deactivated users" do
161 followed = insert(:user, %{deactivated: true})
163 {:error, _} = User.follow(user, followed)
166 test "can't follow a user who blocked us" do
167 blocker = insert(:user)
168 blockee = insert(:user)
170 {:ok, blocker} = User.block(blocker, blockee)
172 {:error, _} = User.follow(blockee, blocker)
175 test "can't subscribe to a user who blocked us" do
176 blocker = insert(:user)
177 blocked = insert(:user)
179 {:ok, blocker} = User.block(blocker, blocked)
181 {:error, _} = User.subscribe(blocked, blocker)
184 test "local users do not automatically follow local locked accounts" do
185 follower = insert(:user, locked: true)
186 followed = insert(:user, locked: true)
188 {:ok, follower} = User.maybe_direct_follow(follower, followed)
190 refute User.following?(follower, followed)
193 describe "unfollow/2" do
195 setting = Pleroma.Config.get([:instance, :external_user_synchronization])
198 Pleroma.Config.put([:instance, :external_user_synchronization], setting)
204 test "unfollow with syncronizes external user" do
205 Pleroma.Config.put([:instance, :external_user_synchronization], true)
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"
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)]
225 {:ok, user, _activity} = User.unfollow(user, followed)
227 user = User.get_cached_by_id(user.id)
229 assert user.following == []
232 test "unfollow takes a user and another user" do
233 followed = insert(:user)
234 user = insert(:user, %{following: [User.ap_followers(followed)]})
236 {:ok, user, _activity} = User.unfollow(user, followed)
238 user = User.get_cached_by_id(user.id)
240 assert user.following == []
243 test "unfollow doesn't unfollow yourself" do
246 {:error, _} = User.unfollow(user, user)
248 user = User.get_cached_by_id(user.id)
249 assert user.following == [user.ap_id]
253 test "test if a user is following another user" do
254 followed = insert(:user)
255 user = insert(:user, %{following: [User.ap_followers(followed)]})
257 assert User.following?(user, followed)
258 refute User.following?(followed, user)
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)
268 describe "user registration" do
274 password_confirmation: "test",
275 email: "email@example.com"
277 clear_config([:instance, :autofollowed_nicknames])
278 clear_config([:instance, :welcome_message])
279 clear_config([:instance, :welcome_user_nickname])
281 test "it autofollows accounts that are set for it" do
283 remote_user = insert(:user, %{local: false})
285 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
290 cng = User.register_changeset(%User{}, @full_user_data)
292 {:ok, registered_user} = User.register(cng)
294 assert User.following?(registered_user, user)
295 refute User.following?(registered_user, remote_user)
298 test "it sends a welcome message if it is set" do
299 welcome_user = insert(:user)
301 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
302 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
304 cng = User.register_changeset(%User{}, @full_user_data)
305 {:ok, registered_user} = User.register(cng)
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
313 test "it requires an email, name, nickname and password, bio is optional" do
316 |> Enum.each(fn key ->
317 params = Map.delete(@full_user_data, key)
318 changeset = User.register_changeset(%User{}, params)
320 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
324 test "it restricts certain nicknames" do
325 [restricted_name | _] = Pleroma.Config.get([User, :restricted_nicknames])
327 assert is_bitstring(restricted_name)
331 |> Map.put(:nickname, restricted_name)
333 changeset = User.register_changeset(%User{}, params)
335 refute changeset.valid?
338 test "it sets the password_hash, ap_id and following fields" do
339 changeset = User.register_changeset(%User{}, @full_user_data)
341 assert changeset.valid?
343 assert is_binary(changeset.changes[:password_hash])
344 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
346 assert changeset.changes[:following] == [
347 User.ap_followers(%User{nickname: @full_user_data.nickname})
350 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
353 test "it ensures info is not nil" do
354 changeset = User.register_changeset(%User{}, @full_user_data)
356 assert changeset.valid?
362 refute is_nil(user.info)
366 describe "user registration, with :account_activation_required" do
372 password_confirmation: "test",
373 email: "email@example.com"
376 clear_config([:instance, :account_activation_required]) do
377 Pleroma.Config.put([:instance, :account_activation_required], true)
380 test "it creates unconfirmed user" do
381 changeset = User.register_changeset(%User{}, @full_user_data)
382 assert changeset.valid?
384 {:ok, user} = Repo.insert(changeset)
386 assert user.confirmation_pending
387 assert user.confirmation_token
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?
394 {:ok, user} = Repo.insert(changeset)
396 refute user.confirmation_pending
397 refute user.confirmation_token
401 describe "get_or_fetch/1" do
402 test "gets an existing user by nickname" do
404 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
406 assert user == fetched_user
409 test "gets an existing user by ap_id" do
410 ap_id = "http://mastodon.example.org/users/admin"
416 nickname: "admin@mastodon.example.org",
421 {:ok, fetched_user} = User.get_or_fetch(ap_id)
422 freshed_user = refresh_record(user)
423 assert freshed_user == fetched_user
427 describe "fetching a user from nickname or trying to build one" do
428 test "gets an existing user" do
430 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
432 assert user == fetched_user
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")
439 assert user == fetched_user
442 test "gets an existing user by fully qualified nickname" do
445 {:ok, fetched_user} =
446 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
448 assert user == fetched_user
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())
455 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
457 assert user == fetched_user
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"
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"
470 test "updates an existing user, if stale" do
471 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
477 nickname: "admin@mastodon.example.org",
478 ap_id: "http://mastodon.example.org/users/admin",
479 last_refreshed_at: a_week_ago,
483 assert orig_user.last_refreshed_at == a_week_ago
485 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
487 assert user.source_data["endpoints"]
489 refute user.last_refreshed_at == orig_user.last_refreshed_at
493 test "returns an ap_id for a user" do
496 assert User.ap_id(user) ==
497 Pleroma.Web.Router.Helpers.feed_url(
498 Pleroma.Web.Endpoint,
504 test "returns an ap_followers link for a user" do
507 assert User.ap_followers(user) ==
508 Pleroma.Web.Router.Helpers.feed_url(
509 Pleroma.Web.Endpoint,
515 describe "remote user creation changeset" do
521 info: %{some: "info"},
522 avatar: %{some: "avatar"}
525 clear_config([:instance, :user_bio_length])
526 clear_config([:instance, :user_name_length])
528 test "it confirms validity" do
529 cs = User.remote_user_creation(@valid_remote)
533 test "it sets the follower_adress" do
534 cs = User.remote_user_creation(@valid_remote)
535 # remote users get a fake local follower address
536 assert cs.changes.follower_address ==
537 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
540 test "it enforces the fqn format for nicknames" do
541 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
542 assert Ecto.Changeset.get_field(cs, :local) == false
543 assert cs.changes.avatar
547 test "it has required fields" do
549 |> Enum.each(fn field ->
550 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
556 describe "followers and friends" do
557 test "gets all followers for a given user" do
559 follower_one = insert(:user)
560 follower_two = insert(:user)
561 not_follower = insert(:user)
563 {:ok, follower_one} = User.follow(follower_one, user)
564 {:ok, follower_two} = User.follow(follower_two, user)
566 res = User.get_followers(user)
568 assert Enum.member?(res, follower_one)
569 assert Enum.member?(res, follower_two)
570 refute Enum.member?(res, not_follower)
573 test "gets all friends (followed users) for a given user" do
575 followed_one = insert(:user)
576 followed_two = insert(:user)
577 not_followed = insert(:user)
579 {:ok, user} = User.follow(user, followed_one)
580 {:ok, user} = User.follow(user, followed_two)
582 res = User.get_friends(user)
584 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
585 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
586 assert Enum.member?(res, followed_one)
587 assert Enum.member?(res, followed_two)
588 refute Enum.member?(res, not_followed)
592 describe "updating note and follower count" do
593 test "it sets the note_count property" do
596 user = User.get_cached_by_ap_id(note.data["actor"])
598 assert user.note_count == 0
600 {:ok, user} = User.update_note_count(user)
602 assert user.note_count == 1
605 test "it increases the note_count property" do
607 user = User.get_cached_by_ap_id(note.data["actor"])
609 assert user.note_count == 0
611 {:ok, user} = User.increase_note_count(user)
613 assert user.note_count == 1
615 {:ok, user} = User.increase_note_count(user)
617 assert user.note_count == 2
620 test "it decreases the note_count property" do
622 user = User.get_cached_by_ap_id(note.data["actor"])
624 assert user.note_count == 0
626 {:ok, user} = User.increase_note_count(user)
628 assert user.note_count == 1
630 {:ok, user} = User.decrease_note_count(user)
632 assert user.note_count == 0
634 {:ok, user} = User.decrease_note_count(user)
636 assert user.note_count == 0
639 test "it sets the follower_count property" do
641 follower = insert(:user)
643 User.follow(follower, user)
645 assert user.follower_count == 0
647 {:ok, user} = User.update_follower_count(user)
649 assert user.follower_count == 1
653 describe "remove duplicates from following list" do
654 test "it removes duplicates" do
656 follower = insert(:user)
658 {:ok, %User{following: following} = follower} = User.follow(follower, user)
659 assert length(following) == 2
663 |> User.update_changeset(%{following: following ++ following})
666 assert length(follower.following) == 4
668 {:ok, follower} = User.remove_duplicated_following(follower)
669 assert length(follower.following) == 2
672 test "it does nothing when following is uniq" do
674 follower = insert(:user)
676 {:ok, follower} = User.follow(follower, user)
677 assert length(follower.following) == 2
679 {:ok, follower} = User.remove_duplicated_following(follower)
680 assert length(follower.following) == 2
684 describe "follow_import" do
685 test "it imports user followings from list" do
686 [user1, user2, user3] = insert_list(3, :user)
693 {:ok, job} = User.follow_import(user1, identifiers)
694 result = ObanHelpers.perform(job)
696 assert is_list(result)
697 assert result == [user2, user3]
702 test "it mutes people" do
704 muted_user = insert(:user)
706 refute User.mutes?(user, muted_user)
707 refute User.muted_notifications?(user, muted_user)
709 {:ok, user} = User.mute(user, muted_user)
711 assert User.mutes?(user, muted_user)
712 assert User.muted_notifications?(user, muted_user)
715 test "it unmutes users" do
717 muted_user = insert(:user)
719 {:ok, user} = User.mute(user, muted_user)
720 {:ok, user} = User.unmute(user, muted_user)
722 refute User.mutes?(user, muted_user)
723 refute User.muted_notifications?(user, muted_user)
726 test "it mutes user without notifications" do
728 muted_user = insert(:user)
730 refute User.mutes?(user, muted_user)
731 refute User.muted_notifications?(user, muted_user)
733 {:ok, user} = User.mute(user, muted_user, false)
735 assert User.mutes?(user, muted_user)
736 refute User.muted_notifications?(user, muted_user)
741 test "it blocks people" do
743 blocked_user = insert(:user)
745 refute User.blocks?(user, blocked_user)
747 {:ok, user} = User.block(user, blocked_user)
749 assert User.blocks?(user, blocked_user)
752 test "it unblocks users" do
754 blocked_user = insert(:user)
756 {:ok, user} = User.block(user, blocked_user)
757 {:ok, user} = User.unblock(user, blocked_user)
759 refute User.blocks?(user, blocked_user)
762 test "blocks tear down cyclical follow relationships" do
763 blocker = insert(:user)
764 blocked = insert(:user)
766 {:ok, blocker} = User.follow(blocker, blocked)
767 {:ok, blocked} = User.follow(blocked, blocker)
769 assert User.following?(blocker, blocked)
770 assert User.following?(blocked, blocker)
772 {:ok, blocker} = User.block(blocker, blocked)
773 blocked = User.get_cached_by_id(blocked.id)
775 assert User.blocks?(blocker, blocked)
777 refute User.following?(blocker, blocked)
778 refute User.following?(blocked, blocker)
781 test "blocks tear down blocker->blocked follow relationships" do
782 blocker = insert(:user)
783 blocked = insert(:user)
785 {:ok, blocker} = User.follow(blocker, blocked)
787 assert User.following?(blocker, blocked)
788 refute User.following?(blocked, blocker)
790 {:ok, blocker} = User.block(blocker, blocked)
791 blocked = User.get_cached_by_id(blocked.id)
793 assert User.blocks?(blocker, blocked)
795 refute User.following?(blocker, blocked)
796 refute User.following?(blocked, blocker)
799 test "blocks tear down blocked->blocker follow relationships" do
800 blocker = insert(:user)
801 blocked = insert(:user)
803 {:ok, blocked} = User.follow(blocked, blocker)
805 refute User.following?(blocker, blocked)
806 assert User.following?(blocked, blocker)
808 {:ok, blocker} = User.block(blocker, blocked)
809 blocked = User.get_cached_by_id(blocked.id)
811 assert User.blocks?(blocker, blocked)
813 refute User.following?(blocker, blocked)
814 refute User.following?(blocked, blocker)
817 test "blocks tear down blocked->blocker subscription relationships" do
818 blocker = insert(:user)
819 blocked = insert(:user)
821 {:ok, blocker} = User.subscribe(blocked, blocker)
823 assert User.subscribed_to?(blocked, blocker)
824 refute User.subscribed_to?(blocker, blocked)
826 {:ok, blocker} = User.block(blocker, blocked)
828 assert User.blocks?(blocker, blocked)
829 refute User.subscribed_to?(blocker, blocked)
830 refute User.subscribed_to?(blocked, blocker)
834 describe "domain blocking" do
835 test "blocks domains" do
837 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
839 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
841 assert User.blocks?(user, collateral_user)
844 test "does not block domain with same end" do
848 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
850 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
852 refute User.blocks?(user, collateral_user)
855 test "does not block domain with same end if wildcard added" do
859 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
861 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
863 refute User.blocks?(user, collateral_user)
866 test "blocks domain with wildcard for subdomain" do
869 user_from_subdomain =
870 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
872 user_with_two_subdomains =
874 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
877 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
879 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
881 assert User.blocks?(user, user_from_subdomain)
882 assert User.blocks?(user, user_with_two_subdomains)
883 assert User.blocks?(user, user_domain)
886 test "unblocks domains" do
888 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
890 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
891 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
893 refute User.blocks?(user, collateral_user)
897 describe "blocks_import" do
898 test "it imports user blocks from list" do
899 [user1, user2, user3] = insert_list(3, :user)
906 {:ok, job} = User.blocks_import(user1, identifiers)
907 result = ObanHelpers.perform(job)
909 assert is_list(result)
910 assert result == [user2, user3]
914 test "get recipients from activity" do
915 actor = insert(:user)
916 user = insert(:user, local: true)
917 user_two = insert(:user, local: false)
918 addressed = insert(:user, local: true)
919 addressed_remote = insert(:user, local: false)
922 CommonAPI.post(actor, %{
923 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
926 assert Enum.map([actor, addressed], & &1.ap_id) --
927 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
929 {:ok, user} = User.follow(user, actor)
930 {:ok, _user_two} = User.follow(user_two, actor)
931 recipients = User.get_recipients_from_activity(activity)
932 assert length(recipients) == 3
933 assert user in recipients
934 assert addressed in recipients
937 describe ".deactivate" do
938 test "can de-activate then re-activate a user" do
940 assert false == user.deactivated
941 {:ok, user} = User.deactivate(user)
942 assert true == user.deactivated
943 {:ok, user} = User.deactivate(user, false)
944 assert false == user.deactivated
947 test "hide a user from followers" do
949 user2 = insert(:user)
951 {:ok, user} = User.follow(user, user2)
952 {:ok, _user} = User.deactivate(user)
954 info = User.get_cached_user_info(user2)
956 assert info.follower_count == 0
957 assert [] = User.get_followers(user2)
960 test "hide a user from friends" do
962 user2 = insert(:user)
964 {:ok, user2} = User.follow(user2, user)
965 assert User.following_count(user2) == 1
967 {:ok, _user} = User.deactivate(user)
969 info = User.get_cached_user_info(user2)
971 assert info.following_count == 0
972 assert User.following_count(user2) == 0
973 assert [] = User.get_friends(user2)
976 test "hide a user's statuses from timelines and notifications" do
978 user2 = insert(:user)
980 {:ok, user2} = User.follow(user2, user)
982 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
984 activity = Repo.preload(activity, :bookmark)
986 [notification] = Pleroma.Notification.for_user(user2)
987 assert notification.activity.id == activity.id
989 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
991 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
992 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
994 {:ok, _user} = User.deactivate(user)
996 assert [] == ActivityPub.fetch_public_activities(%{})
997 assert [] == Pleroma.Notification.for_user(user2)
1000 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
1004 describe "delete" do
1006 {:ok, user} = insert(:user) |> User.set_cache()
1011 clear_config([:instance, :federating])
1013 test ".delete_user_activities deletes all create activities", %{user: user} do
1014 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
1016 User.delete_user_activities(user)
1018 # TODO: Remove favorites, repeats, delete activities.
1019 refute Activity.get_by_id(activity.id)
1022 test "it deletes deactivated user" do
1023 {:ok, user} = insert(:user, deactivated: true) |> User.set_cache()
1025 {:ok, job} = User.delete(user)
1026 {:ok, _user} = ObanHelpers.perform(job)
1028 refute User.get_by_id(user.id)
1031 test "it deletes a user, all follow relationships and all activities", %{user: user} do
1032 follower = insert(:user)
1033 {:ok, follower} = User.follow(follower, user)
1035 object = insert(:note, user: user)
1036 activity = insert(:note_activity, user: user, note: object)
1038 object_two = insert(:note, user: follower)
1039 activity_two = insert(:note_activity, user: follower, note: object_two)
1041 {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
1042 {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
1043 {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
1045 {:ok, job} = User.delete(user)
1046 {:ok, _user} = ObanHelpers.perform(job)
1048 follower = User.get_cached_by_id(follower.id)
1050 refute User.following?(follower, user)
1051 refute User.get_by_id(user.id)
1052 assert {:ok, nil} == Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1056 |> Activity.Queries.by_actor()
1058 |> Enum.map(fn act -> act.data["type"] end)
1060 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1062 refute Activity.get_by_id(activity.id)
1063 refute Activity.get_by_id(like.id)
1064 refute Activity.get_by_id(like_two.id)
1065 refute Activity.get_by_id(repeat.id)
1068 test_with_mock "it sends out User Delete activity",
1070 Pleroma.Web.ActivityPub.Publisher,
1073 Pleroma.Config.put([:instance, :federating], true)
1075 {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
1076 {:ok, _} = User.follow(follower, user)
1078 {:ok, job} = User.delete(user)
1079 {:ok, _user} = ObanHelpers.perform(job)
1081 assert ObanHelpers.member?(
1083 "op" => "publish_one",
1085 "inbox" => "http://mastodon.example.org/inbox",
1086 "id" => "pleroma:fakeid"
1089 all_enqueued(worker: Pleroma.Workers.PublisherWorker)
1094 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1095 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1098 describe "insert or update a user from given data" do
1099 test "with normal data" do
1100 user = insert(:user, %{nickname: "nick@name.de"})
1101 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
1103 assert {:ok, %User{}} = User.insert_or_update_user(data)
1106 test "with overly long fields" do
1107 current_max_length = Pleroma.Config.get([:instance, :account_field_value_length], 255)
1108 user = insert(:user, nickname: "nickname@supergood.domain")
1113 nickname: user.nickname,
1115 %{"name" => "myfield", "value" => String.duplicate("h", current_max_length + 1)}
1119 assert {:ok, %User{}} = User.insert_or_update_user(data)
1122 test "with an overly long bio" do
1123 current_max_length = Pleroma.Config.get([:instance, :user_bio_length], 5000)
1124 user = insert(:user, nickname: "nickname@supergood.domain")
1129 nickname: user.nickname,
1130 bio: String.duplicate("h", current_max_length + 1),
1134 assert {:ok, %User{}} = User.insert_or_update_user(data)
1137 test "with an overly long display name" do
1138 current_max_length = Pleroma.Config.get([:instance, :user_name_length], 100)
1139 user = insert(:user, nickname: "nickname@supergood.domain")
1143 name: String.duplicate("h", current_max_length + 1),
1144 nickname: user.nickname,
1148 assert {:ok, %User{}} = User.insert_or_update_user(data)
1152 describe "per-user rich-text filtering" do
1153 test "html_filter_policy returns default policies, when rich-text is enabled" do
1154 user = insert(:user)
1156 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1159 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1160 user = insert(:user, no_rich_text: true)
1162 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1166 describe "caching" do
1167 test "invalidate_cache works" do
1168 user = insert(:user)
1169 _user_info = User.get_cached_user_info(user)
1171 User.invalidate_cache(user)
1173 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1174 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1175 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
1178 test "User.delete() plugs any possible zombie objects" do
1179 user = insert(:user)
1181 {:ok, job} = User.delete(user)
1182 {:ok, _} = ObanHelpers.perform(job)
1184 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1186 assert cached_user != user
1188 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1190 assert cached_user != user
1194 test "auth_active?/1 works correctly" do
1195 Pleroma.Config.put([:instance, :account_activation_required], true)
1197 local_user = insert(:user, local: true, confirmation_pending: true)
1198 confirmed_user = insert(:user, local: true, confirmation_pending: false)
1199 remote_user = insert(:user, local: false)
1201 refute User.auth_active?(local_user)
1202 assert User.auth_active?(confirmed_user)
1203 assert User.auth_active?(remote_user)
1206 describe "superuser?/1" do
1207 test "returns false for unprivileged users" do
1208 user = insert(:user, local: true)
1210 refute User.superuser?(user)
1213 test "returns false for remote users" do
1214 user = insert(:user, local: false)
1215 remote_admin_user = insert(:user, local: false, is_admin: true)
1217 refute User.superuser?(user)
1218 refute User.superuser?(remote_admin_user)
1221 test "returns true for local moderators" do
1222 user = insert(:user, local: true, is_moderator: true)
1224 assert User.superuser?(user)
1227 test "returns true for local admins" do
1228 user = insert(:user, local: true, is_admin: true)
1230 assert User.superuser?(user)
1234 describe "invisible?/1" do
1235 test "returns true for an invisible user" do
1236 user = insert(:user, local: true, info: %{invisible: true})
1238 assert User.invisible?(user)
1241 test "returns false for a non-invisible user" do
1242 user = insert(:user, local: true)
1244 refute User.invisible?(user)
1248 describe "visible_for?/2" do
1249 test "returns true when the account is itself" do
1250 user = insert(:user, local: true)
1252 assert User.visible_for?(user, user)
1255 test "returns false when the account is unauthenticated and auth is required" do
1256 Pleroma.Config.put([:instance, :account_activation_required], true)
1258 user = insert(:user, local: true, confirmation_pending: true)
1259 other_user = insert(:user, local: true)
1261 refute User.visible_for?(user, other_user)
1264 test "returns true when the account is unauthenticated and auth is not required" do
1265 user = insert(:user, local: true, confirmation_pending: true)
1266 other_user = insert(:user, local: true)
1268 assert User.visible_for?(user, other_user)
1271 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1272 Pleroma.Config.put([:instance, :account_activation_required], true)
1274 user = insert(:user, local: true, confirmation_pending: true)
1275 other_user = insert(:user, local: true, is_admin: true)
1277 assert User.visible_for?(user, other_user)
1281 describe "parse_bio/2" do
1282 test "preserves hosts in user links text" do
1283 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1284 user = insert(:user)
1285 bio = "A.k.a. @nick@domain.com"
1288 ~s(A.k.a. <span class="h-card"><a data-user="#{remote_user.id}" class="u-url mention" href="#{
1290 }" rel="ugc">@<span>nick@domain.com</span></a></span>)
1292 assert expected_text == User.parse_bio(bio, user)
1295 test "Adds rel=me on linkbacked urls" do
1296 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1298 bio = "http://example.com/rel_me/null"
1299 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1300 assert expected_text == User.parse_bio(bio, user)
1302 bio = "http://example.com/rel_me/link"
1303 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1304 assert expected_text == User.parse_bio(bio, user)
1306 bio = "http://example.com/rel_me/anchor"
1307 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1308 assert expected_text == User.parse_bio(bio, user)
1312 test "follower count is updated when a follower is blocked" do
1313 user = insert(:user)
1314 follower = insert(:user)
1315 follower2 = insert(:user)
1316 follower3 = insert(:user)
1318 {:ok, follower} = User.follow(follower, user)
1319 {:ok, _follower2} = User.follow(follower2, user)
1320 {:ok, _follower3} = User.follow(follower3, user)
1322 {:ok, user} = User.block(user, follower)
1324 assert User.user_info(user).follower_count == 2
1327 describe "list_inactive_users_query/1" do
1328 defp days_ago(days) do
1330 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1331 -days * 60 * 60 * 24,
1336 test "Users are inactive by default" do
1340 Enum.map(1..total, fn _ ->
1341 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1344 inactive_users_ids =
1345 Pleroma.User.list_inactive_users_query()
1346 |> Pleroma.Repo.all()
1347 |> Enum.map(& &1.id)
1349 Enum.each(users, fn user ->
1350 assert user.id in inactive_users_ids
1354 test "Only includes users who has no recent activity" do
1358 Enum.map(1..total, fn _ ->
1359 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1362 {inactive, active} = Enum.split(users, trunc(total / 2))
1364 Enum.map(active, fn user ->
1365 to = Enum.random(users -- [user])
1368 CommonAPI.post(user, %{
1369 "status" => "hey @#{to.nickname}"
1373 inactive_users_ids =
1374 Pleroma.User.list_inactive_users_query()
1375 |> Pleroma.Repo.all()
1376 |> Enum.map(& &1.id)
1378 Enum.each(active, fn user ->
1379 refute user.id in inactive_users_ids
1382 Enum.each(inactive, fn user ->
1383 assert user.id in inactive_users_ids
1387 test "Only includes users with no read notifications" do
1391 Enum.map(1..total, fn _ ->
1392 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1395 [sender | recipients] = users
1396 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1398 Enum.each(recipients, fn to ->
1400 CommonAPI.post(sender, %{
1401 "status" => "hey @#{to.nickname}"
1405 CommonAPI.post(sender, %{
1406 "status" => "hey again @#{to.nickname}"
1410 Enum.each(active, fn user ->
1411 [n1, _n2] = Pleroma.Notification.for_user(user)
1412 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1415 inactive_users_ids =
1416 Pleroma.User.list_inactive_users_query()
1417 |> Pleroma.Repo.all()
1418 |> Enum.map(& &1.id)
1420 Enum.each(active, fn user ->
1421 refute user.id in inactive_users_ids
1424 Enum.each(inactive, fn user ->
1425 assert user.id in inactive_users_ids
1430 describe "toggle_confirmation/1" do
1431 test "if user is confirmed" do
1432 user = insert(:user, confirmation_pending: false)
1433 {:ok, user} = User.toggle_confirmation(user)
1435 assert user.confirmation_pending
1436 assert user.confirmation_token
1439 test "if user is unconfirmed" do
1440 user = insert(:user, confirmation_pending: true, confirmation_token: "some token")
1441 {:ok, user} = User.toggle_confirmation(user)
1443 refute user.confirmation_pending
1444 refute user.confirmation_token
1448 describe "ensure_keys_present" do
1449 test "it creates keys for a user and stores them in info" do
1450 user = insert(:user)
1451 refute is_binary(user.keys)
1452 {:ok, user} = User.ensure_keys_present(user)
1453 assert is_binary(user.keys)
1456 test "it doesn't create keys if there already are some" do
1457 user = insert(:user, keys: "xxx")
1458 {:ok, user} = User.ensure_keys_present(user)
1459 assert user.keys == "xxx"
1463 describe "get_ap_ids_by_nicknames" do
1464 test "it returns a list of AP ids for a given set of nicknames" do
1465 user = insert(:user)
1466 user_two = insert(:user)
1468 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1469 assert length(ap_ids) == 2
1470 assert user.ap_id in ap_ids
1471 assert user_two.ap_id in ap_ids
1475 describe "sync followers count" do
1477 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1478 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1479 insert(:user, local: true)
1480 insert(:user, local: false, deactivated: true)
1481 {:ok, user1: user1, user2: user2}
1484 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1485 [fdb_user1] = User.external_users(limit: 1)
1487 assert fdb_user1.ap_id
1488 assert fdb_user1.ap_id == user1.ap_id
1489 assert fdb_user1.id == user1.id
1491 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1493 assert fdb_user2.ap_id
1494 assert fdb_user2.ap_id == user2.ap_id
1495 assert fdb_user2.id == user2.id
1497 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1501 describe "set_info_cache/2" do
1503 user = insert(:user)
1507 test "update from args", %{user: user} do
1508 User.set_info_cache(user, %{following_count: 15, follower_count: 18})
1510 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1511 assert followers == 18
1512 assert following == 15
1515 test "without args", %{user: user} do
1516 User.set_info_cache(user, %{})
1518 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1519 assert followers == 0
1520 assert following == 0
1524 describe "user_info/2" do
1526 user = insert(:user)
1530 test "update from args", %{user: user} do
1531 %{follower_count: followers, following_count: following} =
1532 User.user_info(user, %{following_count: 15, follower_count: 18})
1534 assert followers == 18
1535 assert following == 15
1538 test "without args", %{user: user} do
1539 %{follower_count: followers, following_count: following} = User.user_info(user)
1541 assert followers == 0
1542 assert following == 0
1546 describe "is_internal_user?/1" do
1547 test "non-internal user returns false" do
1548 user = insert(:user)
1549 refute User.is_internal_user?(user)
1552 test "user with no nickname returns true" do
1553 user = insert(:user, %{nickname: nil})
1554 assert User.is_internal_user?(user)
1557 test "user with internal-prefixed nickname returns true" do
1558 user = insert(:user, %{nickname: "internal.test"})
1559 assert User.is_internal_user?(user)
1563 describe "update_and_set_cache/1" do
1564 test "returns error when user is stale instead Ecto.StaleEntryError" do
1565 user = insert(:user)
1567 changeset = Ecto.Changeset.change(user, bio: "test")
1571 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1572 User.update_and_set_cache(changeset)
1575 test "performs update cache if user updated" do
1576 user = insert(:user)
1577 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1579 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1581 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1582 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1583 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1587 describe "following/followers synchronization" do
1588 clear_config([:instance, :external_user_synchronization])
1590 test "updates the counters normally on following/getting a follow when disabled" do
1591 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1592 user = insert(:user)
1597 follower_address: "http://localhost:4001/users/masto_closed/followers",
1598 following_address: "http://localhost:4001/users/masto_closed/following",
1602 assert User.user_info(other_user).following_count == 0
1603 assert User.user_info(other_user).follower_count == 0
1605 {:ok, user} = Pleroma.User.follow(user, other_user)
1606 other_user = Pleroma.User.get_by_id(other_user.id)
1608 assert User.user_info(user).following_count == 1
1609 assert User.user_info(other_user).follower_count == 1
1612 test "syncronizes the counters with the remote instance for the followed when enabled" do
1613 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1615 user = insert(:user)
1620 follower_address: "http://localhost:4001/users/masto_closed/followers",
1621 following_address: "http://localhost:4001/users/masto_closed/following",
1625 assert User.user_info(other_user).following_count == 0
1626 assert User.user_info(other_user).follower_count == 0
1628 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1629 {:ok, _user} = User.follow(user, other_user)
1630 other_user = User.get_by_id(other_user.id)
1632 assert User.user_info(other_user).follower_count == 437
1635 test "syncronizes the counters with the remote instance for the follower when enabled" do
1636 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1638 user = insert(:user)
1643 follower_address: "http://localhost:4001/users/masto_closed/followers",
1644 following_address: "http://localhost:4001/users/masto_closed/following",
1648 assert User.user_info(other_user).following_count == 0
1649 assert User.user_info(other_user).follower_count == 0
1651 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1652 {:ok, other_user} = User.follow(other_user, user)
1654 assert User.user_info(other_user).following_count == 152
1658 describe "change_email/2" do
1660 [user: insert(:user)]
1663 test "blank email returns error", %{user: user} do
1664 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1665 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1668 test "non unique email returns error", %{user: user} do
1669 %{email: email} = insert(:user)
1671 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1672 User.change_email(user, email)
1675 test "invalid email returns error", %{user: user} do
1676 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1677 User.change_email(user, "cofe")
1680 test "changes email", %{user: user} do
1681 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1685 describe "get_cached_by_nickname_or_id" do
1687 limit_to_local_content = Pleroma.Config.get([:instance, :limit_to_local_content])
1688 local_user = insert(:user)
1689 remote_user = insert(:user, nickname: "nickname@example.com", local: false)
1692 Pleroma.Config.put([:instance, :limit_to_local_content], limit_to_local_content)
1695 [local_user: local_user, remote_user: remote_user]
1698 test "allows getting remote users by id no matter what :limit_to_local_content is set to", %{
1699 remote_user: remote_user
1701 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1702 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1704 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1705 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1707 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1708 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1711 test "disallows getting remote users by nickname without authentication when :limit_to_local_content is set to :unauthenticated",
1712 %{remote_user: remote_user} do
1713 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1714 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1717 test "allows getting remote users by nickname with authentication when :limit_to_local_content is set to :unauthenticated",
1718 %{remote_user: remote_user, local_user: local_user} do
1719 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1720 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.nickname, for: local_user)
1723 test "disallows getting remote users by nickname when :limit_to_local_content is set to true",
1724 %{remote_user: remote_user} do
1725 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1726 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1729 test "allows getting local users by nickname no matter what :limit_to_local_content is set to",
1730 %{local_user: local_user} do
1731 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1732 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1734 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1735 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1737 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1738 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1742 describe "update_email_notifications/2" do
1744 user = insert(:user, email_notifications: %{"digest" => true})
1749 test "Notifications are updated", %{user: user} do
1750 true = user.email_notifications["digest"]
1751 assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false})
1752 assert result.email_notifications["digest"] == false