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, %{info: %{locked: true}})
72 follower = insert(:user)
74 CommonAPI.follow(follower, unlocked)
75 CommonAPI.follow(follower, locked)
77 assert {:ok, []} = User.get_follow_requests(unlocked)
78 assert {:ok, [activity]} = User.get_follow_requests(locked)
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)
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 {:ok, [activity]} = User.get_follow_requests(locked)
97 test "clears follow requests when requester is blocked" do
98 followed = insert(:user, %{info: %{locked: true}})
99 follower = insert(:user)
101 CommonAPI.follow(follower, followed)
102 assert {:ok, [_activity]} = User.get_follow_requests(followed)
104 {:ok, _follower} = User.block(followed, follower)
105 assert {:ok, []} = 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.info.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, info: %{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, info: %{locked: true})
186 followed = insert(:user, info: %{locked: true})
188 {:ok, follower} = User.maybe_direct_follow(follower, followed)
190 refute User.following?(follower, followed)
193 # This is a somewhat useless test.
194 # test "following a remote user will ensure a websub subscription is present" do
195 # user = insert(:user)
196 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
198 # assert followed.local == false
200 # {:ok, user} = User.follow(user, followed)
201 # assert User.ap_followers(followed) in user.following
203 # query = from w in WebsubClientSubscription,
204 # where: w.topic == ^followed.info["topic"]
205 # websub = Repo.one(query)
210 describe "unfollow/2" do
212 setting = Pleroma.Config.get([:instance, :external_user_synchronization])
215 Pleroma.Config.put([:instance, :external_user_synchronization], setting)
221 test "unfollow with syncronizes external user" do
222 Pleroma.Config.put([:instance, :external_user_synchronization], true)
227 follower_address: "http://localhost:4001/users/fuser1/followers",
228 following_address: "http://localhost:4001/users/fuser1/following",
229 ap_id: "http://localhost:4001/users/fuser1"
236 ap_id: "http://localhost:4001/users/fuser2",
237 follower_address: "http://localhost:4001/users/fuser2/followers",
238 following_address: "http://localhost:4001/users/fuser2/following",
239 following: [User.ap_followers(followed)]
242 {:ok, user, _activity} = User.unfollow(user, followed)
244 user = User.get_cached_by_id(user.id)
246 assert user.following == []
249 test "unfollow takes a user and another user" do
250 followed = insert(:user)
251 user = insert(:user, %{following: [User.ap_followers(followed)]})
253 {:ok, user, _activity} = User.unfollow(user, followed)
255 user = User.get_cached_by_id(user.id)
257 assert user.following == []
260 test "unfollow doesn't unfollow yourself" do
263 {:error, _} = User.unfollow(user, user)
265 user = User.get_cached_by_id(user.id)
266 assert user.following == [user.ap_id]
270 test "test if a user is following another user" do
271 followed = insert(:user)
272 user = insert(:user, %{following: [User.ap_followers(followed)]})
274 assert User.following?(user, followed)
275 refute User.following?(followed, user)
278 test "fetches correct profile for nickname beginning with number" do
279 # Use old-style integer ID to try to reproduce the problem
280 user = insert(:user, %{id: 1080})
281 user_with_numbers = insert(:user, %{nickname: "#{user.id}garbage"})
282 assert user_with_numbers == User.get_cached_by_nickname_or_id(user_with_numbers.nickname)
285 describe "user registration" do
291 password_confirmation: "test",
292 email: "email@example.com"
294 clear_config([:instance, :autofollowed_nicknames])
295 clear_config([:instance, :welcome_message])
296 clear_config([:instance, :welcome_user_nickname])
298 test "it autofollows accounts that are set for it" do
300 remote_user = insert(:user, %{local: false})
302 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
307 cng = User.register_changeset(%User{}, @full_user_data)
309 {:ok, registered_user} = User.register(cng)
311 assert User.following?(registered_user, user)
312 refute User.following?(registered_user, remote_user)
315 test "it sends a welcome message if it is set" do
316 welcome_user = insert(:user)
318 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
319 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
321 cng = User.register_changeset(%User{}, @full_user_data)
322 {:ok, registered_user} = User.register(cng)
324 activity = Repo.one(Pleroma.Activity)
325 assert registered_user.ap_id in activity.recipients
326 assert Object.normalize(activity).data["content"] =~ "cool site"
327 assert activity.actor == welcome_user.ap_id
330 test "it requires an email, name, nickname and password, bio is optional" do
333 |> Enum.each(fn key ->
334 params = Map.delete(@full_user_data, key)
335 changeset = User.register_changeset(%User{}, params)
337 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
341 test "it restricts certain nicknames" do
342 [restricted_name | _] = Pleroma.Config.get([User, :restricted_nicknames])
344 assert is_bitstring(restricted_name)
348 |> Map.put(:nickname, restricted_name)
350 changeset = User.register_changeset(%User{}, params)
352 refute changeset.valid?
355 test "it sets the password_hash, ap_id and following fields" do
356 changeset = User.register_changeset(%User{}, @full_user_data)
358 assert changeset.valid?
360 assert is_binary(changeset.changes[:password_hash])
361 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
363 assert changeset.changes[:following] == [
364 User.ap_followers(%User{nickname: @full_user_data.nickname})
367 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
370 test "it ensures info is not nil" do
371 changeset = User.register_changeset(%User{}, @full_user_data)
373 assert changeset.valid?
379 refute is_nil(user.info)
383 describe "user registration, with :account_activation_required" do
389 password_confirmation: "test",
390 email: "email@example.com"
393 clear_config([:instance, :account_activation_required]) do
394 Pleroma.Config.put([:instance, :account_activation_required], true)
397 test "it creates unconfirmed user" do
398 changeset = User.register_changeset(%User{}, @full_user_data)
399 assert changeset.valid?
401 {:ok, user} = Repo.insert(changeset)
403 assert user.info.confirmation_pending
404 assert user.info.confirmation_token
407 test "it creates confirmed user if :confirmed option is given" do
408 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
409 assert changeset.valid?
411 {:ok, user} = Repo.insert(changeset)
413 refute user.info.confirmation_pending
414 refute user.info.confirmation_token
418 describe "get_or_fetch/1" do
419 test "gets an existing user by nickname" do
421 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
423 assert user == fetched_user
426 test "gets an existing user by ap_id" do
427 ap_id = "http://mastodon.example.org/users/admin"
433 nickname: "admin@mastodon.example.org",
438 {:ok, fetched_user} = User.get_or_fetch(ap_id)
439 freshed_user = refresh_record(user)
440 assert freshed_user == fetched_user
444 describe "fetching a user from nickname or trying to build one" do
445 test "gets an existing user" do
447 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
449 assert user == fetched_user
452 test "gets an existing user, case insensitive" do
453 user = insert(:user, nickname: "nick")
454 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
456 assert user == fetched_user
459 test "gets an existing user by fully qualified nickname" do
462 {:ok, fetched_user} =
463 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
465 assert user == fetched_user
468 test "gets an existing user by fully qualified nickname, case insensitive" do
469 user = insert(:user, nickname: "nick")
470 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
472 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
474 assert user == fetched_user
477 test "fetches an external user via ostatus if no user exists" do
478 {:ok, fetched_user} = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
479 assert fetched_user.nickname == "shp@social.heldscal.la"
482 test "returns nil if no user could be fetched" do
483 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
484 assert fetched_user == "not found nonexistant@social.heldscal.la"
487 test "returns nil for nonexistant local user" do
488 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
489 assert fetched_user == "not found nonexistant"
492 test "updates an existing user, if stale" do
493 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
499 nickname: "admin@mastodon.example.org",
500 ap_id: "http://mastodon.example.org/users/admin",
501 last_refreshed_at: a_week_ago,
505 assert orig_user.last_refreshed_at == a_week_ago
507 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
508 assert user.info.source_data["endpoints"]
510 refute user.last_refreshed_at == orig_user.last_refreshed_at
514 test "returns an ap_id for a user" do
517 assert User.ap_id(user) ==
518 Pleroma.Web.Router.Helpers.o_status_url(
519 Pleroma.Web.Endpoint,
525 test "returns an ap_followers link for a user" do
528 assert User.ap_followers(user) ==
529 Pleroma.Web.Router.Helpers.o_status_url(
530 Pleroma.Web.Endpoint,
536 describe "remote user creation changeset" do
542 info: %{some: "info"},
543 avatar: %{some: "avatar"}
546 clear_config([:instance, :user_bio_length])
547 clear_config([:instance, :user_name_length])
549 test "it confirms validity" do
550 cs = User.remote_user_creation(@valid_remote)
554 test "it sets the follower_adress" do
555 cs = User.remote_user_creation(@valid_remote)
556 # remote users get a fake local follower address
557 assert cs.changes.follower_address ==
558 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
561 test "it enforces the fqn format for nicknames" do
562 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
563 assert cs.changes.local == false
564 assert cs.changes.avatar
568 test "it has required fields" do
570 |> Enum.each(fn field ->
571 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
577 describe "followers and friends" do
578 test "gets all followers for a given user" do
580 follower_one = insert(:user)
581 follower_two = insert(:user)
582 not_follower = insert(:user)
584 {:ok, follower_one} = User.follow(follower_one, user)
585 {:ok, follower_two} = User.follow(follower_two, user)
587 {:ok, res} = User.get_followers(user)
589 assert Enum.member?(res, follower_one)
590 assert Enum.member?(res, follower_two)
591 refute Enum.member?(res, not_follower)
594 test "gets all friends (followed users) for a given user" do
596 followed_one = insert(:user)
597 followed_two = insert(:user)
598 not_followed = insert(:user)
600 {:ok, user} = User.follow(user, followed_one)
601 {:ok, user} = User.follow(user, followed_two)
603 {:ok, res} = User.get_friends(user)
605 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
606 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
607 assert Enum.member?(res, followed_one)
608 assert Enum.member?(res, followed_two)
609 refute Enum.member?(res, not_followed)
613 describe "updating note and follower count" do
614 test "it sets the info->note_count property" do
617 user = User.get_cached_by_ap_id(note.data["actor"])
619 assert user.info.note_count == 0
621 {:ok, user} = User.update_note_count(user)
623 assert user.info.note_count == 1
626 test "it increases the info->note_count property" do
628 user = User.get_cached_by_ap_id(note.data["actor"])
630 assert user.info.note_count == 0
632 {:ok, user} = User.increase_note_count(user)
634 assert user.info.note_count == 1
636 {:ok, user} = User.increase_note_count(user)
638 assert user.info.note_count == 2
641 test "it decreases the info->note_count property" do
643 user = User.get_cached_by_ap_id(note.data["actor"])
645 assert user.info.note_count == 0
647 {:ok, user} = User.increase_note_count(user)
649 assert user.info.note_count == 1
651 {:ok, user} = User.decrease_note_count(user)
653 assert user.info.note_count == 0
655 {:ok, user} = User.decrease_note_count(user)
657 assert user.info.note_count == 0
660 test "it sets the info->follower_count property" do
662 follower = insert(:user)
664 User.follow(follower, user)
666 assert user.info.follower_count == 0
668 {:ok, user} = User.update_follower_count(user)
670 assert user.info.follower_count == 1
674 describe "remove duplicates from following list" do
675 test "it removes duplicates" do
677 follower = insert(:user)
679 {:ok, %User{following: following} = follower} = User.follow(follower, user)
680 assert length(following) == 2
684 |> User.update_changeset(%{following: following ++ following})
687 assert length(follower.following) == 4
689 {:ok, follower} = User.remove_duplicated_following(follower)
690 assert length(follower.following) == 2
693 test "it does nothing when following is uniq" do
695 follower = insert(:user)
697 {:ok, follower} = User.follow(follower, user)
698 assert length(follower.following) == 2
700 {:ok, follower} = User.remove_duplicated_following(follower)
701 assert length(follower.following) == 2
705 describe "follow_import" do
706 test "it imports user followings from list" do
707 [user1, user2, user3] = insert_list(3, :user)
714 {:ok, job} = User.follow_import(user1, identifiers)
715 result = ObanHelpers.perform(job)
717 assert is_list(result)
718 assert result == [user2, user3]
723 test "it mutes people" do
725 muted_user = insert(:user)
727 refute User.mutes?(user, muted_user)
728 refute User.muted_notifications?(user, muted_user)
730 {:ok, user} = User.mute(user, muted_user)
732 assert User.mutes?(user, muted_user)
733 assert User.muted_notifications?(user, muted_user)
736 test "it unmutes users" do
738 muted_user = insert(:user)
740 {:ok, user} = User.mute(user, muted_user)
741 {:ok, user} = User.unmute(user, muted_user)
743 refute User.mutes?(user, muted_user)
744 refute User.muted_notifications?(user, muted_user)
747 test "it mutes user without notifications" do
749 muted_user = insert(:user)
751 refute User.mutes?(user, muted_user)
752 refute User.muted_notifications?(user, muted_user)
754 {:ok, user} = User.mute(user, muted_user, false)
756 assert User.mutes?(user, muted_user)
757 refute User.muted_notifications?(user, muted_user)
762 test "it blocks people" do
764 blocked_user = insert(:user)
766 refute User.blocks?(user, blocked_user)
768 {:ok, user} = User.block(user, blocked_user)
770 assert User.blocks?(user, blocked_user)
773 test "it unblocks users" do
775 blocked_user = insert(:user)
777 {:ok, user} = User.block(user, blocked_user)
778 {:ok, user} = User.unblock(user, blocked_user)
780 refute User.blocks?(user, blocked_user)
783 test "blocks tear down cyclical follow relationships" do
784 blocker = insert(:user)
785 blocked = insert(:user)
787 {:ok, blocker} = User.follow(blocker, blocked)
788 {:ok, blocked} = User.follow(blocked, blocker)
790 assert User.following?(blocker, blocked)
791 assert User.following?(blocked, blocker)
793 {:ok, blocker} = User.block(blocker, blocked)
794 blocked = User.get_cached_by_id(blocked.id)
796 assert User.blocks?(blocker, blocked)
798 refute User.following?(blocker, blocked)
799 refute User.following?(blocked, blocker)
802 test "blocks tear down blocker->blocked follow relationships" do
803 blocker = insert(:user)
804 blocked = insert(:user)
806 {:ok, blocker} = User.follow(blocker, blocked)
808 assert User.following?(blocker, blocked)
809 refute User.following?(blocked, blocker)
811 {:ok, blocker} = User.block(blocker, blocked)
812 blocked = User.get_cached_by_id(blocked.id)
814 assert User.blocks?(blocker, blocked)
816 refute User.following?(blocker, blocked)
817 refute User.following?(blocked, blocker)
820 test "blocks tear down blocked->blocker follow relationships" do
821 blocker = insert(:user)
822 blocked = insert(:user)
824 {:ok, blocked} = User.follow(blocked, blocker)
826 refute User.following?(blocker, blocked)
827 assert User.following?(blocked, blocker)
829 {:ok, blocker} = User.block(blocker, blocked)
830 blocked = User.get_cached_by_id(blocked.id)
832 assert User.blocks?(blocker, blocked)
834 refute User.following?(blocker, blocked)
835 refute User.following?(blocked, blocker)
838 test "blocks tear down blocked->blocker subscription relationships" do
839 blocker = insert(:user)
840 blocked = insert(:user)
842 {:ok, blocker} = User.subscribe(blocked, blocker)
844 assert User.subscribed_to?(blocked, blocker)
845 refute User.subscribed_to?(blocker, blocked)
847 {:ok, blocker} = User.block(blocker, blocked)
849 assert User.blocks?(blocker, blocked)
850 refute User.subscribed_to?(blocker, blocked)
851 refute User.subscribed_to?(blocked, blocker)
855 describe "domain blocking" do
856 test "blocks domains" do
858 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
860 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
862 assert User.blocks?(user, collateral_user)
865 test "does not block domain with same end" do
869 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
871 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
873 refute User.blocks?(user, collateral_user)
876 test "does not block domain with same end if wildcard added" do
880 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
882 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
884 refute User.blocks?(user, collateral_user)
887 test "blocks domain with wildcard for subdomain" do
890 user_from_subdomain =
891 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
893 user_with_two_subdomains =
895 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
898 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
900 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
902 assert User.blocks?(user, user_from_subdomain)
903 assert User.blocks?(user, user_with_two_subdomains)
904 assert User.blocks?(user, user_domain)
907 test "unblocks domains" do
909 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
911 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
912 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
914 refute User.blocks?(user, collateral_user)
918 describe "blocks_import" do
919 test "it imports user blocks from list" do
920 [user1, user2, user3] = insert_list(3, :user)
927 {:ok, job} = User.blocks_import(user1, identifiers)
928 result = ObanHelpers.perform(job)
930 assert is_list(result)
931 assert result == [user2, user3]
935 test "get recipients from activity" do
936 actor = insert(:user)
937 user = insert(:user, local: true)
938 user_two = insert(:user, local: false)
939 addressed = insert(:user, local: true)
940 addressed_remote = insert(:user, local: false)
943 CommonAPI.post(actor, %{
944 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
947 assert Enum.map([actor, addressed], & &1.ap_id) --
948 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
950 {:ok, user} = User.follow(user, actor)
951 {:ok, _user_two} = User.follow(user_two, actor)
952 recipients = User.get_recipients_from_activity(activity)
953 assert length(recipients) == 3
954 assert user in recipients
955 assert addressed in recipients
958 describe ".deactivate" do
959 test "can de-activate then re-activate a user" do
961 assert false == user.info.deactivated
962 {:ok, user} = User.deactivate(user)
963 assert true == user.info.deactivated
964 {:ok, user} = User.deactivate(user, false)
965 assert false == user.info.deactivated
968 test "hide a user from followers " do
970 user2 = insert(:user)
972 {:ok, user} = User.follow(user, user2)
973 {:ok, _user} = User.deactivate(user)
975 info = User.get_cached_user_info(user2)
977 assert info.follower_count == 0
978 assert {:ok, []} = User.get_followers(user2)
981 test "hide a user from friends" do
983 user2 = insert(:user)
985 {:ok, user2} = User.follow(user2, user)
986 assert User.following_count(user2) == 1
988 {:ok, _user} = User.deactivate(user)
990 info = User.get_cached_user_info(user2)
992 assert info.following_count == 0
993 assert User.following_count(user2) == 0
994 assert {:ok, []} = User.get_friends(user2)
997 test "hide a user's statuses from timelines and notifications" do
999 user2 = insert(:user)
1001 {:ok, user2} = User.follow(user2, user)
1003 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
1005 activity = Repo.preload(activity, :bookmark)
1007 [notification] = Pleroma.Notification.for_user(user2)
1008 assert notification.activity.id == activity.id
1010 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
1012 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
1013 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
1015 {:ok, _user} = User.deactivate(user)
1017 assert [] == ActivityPub.fetch_public_activities(%{})
1018 assert [] == Pleroma.Notification.for_user(user2)
1021 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
1025 describe "delete" do
1027 {:ok, user} = insert(:user) |> User.set_cache()
1032 clear_config([:instance, :federating])
1034 test ".delete_user_activities deletes all create activities", %{user: user} do
1035 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
1037 {:ok, _} = User.delete_user_activities(user)
1039 # TODO: Remove favorites, repeats, delete activities.
1040 refute Activity.get_by_id(activity.id)
1043 test "it deletes deactivated user" do
1044 {:ok, user} = insert(:user, info: %{deactivated: true}) |> User.set_cache()
1046 {:ok, job} = User.delete(user)
1047 {:ok, _user} = ObanHelpers.perform(job)
1049 refute User.get_by_id(user.id)
1052 test "it deletes a user, all follow relationships and all activities", %{user: user} do
1053 follower = insert(:user)
1054 {:ok, follower} = User.follow(follower, user)
1056 object = insert(:note, user: user)
1057 activity = insert(:note_activity, user: user, note: object)
1059 object_two = insert(:note, user: follower)
1060 activity_two = insert(:note_activity, user: follower, note: object_two)
1062 {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
1063 {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
1064 {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
1066 {:ok, job} = User.delete(user)
1067 {:ok, _user} = ObanHelpers.perform(job)
1069 follower = User.get_cached_by_id(follower.id)
1071 refute User.following?(follower, user)
1072 refute User.get_by_id(user.id)
1073 assert {:ok, nil} == Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1077 |> Activity.Queries.by_actor()
1079 |> Enum.map(fn act -> act.data["type"] end)
1081 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1083 refute Activity.get_by_id(activity.id)
1084 refute Activity.get_by_id(like.id)
1085 refute Activity.get_by_id(like_two.id)
1086 refute Activity.get_by_id(repeat.id)
1089 test_with_mock "it sends out User Delete activity",
1091 Pleroma.Web.ActivityPub.Publisher,
1094 Pleroma.Config.put([:instance, :federating], true)
1096 {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
1097 {:ok, _} = User.follow(follower, user)
1099 {:ok, job} = User.delete(user)
1100 {:ok, _user} = ObanHelpers.perform(job)
1102 assert ObanHelpers.member?(
1104 "op" => "publish_one",
1106 "inbox" => "http://mastodon.example.org/inbox",
1107 "id" => "pleroma:fakeid"
1110 all_enqueued(worker: Pleroma.Workers.PublisherWorker)
1115 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1116 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1119 describe "insert or update a user from given data" do
1120 test "with normal data" do
1121 user = insert(:user, %{nickname: "nick@name.de"})
1122 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
1124 assert {:ok, %User{}} = User.insert_or_update_user(data)
1127 test "with overly long fields" do
1128 current_max_length = Pleroma.Config.get([:instance, :account_field_value_length], 255)
1129 user = insert(:user, nickname: "nickname@supergood.domain")
1134 nickname: user.nickname,
1137 %{"name" => "myfield", "value" => String.duplicate("h", current_max_length + 1)}
1142 assert {:ok, %User{}} = User.insert_or_update_user(data)
1145 test "with an overly long bio" do
1146 current_max_length = Pleroma.Config.get([:instance, :user_bio_length], 5000)
1147 user = insert(:user, nickname: "nickname@supergood.domain")
1152 nickname: user.nickname,
1153 bio: String.duplicate("h", current_max_length + 1),
1157 assert {:ok, %User{}} = User.insert_or_update_user(data)
1160 test "with an overly long display name" do
1161 current_max_length = Pleroma.Config.get([:instance, :user_name_length], 100)
1162 user = insert(:user, nickname: "nickname@supergood.domain")
1166 name: String.duplicate("h", current_max_length + 1),
1167 nickname: user.nickname,
1171 assert {:ok, %User{}} = User.insert_or_update_user(data)
1175 describe "per-user rich-text filtering" do
1176 test "html_filter_policy returns default policies, when rich-text is enabled" do
1177 user = insert(:user)
1179 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1182 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1183 user = insert(:user, %{info: %{no_rich_text: true}})
1185 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1189 describe "caching" do
1190 test "invalidate_cache works" do
1191 user = insert(:user)
1192 _user_info = User.get_cached_user_info(user)
1194 User.invalidate_cache(user)
1196 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1197 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1198 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
1201 test "User.delete() plugs any possible zombie objects" do
1202 user = insert(:user)
1204 {:ok, job} = User.delete(user)
1205 {:ok, _} = ObanHelpers.perform(job)
1207 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1209 assert cached_user != user
1211 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1213 assert cached_user != user
1217 test "auth_active?/1 works correctly" do
1218 Pleroma.Config.put([:instance, :account_activation_required], true)
1220 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
1221 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
1222 remote_user = insert(:user, local: false)
1224 refute User.auth_active?(local_user)
1225 assert User.auth_active?(confirmed_user)
1226 assert User.auth_active?(remote_user)
1229 describe "superuser?/1" do
1230 test "returns false for unprivileged users" do
1231 user = insert(:user, local: true)
1233 refute User.superuser?(user)
1236 test "returns false for remote users" do
1237 user = insert(:user, local: false)
1238 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1240 refute User.superuser?(user)
1241 refute User.superuser?(remote_admin_user)
1244 test "returns true for local moderators" do
1245 user = insert(:user, local: true, info: %{is_moderator: true})
1247 assert User.superuser?(user)
1250 test "returns true for local admins" do
1251 user = insert(:user, local: true, info: %{is_admin: true})
1253 assert User.superuser?(user)
1257 describe "visible_for?/2" do
1258 test "returns true when the account is itself" do
1259 user = insert(:user, local: true)
1261 assert User.visible_for?(user, user)
1264 test "returns false when the account is unauthenticated and auth is required" do
1265 Pleroma.Config.put([:instance, :account_activation_required], true)
1267 user = insert(:user, local: true, info: %{confirmation_pending: true})
1268 other_user = insert(:user, local: true)
1270 refute User.visible_for?(user, other_user)
1273 test "returns true when the account is unauthenticated and auth is not required" do
1274 user = insert(:user, local: true, info: %{confirmation_pending: true})
1275 other_user = insert(:user, local: true)
1277 assert User.visible_for?(user, other_user)
1280 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1281 Pleroma.Config.put([:instance, :account_activation_required], true)
1283 user = insert(:user, local: true, info: %{confirmation_pending: true})
1284 other_user = insert(:user, local: true, info: %{is_admin: true})
1286 assert User.visible_for?(user, other_user)
1290 describe "parse_bio/2" do
1291 test "preserves hosts in user links text" do
1292 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1293 user = insert(:user)
1294 bio = "A.k.a. @nick@domain.com"
1297 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1299 }'>@<span>nick@domain.com</span></a></span>"
1301 assert expected_text == User.parse_bio(bio, user)
1304 test "Adds rel=me on linkbacked urls" do
1305 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1307 bio = "http://example.com/rel_me/null"
1308 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1309 assert expected_text == User.parse_bio(bio, user)
1311 bio = "http://example.com/rel_me/link"
1312 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1313 assert expected_text == User.parse_bio(bio, user)
1315 bio = "http://example.com/rel_me/anchor"
1316 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1317 assert expected_text == User.parse_bio(bio, user)
1321 test "follower count is updated when a follower is blocked" do
1322 user = insert(:user)
1323 follower = insert(:user)
1324 follower2 = insert(:user)
1325 follower3 = insert(:user)
1327 {:ok, follower} = User.follow(follower, user)
1328 {:ok, _follower2} = User.follow(follower2, user)
1329 {:ok, _follower3} = User.follow(follower3, user)
1331 {:ok, user} = User.block(user, follower)
1333 assert User.user_info(user).follower_count == 2
1336 describe "list_inactive_users_query/1" do
1337 defp days_ago(days) do
1339 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1340 -days * 60 * 60 * 24,
1345 test "Users are inactive by default" do
1349 Enum.map(1..total, fn _ ->
1350 insert(:user, last_digest_emailed_at: days_ago(20), info: %{deactivated: false})
1353 inactive_users_ids =
1354 Pleroma.User.list_inactive_users_query()
1355 |> Pleroma.Repo.all()
1356 |> Enum.map(& &1.id)
1358 Enum.each(users, fn user ->
1359 assert user.id in inactive_users_ids
1363 test "Only includes users who has no recent activity" do
1367 Enum.map(1..total, fn _ ->
1368 insert(:user, last_digest_emailed_at: days_ago(20), info: %{deactivated: false})
1371 {inactive, active} = Enum.split(users, trunc(total / 2))
1373 Enum.map(active, fn user ->
1374 to = Enum.random(users -- [user])
1377 CommonAPI.post(user, %{
1378 "status" => "hey @#{to.nickname}"
1382 inactive_users_ids =
1383 Pleroma.User.list_inactive_users_query()
1384 |> Pleroma.Repo.all()
1385 |> Enum.map(& &1.id)
1387 Enum.each(active, fn user ->
1388 refute user.id in inactive_users_ids
1391 Enum.each(inactive, fn user ->
1392 assert user.id in inactive_users_ids
1396 test "Only includes users with no read notifications" do
1400 Enum.map(1..total, fn _ ->
1401 insert(:user, last_digest_emailed_at: days_ago(20), info: %{deactivated: false})
1404 [sender | recipients] = users
1405 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1407 Enum.each(recipients, fn to ->
1409 CommonAPI.post(sender, %{
1410 "status" => "hey @#{to.nickname}"
1414 CommonAPI.post(sender, %{
1415 "status" => "hey again @#{to.nickname}"
1419 Enum.each(active, fn user ->
1420 [n1, _n2] = Pleroma.Notification.for_user(user)
1421 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1424 inactive_users_ids =
1425 Pleroma.User.list_inactive_users_query()
1426 |> Pleroma.Repo.all()
1427 |> Enum.map(& &1.id)
1429 Enum.each(active, fn user ->
1430 refute user.id in inactive_users_ids
1433 Enum.each(inactive, fn user ->
1434 assert user.id in inactive_users_ids
1439 describe "toggle_confirmation/1" do
1440 test "if user is confirmed" do
1441 user = insert(:user, info: %{confirmation_pending: false})
1442 {:ok, user} = User.toggle_confirmation(user)
1444 assert user.info.confirmation_pending
1445 assert user.info.confirmation_token
1448 test "if user is unconfirmed" do
1449 user = insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
1450 {:ok, user} = User.toggle_confirmation(user)
1452 refute user.info.confirmation_pending
1453 refute user.info.confirmation_token
1457 describe "ensure_keys_present" do
1458 test "it creates keys for a user and stores them in info" do
1459 user = insert(:user)
1460 refute is_binary(user.info.keys)
1461 {:ok, user} = User.ensure_keys_present(user)
1462 assert is_binary(user.info.keys)
1465 test "it doesn't create keys if there already are some" do
1466 user = insert(:user, %{info: %{keys: "xxx"}})
1467 {:ok, user} = User.ensure_keys_present(user)
1468 assert user.info.keys == "xxx"
1472 describe "get_ap_ids_by_nicknames" do
1473 test "it returns a list of AP ids for a given set of nicknames" do
1474 user = insert(:user)
1475 user_two = insert(:user)
1477 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1478 assert length(ap_ids) == 2
1479 assert user.ap_id in ap_ids
1480 assert user_two.ap_id in ap_ids
1484 describe "sync followers count" do
1486 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1487 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1488 insert(:user, local: true)
1489 insert(:user, local: false, info: %{deactivated: true})
1490 {:ok, user1: user1, user2: user2}
1493 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1494 [fdb_user1] = User.external_users(limit: 1)
1496 assert fdb_user1.ap_id
1497 assert fdb_user1.ap_id == user1.ap_id
1498 assert fdb_user1.id == user1.id
1500 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1502 assert fdb_user2.ap_id
1503 assert fdb_user2.ap_id == user2.ap_id
1504 assert fdb_user2.id == user2.id
1506 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1510 describe "set_info_cache/2" do
1512 user = insert(:user)
1516 test "update from args", %{user: user} do
1517 User.set_info_cache(user, %{following_count: 15, follower_count: 18})
1519 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1520 assert followers == 18
1521 assert following == 15
1524 test "without args", %{user: user} do
1525 User.set_info_cache(user, %{})
1527 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1528 assert followers == 0
1529 assert following == 0
1533 describe "user_info/2" do
1535 user = insert(:user)
1539 test "update from args", %{user: user} do
1540 %{follower_count: followers, following_count: following} =
1541 User.user_info(user, %{following_count: 15, follower_count: 18})
1543 assert followers == 18
1544 assert following == 15
1547 test "without args", %{user: user} do
1548 %{follower_count: followers, following_count: following} = User.user_info(user)
1550 assert followers == 0
1551 assert following == 0
1555 describe "is_internal_user?/1" do
1556 test "non-internal user returns false" do
1557 user = insert(:user)
1558 refute User.is_internal_user?(user)
1561 test "user with no nickname returns true" do
1562 user = insert(:user, %{nickname: nil})
1563 assert User.is_internal_user?(user)
1566 test "user with internal-prefixed nickname returns true" do
1567 user = insert(:user, %{nickname: "internal.test"})
1568 assert User.is_internal_user?(user)
1572 describe "update_and_set_cache/1" do
1573 test "returns error when user is stale instead Ecto.StaleEntryError" do
1574 user = insert(:user)
1576 changeset = Ecto.Changeset.change(user, bio: "test")
1580 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1581 User.update_and_set_cache(changeset)
1584 test "performs update cache if user updated" do
1585 user = insert(:user)
1586 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1588 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1590 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1591 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1592 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1596 describe "following/followers synchronization" do
1597 clear_config([:instance, :external_user_synchronization])
1599 test "updates the counters normally on following/getting a follow when disabled" do
1600 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1601 user = insert(:user)
1606 follower_address: "http://localhost:4001/users/masto_closed/followers",
1607 following_address: "http://localhost:4001/users/masto_closed/following",
1608 info: %{ap_enabled: true}
1611 assert User.user_info(other_user).following_count == 0
1612 assert User.user_info(other_user).follower_count == 0
1614 {:ok, user} = Pleroma.User.follow(user, other_user)
1615 other_user = Pleroma.User.get_by_id(other_user.id)
1617 assert User.user_info(user).following_count == 1
1618 assert User.user_info(other_user).follower_count == 1
1621 test "syncronizes the counters with the remote instance for the followed when enabled" do
1622 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1624 user = insert(:user)
1629 follower_address: "http://localhost:4001/users/masto_closed/followers",
1630 following_address: "http://localhost:4001/users/masto_closed/following",
1631 info: %{ap_enabled: true}
1634 assert User.user_info(other_user).following_count == 0
1635 assert User.user_info(other_user).follower_count == 0
1637 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1638 {:ok, _user} = User.follow(user, other_user)
1639 other_user = User.get_by_id(other_user.id)
1641 assert User.user_info(other_user).follower_count == 437
1644 test "syncronizes the counters with the remote instance for the follower when enabled" do
1645 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1647 user = insert(:user)
1652 follower_address: "http://localhost:4001/users/masto_closed/followers",
1653 following_address: "http://localhost:4001/users/masto_closed/following",
1654 info: %{ap_enabled: true}
1657 assert User.user_info(other_user).following_count == 0
1658 assert User.user_info(other_user).follower_count == 0
1660 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1661 {:ok, other_user} = User.follow(other_user, user)
1663 assert User.user_info(other_user).following_count == 152
1667 describe "change_email/2" do
1669 [user: insert(:user)]
1672 test "blank email returns error", %{user: user} do
1673 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1674 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1677 test "non unique email returns error", %{user: user} do
1678 %{email: email} = insert(:user)
1680 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1681 User.change_email(user, email)
1684 test "invalid email returns error", %{user: user} do
1685 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1686 User.change_email(user, "cofe")
1689 test "changes email", %{user: user} do
1690 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1694 describe "set_password_reset_pending/2" do
1696 [user: insert(:user)]
1699 test "sets password_reset_pending to true", %{user: user} do
1700 %{password_reset_pending: password_reset_pending} = user.info
1702 refute password_reset_pending
1704 {:ok, %{info: %{password_reset_pending: password_reset_pending}}} =
1705 User.force_password_reset(user)
1707 assert password_reset_pending