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
8 alias Pleroma.ObanHelpers
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.CommonAPI
16 use Oban.Testing, repo: Pleroma.Repo
18 import Pleroma.Factory
21 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
25 describe "when tags are nil" do
26 test "tagging a user" do
27 user = insert(:user, %{tags: nil})
28 user = User.tag(user, ["cool", "dude"])
30 assert "cool" in user.tags
31 assert "dude" in user.tags
34 test "untagging a user" do
35 user = insert(:user, %{tags: nil})
36 user = User.untag(user, ["cool", "dude"])
38 assert user.tags == []
42 test "ap_id returns the activity pub id for the user" do
43 user = UserBuilder.build()
45 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
47 assert expected_ap_id == User.ap_id(user)
50 test "ap_followers returns the followers collection for the user" do
51 user = UserBuilder.build()
53 expected_followers_collection = "#{User.ap_id(user)}/followers"
55 assert expected_followers_collection == User.ap_followers(user)
58 test "ap_following returns the following collection for the user" do
59 user = UserBuilder.build()
61 expected_followers_collection = "#{User.ap_id(user)}/following"
63 assert expected_followers_collection == User.ap_following(user)
66 test "returns all pending follow requests" do
67 unlocked = insert(:user)
68 locked = insert(:user, %{info: %{locked: true}})
69 follower = insert(:user)
71 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => unlocked.id})
72 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => locked.id})
74 assert {:ok, []} = User.get_follow_requests(unlocked)
75 assert {:ok, [activity]} = User.get_follow_requests(locked)
80 test "doesn't return already accepted or duplicate follow requests" do
81 locked = insert(:user, %{info: %{locked: true}})
82 pending_follower = insert(:user)
83 accepted_follower = insert(:user)
85 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
86 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
87 Pleroma.Web.TwitterAPI.TwitterAPI.follow(accepted_follower, %{"user_id" => locked.id})
88 User.follow(accepted_follower, locked)
90 assert {:ok, [activity]} = User.get_follow_requests(locked)
94 test "follow_all follows mutliple users" do
96 followed_zero = insert(:user)
97 followed_one = insert(:user)
98 followed_two = insert(:user)
99 blocked = insert(:user)
100 not_followed = insert(:user)
101 reverse_blocked = insert(:user)
103 {:ok, user} = User.block(user, blocked)
104 {:ok, reverse_blocked} = User.block(reverse_blocked, user)
106 {:ok, user} = User.follow(user, followed_zero)
108 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
110 assert User.following?(user, followed_one)
111 assert User.following?(user, followed_two)
112 assert User.following?(user, followed_zero)
113 refute User.following?(user, not_followed)
114 refute User.following?(user, blocked)
115 refute User.following?(user, reverse_blocked)
118 test "follow_all follows mutliple users without duplicating" do
120 followed_zero = insert(:user)
121 followed_one = insert(:user)
122 followed_two = insert(:user)
124 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
125 assert length(user.following) == 3
127 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
128 assert length(user.following) == 4
131 test "follow takes a user and another user" do
133 followed = insert(:user)
135 {:ok, user} = User.follow(user, followed)
137 user = User.get_cached_by_id(user.id)
139 followed = User.get_cached_by_ap_id(followed.ap_id)
140 assert followed.info.follower_count == 1
142 assert User.ap_followers(followed) in user.following
145 test "can't follow a deactivated users" do
147 followed = insert(:user, info: %{deactivated: true})
149 {:error, _} = User.follow(user, followed)
152 test "can't follow a user who blocked us" do
153 blocker = insert(:user)
154 blockee = insert(:user)
156 {:ok, blocker} = User.block(blocker, blockee)
158 {:error, _} = User.follow(blockee, blocker)
161 test "can't subscribe to a user who blocked us" do
162 blocker = insert(:user)
163 blocked = insert(:user)
165 {:ok, blocker} = User.block(blocker, blocked)
167 {:error, _} = User.subscribe(blocked, blocker)
170 test "local users do not automatically follow local locked accounts" do
171 follower = insert(:user, info: %{locked: true})
172 followed = insert(:user, info: %{locked: true})
174 {:ok, follower} = User.maybe_direct_follow(follower, followed)
176 refute User.following?(follower, followed)
179 # This is a somewhat useless test.
180 # test "following a remote user will ensure a websub subscription is present" do
181 # user = insert(:user)
182 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
184 # assert followed.local == false
186 # {:ok, user} = User.follow(user, followed)
187 # assert User.ap_followers(followed) in user.following
189 # query = from w in WebsubClientSubscription,
190 # where: w.topic == ^followed.info["topic"]
191 # websub = Repo.one(query)
196 test "unfollow takes a user and another user" do
197 followed = insert(:user)
198 user = insert(:user, %{following: [User.ap_followers(followed)]})
200 {:ok, user, _activity} = User.unfollow(user, followed)
202 user = User.get_cached_by_id(user.id)
204 assert user.following == []
207 test "unfollow doesn't unfollow yourself" do
210 {:error, _} = User.unfollow(user, user)
212 user = User.get_cached_by_id(user.id)
213 assert user.following == [user.ap_id]
216 test "test if a user is following another user" do
217 followed = insert(:user)
218 user = insert(:user, %{following: [User.ap_followers(followed)]})
220 assert User.following?(user, followed)
221 refute User.following?(followed, user)
224 test "fetches correct profile for nickname beginning with number" do
225 # Use old-style integer ID to try to reproduce the problem
226 user = insert(:user, %{id: 1080})
227 user_with_numbers = insert(:user, %{nickname: "#{user.id}garbage"})
228 assert user_with_numbers == User.get_cached_by_nickname_or_id(user_with_numbers.nickname)
231 describe "user registration" do
237 password_confirmation: "test",
238 email: "email@example.com"
241 test "it autofollows accounts that are set for it" do
243 remote_user = insert(:user, %{local: false})
245 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
250 cng = User.register_changeset(%User{}, @full_user_data)
252 {:ok, registered_user} = User.register(cng)
254 assert User.following?(registered_user, user)
255 refute User.following?(registered_user, remote_user)
257 Pleroma.Config.put([:instance, :autofollowed_nicknames], [])
260 test "it sends a welcome message if it is set" do
261 welcome_user = insert(:user)
263 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
264 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
266 cng = User.register_changeset(%User{}, @full_user_data)
267 {:ok, registered_user} = User.register(cng)
269 activity = Repo.one(Pleroma.Activity)
270 assert registered_user.ap_id in activity.recipients
271 assert Object.normalize(activity).data["content"] =~ "cool site"
272 assert activity.actor == welcome_user.ap_id
274 Pleroma.Config.put([:instance, :welcome_user_nickname], nil)
275 Pleroma.Config.put([:instance, :welcome_message], nil)
278 test "it requires an email, name, nickname and password, bio is optional" do
281 |> Enum.each(fn key ->
282 params = Map.delete(@full_user_data, key)
283 changeset = User.register_changeset(%User{}, params)
285 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
289 test "it restricts certain nicknames" do
290 [restricted_name | _] = Pleroma.Config.get([User, :restricted_nicknames])
292 assert is_bitstring(restricted_name)
296 |> Map.put(:nickname, restricted_name)
298 changeset = User.register_changeset(%User{}, params)
300 refute changeset.valid?
303 test "it sets the password_hash, ap_id and following fields" do
304 changeset = User.register_changeset(%User{}, @full_user_data)
306 assert changeset.valid?
308 assert is_binary(changeset.changes[:password_hash])
309 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
311 assert changeset.changes[:following] == [
312 User.ap_followers(%User{nickname: @full_user_data.nickname})
315 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
318 test "it ensures info is not nil" do
319 changeset = User.register_changeset(%User{}, @full_user_data)
321 assert changeset.valid?
327 refute is_nil(user.info)
331 describe "user registration, with :account_activation_required" do
337 password_confirmation: "test",
338 email: "email@example.com"
342 setting = Pleroma.Config.get([:instance, :account_activation_required])
345 Pleroma.Config.put([:instance, :account_activation_required], true)
346 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
352 test "it creates unconfirmed user" do
353 changeset = User.register_changeset(%User{}, @full_user_data)
354 assert changeset.valid?
356 {:ok, user} = Repo.insert(changeset)
358 assert user.info.confirmation_pending
359 assert user.info.confirmation_token
362 test "it creates confirmed user if :confirmed option is given" do
363 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
364 assert changeset.valid?
366 {:ok, user} = Repo.insert(changeset)
368 refute user.info.confirmation_pending
369 refute user.info.confirmation_token
373 describe "get_or_fetch/1" do
374 test "gets an existing user by nickname" do
376 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
378 assert user == fetched_user
381 test "gets an existing user by ap_id" do
382 ap_id = "http://mastodon.example.org/users/admin"
388 nickname: "admin@mastodon.example.org",
393 {:ok, fetched_user} = User.get_or_fetch(ap_id)
394 freshed_user = refresh_record(user)
395 assert freshed_user == fetched_user
399 describe "fetching a user from nickname or trying to build one" do
400 test "gets an existing user" do
402 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
404 assert user == fetched_user
407 test "gets an existing user, case insensitive" do
408 user = insert(:user, nickname: "nick")
409 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
411 assert user == fetched_user
414 test "gets an existing user by fully qualified nickname" do
417 {:ok, fetched_user} =
418 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
420 assert user == fetched_user
423 test "gets an existing user by fully qualified nickname, case insensitive" do
424 user = insert(:user, nickname: "nick")
425 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
427 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
429 assert user == fetched_user
432 test "fetches an external user via ostatus if no user exists" do
433 {:ok, fetched_user} = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
434 assert fetched_user.nickname == "shp@social.heldscal.la"
437 test "returns nil if no user could be fetched" do
438 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
439 assert fetched_user == "not found nonexistant@social.heldscal.la"
442 test "returns nil for nonexistant local user" do
443 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
444 assert fetched_user == "not found nonexistant"
447 test "updates an existing user, if stale" do
448 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
454 nickname: "admin@mastodon.example.org",
455 ap_id: "http://mastodon.example.org/users/admin",
456 last_refreshed_at: a_week_ago,
460 assert orig_user.last_refreshed_at == a_week_ago
462 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
463 assert user.info.source_data["endpoints"]
465 refute user.last_refreshed_at == orig_user.last_refreshed_at
469 test "returns an ap_id for a user" do
472 assert User.ap_id(user) ==
473 Pleroma.Web.Router.Helpers.o_status_url(
474 Pleroma.Web.Endpoint,
480 test "returns an ap_followers link for a user" do
483 assert User.ap_followers(user) ==
484 Pleroma.Web.Router.Helpers.o_status_url(
485 Pleroma.Web.Endpoint,
491 describe "remote user creation changeset" do
497 info: %{some: "info"},
498 avatar: %{some: "avatar"}
501 test "it confirms validity" do
502 cs = User.remote_user_creation(@valid_remote)
506 test "it sets the follower_adress" do
507 cs = User.remote_user_creation(@valid_remote)
508 # remote users get a fake local follower address
509 assert cs.changes.follower_address ==
510 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
513 test "it enforces the fqn format for nicknames" do
514 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
515 assert cs.changes.local == false
516 assert cs.changes.avatar
520 test "it has required fields" do
522 |> Enum.each(fn field ->
523 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
528 test "it restricts some sizes" do
529 [bio: 5000, name: 100]
530 |> Enum.each(fn {field, size} ->
531 string = String.pad_leading(".", size)
532 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
535 string = String.pad_leading(".", size + 1)
536 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
542 describe "followers and friends" do
543 test "gets all followers for a given user" do
545 follower_one = insert(:user)
546 follower_two = insert(:user)
547 not_follower = insert(:user)
549 {:ok, follower_one} = User.follow(follower_one, user)
550 {:ok, follower_two} = User.follow(follower_two, user)
552 {:ok, res} = User.get_followers(user)
554 assert Enum.member?(res, follower_one)
555 assert Enum.member?(res, follower_two)
556 refute Enum.member?(res, not_follower)
559 test "gets all friends (followed users) for a given user" do
561 followed_one = insert(:user)
562 followed_two = insert(:user)
563 not_followed = insert(:user)
565 {:ok, user} = User.follow(user, followed_one)
566 {:ok, user} = User.follow(user, followed_two)
568 {:ok, res} = User.get_friends(user)
570 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
571 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
572 assert Enum.member?(res, followed_one)
573 assert Enum.member?(res, followed_two)
574 refute Enum.member?(res, not_followed)
578 describe "updating note and follower count" do
579 test "it sets the info->note_count property" do
582 user = User.get_cached_by_ap_id(note.data["actor"])
584 assert user.info.note_count == 0
586 {:ok, user} = User.update_note_count(user)
588 assert user.info.note_count == 1
591 test "it increases the info->note_count property" do
593 user = User.get_cached_by_ap_id(note.data["actor"])
595 assert user.info.note_count == 0
597 {:ok, user} = User.increase_note_count(user)
599 assert user.info.note_count == 1
601 {:ok, user} = User.increase_note_count(user)
603 assert user.info.note_count == 2
606 test "it decreases the info->note_count property" do
608 user = User.get_cached_by_ap_id(note.data["actor"])
610 assert user.info.note_count == 0
612 {:ok, user} = User.increase_note_count(user)
614 assert user.info.note_count == 1
616 {:ok, user} = User.decrease_note_count(user)
618 assert user.info.note_count == 0
620 {:ok, user} = User.decrease_note_count(user)
622 assert user.info.note_count == 0
625 test "it sets the info->follower_count property" do
627 follower = insert(:user)
629 User.follow(follower, user)
631 assert user.info.follower_count == 0
633 {:ok, user} = User.update_follower_count(user)
635 assert user.info.follower_count == 1
639 describe "remove duplicates from following list" do
640 test "it removes duplicates" do
642 follower = insert(:user)
644 {:ok, %User{following: following} = follower} = User.follow(follower, user)
645 assert length(following) == 2
649 |> User.update_changeset(%{following: following ++ following})
652 assert length(follower.following) == 4
654 {:ok, follower} = User.remove_duplicated_following(follower)
655 assert length(follower.following) == 2
658 test "it does nothing when following is uniq" do
660 follower = insert(:user)
662 {:ok, follower} = User.follow(follower, user)
663 assert length(follower.following) == 2
665 {:ok, follower} = User.remove_duplicated_following(follower)
666 assert length(follower.following) == 2
670 describe "follow_import" do
671 test "it imports user followings from list" do
672 [user1, user2, user3] = insert_list(3, :user)
679 result = User.follow_import(user1, identifiers)
680 assert is_list(result)
681 assert result == [user2, user3]
686 test "it mutes people" do
688 muted_user = insert(:user)
690 refute User.mutes?(user, muted_user)
691 refute User.muted_notifications?(user, muted_user)
693 {:ok, user} = User.mute(user, muted_user)
695 assert User.mutes?(user, muted_user)
696 assert User.muted_notifications?(user, muted_user)
699 test "it unmutes users" do
701 muted_user = insert(:user)
703 {:ok, user} = User.mute(user, muted_user)
704 {:ok, user} = User.unmute(user, muted_user)
706 refute User.mutes?(user, muted_user)
707 refute User.muted_notifications?(user, muted_user)
710 test "it mutes user without notifications" do
712 muted_user = insert(:user)
714 refute User.mutes?(user, muted_user)
715 refute User.muted_notifications?(user, muted_user)
717 {:ok, user} = User.mute(user, muted_user, false)
719 assert User.mutes?(user, muted_user)
720 refute User.muted_notifications?(user, muted_user)
725 test "it blocks people" do
727 blocked_user = insert(:user)
729 refute User.blocks?(user, blocked_user)
731 {:ok, user} = User.block(user, blocked_user)
733 assert User.blocks?(user, blocked_user)
736 test "it unblocks users" do
738 blocked_user = insert(:user)
740 {:ok, user} = User.block(user, blocked_user)
741 {:ok, user} = User.unblock(user, blocked_user)
743 refute User.blocks?(user, blocked_user)
746 test "blocks tear down cyclical follow relationships" do
747 blocker = insert(:user)
748 blocked = insert(:user)
750 {:ok, blocker} = User.follow(blocker, blocked)
751 {:ok, blocked} = User.follow(blocked, blocker)
753 assert User.following?(blocker, blocked)
754 assert User.following?(blocked, blocker)
756 {:ok, blocker} = User.block(blocker, blocked)
757 blocked = User.get_cached_by_id(blocked.id)
759 assert User.blocks?(blocker, blocked)
761 refute User.following?(blocker, blocked)
762 refute User.following?(blocked, blocker)
765 test "blocks tear down blocker->blocked follow relationships" do
766 blocker = insert(:user)
767 blocked = insert(:user)
769 {:ok, blocker} = User.follow(blocker, blocked)
771 assert User.following?(blocker, blocked)
772 refute User.following?(blocked, blocker)
774 {:ok, blocker} = User.block(blocker, blocked)
775 blocked = User.get_cached_by_id(blocked.id)
777 assert User.blocks?(blocker, blocked)
779 refute User.following?(blocker, blocked)
780 refute User.following?(blocked, blocker)
783 test "blocks tear down blocked->blocker follow relationships" do
784 blocker = insert(:user)
785 blocked = insert(:user)
787 {:ok, blocked} = User.follow(blocked, blocker)
789 refute User.following?(blocker, blocked)
790 assert User.following?(blocked, blocker)
792 {:ok, blocker} = User.block(blocker, blocked)
793 blocked = User.get_cached_by_id(blocked.id)
795 assert User.blocks?(blocker, blocked)
797 refute User.following?(blocker, blocked)
798 refute User.following?(blocked, blocker)
801 test "blocks tear down blocked->blocker subscription relationships" do
802 blocker = insert(:user)
803 blocked = insert(:user)
805 {:ok, blocker} = User.subscribe(blocked, blocker)
807 assert User.subscribed_to?(blocked, blocker)
808 refute User.subscribed_to?(blocker, blocked)
810 {:ok, blocker} = User.block(blocker, blocked)
812 assert User.blocks?(blocker, blocked)
813 refute User.subscribed_to?(blocker, blocked)
814 refute User.subscribed_to?(blocked, blocker)
818 describe "domain blocking" do
819 test "blocks domains" do
821 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
823 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
825 assert User.blocks?(user, collateral_user)
828 test "does not block domain with same end" do
832 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
834 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
836 refute User.blocks?(user, collateral_user)
839 test "does not block domain with same end if wildcard added" do
843 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
845 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
847 refute User.blocks?(user, collateral_user)
850 test "blocks domain with wildcard for subdomain" do
853 user_from_subdomain =
854 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
856 user_with_two_subdomains =
858 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
861 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
863 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
865 assert User.blocks?(user, user_from_subdomain)
866 assert User.blocks?(user, user_with_two_subdomains)
867 assert User.blocks?(user, user_domain)
870 test "unblocks domains" do
872 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
874 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
875 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
877 refute User.blocks?(user, collateral_user)
881 describe "blocks_import" do
882 test "it imports user blocks from list" do
883 [user1, user2, user3] = insert_list(3, :user)
890 result = User.blocks_import(user1, identifiers)
891 assert is_list(result)
892 assert result == [user2, user3]
896 test "get recipients from activity" do
897 actor = insert(:user)
898 user = insert(:user, local: true)
899 user_two = insert(:user, local: false)
900 addressed = insert(:user, local: true)
901 addressed_remote = insert(:user, local: false)
904 CommonAPI.post(actor, %{
905 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
908 assert Enum.map([actor, addressed], & &1.ap_id) --
909 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
911 {:ok, user} = User.follow(user, actor)
912 {:ok, _user_two} = User.follow(user_two, actor)
913 recipients = User.get_recipients_from_activity(activity)
914 assert length(recipients) == 3
915 assert user in recipients
916 assert addressed in recipients
919 describe ".deactivate" do
920 test "can de-activate then re-activate a user" do
922 assert false == user.info.deactivated
923 {:ok, user} = User.deactivate(user)
924 assert true == user.info.deactivated
925 {:ok, user} = User.deactivate(user, false)
926 assert false == user.info.deactivated
929 test "hide a user from followers " do
931 user2 = insert(:user)
933 {:ok, user} = User.follow(user, user2)
934 {:ok, _user} = User.deactivate(user)
936 info = User.get_cached_user_info(user2)
938 assert info.follower_count == 0
939 assert {:ok, []} = User.get_followers(user2)
942 test "hide a user from friends" do
944 user2 = insert(:user)
946 {:ok, user2} = User.follow(user2, user)
947 assert User.following_count(user2) == 1
949 {:ok, _user} = User.deactivate(user)
951 info = User.get_cached_user_info(user2)
953 assert info.following_count == 0
954 assert User.following_count(user2) == 0
955 assert {:ok, []} = User.get_friends(user2)
958 test "hide a user's statuses from timelines and notifications" do
960 user2 = insert(:user)
962 {:ok, user2} = User.follow(user2, user)
964 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
966 activity = Repo.preload(activity, :bookmark)
968 [notification] = Pleroma.Notification.for_user(user2)
969 assert notification.activity.id == activity.id
971 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
973 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
974 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
976 {:ok, _user} = User.deactivate(user)
978 assert [] == ActivityPub.fetch_public_activities(%{})
979 assert [] == Pleroma.Notification.for_user(user2)
982 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
988 {:ok, user} = insert(:user) |> User.set_cache()
993 test ".delete_user_activities deletes all create activities", %{user: user} do
994 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
996 {:ok, _} = User.delete_user_activities(user)
998 # TODO: Remove favorites, repeats, delete activities.
999 refute Activity.get_by_id(activity.id)
1002 test "it deletes a user, all follow relationships and all activities", %{user: user} do
1003 follower = insert(:user)
1004 {:ok, follower} = User.follow(follower, user)
1006 object = insert(:note, user: user)
1007 activity = insert(:note_activity, user: user, note: object)
1009 object_two = insert(:note, user: follower)
1010 activity_two = insert(:note_activity, user: follower, note: object_two)
1012 {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
1013 {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
1014 {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
1016 {:ok, _} = User.delete(user)
1018 follower = User.get_cached_by_id(follower.id)
1020 refute User.following?(follower, user)
1021 refute User.get_by_id(user.id)
1022 assert {:ok, nil} == Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1026 |> Activity.query_by_actor()
1028 |> Enum.map(fn act -> act.data["type"] end)
1030 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1032 refute Activity.get_by_id(activity.id)
1033 refute Activity.get_by_id(like.id)
1034 refute Activity.get_by_id(like_two.id)
1035 refute Activity.get_by_id(repeat.id)
1038 test "it sends out User Delete activity", %{user: user} do
1039 config_path = [:instance, :federating]
1040 initial_setting = Pleroma.Config.get(config_path)
1041 Pleroma.Config.put(config_path, true)
1043 {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
1044 {:ok, _} = User.follow(follower, user)
1046 {:ok, _user} = User.delete(user)
1048 assert ObanHelpers.member?(
1050 "op" => "publish_one",
1052 "inbox" => "http://mastodon.example.org/inbox",
1053 "id" => "pleroma:fakeid"
1056 all_enqueued(worker: Pleroma.Workers.Publisher)
1059 Pleroma.Config.put(config_path, initial_setting)
1063 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1064 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1067 test "insert or update a user from given data" do
1068 user = insert(:user, %{nickname: "nick@name.de"})
1069 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
1071 assert {:ok, %User{}} = User.insert_or_update_user(data)
1074 describe "per-user rich-text filtering" do
1075 test "html_filter_policy returns default policies, when rich-text is enabled" do
1076 user = insert(:user)
1078 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1081 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1082 user = insert(:user, %{info: %{no_rich_text: true}})
1084 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1088 describe "caching" do
1089 test "invalidate_cache works" do
1090 user = insert(:user)
1091 _user_info = User.get_cached_user_info(user)
1093 User.invalidate_cache(user)
1095 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1096 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1097 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
1100 test "User.delete() plugs any possible zombie objects" do
1101 user = insert(:user)
1103 {:ok, _} = User.delete(user)
1105 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1107 assert cached_user != user
1109 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1111 assert cached_user != user
1115 test "auth_active?/1 works correctly" do
1116 Pleroma.Config.put([:instance, :account_activation_required], true)
1118 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
1119 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
1120 remote_user = insert(:user, local: false)
1122 refute User.auth_active?(local_user)
1123 assert User.auth_active?(confirmed_user)
1124 assert User.auth_active?(remote_user)
1126 Pleroma.Config.put([:instance, :account_activation_required], false)
1129 describe "superuser?/1" do
1130 test "returns false for unprivileged users" do
1131 user = insert(:user, local: true)
1133 refute User.superuser?(user)
1136 test "returns false for remote users" do
1137 user = insert(:user, local: false)
1138 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1140 refute User.superuser?(user)
1141 refute User.superuser?(remote_admin_user)
1144 test "returns true for local moderators" do
1145 user = insert(:user, local: true, info: %{is_moderator: true})
1147 assert User.superuser?(user)
1150 test "returns true for local admins" do
1151 user = insert(:user, local: true, info: %{is_admin: true})
1153 assert User.superuser?(user)
1157 describe "visible_for?/2" do
1158 test "returns true when the account is itself" do
1159 user = insert(:user, local: true)
1161 assert User.visible_for?(user, user)
1164 test "returns false when the account is unauthenticated and auth is required" do
1165 Pleroma.Config.put([:instance, :account_activation_required], true)
1167 user = insert(:user, local: true, info: %{confirmation_pending: true})
1168 other_user = insert(:user, local: true)
1170 refute User.visible_for?(user, other_user)
1172 Pleroma.Config.put([:instance, :account_activation_required], false)
1175 test "returns true when the account is unauthenticated and auth is not required" do
1176 user = insert(:user, local: true, info: %{confirmation_pending: true})
1177 other_user = insert(:user, local: true)
1179 assert User.visible_for?(user, other_user)
1182 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1183 Pleroma.Config.put([:instance, :account_activation_required], true)
1185 user = insert(:user, local: true, info: %{confirmation_pending: true})
1186 other_user = insert(:user, local: true, info: %{is_admin: true})
1188 assert User.visible_for?(user, other_user)
1190 Pleroma.Config.put([:instance, :account_activation_required], false)
1194 describe "parse_bio/2" do
1195 test "preserves hosts in user links text" do
1196 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1197 user = insert(:user)
1198 bio = "A.k.a. @nick@domain.com"
1201 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1203 }'>@<span>nick@domain.com</span></a></span>"
1205 assert expected_text == User.parse_bio(bio, user)
1208 test "Adds rel=me on linkbacked urls" do
1209 user = insert(:user, ap_id: "http://social.example.org/users/lain")
1211 bio = "http://example.org/rel_me/null"
1212 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1213 assert expected_text == User.parse_bio(bio, user)
1215 bio = "http://example.org/rel_me/link"
1216 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1217 assert expected_text == User.parse_bio(bio, user)
1219 bio = "http://example.org/rel_me/anchor"
1220 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1221 assert expected_text == User.parse_bio(bio, user)
1225 test "follower count is updated when a follower is blocked" do
1226 user = insert(:user)
1227 follower = insert(:user)
1228 follower2 = insert(:user)
1229 follower3 = insert(:user)
1231 {:ok, follower} = User.follow(follower, user)
1232 {:ok, _follower2} = User.follow(follower2, user)
1233 {:ok, _follower3} = User.follow(follower3, user)
1235 {:ok, _} = User.block(user, follower)
1237 user_show = Pleroma.Web.TwitterAPI.UserView.render("show.json", %{user: user})
1239 assert Map.get(user_show, "followers_count") == 2
1242 describe "list_inactive_users_query/1" do
1243 defp days_ago(days) do
1245 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1246 -days * 60 * 60 * 24,
1251 test "Users are inactive by default" do
1255 Enum.map(1..total, fn _ ->
1256 insert(:user, last_digest_emailed_at: days_ago(20), info: %{deactivated: false})
1259 inactive_users_ids =
1260 Pleroma.User.list_inactive_users_query()
1261 |> Pleroma.Repo.all()
1262 |> Enum.map(& &1.id)
1264 Enum.each(users, fn user ->
1265 assert user.id in inactive_users_ids
1269 test "Only includes users who has no recent activity" do
1273 Enum.map(1..total, fn _ ->
1274 insert(:user, last_digest_emailed_at: days_ago(20), info: %{deactivated: false})
1277 {inactive, active} = Enum.split(users, trunc(total / 2))
1279 Enum.map(active, fn user ->
1280 to = Enum.random(users -- [user])
1283 Pleroma.Web.TwitterAPI.TwitterAPI.create_status(user, %{
1284 "status" => "hey @#{to.nickname}"
1288 inactive_users_ids =
1289 Pleroma.User.list_inactive_users_query()
1290 |> Pleroma.Repo.all()
1291 |> Enum.map(& &1.id)
1293 Enum.each(active, fn user ->
1294 refute user.id in inactive_users_ids
1297 Enum.each(inactive, fn user ->
1298 assert user.id in inactive_users_ids
1302 test "Only includes users with no read notifications" do
1306 Enum.map(1..total, fn _ ->
1307 insert(:user, last_digest_emailed_at: days_ago(20), info: %{deactivated: false})
1310 [sender | recipients] = users
1311 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1313 Enum.each(recipients, fn to ->
1315 Pleroma.Web.TwitterAPI.TwitterAPI.create_status(sender, %{
1316 "status" => "hey @#{to.nickname}"
1320 Pleroma.Web.TwitterAPI.TwitterAPI.create_status(sender, %{
1321 "status" => "hey again @#{to.nickname}"
1325 Enum.each(active, fn user ->
1326 [n1, _n2] = Pleroma.Notification.for_user(user)
1327 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1330 inactive_users_ids =
1331 Pleroma.User.list_inactive_users_query()
1332 |> Pleroma.Repo.all()
1333 |> Enum.map(& &1.id)
1335 Enum.each(active, fn user ->
1336 refute user.id in inactive_users_ids
1339 Enum.each(inactive, fn user ->
1340 assert user.id in inactive_users_ids
1345 describe "toggle_confirmation/1" do
1346 test "if user is confirmed" do
1347 user = insert(:user, info: %{confirmation_pending: false})
1348 {:ok, user} = User.toggle_confirmation(user)
1350 assert user.info.confirmation_pending
1351 assert user.info.confirmation_token
1354 test "if user is unconfirmed" do
1355 user = insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
1356 {:ok, user} = User.toggle_confirmation(user)
1358 refute user.info.confirmation_pending
1359 refute user.info.confirmation_token
1363 describe "ensure_keys_present" do
1364 test "it creates keys for a user and stores them in info" do
1365 user = insert(:user)
1366 refute is_binary(user.info.keys)
1367 {:ok, user} = User.ensure_keys_present(user)
1368 assert is_binary(user.info.keys)
1371 test "it doesn't create keys if there already are some" do
1372 user = insert(:user, %{info: %{keys: "xxx"}})
1373 {:ok, user} = User.ensure_keys_present(user)
1374 assert user.info.keys == "xxx"
1378 describe "get_ap_ids_by_nicknames" do
1379 test "it returns a list of AP ids for a given set of nicknames" do
1380 user = insert(:user)
1381 user_two = insert(:user)
1383 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1384 assert length(ap_ids) == 2
1385 assert user.ap_id in ap_ids
1386 assert user_two.ap_id in ap_ids
1390 describe "sync followers count" do
1392 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1393 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1394 insert(:user, local: true)
1395 insert(:user, local: false, info: %{deactivated: true})
1396 {:ok, user1: user1, user2: user2}
1399 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1400 [fdb_user1] = User.external_users(limit: 1)
1402 assert fdb_user1.ap_id
1403 assert fdb_user1.ap_id == user1.ap_id
1404 assert fdb_user1.id == user1.id
1406 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1408 assert fdb_user2.ap_id
1409 assert fdb_user2.ap_id == user2.ap_id
1410 assert fdb_user2.id == user2.id
1412 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1416 describe "set_info_cache/2" do
1418 user = insert(:user)
1422 test "update from args", %{user: user} do
1423 User.set_info_cache(user, %{following_count: 15, follower_count: 18})
1425 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1426 assert followers == 18
1427 assert following == 15
1430 test "without args", %{user: user} do
1431 User.set_info_cache(user, %{})
1433 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1434 assert followers == 0
1435 assert following == 0
1439 describe "user_info/2" do
1441 user = insert(:user)
1445 test "update from args", %{user: user} do
1446 %{follower_count: followers, following_count: following} =
1447 User.user_info(user, %{following_count: 15, follower_count: 18})
1449 assert followers == 18
1450 assert following == 15
1453 test "without args", %{user: user} do
1454 %{follower_count: followers, following_count: following} = User.user_info(user)
1456 assert followers == 0
1457 assert following == 0
1461 describe "is_internal_user?/1" do
1462 test "non-internal user returns false" do
1463 user = insert(:user)
1464 refute User.is_internal_user?(user)
1467 test "user with no nickname returns true" do
1468 user = insert(:user, %{nickname: nil})
1469 assert User.is_internal_user?(user)
1472 test "user with internal-prefixed nickname returns true" do
1473 user = insert(:user, %{nickname: "internal.test"})
1474 assert User.is_internal_user?(user)
1478 describe "update_and_set_cache/1" do
1479 test "returns error when user is stale instead Ecto.StaleEntryError" do
1480 user = insert(:user)
1482 changeset = Ecto.Changeset.change(user, bio: "test")
1486 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1487 User.update_and_set_cache(changeset)
1490 test "performs update cache if user updated" do
1491 user = insert(:user)
1492 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1494 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1496 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1497 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1498 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1502 describe "following/followers synchronization" do
1504 sync = Pleroma.Config.get([:instance, :external_user_synchronization])
1505 on_exit(fn -> Pleroma.Config.put([:instance, :external_user_synchronization], sync) end)
1508 test "updates the counters normally on following/getting a follow when disabled" do
1509 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1510 user = insert(:user)
1515 follower_address: "http://localhost:4001/users/masto_closed/followers",
1516 following_address: "http://localhost:4001/users/masto_closed/following",
1517 info: %{ap_enabled: true}
1520 assert User.user_info(other_user).following_count == 0
1521 assert User.user_info(other_user).follower_count == 0
1523 {:ok, user} = Pleroma.User.follow(user, other_user)
1524 other_user = Pleroma.User.get_by_id(other_user.id)
1526 assert User.user_info(user).following_count == 1
1527 assert User.user_info(other_user).follower_count == 1
1530 test "syncronizes the counters with the remote instance for the followed when enabled" do
1531 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1533 user = insert(:user)
1538 follower_address: "http://localhost:4001/users/masto_closed/followers",
1539 following_address: "http://localhost:4001/users/masto_closed/following",
1540 info: %{ap_enabled: true}
1543 assert User.user_info(other_user).following_count == 0
1544 assert User.user_info(other_user).follower_count == 0
1546 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1547 {:ok, _user} = User.follow(user, other_user)
1548 other_user = User.get_by_id(other_user.id)
1550 assert User.user_info(other_user).follower_count == 437
1553 test "syncronizes the counters with the remote instance for the follower when enabled" do
1554 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1556 user = insert(:user)
1561 follower_address: "http://localhost:4001/users/masto_closed/followers",
1562 following_address: "http://localhost:4001/users/masto_closed/following",
1563 info: %{ap_enabled: true}
1566 assert User.user_info(other_user).following_count == 0
1567 assert User.user_info(other_user).follower_count == 0
1569 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1570 {:ok, other_user} = User.follow(other_user, user)
1572 assert User.user_info(other_user).following_count == 152