1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UserTest do
7 alias Pleroma.Builders.UserBuilder
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.CommonAPI
16 import Pleroma.Factory
20 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 describe "when tags are nil" do
25 test "tagging a user" do
26 user = insert(:user, %{tags: nil})
27 user = User.tag(user, ["cool", "dude"])
29 assert "cool" in user.tags
30 assert "dude" in user.tags
33 test "untagging a user" do
34 user = insert(:user, %{tags: nil})
35 user = User.untag(user, ["cool", "dude"])
37 assert user.tags == []
41 test "ap_id returns the activity pub id for the user" do
42 user = UserBuilder.build()
44 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
46 assert expected_ap_id == User.ap_id(user)
49 test "ap_followers returns the followers collection for the user" do
50 user = UserBuilder.build()
52 expected_followers_collection = "#{User.ap_id(user)}/followers"
54 assert expected_followers_collection == User.ap_followers(user)
57 test "ap_following returns the following collection for the user" do
58 user = UserBuilder.build()
60 expected_followers_collection = "#{User.ap_id(user)}/following"
62 assert expected_followers_collection == User.ap_following(user)
65 test "returns all pending follow requests" do
66 unlocked = insert(:user)
67 locked = insert(:user, %{info: %{locked: true}})
68 follower = insert(:user)
70 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => unlocked.id})
71 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => locked.id})
73 assert {:ok, []} = User.get_follow_requests(unlocked)
74 assert {:ok, [activity]} = User.get_follow_requests(locked)
79 test "doesn't return already accepted or duplicate follow requests" do
80 locked = insert(:user, %{info: %{locked: true}})
81 pending_follower = insert(:user)
82 accepted_follower = insert(:user)
84 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
85 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
86 Pleroma.Web.TwitterAPI.TwitterAPI.follow(accepted_follower, %{"user_id" => locked.id})
87 User.follow(accepted_follower, locked)
89 assert {:ok, [activity]} = User.get_follow_requests(locked)
93 test "follow_all follows mutliple users" do
95 followed_zero = insert(:user)
96 followed_one = insert(:user)
97 followed_two = insert(:user)
98 blocked = insert(:user)
99 not_followed = insert(:user)
100 reverse_blocked = insert(:user)
102 {:ok, user} = User.block(user, blocked)
103 {:ok, reverse_blocked} = User.block(reverse_blocked, user)
105 {:ok, user} = User.follow(user, followed_zero)
107 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
109 assert User.following?(user, followed_one)
110 assert User.following?(user, followed_two)
111 assert User.following?(user, followed_zero)
112 refute User.following?(user, not_followed)
113 refute User.following?(user, blocked)
114 refute User.following?(user, reverse_blocked)
117 test "follow_all follows mutliple users without duplicating" do
119 followed_zero = insert(:user)
120 followed_one = insert(:user)
121 followed_two = insert(:user)
123 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
124 assert length(user.following) == 3
126 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
127 assert length(user.following) == 4
130 test "follow takes a user and another user" do
132 followed = insert(:user)
134 {:ok, user} = User.follow(user, followed)
136 user = User.get_cached_by_id(user.id)
138 followed = User.get_cached_by_ap_id(followed.ap_id)
139 assert followed.info.follower_count == 1
141 assert User.ap_followers(followed) in user.following
144 test "can't follow a deactivated users" do
146 followed = insert(:user, info: %{deactivated: true})
148 {:error, _} = User.follow(user, followed)
151 test "can't follow a user who blocked us" do
152 blocker = insert(:user)
153 blockee = insert(:user)
155 {:ok, blocker} = User.block(blocker, blockee)
157 {:error, _} = User.follow(blockee, blocker)
160 test "can't subscribe to a user who blocked us" do
161 blocker = insert(:user)
162 blocked = insert(:user)
164 {:ok, blocker} = User.block(blocker, blocked)
166 {:error, _} = User.subscribe(blocked, blocker)
169 test "local users do not automatically follow local locked accounts" do
170 follower = insert(:user, info: %{locked: true})
171 followed = insert(:user, info: %{locked: true})
173 {:ok, follower} = User.maybe_direct_follow(follower, followed)
175 refute User.following?(follower, followed)
178 # This is a somewhat useless test.
179 # test "following a remote user will ensure a websub subscription is present" do
180 # user = insert(:user)
181 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
183 # assert followed.local == false
185 # {:ok, user} = User.follow(user, followed)
186 # assert User.ap_followers(followed) in user.following
188 # query = from w in WebsubClientSubscription,
189 # where: w.topic == ^followed.info["topic"]
190 # websub = Repo.one(query)
195 test "unfollow takes a user and another user" do
196 followed = insert(:user)
197 user = insert(:user, %{following: [User.ap_followers(followed)]})
199 {:ok, user, _activity} = User.unfollow(user, followed)
201 user = User.get_cached_by_id(user.id)
203 assert user.following == []
206 test "unfollow doesn't unfollow yourself" do
209 {:error, _} = User.unfollow(user, user)
211 user = User.get_cached_by_id(user.id)
212 assert user.following == [user.ap_id]
215 test "test if a user is following another user" do
216 followed = insert(:user)
217 user = insert(:user, %{following: [User.ap_followers(followed)]})
219 assert User.following?(user, followed)
220 refute User.following?(followed, user)
223 test "fetches correct profile for nickname beginning with number" do
224 # Use old-style integer ID to try to reproduce the problem
225 user = insert(:user, %{id: 1080})
226 user_with_numbers = insert(:user, %{nickname: "#{user.id}garbage"})
227 assert user_with_numbers == User.get_cached_by_nickname_or_id(user_with_numbers.nickname)
230 describe "user registration" do
236 password_confirmation: "test",
237 email: "email@example.com"
240 test "it autofollows accounts that are set for it" do
242 remote_user = insert(:user, %{local: false})
244 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
249 cng = User.register_changeset(%User{}, @full_user_data)
251 {:ok, registered_user} = User.register(cng)
253 assert User.following?(registered_user, user)
254 refute User.following?(registered_user, remote_user)
256 Pleroma.Config.put([:instance, :autofollowed_nicknames], [])
259 test "it sends a welcome message if it is set" do
260 welcome_user = insert(:user)
262 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
263 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
265 cng = User.register_changeset(%User{}, @full_user_data)
266 {:ok, registered_user} = User.register(cng)
268 activity = Repo.one(Pleroma.Activity)
269 assert registered_user.ap_id in activity.recipients
270 assert Object.normalize(activity).data["content"] =~ "cool site"
271 assert activity.actor == welcome_user.ap_id
273 Pleroma.Config.put([:instance, :welcome_user_nickname], nil)
274 Pleroma.Config.put([:instance, :welcome_message], nil)
277 test "it requires an email, name, nickname and password, bio is optional" do
280 |> Enum.each(fn key ->
281 params = Map.delete(@full_user_data, key)
282 changeset = User.register_changeset(%User{}, params)
284 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
288 test "it restricts certain nicknames" do
289 [restricted_name | _] = Pleroma.Config.get([User, :restricted_nicknames])
291 assert is_bitstring(restricted_name)
295 |> Map.put(:nickname, restricted_name)
297 changeset = User.register_changeset(%User{}, params)
299 refute changeset.valid?
302 test "it sets the password_hash, ap_id and following fields" do
303 changeset = User.register_changeset(%User{}, @full_user_data)
305 assert changeset.valid?
307 assert is_binary(changeset.changes[:password_hash])
308 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
310 assert changeset.changes[:following] == [
311 User.ap_followers(%User{nickname: @full_user_data.nickname})
314 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
317 test "it ensures info is not nil" do
318 changeset = User.register_changeset(%User{}, @full_user_data)
320 assert changeset.valid?
326 refute is_nil(user.info)
330 describe "user registration, with :account_activation_required" do
336 password_confirmation: "test",
337 email: "email@example.com"
341 setting = Pleroma.Config.get([:instance, :account_activation_required])
344 Pleroma.Config.put([:instance, :account_activation_required], true)
345 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
351 test "it creates unconfirmed user" do
352 changeset = User.register_changeset(%User{}, @full_user_data)
353 assert changeset.valid?
355 {:ok, user} = Repo.insert(changeset)
357 assert user.info.confirmation_pending
358 assert user.info.confirmation_token
361 test "it creates confirmed user if :confirmed option is given" do
362 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
363 assert changeset.valid?
365 {:ok, user} = Repo.insert(changeset)
367 refute user.info.confirmation_pending
368 refute user.info.confirmation_token
372 describe "get_or_fetch/1" do
373 test "gets an existing user by nickname" do
375 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
377 assert user == fetched_user
380 test "gets an existing user by ap_id" do
381 ap_id = "http://mastodon.example.org/users/admin"
387 nickname: "admin@mastodon.example.org",
392 {:ok, fetched_user} = User.get_or_fetch(ap_id)
393 freshed_user = refresh_record(user)
394 assert freshed_user == fetched_user
398 describe "fetching a user from nickname or trying to build one" do
399 test "gets an existing user" do
401 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
403 assert user == fetched_user
406 test "gets an existing user, case insensitive" do
407 user = insert(:user, nickname: "nick")
408 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
410 assert user == fetched_user
413 test "gets an existing user by fully qualified nickname" do
416 {:ok, fetched_user} =
417 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
419 assert user == fetched_user
422 test "gets an existing user by fully qualified nickname, case insensitive" do
423 user = insert(:user, nickname: "nick")
424 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
426 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
428 assert user == fetched_user
431 test "fetches an external user via ostatus if no user exists" do
432 {:ok, fetched_user} = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
433 assert fetched_user.nickname == "shp@social.heldscal.la"
436 test "returns nil if no user could be fetched" do
437 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
438 assert fetched_user == "not found nonexistant@social.heldscal.la"
441 test "returns nil for nonexistant local user" do
442 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
443 assert fetched_user == "not found nonexistant"
446 test "updates an existing user, if stale" do
447 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
453 nickname: "admin@mastodon.example.org",
454 ap_id: "http://mastodon.example.org/users/admin",
455 last_refreshed_at: a_week_ago,
459 assert orig_user.last_refreshed_at == a_week_ago
461 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
462 assert user.info.source_data["endpoints"]
464 refute user.last_refreshed_at == orig_user.last_refreshed_at
468 test "returns an ap_id for a user" do
471 assert User.ap_id(user) ==
472 Pleroma.Web.Router.Helpers.o_status_url(
473 Pleroma.Web.Endpoint,
479 test "returns an ap_followers link for a user" do
482 assert User.ap_followers(user) ==
483 Pleroma.Web.Router.Helpers.o_status_url(
484 Pleroma.Web.Endpoint,
490 describe "remote user creation changeset" do
496 info: %{some: "info"},
497 avatar: %{some: "avatar"}
500 test "it confirms validity" do
501 cs = User.remote_user_creation(@valid_remote)
505 test "it sets the follower_adress" do
506 cs = User.remote_user_creation(@valid_remote)
507 # remote users get a fake local follower address
508 assert cs.changes.follower_address ==
509 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
512 test "it enforces the fqn format for nicknames" do
513 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
514 assert cs.changes.local == false
515 assert cs.changes.avatar
519 test "it has required fields" do
521 |> Enum.each(fn field ->
522 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
527 test "it restricts some sizes" do
528 [bio: 5000, name: 100]
529 |> Enum.each(fn {field, size} ->
530 string = String.pad_leading(".", size)
531 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
534 string = String.pad_leading(".", size + 1)
535 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
541 describe "followers and friends" do
542 test "gets all followers for a given user" do
544 follower_one = insert(:user)
545 follower_two = insert(:user)
546 not_follower = insert(:user)
548 {:ok, follower_one} = User.follow(follower_one, user)
549 {:ok, follower_two} = User.follow(follower_two, user)
551 {:ok, res} = User.get_followers(user)
553 assert Enum.member?(res, follower_one)
554 assert Enum.member?(res, follower_two)
555 refute Enum.member?(res, not_follower)
558 test "gets all friends (followed users) for a given user" do
560 followed_one = insert(:user)
561 followed_two = insert(:user)
562 not_followed = insert(:user)
564 {:ok, user} = User.follow(user, followed_one)
565 {:ok, user} = User.follow(user, followed_two)
567 {:ok, res} = User.get_friends(user)
569 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
570 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
571 assert Enum.member?(res, followed_one)
572 assert Enum.member?(res, followed_two)
573 refute Enum.member?(res, not_followed)
577 describe "updating note and follower count" do
578 test "it sets the info->note_count property" do
581 user = User.get_cached_by_ap_id(note.data["actor"])
583 assert user.info.note_count == 0
585 {:ok, user} = User.update_note_count(user)
587 assert user.info.note_count == 1
590 test "it increases the info->note_count property" do
592 user = User.get_cached_by_ap_id(note.data["actor"])
594 assert user.info.note_count == 0
596 {:ok, user} = User.increase_note_count(user)
598 assert user.info.note_count == 1
600 {:ok, user} = User.increase_note_count(user)
602 assert user.info.note_count == 2
605 test "it decreases the info->note_count property" do
607 user = User.get_cached_by_ap_id(note.data["actor"])
609 assert user.info.note_count == 0
611 {:ok, user} = User.increase_note_count(user)
613 assert user.info.note_count == 1
615 {:ok, user} = User.decrease_note_count(user)
617 assert user.info.note_count == 0
619 {:ok, user} = User.decrease_note_count(user)
621 assert user.info.note_count == 0
624 test "it sets the info->follower_count property" do
626 follower = insert(:user)
628 User.follow(follower, user)
630 assert user.info.follower_count == 0
632 {:ok, user} = User.update_follower_count(user)
634 assert user.info.follower_count == 1
638 describe "remove duplicates from following list" do
639 test "it removes duplicates" do
641 follower = insert(:user)
643 {:ok, %User{following: following} = follower} = User.follow(follower, user)
644 assert length(following) == 2
648 |> User.update_changeset(%{following: following ++ following})
651 assert length(follower.following) == 4
653 {:ok, follower} = User.remove_duplicated_following(follower)
654 assert length(follower.following) == 2
657 test "it does nothing when following is uniq" do
659 follower = insert(:user)
661 {:ok, follower} = User.follow(follower, user)
662 assert length(follower.following) == 2
664 {:ok, follower} = User.remove_duplicated_following(follower)
665 assert length(follower.following) == 2
669 describe "follow_import" do
670 test "it imports user followings from list" do
671 [user1, user2, user3] = insert_list(3, :user)
678 result = User.follow_import(user1, identifiers)
679 assert is_list(result)
680 assert result == [user2, user3]
685 test "it mutes people" do
687 muted_user = insert(:user)
689 refute User.mutes?(user, muted_user)
690 refute User.muted_notifications?(user, muted_user)
692 {:ok, user} = User.mute(user, muted_user)
694 assert User.mutes?(user, muted_user)
695 assert User.muted_notifications?(user, muted_user)
698 test "it unmutes users" do
700 muted_user = insert(:user)
702 {:ok, user} = User.mute(user, muted_user)
703 {:ok, user} = User.unmute(user, muted_user)
705 refute User.mutes?(user, muted_user)
706 refute User.muted_notifications?(user, muted_user)
709 test "it mutes user without notifications" do
711 muted_user = insert(:user)
713 refute User.mutes?(user, muted_user)
714 refute User.muted_notifications?(user, muted_user)
716 {:ok, user} = User.mute(user, muted_user, false)
718 assert User.mutes?(user, muted_user)
719 refute User.muted_notifications?(user, muted_user)
724 test "it blocks people" do
726 blocked_user = insert(:user)
728 refute User.blocks?(user, blocked_user)
730 {:ok, user} = User.block(user, blocked_user)
732 assert User.blocks?(user, blocked_user)
735 test "it unblocks users" do
737 blocked_user = insert(:user)
739 {:ok, user} = User.block(user, blocked_user)
740 {:ok, user} = User.unblock(user, blocked_user)
742 refute User.blocks?(user, blocked_user)
745 test "blocks tear down cyclical follow relationships" do
746 blocker = insert(:user)
747 blocked = insert(:user)
749 {:ok, blocker} = User.follow(blocker, blocked)
750 {:ok, blocked} = User.follow(blocked, blocker)
752 assert User.following?(blocker, blocked)
753 assert User.following?(blocked, blocker)
755 {:ok, blocker} = User.block(blocker, blocked)
756 blocked = User.get_cached_by_id(blocked.id)
758 assert User.blocks?(blocker, blocked)
760 refute User.following?(blocker, blocked)
761 refute User.following?(blocked, blocker)
764 test "blocks tear down blocker->blocked follow relationships" do
765 blocker = insert(:user)
766 blocked = insert(:user)
768 {:ok, blocker} = User.follow(blocker, blocked)
770 assert User.following?(blocker, blocked)
771 refute User.following?(blocked, blocker)
773 {:ok, blocker} = User.block(blocker, blocked)
774 blocked = User.get_cached_by_id(blocked.id)
776 assert User.blocks?(blocker, blocked)
778 refute User.following?(blocker, blocked)
779 refute User.following?(blocked, blocker)
782 test "blocks tear down blocked->blocker follow relationships" do
783 blocker = insert(:user)
784 blocked = insert(:user)
786 {:ok, blocked} = User.follow(blocked, blocker)
788 refute User.following?(blocker, blocked)
789 assert User.following?(blocked, blocker)
791 {:ok, blocker} = User.block(blocker, blocked)
792 blocked = User.get_cached_by_id(blocked.id)
794 assert User.blocks?(blocker, blocked)
796 refute User.following?(blocker, blocked)
797 refute User.following?(blocked, blocker)
800 test "blocks tear down blocked->blocker subscription relationships" do
801 blocker = insert(:user)
802 blocked = insert(:user)
804 {:ok, blocker} = User.subscribe(blocked, blocker)
806 assert User.subscribed_to?(blocked, blocker)
807 refute User.subscribed_to?(blocker, blocked)
809 {:ok, blocker} = User.block(blocker, blocked)
811 assert User.blocks?(blocker, blocked)
812 refute User.subscribed_to?(blocker, blocked)
813 refute User.subscribed_to?(blocked, blocker)
817 describe "domain blocking" do
818 test "blocks domains" do
820 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
822 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
824 assert User.blocks?(user, collateral_user)
827 test "does not block domain with same end" do
831 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
833 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
835 refute User.blocks?(user, collateral_user)
838 test "does not block domain with same end if wildcard added" do
842 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
844 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
846 refute User.blocks?(user, collateral_user)
849 test "blocks domain with wildcard for subdomain" do
852 user_from_subdomain =
853 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
855 user_with_two_subdomains =
857 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
860 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
862 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
864 assert User.blocks?(user, user_from_subdomain)
865 assert User.blocks?(user, user_with_two_subdomains)
866 assert User.blocks?(user, user_domain)
869 test "unblocks domains" do
871 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
873 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
874 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
876 refute User.blocks?(user, collateral_user)
880 describe "blocks_import" do
881 test "it imports user blocks from list" do
882 [user1, user2, user3] = insert_list(3, :user)
889 result = User.blocks_import(user1, identifiers)
890 assert is_list(result)
891 assert result == [user2, user3]
895 test "get recipients from activity" do
896 actor = insert(:user)
897 user = insert(:user, local: true)
898 user_two = insert(:user, local: false)
899 addressed = insert(:user, local: true)
900 addressed_remote = insert(:user, local: false)
903 CommonAPI.post(actor, %{
904 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
907 assert Enum.map([actor, addressed], & &1.ap_id) --
908 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
910 {:ok, user} = User.follow(user, actor)
911 {:ok, _user_two} = User.follow(user_two, actor)
912 recipients = User.get_recipients_from_activity(activity)
913 assert length(recipients) == 3
914 assert user in recipients
915 assert addressed in recipients
918 describe ".deactivate" do
919 test "can de-activate then re-activate a user" do
921 assert false == user.info.deactivated
922 {:ok, user} = User.deactivate(user)
923 assert true == user.info.deactivated
924 {:ok, user} = User.deactivate(user, false)
925 assert false == user.info.deactivated
928 test "hide a user from followers " do
930 user2 = insert(:user)
932 {:ok, user} = User.follow(user, user2)
933 {:ok, _user} = User.deactivate(user)
935 info = User.get_cached_user_info(user2)
937 assert info.follower_count == 0
938 assert {:ok, []} = User.get_followers(user2)
941 test "hide a user from friends" do
943 user2 = insert(:user)
945 {:ok, user2} = User.follow(user2, user)
946 assert User.following_count(user2) == 1
948 {:ok, _user} = User.deactivate(user)
950 info = User.get_cached_user_info(user2)
952 assert info.following_count == 0
953 assert User.following_count(user2) == 0
954 assert {:ok, []} = User.get_friends(user2)
957 test "hide a user's statuses from timelines and notifications" do
959 user2 = insert(:user)
961 {:ok, user2} = User.follow(user2, user)
963 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
965 activity = Repo.preload(activity, :bookmark)
967 [notification] = Pleroma.Notification.for_user(user2)
968 assert notification.activity.id == activity.id
970 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
972 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
973 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
975 {:ok, _user} = User.deactivate(user)
977 assert [] == ActivityPub.fetch_public_activities(%{})
978 assert [] == Pleroma.Notification.for_user(user2)
981 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
987 {:ok, user} = insert(:user) |> User.set_cache()
992 test ".delete_user_activities deletes all create activities", %{user: user} do
993 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
995 {:ok, _} = User.delete_user_activities(user)
997 # TODO: Remove favorites, repeats, delete activities.
998 refute Activity.get_by_id(activity.id)
1001 test "it deletes a user, all follow relationships and all activities", %{user: user} do
1002 follower = insert(:user)
1003 {:ok, follower} = User.follow(follower, user)
1005 object = insert(:note, user: user)
1006 activity = insert(:note_activity, user: user, note: object)
1008 object_two = insert(:note, user: follower)
1009 activity_two = insert(:note_activity, user: follower, note: object_two)
1011 {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
1012 {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
1013 {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
1015 {:ok, _} = User.delete(user)
1017 follower = User.get_cached_by_id(follower.id)
1019 refute User.following?(follower, user)
1020 refute User.get_by_id(user.id)
1021 assert {:ok, nil} == Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1025 |> Activity.query_by_actor()
1027 |> Enum.map(fn act -> act.data["type"] end)
1029 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1031 refute Activity.get_by_id(activity.id)
1032 refute Activity.get_by_id(like.id)
1033 refute Activity.get_by_id(like_two.id)
1034 refute Activity.get_by_id(repeat.id)
1037 test_with_mock "it sends out User Delete activity",
1039 Pleroma.Web.ActivityPub.Publisher,
1042 config_path = [:instance, :federating]
1043 initial_setting = Pleroma.Config.get(config_path)
1044 Pleroma.Config.put(config_path, true)
1046 {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
1047 {:ok, _} = User.follow(follower, user)
1049 {:ok, _user} = User.delete(user)
1052 Pleroma.Web.ActivityPub.Publisher.publish_one(%{
1053 inbox: "http://mastodon.example.org/inbox"
1057 Pleroma.Config.put(config_path, initial_setting)
1061 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1062 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1065 test "insert or update a user from given data" do
1066 user = insert(:user, %{nickname: "nick@name.de"})
1067 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
1069 assert {:ok, %User{}} = User.insert_or_update_user(data)
1072 describe "per-user rich-text filtering" do
1073 test "html_filter_policy returns default policies, when rich-text is enabled" do
1074 user = insert(:user)
1076 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1079 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1080 user = insert(:user, %{info: %{no_rich_text: true}})
1082 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1086 describe "caching" do
1087 test "invalidate_cache works" do
1088 user = insert(:user)
1089 _user_info = User.get_cached_user_info(user)
1091 User.invalidate_cache(user)
1093 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1094 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1095 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
1098 test "User.delete() plugs any possible zombie objects" do
1099 user = insert(:user)
1101 {:ok, _} = User.delete(user)
1103 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1105 assert cached_user != user
1107 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1109 assert cached_user != user
1113 test "auth_active?/1 works correctly" do
1114 Pleroma.Config.put([:instance, :account_activation_required], true)
1116 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
1117 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
1118 remote_user = insert(:user, local: false)
1120 refute User.auth_active?(local_user)
1121 assert User.auth_active?(confirmed_user)
1122 assert User.auth_active?(remote_user)
1124 Pleroma.Config.put([:instance, :account_activation_required], false)
1127 describe "superuser?/1" do
1128 test "returns false for unprivileged users" do
1129 user = insert(:user, local: true)
1131 refute User.superuser?(user)
1134 test "returns false for remote users" do
1135 user = insert(:user, local: false)
1136 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1138 refute User.superuser?(user)
1139 refute User.superuser?(remote_admin_user)
1142 test "returns true for local moderators" do
1143 user = insert(:user, local: true, info: %{is_moderator: true})
1145 assert User.superuser?(user)
1148 test "returns true for local admins" do
1149 user = insert(:user, local: true, info: %{is_admin: true})
1151 assert User.superuser?(user)
1155 describe "visible_for?/2" do
1156 test "returns true when the account is itself" do
1157 user = insert(:user, local: true)
1159 assert User.visible_for?(user, user)
1162 test "returns false when the account is unauthenticated and auth is required" do
1163 Pleroma.Config.put([:instance, :account_activation_required], true)
1165 user = insert(:user, local: true, info: %{confirmation_pending: true})
1166 other_user = insert(:user, local: true)
1168 refute User.visible_for?(user, other_user)
1170 Pleroma.Config.put([:instance, :account_activation_required], false)
1173 test "returns true when the account is unauthenticated and auth is not required" do
1174 user = insert(:user, local: true, info: %{confirmation_pending: true})
1175 other_user = insert(:user, local: true)
1177 assert User.visible_for?(user, other_user)
1180 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1181 Pleroma.Config.put([:instance, :account_activation_required], true)
1183 user = insert(:user, local: true, info: %{confirmation_pending: true})
1184 other_user = insert(:user, local: true, info: %{is_admin: true})
1186 assert User.visible_for?(user, other_user)
1188 Pleroma.Config.put([:instance, :account_activation_required], false)
1192 describe "parse_bio/2" do
1193 test "preserves hosts in user links text" do
1194 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1195 user = insert(:user)
1196 bio = "A.k.a. @nick@domain.com"
1199 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1201 }'>@<span>nick@domain.com</span></a></span>"
1203 assert expected_text == User.parse_bio(bio, user)
1206 test "Adds rel=me on linkbacked urls" do
1207 user = insert(:user, ap_id: "http://social.example.org/users/lain")
1209 bio = "http://example.org/rel_me/null"
1210 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1211 assert expected_text == User.parse_bio(bio, user)
1213 bio = "http://example.org/rel_me/link"
1214 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1215 assert expected_text == User.parse_bio(bio, user)
1217 bio = "http://example.org/rel_me/anchor"
1218 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1219 assert expected_text == User.parse_bio(bio, user)
1223 test "follower count is updated when a follower is blocked" do
1224 user = insert(:user)
1225 follower = insert(:user)
1226 follower2 = insert(:user)
1227 follower3 = insert(:user)
1229 {:ok, follower} = User.follow(follower, user)
1230 {:ok, _follower2} = User.follow(follower2, user)
1231 {:ok, _follower3} = User.follow(follower3, user)
1233 {:ok, _} = User.block(user, follower)
1235 user_show = Pleroma.Web.TwitterAPI.UserView.render("show.json", %{user: user})
1237 assert Map.get(user_show, "followers_count") == 2
1240 describe "toggle_confirmation/1" do
1241 test "if user is confirmed" do
1242 user = insert(:user, info: %{confirmation_pending: false})
1243 {:ok, user} = User.toggle_confirmation(user)
1245 assert user.info.confirmation_pending
1246 assert user.info.confirmation_token
1249 test "if user is unconfirmed" do
1250 user = insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
1251 {:ok, user} = User.toggle_confirmation(user)
1253 refute user.info.confirmation_pending
1254 refute user.info.confirmation_token
1258 describe "ensure_keys_present" do
1259 test "it creates keys for a user and stores them in info" do
1260 user = insert(:user)
1261 refute is_binary(user.info.keys)
1262 {:ok, user} = User.ensure_keys_present(user)
1263 assert is_binary(user.info.keys)
1266 test "it doesn't create keys if there already are some" do
1267 user = insert(:user, %{info: %{keys: "xxx"}})
1268 {:ok, user} = User.ensure_keys_present(user)
1269 assert user.info.keys == "xxx"
1273 describe "get_ap_ids_by_nicknames" do
1274 test "it returns a list of AP ids for a given set of nicknames" do
1275 user = insert(:user)
1276 user_two = insert(:user)
1278 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1279 assert length(ap_ids) == 2
1280 assert user.ap_id in ap_ids
1281 assert user_two.ap_id in ap_ids
1285 describe "sync followers count" do
1287 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1288 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1289 insert(:user, local: true)
1290 insert(:user, local: false, info: %{deactivated: true})
1291 {:ok, user1: user1, user2: user2}
1294 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1295 [fdb_user1] = User.external_users(limit: 1)
1297 assert fdb_user1.ap_id
1298 assert fdb_user1.ap_id == user1.ap_id
1299 assert fdb_user1.id == user1.id
1301 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1303 assert fdb_user2.ap_id
1304 assert fdb_user2.ap_id == user2.ap_id
1305 assert fdb_user2.id == user2.id
1307 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1311 describe "set_info_cache/2" do
1313 user = insert(:user)
1317 test "update from args", %{user: user} do
1318 User.set_info_cache(user, %{following_count: 15, follower_count: 18})
1320 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1321 assert followers == 18
1322 assert following == 15
1325 test "without args", %{user: user} do
1326 User.set_info_cache(user, %{})
1328 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1329 assert followers == 0
1330 assert following == 0
1334 describe "user_info/2" do
1336 user = insert(:user)
1340 test "update from args", %{user: user} do
1341 %{follower_count: followers, following_count: following} =
1342 User.user_info(user, %{following_count: 15, follower_count: 18})
1344 assert followers == 18
1345 assert following == 15
1348 test "without args", %{user: user} do
1349 %{follower_count: followers, following_count: following} = User.user_info(user)
1351 assert followers == 0
1352 assert following == 0
1356 describe "is_internal_user?/1" do
1357 test "non-internal user returns false" do
1358 user = insert(:user)
1359 refute User.is_internal_user?(user)
1362 test "user with no nickname returns true" do
1363 user = insert(:user, %{nickname: nil})
1364 assert User.is_internal_user?(user)
1367 test "user with internal-prefixed nickname returns true" do
1368 user = insert(:user, %{nickname: "internal.test"})
1369 assert User.is_internal_user?(user)
1373 describe "update_and_set_cache/1" do
1374 test "returns error when user is stale instead Ecto.StaleEntryError" do
1375 user = insert(:user)
1377 changeset = Ecto.Changeset.change(user, bio: "test")
1381 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1382 User.update_and_set_cache(changeset)
1385 test "performs update cache if user updated" do
1386 user = insert(:user)
1387 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1389 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1391 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1392 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1393 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1397 describe "following/followers synchronization" do
1399 sync = Pleroma.Config.get([:instance, :external_user_synchronization])
1400 on_exit(fn -> Pleroma.Config.put([:instance, :external_user_synchronization], sync) end)
1403 test "updates the counters normally on following/getting a follow when disabled" do
1404 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1405 user = insert(:user)
1410 follower_address: "http://localhost:4001/users/masto_closed/followers",
1411 following_address: "http://localhost:4001/users/masto_closed/following",
1412 info: %{ap_enabled: true}
1415 assert User.user_info(other_user).following_count == 0
1416 assert User.user_info(other_user).follower_count == 0
1418 {:ok, user} = Pleroma.User.follow(user, other_user)
1419 other_user = Pleroma.User.get_by_id(other_user.id)
1421 assert User.user_info(user).following_count == 1
1422 assert User.user_info(other_user).follower_count == 1
1425 test "syncronizes the counters with the remote instance for the followed when enabled" do
1426 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1428 user = insert(:user)
1433 follower_address: "http://localhost:4001/users/masto_closed/followers",
1434 following_address: "http://localhost:4001/users/masto_closed/following",
1435 info: %{ap_enabled: true}
1438 assert User.user_info(other_user).following_count == 0
1439 assert User.user_info(other_user).follower_count == 0
1441 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1442 {:ok, _user} = User.follow(user, other_user)
1443 other_user = User.get_by_id(other_user.id)
1445 assert User.user_info(other_user).follower_count == 437
1448 test "syncronizes the counters with the remote instance for the follower when enabled" do
1449 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1451 user = insert(:user)
1456 follower_address: "http://localhost:4001/users/masto_closed/followers",
1457 following_address: "http://localhost:4001/users/masto_closed/following",
1458 info: %{ap_enabled: true}
1461 assert User.user_info(other_user).following_count == 0
1462 assert User.user_info(other_user).follower_count == 0
1464 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1465 {:ok, other_user} = User.follow(other_user, user)
1467 assert User.user_info(other_user).following_count == 152