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 clear_config([:instance, :account_activation_required])
26 describe "when tags are nil" do
27 test "tagging a user" do
28 user = insert(:user, %{tags: nil})
29 user = User.tag(user, ["cool", "dude"])
31 assert "cool" in user.tags
32 assert "dude" in user.tags
35 test "untagging a user" do
36 user = insert(:user, %{tags: nil})
37 user = User.untag(user, ["cool", "dude"])
39 assert user.tags == []
43 test "ap_id returns the activity pub id for the user" do
44 user = UserBuilder.build()
46 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
48 assert expected_ap_id == User.ap_id(user)
51 test "ap_followers returns the followers collection for the user" do
52 user = UserBuilder.build()
54 expected_followers_collection = "#{User.ap_id(user)}/followers"
56 assert expected_followers_collection == User.ap_followers(user)
59 test "ap_following returns the following collection for the user" do
60 user = UserBuilder.build()
62 expected_followers_collection = "#{User.ap_id(user)}/following"
64 assert expected_followers_collection == User.ap_following(user)
67 test "returns all pending follow requests" do
68 unlocked = insert(:user)
69 locked = insert(:user, %{info: %{locked: true}})
70 follower = insert(:user)
72 CommonAPI.follow(follower, unlocked)
73 CommonAPI.follow(follower, locked)
75 assert {:ok, []} = User.get_follow_requests(unlocked)
76 assert {:ok, [activity]} = User.get_follow_requests(locked)
81 test "doesn't return already accepted or duplicate follow requests" do
82 locked = insert(:user, %{info: %{locked: true}})
83 pending_follower = insert(:user)
84 accepted_follower = insert(:user)
86 CommonAPI.follow(pending_follower, locked)
87 CommonAPI.follow(pending_follower, locked)
88 CommonAPI.follow(accepted_follower, locked)
89 User.follow(accepted_follower, locked)
91 assert {:ok, [activity]} = User.get_follow_requests(locked)
95 test "clears follow requests when requester is blocked" do
96 followed = insert(:user, %{info: %{locked: true}})
97 follower = insert(:user)
99 CommonAPI.follow(follower, followed)
100 assert {:ok, [_activity]} = User.get_follow_requests(followed)
102 {:ok, _follower} = User.block(followed, follower)
103 assert {:ok, []} = User.get_follow_requests(followed)
106 test "follow_all follows mutliple users" do
108 followed_zero = insert(:user)
109 followed_one = insert(:user)
110 followed_two = insert(:user)
111 blocked = insert(:user)
112 not_followed = insert(:user)
113 reverse_blocked = insert(:user)
115 {:ok, user} = User.block(user, blocked)
116 {:ok, reverse_blocked} = User.block(reverse_blocked, user)
118 {:ok, user} = User.follow(user, followed_zero)
120 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
122 assert User.following?(user, followed_one)
123 assert User.following?(user, followed_two)
124 assert User.following?(user, followed_zero)
125 refute User.following?(user, not_followed)
126 refute User.following?(user, blocked)
127 refute User.following?(user, reverse_blocked)
130 test "follow_all follows mutliple users without duplicating" do
132 followed_zero = insert(:user)
133 followed_one = insert(:user)
134 followed_two = insert(:user)
136 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
137 assert length(user.following) == 3
139 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
140 assert length(user.following) == 4
143 test "follow takes a user and another user" do
145 followed = insert(:user)
147 {:ok, user} = User.follow(user, followed)
149 user = User.get_cached_by_id(user.id)
151 followed = User.get_cached_by_ap_id(followed.ap_id)
152 assert followed.info.follower_count == 1
154 assert User.ap_followers(followed) in user.following
157 test "can't follow a deactivated users" do
159 followed = insert(:user, info: %{deactivated: true})
161 {:error, _} = User.follow(user, followed)
164 test "can't follow a user who blocked us" do
165 blocker = insert(:user)
166 blockee = insert(:user)
168 {:ok, blocker} = User.block(blocker, blockee)
170 {:error, _} = User.follow(blockee, blocker)
173 test "can't subscribe to a user who blocked us" do
174 blocker = insert(:user)
175 blocked = insert(:user)
177 {:ok, blocker} = User.block(blocker, blocked)
179 {:error, _} = User.subscribe(blocked, blocker)
182 test "local users do not automatically follow local locked accounts" do
183 follower = insert(:user, info: %{locked: true})
184 followed = insert(:user, info: %{locked: true})
186 {:ok, follower} = User.maybe_direct_follow(follower, followed)
188 refute User.following?(follower, followed)
191 # This is a somewhat useless test.
192 # test "following a remote user will ensure a websub subscription is present" do
193 # user = insert(:user)
194 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
196 # assert followed.local == false
198 # {:ok, user} = User.follow(user, followed)
199 # assert User.ap_followers(followed) in user.following
201 # query = from w in WebsubClientSubscription,
202 # where: w.topic == ^followed.info["topic"]
203 # websub = Repo.one(query)
208 describe "unfollow/2" do
210 setting = Pleroma.Config.get([:instance, :external_user_synchronization])
213 Pleroma.Config.put([:instance, :external_user_synchronization], setting)
219 test "unfollow with syncronizes external user" do
220 Pleroma.Config.put([:instance, :external_user_synchronization], true)
225 follower_address: "http://localhost:4001/users/fuser1/followers",
226 following_address: "http://localhost:4001/users/fuser1/following",
227 ap_id: "http://localhost:4001/users/fuser1"
234 ap_id: "http://localhost:4001/users/fuser2",
235 follower_address: "http://localhost:4001/users/fuser2/followers",
236 following_address: "http://localhost:4001/users/fuser2/following",
237 following: [User.ap_followers(followed)]
240 {:ok, user, _activity} = User.unfollow(user, followed)
242 user = User.get_cached_by_id(user.id)
244 assert user.following == []
247 test "unfollow takes a user and another user" do
248 followed = insert(:user)
249 user = insert(:user, %{following: [User.ap_followers(followed)]})
251 {:ok, user, _activity} = User.unfollow(user, followed)
253 user = User.get_cached_by_id(user.id)
255 assert user.following == []
258 test "unfollow doesn't unfollow yourself" do
261 {:error, _} = User.unfollow(user, user)
263 user = User.get_cached_by_id(user.id)
264 assert user.following == [user.ap_id]
268 test "test if a user is following another user" do
269 followed = insert(:user)
270 user = insert(:user, %{following: [User.ap_followers(followed)]})
272 assert User.following?(user, followed)
273 refute User.following?(followed, user)
276 test "fetches correct profile for nickname beginning with number" do
277 # Use old-style integer ID to try to reproduce the problem
278 user = insert(:user, %{id: 1080})
279 user_with_numbers = insert(:user, %{nickname: "#{user.id}garbage"})
280 assert user_with_numbers == User.get_cached_by_nickname_or_id(user_with_numbers.nickname)
283 describe "user registration" do
289 password_confirmation: "test",
290 email: "email@example.com"
292 clear_config([:instance, :autofollowed_nicknames])
293 clear_config([:instance, :welcome_message])
294 clear_config([:instance, :welcome_user_nickname])
296 test "it autofollows accounts that are set for it" do
298 remote_user = insert(:user, %{local: false})
300 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
305 cng = User.register_changeset(%User{}, @full_user_data)
307 {:ok, registered_user} = User.register(cng)
309 assert User.following?(registered_user, user)
310 refute User.following?(registered_user, remote_user)
313 test "it sends a welcome message if it is set" do
314 welcome_user = insert(:user)
316 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
317 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
319 cng = User.register_changeset(%User{}, @full_user_data)
320 {:ok, registered_user} = User.register(cng)
322 activity = Repo.one(Pleroma.Activity)
323 assert registered_user.ap_id in activity.recipients
324 assert Object.normalize(activity).data["content"] =~ "cool site"
325 assert activity.actor == welcome_user.ap_id
328 test "it requires an email, name, nickname and password, bio is optional" do
331 |> Enum.each(fn key ->
332 params = Map.delete(@full_user_data, key)
333 changeset = User.register_changeset(%User{}, params)
335 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
339 test "it restricts certain nicknames" do
340 [restricted_name | _] = Pleroma.Config.get([User, :restricted_nicknames])
342 assert is_bitstring(restricted_name)
346 |> Map.put(:nickname, restricted_name)
348 changeset = User.register_changeset(%User{}, params)
350 refute changeset.valid?
353 test "it sets the password_hash, ap_id and following fields" do
354 changeset = User.register_changeset(%User{}, @full_user_data)
356 assert changeset.valid?
358 assert is_binary(changeset.changes[:password_hash])
359 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
361 assert changeset.changes[:following] == [
362 User.ap_followers(%User{nickname: @full_user_data.nickname})
365 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
368 test "it ensures info is not nil" do
369 changeset = User.register_changeset(%User{}, @full_user_data)
371 assert changeset.valid?
377 refute is_nil(user.info)
381 describe "user registration, with :account_activation_required" do
387 password_confirmation: "test",
388 email: "email@example.com"
391 clear_config([:instance, :account_activation_required]) do
392 Pleroma.Config.put([:instance, :account_activation_required], true)
395 test "it creates unconfirmed user" do
396 changeset = User.register_changeset(%User{}, @full_user_data)
397 assert changeset.valid?
399 {:ok, user} = Repo.insert(changeset)
401 assert user.info.confirmation_pending
402 assert user.info.confirmation_token
405 test "it creates confirmed user if :confirmed option is given" do
406 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
407 assert changeset.valid?
409 {:ok, user} = Repo.insert(changeset)
411 refute user.info.confirmation_pending
412 refute user.info.confirmation_token
416 describe "get_or_fetch/1" do
417 test "gets an existing user by nickname" do
419 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
421 assert user == fetched_user
424 test "gets an existing user by ap_id" do
425 ap_id = "http://mastodon.example.org/users/admin"
431 nickname: "admin@mastodon.example.org",
436 {:ok, fetched_user} = User.get_or_fetch(ap_id)
437 freshed_user = refresh_record(user)
438 assert freshed_user == fetched_user
442 describe "fetching a user from nickname or trying to build one" do
443 test "gets an existing user" do
445 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
447 assert user == fetched_user
450 test "gets an existing user, case insensitive" do
451 user = insert(:user, nickname: "nick")
452 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
454 assert user == fetched_user
457 test "gets an existing user by fully qualified nickname" do
460 {:ok, fetched_user} =
461 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
463 assert user == fetched_user
466 test "gets an existing user by fully qualified nickname, case insensitive" do
467 user = insert(:user, nickname: "nick")
468 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
470 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
472 assert user == fetched_user
475 test "fetches an external user via ostatus if no user exists" do
476 {:ok, fetched_user} = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
477 assert fetched_user.nickname == "shp@social.heldscal.la"
480 test "returns nil if no user could be fetched" do
481 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
482 assert fetched_user == "not found nonexistant@social.heldscal.la"
485 test "returns nil for nonexistant local user" do
486 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
487 assert fetched_user == "not found nonexistant"
490 test "updates an existing user, if stale" do
491 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
497 nickname: "admin@mastodon.example.org",
498 ap_id: "http://mastodon.example.org/users/admin",
499 last_refreshed_at: a_week_ago,
503 assert orig_user.last_refreshed_at == a_week_ago
505 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
506 assert user.info.source_data["endpoints"]
508 refute user.last_refreshed_at == orig_user.last_refreshed_at
512 test "returns an ap_id for a user" do
515 assert User.ap_id(user) ==
516 Pleroma.Web.Router.Helpers.o_status_url(
517 Pleroma.Web.Endpoint,
523 test "returns an ap_followers link for a user" do
526 assert User.ap_followers(user) ==
527 Pleroma.Web.Router.Helpers.o_status_url(
528 Pleroma.Web.Endpoint,
534 describe "remote user creation changeset" do
540 info: %{some: "info"},
541 avatar: %{some: "avatar"}
544 clear_config([:instance, :user_bio_length])
545 clear_config([:instance, :user_name_length])
547 test "it confirms validity" do
548 cs = User.remote_user_creation(@valid_remote)
552 test "it sets the follower_adress" do
553 cs = User.remote_user_creation(@valid_remote)
554 # remote users get a fake local follower address
555 assert cs.changes.follower_address ==
556 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
559 test "it enforces the fqn format for nicknames" do
560 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
561 assert cs.changes.local == false
562 assert cs.changes.avatar
566 test "it has required fields" do
568 |> Enum.each(fn field ->
569 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
574 test "it restricts some sizes" do
575 bio_limit = Pleroma.Config.get([:instance, :user_bio_length], 5000)
576 name_limit = Pleroma.Config.get([:instance, :user_name_length], 100)
578 [bio: bio_limit, name: name_limit]
579 |> Enum.each(fn {field, size} ->
580 string = String.pad_leading(".", size)
581 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
584 string = String.pad_leading(".", size + 1)
585 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
591 describe "followers and friends" do
592 test "gets all followers for a given user" do
594 follower_one = insert(:user)
595 follower_two = insert(:user)
596 not_follower = insert(:user)
598 {:ok, follower_one} = User.follow(follower_one, user)
599 {:ok, follower_two} = User.follow(follower_two, user)
601 {:ok, res} = User.get_followers(user)
603 assert Enum.member?(res, follower_one)
604 assert Enum.member?(res, follower_two)
605 refute Enum.member?(res, not_follower)
608 test "gets all friends (followed users) for a given user" do
610 followed_one = insert(:user)
611 followed_two = insert(:user)
612 not_followed = insert(:user)
614 {:ok, user} = User.follow(user, followed_one)
615 {:ok, user} = User.follow(user, followed_two)
617 {:ok, res} = User.get_friends(user)
619 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
620 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
621 assert Enum.member?(res, followed_one)
622 assert Enum.member?(res, followed_two)
623 refute Enum.member?(res, not_followed)
627 describe "updating note and follower count" do
628 test "it sets the info->note_count property" do
631 user = User.get_cached_by_ap_id(note.data["actor"])
633 assert user.info.note_count == 0
635 {:ok, user} = User.update_note_count(user)
637 assert user.info.note_count == 1
640 test "it increases the info->note_count property" do
642 user = User.get_cached_by_ap_id(note.data["actor"])
644 assert user.info.note_count == 0
646 {:ok, user} = User.increase_note_count(user)
648 assert user.info.note_count == 1
650 {:ok, user} = User.increase_note_count(user)
652 assert user.info.note_count == 2
655 test "it decreases the info->note_count property" do
657 user = User.get_cached_by_ap_id(note.data["actor"])
659 assert user.info.note_count == 0
661 {:ok, user} = User.increase_note_count(user)
663 assert user.info.note_count == 1
665 {:ok, user} = User.decrease_note_count(user)
667 assert user.info.note_count == 0
669 {:ok, user} = User.decrease_note_count(user)
671 assert user.info.note_count == 0
674 test "it sets the info->follower_count property" do
676 follower = insert(:user)
678 User.follow(follower, user)
680 assert user.info.follower_count == 0
682 {:ok, user} = User.update_follower_count(user)
684 assert user.info.follower_count == 1
688 describe "remove duplicates from following list" do
689 test "it removes duplicates" do
691 follower = insert(:user)
693 {:ok, %User{following: following} = follower} = User.follow(follower, user)
694 assert length(following) == 2
698 |> User.update_changeset(%{following: following ++ following})
701 assert length(follower.following) == 4
703 {:ok, follower} = User.remove_duplicated_following(follower)
704 assert length(follower.following) == 2
707 test "it does nothing when following is uniq" do
709 follower = insert(:user)
711 {:ok, follower} = User.follow(follower, user)
712 assert length(follower.following) == 2
714 {:ok, follower} = User.remove_duplicated_following(follower)
715 assert length(follower.following) == 2
719 describe "follow_import" do
720 test "it imports user followings from list" do
721 [user1, user2, user3] = insert_list(3, :user)
728 result = User.follow_import(user1, identifiers)
729 assert is_list(result)
730 assert result == [user2, user3]
735 test "it mutes people" do
737 muted_user = insert(:user)
739 refute User.mutes?(user, muted_user)
740 refute User.muted_notifications?(user, muted_user)
742 {:ok, user} = User.mute(user, muted_user)
744 assert User.mutes?(user, muted_user)
745 assert User.muted_notifications?(user, muted_user)
748 test "it unmutes users" do
750 muted_user = insert(:user)
752 {:ok, user} = User.mute(user, muted_user)
753 {:ok, user} = User.unmute(user, muted_user)
755 refute User.mutes?(user, muted_user)
756 refute User.muted_notifications?(user, muted_user)
759 test "it mutes user without notifications" do
761 muted_user = insert(:user)
763 refute User.mutes?(user, muted_user)
764 refute User.muted_notifications?(user, muted_user)
766 {:ok, user} = User.mute(user, muted_user, false)
768 assert User.mutes?(user, muted_user)
769 refute User.muted_notifications?(user, muted_user)
774 test "it blocks people" do
776 blocked_user = insert(:user)
778 refute User.blocks?(user, blocked_user)
780 {:ok, user} = User.block(user, blocked_user)
782 assert User.blocks?(user, blocked_user)
785 test "it unblocks users" do
787 blocked_user = insert(:user)
789 {:ok, user} = User.block(user, blocked_user)
790 {:ok, user} = User.unblock(user, blocked_user)
792 refute User.blocks?(user, blocked_user)
795 test "blocks tear down cyclical follow relationships" do
796 blocker = insert(:user)
797 blocked = insert(:user)
799 {:ok, blocker} = User.follow(blocker, blocked)
800 {:ok, blocked} = User.follow(blocked, blocker)
802 assert User.following?(blocker, blocked)
803 assert User.following?(blocked, blocker)
805 {:ok, blocker} = User.block(blocker, blocked)
806 blocked = User.get_cached_by_id(blocked.id)
808 assert User.blocks?(blocker, blocked)
810 refute User.following?(blocker, blocked)
811 refute User.following?(blocked, blocker)
814 test "blocks tear down blocker->blocked follow relationships" do
815 blocker = insert(:user)
816 blocked = insert(:user)
818 {:ok, blocker} = User.follow(blocker, blocked)
820 assert User.following?(blocker, blocked)
821 refute User.following?(blocked, blocker)
823 {:ok, blocker} = User.block(blocker, blocked)
824 blocked = User.get_cached_by_id(blocked.id)
826 assert User.blocks?(blocker, blocked)
828 refute User.following?(blocker, blocked)
829 refute User.following?(blocked, blocker)
832 test "blocks tear down blocked->blocker follow relationships" do
833 blocker = insert(:user)
834 blocked = insert(:user)
836 {:ok, blocked} = User.follow(blocked, blocker)
838 refute User.following?(blocker, blocked)
839 assert User.following?(blocked, blocker)
841 {:ok, blocker} = User.block(blocker, blocked)
842 blocked = User.get_cached_by_id(blocked.id)
844 assert User.blocks?(blocker, blocked)
846 refute User.following?(blocker, blocked)
847 refute User.following?(blocked, blocker)
850 test "blocks tear down blocked->blocker subscription relationships" do
851 blocker = insert(:user)
852 blocked = insert(:user)
854 {:ok, blocker} = User.subscribe(blocked, blocker)
856 assert User.subscribed_to?(blocked, blocker)
857 refute User.subscribed_to?(blocker, blocked)
859 {:ok, blocker} = User.block(blocker, blocked)
861 assert User.blocks?(blocker, blocked)
862 refute User.subscribed_to?(blocker, blocked)
863 refute User.subscribed_to?(blocked, blocker)
867 describe "domain blocking" do
868 test "blocks domains" do
870 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
872 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
874 assert User.blocks?(user, collateral_user)
877 test "does not block domain with same end" do
881 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
883 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
885 refute User.blocks?(user, collateral_user)
888 test "does not block domain with same end if wildcard added" do
892 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
894 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
896 refute User.blocks?(user, collateral_user)
899 test "blocks domain with wildcard for subdomain" do
902 user_from_subdomain =
903 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
905 user_with_two_subdomains =
907 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
910 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
912 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
914 assert User.blocks?(user, user_from_subdomain)
915 assert User.blocks?(user, user_with_two_subdomains)
916 assert User.blocks?(user, user_domain)
919 test "unblocks domains" do
921 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
923 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
924 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
926 refute User.blocks?(user, collateral_user)
930 describe "blocks_import" do
931 test "it imports user blocks from list" do
932 [user1, user2, user3] = insert_list(3, :user)
939 result = User.blocks_import(user1, identifiers)
940 assert is_list(result)
941 assert result == [user2, user3]
945 test "get recipients from activity" do
946 actor = insert(:user)
947 user = insert(:user, local: true)
948 user_two = insert(:user, local: false)
949 addressed = insert(:user, local: true)
950 addressed_remote = insert(:user, local: false)
953 CommonAPI.post(actor, %{
954 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
957 assert Enum.map([actor, addressed], & &1.ap_id) --
958 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
960 {:ok, user} = User.follow(user, actor)
961 {:ok, _user_two} = User.follow(user_two, actor)
962 recipients = User.get_recipients_from_activity(activity)
963 assert length(recipients) == 3
964 assert user in recipients
965 assert addressed in recipients
968 describe ".deactivate" do
969 test "can de-activate then re-activate a user" do
971 assert false == user.info.deactivated
972 {:ok, user} = User.deactivate(user)
973 assert true == user.info.deactivated
974 {:ok, user} = User.deactivate(user, false)
975 assert false == user.info.deactivated
978 test "hide a user from followers " do
980 user2 = insert(:user)
982 {:ok, user} = User.follow(user, user2)
983 {:ok, _user} = User.deactivate(user)
985 info = User.get_cached_user_info(user2)
987 assert info.follower_count == 0
988 assert {:ok, []} = User.get_followers(user2)
991 test "hide a user from friends" do
993 user2 = insert(:user)
995 {:ok, user2} = User.follow(user2, user)
996 assert User.following_count(user2) == 1
998 {:ok, _user} = User.deactivate(user)
1000 info = User.get_cached_user_info(user2)
1002 assert info.following_count == 0
1003 assert User.following_count(user2) == 0
1004 assert {:ok, []} = User.get_friends(user2)
1007 test "hide a user's statuses from timelines and notifications" do
1008 user = insert(:user)
1009 user2 = insert(:user)
1011 {:ok, user2} = User.follow(user2, user)
1013 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
1015 activity = Repo.preload(activity, :bookmark)
1017 [notification] = Pleroma.Notification.for_user(user2)
1018 assert notification.activity.id == activity.id
1020 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
1022 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
1023 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
1025 {:ok, _user} = User.deactivate(user)
1027 assert [] == ActivityPub.fetch_public_activities(%{})
1028 assert [] == Pleroma.Notification.for_user(user2)
1031 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
1035 describe "delete" do
1037 {:ok, user} = insert(:user) |> User.set_cache()
1042 clear_config([:instance, :federating])
1044 test ".delete_user_activities deletes all create activities", %{user: user} do
1045 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
1047 {:ok, _} = User.delete_user_activities(user)
1049 # TODO: Remove favorites, repeats, delete activities.
1050 refute Activity.get_by_id(activity.id)
1053 test "it deletes deactivated user" do
1054 {:ok, user} = insert(:user, info: %{deactivated: true}) |> User.set_cache()
1056 assert {:ok, _} = User.delete(user)
1057 refute User.get_by_id(user.id)
1060 test "it deletes a user, all follow relationships and all activities", %{user: user} do
1061 follower = insert(:user)
1062 {:ok, follower} = User.follow(follower, user)
1064 object = insert(:note, user: user)
1065 activity = insert(:note_activity, user: user, note: object)
1067 object_two = insert(:note, user: follower)
1068 activity_two = insert(:note_activity, user: follower, note: object_two)
1070 {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
1071 {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
1072 {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
1074 {:ok, _} = User.delete(user)
1076 follower = User.get_cached_by_id(follower.id)
1078 refute User.following?(follower, user)
1079 refute User.get_by_id(user.id)
1080 assert {:ok, nil} == Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1084 |> Activity.Queries.by_actor()
1086 |> Enum.map(fn act -> act.data["type"] end)
1088 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1090 refute Activity.get_by_id(activity.id)
1091 refute Activity.get_by_id(like.id)
1092 refute Activity.get_by_id(like_two.id)
1093 refute Activity.get_by_id(repeat.id)
1096 test_with_mock "it sends out User Delete activity",
1098 Pleroma.Web.ActivityPub.Publisher,
1101 Pleroma.Config.put([:instance, :federating], true)
1103 {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
1104 {:ok, _} = User.follow(follower, user)
1106 {:ok, _user} = User.delete(user)
1109 Pleroma.Web.ActivityPub.Publisher.publish_one(%{
1110 inbox: "http://mastodon.example.org/inbox"
1116 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1117 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1120 test "insert or update a user from given 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 describe "per-user rich-text filtering" do
1128 test "html_filter_policy returns default policies, when rich-text is enabled" do
1129 user = insert(:user)
1131 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1134 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1135 user = insert(:user, %{info: %{no_rich_text: true}})
1137 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1141 describe "caching" do
1142 test "invalidate_cache works" do
1143 user = insert(:user)
1144 _user_info = User.get_cached_user_info(user)
1146 User.invalidate_cache(user)
1148 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1149 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1150 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
1153 test "User.delete() plugs any possible zombie objects" do
1154 user = insert(:user)
1156 {:ok, _} = User.delete(user)
1158 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1160 assert cached_user != user
1162 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1164 assert cached_user != user
1168 test "auth_active?/1 works correctly" do
1169 Pleroma.Config.put([:instance, :account_activation_required], true)
1171 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
1172 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
1173 remote_user = insert(:user, local: false)
1175 refute User.auth_active?(local_user)
1176 assert User.auth_active?(confirmed_user)
1177 assert User.auth_active?(remote_user)
1180 describe "superuser?/1" do
1181 test "returns false for unprivileged users" do
1182 user = insert(:user, local: true)
1184 refute User.superuser?(user)
1187 test "returns false for remote users" do
1188 user = insert(:user, local: false)
1189 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1191 refute User.superuser?(user)
1192 refute User.superuser?(remote_admin_user)
1195 test "returns true for local moderators" do
1196 user = insert(:user, local: true, info: %{is_moderator: true})
1198 assert User.superuser?(user)
1201 test "returns true for local admins" do
1202 user = insert(:user, local: true, info: %{is_admin: true})
1204 assert User.superuser?(user)
1208 describe "visible_for?/2" do
1209 test "returns true when the account is itself" do
1210 user = insert(:user, local: true)
1212 assert User.visible_for?(user, user)
1215 test "returns false when the account is unauthenticated and auth is required" do
1216 Pleroma.Config.put([:instance, :account_activation_required], true)
1218 user = insert(:user, local: true, info: %{confirmation_pending: true})
1219 other_user = insert(:user, local: true)
1221 refute User.visible_for?(user, other_user)
1224 test "returns true when the account is unauthenticated and auth is not required" do
1225 user = insert(:user, local: true, info: %{confirmation_pending: true})
1226 other_user = insert(:user, local: true)
1228 assert User.visible_for?(user, other_user)
1231 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1232 Pleroma.Config.put([:instance, :account_activation_required], true)
1234 user = insert(:user, local: true, info: %{confirmation_pending: true})
1235 other_user = insert(:user, local: true, info: %{is_admin: true})
1237 assert User.visible_for?(user, other_user)
1241 describe "parse_bio/2" do
1242 test "preserves hosts in user links text" do
1243 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1244 user = insert(:user)
1245 bio = "A.k.a. @nick@domain.com"
1248 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1250 }'>@<span>nick@domain.com</span></a></span>"
1252 assert expected_text == User.parse_bio(bio, user)
1255 test "Adds rel=me on linkbacked urls" do
1256 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1258 bio = "http://example.com/rel_me/null"
1259 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1260 assert expected_text == User.parse_bio(bio, user)
1262 bio = "http://example.com/rel_me/link"
1263 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1264 assert expected_text == User.parse_bio(bio, user)
1266 bio = "http://example.com/rel_me/anchor"
1267 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1268 assert expected_text == User.parse_bio(bio, user)
1272 test "follower count is updated when a follower is blocked" do
1273 user = insert(:user)
1274 follower = insert(:user)
1275 follower2 = insert(:user)
1276 follower3 = insert(:user)
1278 {:ok, follower} = User.follow(follower, user)
1279 {:ok, _follower2} = User.follow(follower2, user)
1280 {:ok, _follower3} = User.follow(follower3, user)
1282 {:ok, user} = User.block(user, follower)
1284 assert User.user_info(user).follower_count == 2
1287 describe "list_inactive_users_query/1" do
1288 defp days_ago(days) do
1290 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1291 -days * 60 * 60 * 24,
1296 test "Users are inactive by default" do
1300 Enum.map(1..total, fn _ ->
1301 insert(:user, last_digest_emailed_at: days_ago(20), info: %{deactivated: false})
1304 inactive_users_ids =
1305 Pleroma.User.list_inactive_users_query()
1306 |> Pleroma.Repo.all()
1307 |> Enum.map(& &1.id)
1309 Enum.each(users, fn user ->
1310 assert user.id in inactive_users_ids
1314 test "Only includes users who has no recent activity" do
1318 Enum.map(1..total, fn _ ->
1319 insert(:user, last_digest_emailed_at: days_ago(20), info: %{deactivated: false})
1322 {inactive, active} = Enum.split(users, trunc(total / 2))
1324 Enum.map(active, fn user ->
1325 to = Enum.random(users -- [user])
1328 CommonAPI.post(user, %{
1329 "status" => "hey @#{to.nickname}"
1333 inactive_users_ids =
1334 Pleroma.User.list_inactive_users_query()
1335 |> Pleroma.Repo.all()
1336 |> Enum.map(& &1.id)
1338 Enum.each(active, fn user ->
1339 refute user.id in inactive_users_ids
1342 Enum.each(inactive, fn user ->
1343 assert user.id in inactive_users_ids
1347 test "Only includes users with no read notifications" do
1351 Enum.map(1..total, fn _ ->
1352 insert(:user, last_digest_emailed_at: days_ago(20), info: %{deactivated: false})
1355 [sender | recipients] = users
1356 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1358 Enum.each(recipients, fn to ->
1360 CommonAPI.post(sender, %{
1361 "status" => "hey @#{to.nickname}"
1365 CommonAPI.post(sender, %{
1366 "status" => "hey again @#{to.nickname}"
1370 Enum.each(active, fn user ->
1371 [n1, _n2] = Pleroma.Notification.for_user(user)
1372 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1375 inactive_users_ids =
1376 Pleroma.User.list_inactive_users_query()
1377 |> Pleroma.Repo.all()
1378 |> Enum.map(& &1.id)
1380 Enum.each(active, fn user ->
1381 refute user.id in inactive_users_ids
1384 Enum.each(inactive, fn user ->
1385 assert user.id in inactive_users_ids
1390 describe "toggle_confirmation/1" do
1391 test "if user is confirmed" do
1392 user = insert(:user, info: %{confirmation_pending: false})
1393 {:ok, user} = User.toggle_confirmation(user)
1395 assert user.info.confirmation_pending
1396 assert user.info.confirmation_token
1399 test "if user is unconfirmed" do
1400 user = insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
1401 {:ok, user} = User.toggle_confirmation(user)
1403 refute user.info.confirmation_pending
1404 refute user.info.confirmation_token
1408 describe "ensure_keys_present" do
1409 test "it creates keys for a user and stores them in info" do
1410 user = insert(:user)
1411 refute is_binary(user.info.keys)
1412 {:ok, user} = User.ensure_keys_present(user)
1413 assert is_binary(user.info.keys)
1416 test "it doesn't create keys if there already are some" do
1417 user = insert(:user, %{info: %{keys: "xxx"}})
1418 {:ok, user} = User.ensure_keys_present(user)
1419 assert user.info.keys == "xxx"
1423 describe "get_ap_ids_by_nicknames" do
1424 test "it returns a list of AP ids for a given set of nicknames" do
1425 user = insert(:user)
1426 user_two = insert(:user)
1428 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1429 assert length(ap_ids) == 2
1430 assert user.ap_id in ap_ids
1431 assert user_two.ap_id in ap_ids
1435 describe "sync followers count" do
1437 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1438 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1439 insert(:user, local: true)
1440 insert(:user, local: false, info: %{deactivated: true})
1441 {:ok, user1: user1, user2: user2}
1444 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1445 [fdb_user1] = User.external_users(limit: 1)
1447 assert fdb_user1.ap_id
1448 assert fdb_user1.ap_id == user1.ap_id
1449 assert fdb_user1.id == user1.id
1451 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1453 assert fdb_user2.ap_id
1454 assert fdb_user2.ap_id == user2.ap_id
1455 assert fdb_user2.id == user2.id
1457 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1461 describe "set_info_cache/2" do
1463 user = insert(:user)
1467 test "update from args", %{user: user} do
1468 User.set_info_cache(user, %{following_count: 15, follower_count: 18})
1470 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1471 assert followers == 18
1472 assert following == 15
1475 test "without args", %{user: user} do
1476 User.set_info_cache(user, %{})
1478 %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
1479 assert followers == 0
1480 assert following == 0
1484 describe "user_info/2" do
1486 user = insert(:user)
1490 test "update from args", %{user: user} do
1491 %{follower_count: followers, following_count: following} =
1492 User.user_info(user, %{following_count: 15, follower_count: 18})
1494 assert followers == 18
1495 assert following == 15
1498 test "without args", %{user: user} do
1499 %{follower_count: followers, following_count: following} = User.user_info(user)
1501 assert followers == 0
1502 assert following == 0
1506 describe "is_internal_user?/1" do
1507 test "non-internal user returns false" do
1508 user = insert(:user)
1509 refute User.is_internal_user?(user)
1512 test "user with no nickname returns true" do
1513 user = insert(:user, %{nickname: nil})
1514 assert User.is_internal_user?(user)
1517 test "user with internal-prefixed nickname returns true" do
1518 user = insert(:user, %{nickname: "internal.test"})
1519 assert User.is_internal_user?(user)
1523 describe "update_and_set_cache/1" do
1524 test "returns error when user is stale instead Ecto.StaleEntryError" do
1525 user = insert(:user)
1527 changeset = Ecto.Changeset.change(user, bio: "test")
1531 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1532 User.update_and_set_cache(changeset)
1535 test "performs update cache if user updated" do
1536 user = insert(:user)
1537 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1539 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1541 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1542 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1543 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1547 describe "following/followers synchronization" do
1548 clear_config([:instance, :external_user_synchronization])
1550 test "updates the counters normally on following/getting a follow when disabled" do
1551 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1552 user = insert(:user)
1557 follower_address: "http://localhost:4001/users/masto_closed/followers",
1558 following_address: "http://localhost:4001/users/masto_closed/following",
1559 info: %{ap_enabled: true}
1562 assert User.user_info(other_user).following_count == 0
1563 assert User.user_info(other_user).follower_count == 0
1565 {:ok, user} = Pleroma.User.follow(user, other_user)
1566 other_user = Pleroma.User.get_by_id(other_user.id)
1568 assert User.user_info(user).following_count == 1
1569 assert User.user_info(other_user).follower_count == 1
1572 test "syncronizes the counters with the remote instance for the followed when enabled" do
1573 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1575 user = insert(:user)
1580 follower_address: "http://localhost:4001/users/masto_closed/followers",
1581 following_address: "http://localhost:4001/users/masto_closed/following",
1582 info: %{ap_enabled: true}
1585 assert User.user_info(other_user).following_count == 0
1586 assert User.user_info(other_user).follower_count == 0
1588 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1589 {:ok, _user} = User.follow(user, other_user)
1590 other_user = User.get_by_id(other_user.id)
1592 assert User.user_info(other_user).follower_count == 437
1595 test "syncronizes the counters with the remote instance for the follower when enabled" do
1596 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1598 user = insert(:user)
1603 follower_address: "http://localhost:4001/users/masto_closed/followers",
1604 following_address: "http://localhost:4001/users/masto_closed/following",
1605 info: %{ap_enabled: true}
1608 assert User.user_info(other_user).following_count == 0
1609 assert User.user_info(other_user).follower_count == 0
1611 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1612 {:ok, other_user} = User.follow(other_user, user)
1614 assert User.user_info(other_user).following_count == 152
1618 describe "change_email/2" do
1620 [user: insert(:user)]
1623 test "blank email returns error", %{user: user} do
1624 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1625 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1628 test "non unique email returns error", %{user: user} do
1629 %{email: email} = insert(:user)
1631 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1632 User.change_email(user, email)
1635 test "invalid email returns error", %{user: user} do
1636 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1637 User.change_email(user, "cofe")
1640 test "changes email", %{user: user} do
1641 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")