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
19 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 describe "when tags are nil" do
24 test "tagging a user" do
25 user = insert(:user, %{tags: nil})
26 user = User.tag(user, ["cool", "dude"])
28 assert "cool" in user.tags
29 assert "dude" in user.tags
32 test "untagging a user" do
33 user = insert(:user, %{tags: nil})
34 user = User.untag(user, ["cool", "dude"])
36 assert user.tags == []
40 test "ap_id returns the activity pub id for the user" do
41 user = UserBuilder.build()
43 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
45 assert expected_ap_id == User.ap_id(user)
48 test "ap_followers returns the followers collection for the user" do
49 user = UserBuilder.build()
51 expected_followers_collection = "#{User.ap_id(user)}/followers"
53 assert expected_followers_collection == User.ap_followers(user)
56 test "returns all pending follow requests" do
57 unlocked = insert(:user)
58 locked = insert(:user, %{info: %{locked: true}})
59 follower = insert(:user)
61 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => unlocked.id})
62 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => locked.id})
64 assert {:ok, []} = User.get_follow_requests(unlocked)
65 assert {:ok, [activity]} = User.get_follow_requests(locked)
70 test "doesn't return already accepted or duplicate follow requests" do
71 locked = insert(:user, %{info: %{locked: true}})
72 pending_follower = insert(:user)
73 accepted_follower = insert(:user)
75 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
76 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
77 Pleroma.Web.TwitterAPI.TwitterAPI.follow(accepted_follower, %{"user_id" => locked.id})
78 User.follow(accepted_follower, locked)
80 assert {:ok, [activity]} = User.get_follow_requests(locked)
84 test "follow_all follows mutliple users" do
86 followed_zero = insert(:user)
87 followed_one = insert(:user)
88 followed_two = insert(:user)
89 blocked = insert(:user)
90 not_followed = insert(:user)
91 reverse_blocked = insert(:user)
93 {:ok, user} = User.block(user, blocked)
94 {:ok, reverse_blocked} = User.block(reverse_blocked, user)
96 {:ok, user} = User.follow(user, followed_zero)
98 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
100 assert User.following?(user, followed_one)
101 assert User.following?(user, followed_two)
102 assert User.following?(user, followed_zero)
103 refute User.following?(user, not_followed)
104 refute User.following?(user, blocked)
105 refute User.following?(user, reverse_blocked)
108 test "follow_all follows mutliple users without duplicating" do
110 followed_zero = insert(:user)
111 followed_one = insert(:user)
112 followed_two = insert(:user)
114 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
115 assert length(user.following) == 3
117 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
118 assert length(user.following) == 4
121 test "follow takes a user and another user" do
123 followed = insert(:user)
125 {:ok, user} = User.follow(user, followed)
127 user = User.get_cached_by_id(user.id)
129 followed = User.get_cached_by_ap_id(followed.ap_id)
130 assert followed.info.follower_count == 1
132 assert User.ap_followers(followed) in user.following
135 test "can't follow a deactivated users" do
137 followed = insert(:user, info: %{deactivated: true})
139 {:error, _} = User.follow(user, followed)
142 test "can't follow a user who blocked us" do
143 blocker = insert(:user)
144 blockee = insert(:user)
146 {:ok, blocker} = User.block(blocker, blockee)
148 {:error, _} = User.follow(blockee, blocker)
151 test "can't subscribe to a user who blocked us" do
152 blocker = insert(:user)
153 blocked = insert(:user)
155 {:ok, blocker} = User.block(blocker, blocked)
157 {:error, _} = User.subscribe(blocked, blocker)
160 test "local users do not automatically follow local locked accounts" do
161 follower = insert(:user, info: %{locked: true})
162 followed = insert(:user, info: %{locked: true})
164 {:ok, follower} = User.maybe_direct_follow(follower, followed)
166 refute User.following?(follower, followed)
169 # This is a somewhat useless test.
170 # test "following a remote user will ensure a websub subscription is present" do
171 # user = insert(:user)
172 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
174 # assert followed.local == false
176 # {:ok, user} = User.follow(user, followed)
177 # assert User.ap_followers(followed) in user.following
179 # query = from w in WebsubClientSubscription,
180 # where: w.topic == ^followed.info["topic"]
181 # websub = Repo.one(query)
186 test "unfollow takes a user and another user" do
187 followed = insert(:user)
188 user = insert(:user, %{following: [User.ap_followers(followed)]})
190 {:ok, user, _activity} = User.unfollow(user, followed)
192 user = User.get_cached_by_id(user.id)
194 assert user.following == []
197 test "unfollow doesn't unfollow yourself" do
200 {:error, _} = User.unfollow(user, user)
202 user = User.get_cached_by_id(user.id)
203 assert user.following == [user.ap_id]
206 test "test if a user is following another user" do
207 followed = insert(:user)
208 user = insert(:user, %{following: [User.ap_followers(followed)]})
210 assert User.following?(user, followed)
211 refute User.following?(followed, user)
214 test "fetches correct profile for nickname beginning with number" do
215 # Use old-style integer ID to try to reproduce the problem
216 user = insert(:user, %{id: 1080})
217 user_with_numbers = insert(:user, %{nickname: "#{user.id}garbage"})
218 assert user_with_numbers == User.get_cached_by_nickname_or_id(user_with_numbers.nickname)
221 describe "user registration" do
227 password_confirmation: "test",
228 email: "email@example.com"
231 test "it autofollows accounts that are set for it" do
233 remote_user = insert(:user, %{local: false})
235 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
240 cng = User.register_changeset(%User{}, @full_user_data)
242 {:ok, registered_user} = User.register(cng)
244 assert User.following?(registered_user, user)
245 refute User.following?(registered_user, remote_user)
247 Pleroma.Config.put([:instance, :autofollowed_nicknames], [])
250 test "it sends a welcome message if it is set" do
251 welcome_user = insert(:user)
253 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
254 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
256 cng = User.register_changeset(%User{}, @full_user_data)
257 {:ok, registered_user} = User.register(cng)
259 activity = Repo.one(Pleroma.Activity)
260 assert registered_user.ap_id in activity.recipients
261 assert Object.normalize(activity).data["content"] =~ "cool site"
262 assert activity.actor == welcome_user.ap_id
264 Pleroma.Config.put([:instance, :welcome_user_nickname], nil)
265 Pleroma.Config.put([:instance, :welcome_message], nil)
268 test "it requires an email, name, nickname and password, bio is optional" do
271 |> Enum.each(fn key ->
272 params = Map.delete(@full_user_data, key)
273 changeset = User.register_changeset(%User{}, params)
275 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
279 test "it restricts certain nicknames" do
280 [restricted_name | _] = Pleroma.Config.get([User, :restricted_nicknames])
282 assert is_bitstring(restricted_name)
286 |> Map.put(:nickname, restricted_name)
288 changeset = User.register_changeset(%User{}, params)
290 refute changeset.valid?
293 test "it sets the password_hash, ap_id and following fields" do
294 changeset = User.register_changeset(%User{}, @full_user_data)
296 assert changeset.valid?
298 assert is_binary(changeset.changes[:password_hash])
299 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
301 assert changeset.changes[:following] == [
302 User.ap_followers(%User{nickname: @full_user_data.nickname})
305 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
308 test "it ensures info is not nil" do
309 changeset = User.register_changeset(%User{}, @full_user_data)
311 assert changeset.valid?
317 refute is_nil(user.info)
321 describe "user registration, with :account_activation_required" do
327 password_confirmation: "test",
328 email: "email@example.com"
332 setting = Pleroma.Config.get([:instance, :account_activation_required])
335 Pleroma.Config.put([:instance, :account_activation_required], true)
336 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
342 test "it creates unconfirmed user" do
343 changeset = User.register_changeset(%User{}, @full_user_data)
344 assert changeset.valid?
346 {:ok, user} = Repo.insert(changeset)
348 assert user.info.confirmation_pending
349 assert user.info.confirmation_token
352 test "it creates confirmed user if :confirmed option is given" do
353 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
354 assert changeset.valid?
356 {:ok, user} = Repo.insert(changeset)
358 refute user.info.confirmation_pending
359 refute user.info.confirmation_token
363 describe "get_or_fetch/1" do
364 test "gets an existing user by nickname" do
366 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
368 assert user == fetched_user
371 test "gets an existing user by ap_id" do
372 ap_id = "http://mastodon.example.org/users/admin"
378 nickname: "admin@mastodon.example.org",
383 {:ok, fetched_user} = User.get_or_fetch(ap_id)
384 freshed_user = refresh_record(user)
385 assert freshed_user == fetched_user
389 describe "fetching a user from nickname or trying to build one" do
390 test "gets an existing user" do
392 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
394 assert user == fetched_user
397 test "gets an existing user, case insensitive" do
398 user = insert(:user, nickname: "nick")
399 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
401 assert user == fetched_user
404 test "gets an existing user by fully qualified nickname" do
407 {:ok, fetched_user} =
408 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
410 assert user == fetched_user
413 test "gets an existing user by fully qualified nickname, case insensitive" do
414 user = insert(:user, nickname: "nick")
415 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
417 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
419 assert user == fetched_user
422 test "fetches an external user via ostatus if no user exists" do
423 {:ok, fetched_user} = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
424 assert fetched_user.nickname == "shp@social.heldscal.la"
427 test "returns nil if no user could be fetched" do
428 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
429 assert fetched_user == "not found nonexistant@social.heldscal.la"
432 test "returns nil for nonexistant local user" do
433 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
434 assert fetched_user == "not found nonexistant"
437 test "updates an existing user, if stale" do
438 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
444 nickname: "admin@mastodon.example.org",
445 ap_id: "http://mastodon.example.org/users/admin",
446 last_refreshed_at: a_week_ago,
450 assert orig_user.last_refreshed_at == a_week_ago
452 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
453 assert user.info.source_data["endpoints"]
455 refute user.last_refreshed_at == orig_user.last_refreshed_at
459 test "returns an ap_id for a user" do
462 assert User.ap_id(user) ==
463 Pleroma.Web.Router.Helpers.o_status_url(
464 Pleroma.Web.Endpoint,
470 test "returns an ap_followers link for a user" do
473 assert User.ap_followers(user) ==
474 Pleroma.Web.Router.Helpers.o_status_url(
475 Pleroma.Web.Endpoint,
481 describe "remote user creation changeset" do
487 info: %{some: "info"},
488 avatar: %{some: "avatar"}
491 test "it confirms validity" do
492 cs = User.remote_user_creation(@valid_remote)
496 test "it sets the follower_adress" do
497 cs = User.remote_user_creation(@valid_remote)
498 # remote users get a fake local follower address
499 assert cs.changes.follower_address ==
500 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
503 test "it enforces the fqn format for nicknames" do
504 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
505 assert cs.changes.local == false
506 assert cs.changes.avatar
510 test "it has required fields" do
512 |> Enum.each(fn field ->
513 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
518 test "it restricts some sizes" do
519 [bio: 5000, name: 100]
520 |> Enum.each(fn {field, size} ->
521 string = String.pad_leading(".", size)
522 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
525 string = String.pad_leading(".", size + 1)
526 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
532 describe "followers and friends" do
533 test "gets all followers for a given user" do
535 follower_one = insert(:user)
536 follower_two = insert(:user)
537 not_follower = insert(:user)
539 {:ok, follower_one} = User.follow(follower_one, user)
540 {:ok, follower_two} = User.follow(follower_two, user)
542 {:ok, res} = User.get_followers(user)
544 assert Enum.member?(res, follower_one)
545 assert Enum.member?(res, follower_two)
546 refute Enum.member?(res, not_follower)
549 test "gets all friends (followed users) for a given user" do
551 followed_one = insert(:user)
552 followed_two = insert(:user)
553 not_followed = insert(:user)
555 {:ok, user} = User.follow(user, followed_one)
556 {:ok, user} = User.follow(user, followed_two)
558 {:ok, res} = User.get_friends(user)
560 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
561 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
562 assert Enum.member?(res, followed_one)
563 assert Enum.member?(res, followed_two)
564 refute Enum.member?(res, not_followed)
568 describe "updating note and follower count" do
569 test "it sets the info->note_count property" do
572 user = User.get_cached_by_ap_id(note.data["actor"])
574 assert user.info.note_count == 0
576 {:ok, user} = User.update_note_count(user)
578 assert user.info.note_count == 1
581 test "it increases the info->note_count property" do
583 user = User.get_cached_by_ap_id(note.data["actor"])
585 assert user.info.note_count == 0
587 {:ok, user} = User.increase_note_count(user)
589 assert user.info.note_count == 1
591 {:ok, user} = User.increase_note_count(user)
593 assert user.info.note_count == 2
596 test "it decreases the info->note_count property" do
598 user = User.get_cached_by_ap_id(note.data["actor"])
600 assert user.info.note_count == 0
602 {:ok, user} = User.increase_note_count(user)
604 assert user.info.note_count == 1
606 {:ok, user} = User.decrease_note_count(user)
608 assert user.info.note_count == 0
610 {:ok, user} = User.decrease_note_count(user)
612 assert user.info.note_count == 0
615 test "it sets the info->follower_count property" do
617 follower = insert(:user)
619 User.follow(follower, user)
621 assert user.info.follower_count == 0
623 {:ok, user} = User.update_follower_count(user)
625 assert user.info.follower_count == 1
629 describe "remove duplicates from following list" do
630 test "it removes duplicates" do
632 follower = insert(:user)
634 {:ok, %User{following: following} = follower} = User.follow(follower, user)
635 assert length(following) == 2
639 |> User.update_changeset(%{following: following ++ following})
642 assert length(follower.following) == 4
644 {:ok, follower} = User.remove_duplicated_following(follower)
645 assert length(follower.following) == 2
648 test "it does nothing when following is uniq" do
650 follower = insert(:user)
652 {:ok, follower} = User.follow(follower, user)
653 assert length(follower.following) == 2
655 {:ok, follower} = User.remove_duplicated_following(follower)
656 assert length(follower.following) == 2
660 describe "follow_import" do
661 test "it imports user followings from list" do
662 [user1, user2, user3] = insert_list(3, :user)
669 result = User.follow_import(user1, identifiers)
670 assert is_list(result)
671 assert result == [user2, user3]
676 test "it mutes people" do
678 muted_user = insert(:user)
680 refute User.mutes?(user, muted_user)
682 {:ok, user} = User.mute(user, muted_user)
684 assert User.mutes?(user, muted_user)
687 test "it unmutes users" do
689 muted_user = insert(:user)
691 {:ok, user} = User.mute(user, muted_user)
692 {:ok, user} = User.unmute(user, muted_user)
694 refute User.mutes?(user, muted_user)
699 test "it blocks people" do
701 blocked_user = insert(:user)
703 refute User.blocks?(user, blocked_user)
705 {:ok, user} = User.block(user, blocked_user)
707 assert User.blocks?(user, blocked_user)
710 test "it unblocks users" do
712 blocked_user = insert(:user)
714 {:ok, user} = User.block(user, blocked_user)
715 {:ok, user} = User.unblock(user, blocked_user)
717 refute User.blocks?(user, blocked_user)
720 test "blocks tear down cyclical follow relationships" do
721 blocker = insert(:user)
722 blocked = insert(:user)
724 {:ok, blocker} = User.follow(blocker, blocked)
725 {:ok, blocked} = User.follow(blocked, blocker)
727 assert User.following?(blocker, blocked)
728 assert User.following?(blocked, blocker)
730 {:ok, blocker} = User.block(blocker, blocked)
731 blocked = User.get_cached_by_id(blocked.id)
733 assert User.blocks?(blocker, blocked)
735 refute User.following?(blocker, blocked)
736 refute User.following?(blocked, blocker)
739 test "blocks tear down blocker->blocked follow relationships" do
740 blocker = insert(:user)
741 blocked = insert(:user)
743 {:ok, blocker} = User.follow(blocker, blocked)
745 assert User.following?(blocker, blocked)
746 refute User.following?(blocked, blocker)
748 {:ok, blocker} = User.block(blocker, blocked)
749 blocked = User.get_cached_by_id(blocked.id)
751 assert User.blocks?(blocker, blocked)
753 refute User.following?(blocker, blocked)
754 refute User.following?(blocked, blocker)
757 test "blocks tear down blocked->blocker follow relationships" do
758 blocker = insert(:user)
759 blocked = insert(:user)
761 {:ok, blocked} = User.follow(blocked, blocker)
763 refute User.following?(blocker, blocked)
764 assert User.following?(blocked, blocker)
766 {:ok, blocker} = User.block(blocker, blocked)
767 blocked = User.get_cached_by_id(blocked.id)
769 assert User.blocks?(blocker, blocked)
771 refute User.following?(blocker, blocked)
772 refute User.following?(blocked, blocker)
775 test "blocks tear down blocked->blocker subscription relationships" do
776 blocker = insert(:user)
777 blocked = insert(:user)
779 {:ok, blocker} = User.subscribe(blocked, blocker)
781 assert User.subscribed_to?(blocked, blocker)
782 refute User.subscribed_to?(blocker, blocked)
784 {:ok, blocker} = User.block(blocker, blocked)
786 assert User.blocks?(blocker, blocked)
787 refute User.subscribed_to?(blocker, blocked)
788 refute User.subscribed_to?(blocked, blocker)
792 describe "domain blocking" do
793 test "blocks domains" do
795 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
797 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
799 assert User.blocks?(user, collateral_user)
802 test "unblocks domains" do
804 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
806 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
807 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
809 refute User.blocks?(user, collateral_user)
813 describe "blocks_import" do
814 test "it imports user blocks from list" do
815 [user1, user2, user3] = insert_list(3, :user)
822 result = User.blocks_import(user1, identifiers)
823 assert is_list(result)
824 assert result == [user2, user3]
828 test "get recipients from activity" do
829 actor = insert(:user)
830 user = insert(:user, local: true)
831 user_two = insert(:user, local: false)
832 addressed = insert(:user, local: true)
833 addressed_remote = insert(:user, local: false)
836 CommonAPI.post(actor, %{
837 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
840 assert Enum.map([actor, addressed], & &1.ap_id) --
841 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
843 {:ok, user} = User.follow(user, actor)
844 {:ok, _user_two} = User.follow(user_two, actor)
845 recipients = User.get_recipients_from_activity(activity)
846 assert length(recipients) == 3
847 assert user in recipients
848 assert addressed in recipients
851 describe ".deactivate" do
852 test "can de-activate then re-activate a user" do
854 assert false == user.info.deactivated
855 {:ok, user} = User.deactivate(user)
856 assert true == user.info.deactivated
857 {:ok, user} = User.deactivate(user, false)
858 assert false == user.info.deactivated
861 test "hide a user from followers " do
863 user2 = insert(:user)
865 {:ok, user} = User.follow(user, user2)
866 {:ok, _user} = User.deactivate(user)
868 info = User.get_cached_user_info(user2)
870 assert info.follower_count == 0
871 assert {:ok, []} = User.get_followers(user2)
874 test "hide a user from friends" do
876 user2 = insert(:user)
878 {:ok, user2} = User.follow(user2, user)
879 assert User.following_count(user2) == 1
881 {:ok, _user} = User.deactivate(user)
883 info = User.get_cached_user_info(user2)
885 assert info.following_count == 0
886 assert User.following_count(user2) == 0
887 assert {:ok, []} = User.get_friends(user2)
890 test "hide a user's statuses from timelines and notifications" do
892 user2 = insert(:user)
894 {:ok, user2} = User.follow(user2, user)
896 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
898 activity = Repo.preload(activity, :bookmark)
900 [notification] = Pleroma.Notification.for_user(user2)
901 assert notification.activity.id == activity.id
903 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
905 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
906 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
908 {:ok, _user} = User.deactivate(user)
910 assert [] == ActivityPub.fetch_public_activities(%{})
911 assert [] == Pleroma.Notification.for_user(user2)
914 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
918 test ".delete_user_activities deletes all create activities" do
921 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
923 {:ok, _} = User.delete_user_activities(user)
925 # TODO: Remove favorites, repeats, delete activities.
926 refute Activity.get_by_id(activity.id)
929 test ".delete deactivates a user, all follow relationships and all activities" do
931 follower = insert(:user)
933 {:ok, follower} = User.follow(follower, user)
935 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
936 {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
938 {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
939 {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
940 {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
942 {:ok, _} = User.delete(user)
944 follower = User.get_cached_by_id(follower.id)
946 refute User.following?(follower, user)
947 refute User.get_by_id(user.id)
951 |> Activity.query_by_actor()
953 |> Enum.map(fn act -> act.data["type"] end)
955 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
957 refute Activity.get_by_id(activity.id)
958 refute Activity.get_by_id(like.id)
959 refute Activity.get_by_id(like_two.id)
960 refute Activity.get_by_id(repeat.id)
963 test "get_public_key_for_ap_id fetches a user that's not in the db" do
964 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
967 test "insert or update a user from given data" do
968 user = insert(:user, %{nickname: "nick@name.de"})
969 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
971 assert {:ok, %User{}} = User.insert_or_update_user(data)
974 describe "per-user rich-text filtering" do
975 test "html_filter_policy returns default policies, when rich-text is enabled" do
978 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
981 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
982 user = insert(:user, %{info: %{no_rich_text: true}})
984 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
988 describe "caching" do
989 test "invalidate_cache works" do
991 _user_info = User.get_cached_user_info(user)
993 User.invalidate_cache(user)
995 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
996 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
997 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
1000 test "User.delete() plugs any possible zombie objects" do
1001 user = insert(:user)
1003 {:ok, _} = User.delete(user)
1005 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1007 assert cached_user != user
1009 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1011 assert cached_user != user
1015 describe "User.search" do
1016 test "accepts limit parameter" do
1017 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
1018 assert length(User.search("john", limit: 3)) == 3
1019 assert length(User.search("john")) == 5
1022 test "accepts offset parameter" do
1023 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
1024 assert length(User.search("john", limit: 3)) == 3
1025 assert length(User.search("john", limit: 3, offset: 3)) == 2
1028 test "finds a user by full or partial nickname" do
1029 user = insert(:user, %{nickname: "john"})
1031 Enum.each(["john", "jo", "j"], fn query ->
1035 |> Map.put(:search_rank, nil)
1036 |> Map.put(:search_type, nil)
1040 test "finds a user by full or partial name" do
1041 user = insert(:user, %{name: "John Doe"})
1043 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
1047 |> Map.put(:search_rank, nil)
1048 |> Map.put(:search_type, nil)
1052 test "finds users, preferring nickname matches over name matches" do
1053 u1 = insert(:user, %{name: "lain", nickname: "nick1"})
1054 u2 = insert(:user, %{nickname: "lain", name: "nick1"})
1056 assert [u2.id, u1.id] == Enum.map(User.search("lain"), & &1.id)
1059 test "finds users, considering density of matched tokens" do
1060 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
1061 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
1063 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
1066 test "finds users, ranking by similarity" do
1067 u1 = insert(:user, %{name: "lain"})
1068 _u2 = insert(:user, %{name: "ean"})
1069 u3 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
1070 u4 = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
1072 assert [u4.id, u3.id, u1.id] == Enum.map(User.search("lain@ple", for_user: u1), & &1.id)
1075 test "finds users, handling misspelled requests" do
1076 u1 = insert(:user, %{name: "lain"})
1078 assert [u1.id] == Enum.map(User.search("laiin"), & &1.id)
1081 test "finds users, boosting ranks of friends and followers" do
1083 u2 = insert(:user, %{name: "Doe"})
1084 follower = insert(:user, %{name: "Doe"})
1085 friend = insert(:user, %{name: "Doe"})
1087 {:ok, follower} = User.follow(follower, u1)
1088 {:ok, u1} = User.follow(u1, friend)
1090 assert [friend.id, follower.id, u2.id] --
1091 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
1094 test "finds followers of user by partial name" do
1096 u2 = insert(:user, %{name: "Jimi"})
1097 follower_jimi = insert(:user, %{name: "Jimi Hendrix"})
1098 follower_lizz = insert(:user, %{name: "Lizz Wright"})
1099 friend = insert(:user, %{name: "Jimi"})
1101 {:ok, follower_jimi} = User.follow(follower_jimi, u1)
1102 {:ok, _follower_lizz} = User.follow(follower_lizz, u2)
1103 {:ok, u1} = User.follow(u1, friend)
1105 assert Enum.map(User.search("jimi", following: true, for_user: u1), & &1.id) == [
1109 assert User.search("lizz", following: true, for_user: u1) == []
1112 test "find local and remote users for authenticated users" do
1113 u1 = insert(:user, %{name: "lain"})
1114 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
1115 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
1119 |> User.search(for_user: u1)
1120 |> Enum.map(& &1.id)
1123 assert [u1.id, u2.id, u3.id] == results
1126 test "find only local users for unauthenticated users" do
1127 %{id: id} = insert(:user, %{name: "lain"})
1128 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
1129 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
1131 assert [%{id: ^id}] = User.search("lain")
1134 test "find only local users for authenticated users when `limit_to_local_content` is `:all`" do
1135 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
1137 %{id: id} = insert(:user, %{name: "lain"})
1138 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
1139 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
1141 assert [%{id: ^id}] = User.search("lain")
1143 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1146 test "find all users for unauthenticated users when `limit_to_local_content` is `false`" do
1147 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1149 u1 = insert(:user, %{name: "lain"})
1150 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
1151 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
1156 |> Enum.map(& &1.id)
1159 assert [u1.id, u2.id, u3.id] == results
1161 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1164 test "finds a user whose name is nil" do
1165 _user = insert(:user, %{name: "notamatch", nickname: "testuser@pleroma.amplifie.red"})
1166 user_two = insert(:user, %{name: nil, nickname: "lain@pleroma.soykaf.com"})
1169 User.search("lain@pleroma.soykaf.com")
1171 |> Map.put(:search_rank, nil)
1172 |> Map.put(:search_type, nil)
1175 test "does not yield false-positive matches" do
1176 insert(:user, %{name: "John Doe"})
1178 Enum.each(["mary", "a", ""], fn query ->
1179 assert [] == User.search(query)
1183 test "works with URIs" do
1184 user = insert(:user)
1187 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
1189 result = results |> List.first()
1191 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
1193 assert length(results) == 1
1194 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
1198 test "auth_active?/1 works correctly" do
1199 Pleroma.Config.put([:instance, :account_activation_required], true)
1201 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
1202 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
1203 remote_user = insert(:user, local: false)
1205 refute User.auth_active?(local_user)
1206 assert User.auth_active?(confirmed_user)
1207 assert User.auth_active?(remote_user)
1209 Pleroma.Config.put([:instance, :account_activation_required], false)
1212 describe "superuser?/1" do
1213 test "returns false for unprivileged users" do
1214 user = insert(:user, local: true)
1216 refute User.superuser?(user)
1219 test "returns false for remote users" do
1220 user = insert(:user, local: false)
1221 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1223 refute User.superuser?(user)
1224 refute User.superuser?(remote_admin_user)
1227 test "returns true for local moderators" do
1228 user = insert(:user, local: true, info: %{is_moderator: true})
1230 assert User.superuser?(user)
1233 test "returns true for local admins" do
1234 user = insert(:user, local: true, info: %{is_admin: true})
1236 assert User.superuser?(user)
1240 describe "visible_for?/2" do
1241 test "returns true when the account is itself" do
1242 user = insert(:user, local: true)
1244 assert User.visible_for?(user, user)
1247 test "returns false when the account is unauthenticated and auth is required" do
1248 Pleroma.Config.put([:instance, :account_activation_required], true)
1250 user = insert(:user, local: true, info: %{confirmation_pending: true})
1251 other_user = insert(:user, local: true)
1253 refute User.visible_for?(user, other_user)
1255 Pleroma.Config.put([:instance, :account_activation_required], false)
1258 test "returns true when the account is unauthenticated and auth is not required" do
1259 user = insert(:user, local: true, info: %{confirmation_pending: true})
1260 other_user = insert(:user, local: true)
1262 assert User.visible_for?(user, other_user)
1265 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1266 Pleroma.Config.put([:instance, :account_activation_required], true)
1268 user = insert(:user, local: true, info: %{confirmation_pending: true})
1269 other_user = insert(:user, local: true, info: %{is_admin: true})
1271 assert User.visible_for?(user, other_user)
1273 Pleroma.Config.put([:instance, :account_activation_required], false)
1277 describe "parse_bio/2" do
1278 test "preserves hosts in user links text" do
1279 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1280 user = insert(:user)
1281 bio = "A.k.a. @nick@domain.com"
1284 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1286 }'>@<span>nick@domain.com</span></a></span>"
1288 assert expected_text == User.parse_bio(bio, user)
1291 test "Adds rel=me on linkbacked urls" do
1292 user = insert(:user, ap_id: "http://social.example.org/users/lain")
1294 bio = "http://example.org/rel_me/null"
1295 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1296 assert expected_text == User.parse_bio(bio, user)
1298 bio = "http://example.org/rel_me/link"
1299 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1300 assert expected_text == User.parse_bio(bio, user)
1302 bio = "http://example.org/rel_me/anchor"
1303 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1304 assert expected_text == User.parse_bio(bio, user)
1308 test "follower count is updated when a follower is blocked" do
1309 user = insert(:user)
1310 follower = insert(:user)
1311 follower2 = insert(:user)
1312 follower3 = insert(:user)
1314 {:ok, follower} = User.follow(follower, user)
1315 {:ok, _follower2} = User.follow(follower2, user)
1316 {:ok, _follower3} = User.follow(follower3, user)
1318 {:ok, _} = User.block(user, follower)
1320 user_show = Pleroma.Web.TwitterAPI.UserView.render("show.json", %{user: user})
1322 assert Map.get(user_show, "followers_count") == 2
1325 describe "toggle_confirmation/1" do
1326 test "if user is confirmed" do
1327 user = insert(:user, info: %{confirmation_pending: false})
1328 {:ok, user} = User.toggle_confirmation(user)
1330 assert user.info.confirmation_pending
1331 assert user.info.confirmation_token
1334 test "if user is unconfirmed" do
1335 user = insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
1336 {:ok, user} = User.toggle_confirmation(user)
1338 refute user.info.confirmation_pending
1339 refute user.info.confirmation_token
1343 describe "ensure_keys_present" do
1344 test "it creates keys for a user and stores them in info" do
1345 user = insert(:user)
1346 refute is_binary(user.info.keys)
1347 {:ok, user} = User.ensure_keys_present(user)
1348 assert is_binary(user.info.keys)
1351 test "it doesn't create keys if there already are some" do
1352 user = insert(:user, %{info: %{keys: "xxx"}})
1353 {:ok, user} = User.ensure_keys_present(user)
1354 assert user.info.keys == "xxx"
1358 describe "get_ap_ids_by_nicknames" do
1359 test "it returns a list of AP ids for a given set of nicknames" do
1360 user = insert(:user)
1361 user_two = insert(:user)
1363 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1364 assert length(ap_ids) == 2
1365 assert user.ap_id in ap_ids
1366 assert user_two.ap_id in ap_ids