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.CommonAPI
15 import Pleroma.Factory
18 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 describe "when tags are nil" do
23 test "tagging a user" do
24 user = insert(:user, %{tags: nil})
25 user = User.tag(user, ["cool", "dude"])
27 assert "cool" in user.tags
28 assert "dude" in user.tags
31 test "untagging a user" do
32 user = insert(:user, %{tags: nil})
33 user = User.untag(user, ["cool", "dude"])
35 assert user.tags == []
39 test "ap_id returns the activity pub id for the user" do
40 user = UserBuilder.build()
42 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
44 assert expected_ap_id == User.ap_id(user)
47 test "ap_followers returns the followers collection for the user" do
48 user = UserBuilder.build()
50 expected_followers_collection = "#{User.ap_id(user)}/followers"
52 assert expected_followers_collection == User.ap_followers(user)
55 test "returns all pending follow requests" do
56 unlocked = insert(:user)
57 locked = insert(:user, %{info: %{locked: true}})
58 follower = insert(:user)
60 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => unlocked.id})
61 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => locked.id})
63 assert {:ok, []} = User.get_follow_requests(unlocked)
64 assert {:ok, [activity]} = User.get_follow_requests(locked)
69 test "doesn't return already accepted or duplicate follow requests" do
70 locked = insert(:user, %{info: %{locked: true}})
71 pending_follower = insert(:user)
72 accepted_follower = insert(:user)
74 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
75 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
76 Pleroma.Web.TwitterAPI.TwitterAPI.follow(accepted_follower, %{"user_id" => locked.id})
77 User.maybe_follow(accepted_follower, locked)
79 assert {:ok, [activity]} = User.get_follow_requests(locked)
83 test "follow_all follows mutliple users" do
85 followed_zero = insert(:user)
86 followed_one = insert(:user)
87 followed_two = insert(:user)
88 blocked = insert(:user)
89 not_followed = insert(:user)
90 reverse_blocked = insert(:user)
92 {:ok, user} = User.block(user, blocked)
93 {:ok, reverse_blocked} = User.block(reverse_blocked, user)
95 {:ok, user} = User.follow(user, followed_zero)
97 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
99 assert User.following?(user, followed_one)
100 assert User.following?(user, followed_two)
101 assert User.following?(user, followed_zero)
102 refute User.following?(user, not_followed)
103 refute User.following?(user, blocked)
104 refute User.following?(user, reverse_blocked)
107 test "follow_all follows mutliple users without duplicating" do
109 followed_zero = insert(:user)
110 followed_one = insert(:user)
111 followed_two = insert(:user)
113 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
114 assert length(user.following) == 3
116 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
117 assert length(user.following) == 4
120 test "follow takes a user and another user" do
122 followed = insert(:user)
124 {:ok, user} = User.follow(user, followed)
126 user = User.get_cached_by_id(user.id)
128 followed = User.get_cached_by_ap_id(followed.ap_id)
129 assert followed.info.follower_count == 1
131 assert User.ap_followers(followed) in user.following
134 test "can't follow a deactivated users" do
136 followed = insert(:user, info: %{deactivated: true})
138 {:error, _} = User.follow(user, followed)
141 test "can't follow a user who blocked us" do
142 blocker = insert(:user)
143 blockee = insert(:user)
145 {:ok, blocker} = User.block(blocker, blockee)
147 {:error, _} = User.follow(blockee, blocker)
150 test "can't subscribe to a user who blocked us" do
151 blocker = insert(:user)
152 blocked = insert(:user)
154 {:ok, blocker} = User.block(blocker, blocked)
156 {:error, _} = User.subscribe(blocked, blocker)
159 test "local users do not automatically follow local locked accounts" do
160 follower = insert(:user, info: %{locked: true})
161 followed = insert(:user, info: %{locked: true})
163 {:ok, follower} = User.maybe_direct_follow(follower, followed)
165 refute User.following?(follower, followed)
168 # This is a somewhat useless test.
169 # test "following a remote user will ensure a websub subscription is present" do
170 # user = insert(:user)
171 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
173 # assert followed.local == false
175 # {:ok, user} = User.follow(user, followed)
176 # assert User.ap_followers(followed) in user.following
178 # query = from w in WebsubClientSubscription,
179 # where: w.topic == ^followed.info["topic"]
180 # websub = Repo.one(query)
185 test "unfollow takes a user and another user" do
186 followed = insert(:user)
187 user = insert(:user, %{following: [User.ap_followers(followed)]})
189 {:ok, user, _activity} = User.unfollow(user, followed)
191 user = User.get_cached_by_id(user.id)
193 assert user.following == []
196 test "unfollow doesn't unfollow yourself" do
199 {:error, _} = User.unfollow(user, user)
201 user = User.get_cached_by_id(user.id)
202 assert user.following == [user.ap_id]
205 test "test if a user is following another user" do
206 followed = insert(:user)
207 user = insert(:user, %{following: [User.ap_followers(followed)]})
209 assert User.following?(user, followed)
210 refute User.following?(followed, user)
213 test "fetches correct profile for nickname beginning with number" do
214 # Use old-style integer ID to try to reproduce the problem
215 user = insert(:user, %{id: 1080})
216 userwithnumbers = insert(:user, %{nickname: "#{user.id}garbage"})
217 assert userwithnumbers == User.get_cached_by_nickname_or_id(userwithnumbers.nickname)
220 describe "user registration" do
226 password_confirmation: "test",
227 email: "email@example.com"
230 test "it autofollows accounts that are set for it" do
232 remote_user = insert(:user, %{local: false})
234 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
239 cng = User.register_changeset(%User{}, @full_user_data)
241 {:ok, registered_user} = User.register(cng)
243 assert User.following?(registered_user, user)
244 refute User.following?(registered_user, remote_user)
246 Pleroma.Config.put([:instance, :autofollowed_nicknames], [])
249 test "it sends a welcome message if it is set" do
250 welcome_user = insert(:user)
252 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
253 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
255 cng = User.register_changeset(%User{}, @full_user_data)
256 {:ok, registered_user} = User.register(cng)
258 activity = Repo.one(Pleroma.Activity)
259 assert registered_user.ap_id in activity.recipients
260 assert Object.normalize(activity).data["content"] =~ "cool site"
261 assert activity.actor == welcome_user.ap_id
263 Pleroma.Config.put([:instance, :welcome_user_nickname], nil)
264 Pleroma.Config.put([:instance, :welcome_message], nil)
267 test "it requires an email, name, nickname and password, bio is optional" do
270 |> Enum.each(fn key ->
271 params = Map.delete(@full_user_data, key)
272 changeset = User.register_changeset(%User{}, params)
274 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
278 test "it restricts certain nicknames" do
279 [restricted_name | _] = Pleroma.Config.get([Pleroma.User, :restricted_nicknames])
281 assert is_bitstring(restricted_name)
285 |> Map.put(:nickname, restricted_name)
287 changeset = User.register_changeset(%User{}, params)
289 refute changeset.valid?
292 test "it sets the password_hash, ap_id and following fields" do
293 changeset = User.register_changeset(%User{}, @full_user_data)
295 assert changeset.valid?
297 assert is_binary(changeset.changes[:password_hash])
298 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
300 assert changeset.changes[:following] == [
301 User.ap_followers(%User{nickname: @full_user_data.nickname})
304 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
307 test "it ensures info is not nil" do
308 changeset = User.register_changeset(%User{}, @full_user_data)
310 assert changeset.valid?
316 refute is_nil(user.info)
320 describe "user registration, with :account_activation_required" do
326 password_confirmation: "test",
327 email: "email@example.com"
331 setting = Pleroma.Config.get([:instance, :account_activation_required])
334 Pleroma.Config.put([:instance, :account_activation_required], true)
335 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
341 test "it creates unconfirmed user" do
342 changeset = User.register_changeset(%User{}, @full_user_data)
343 assert changeset.valid?
345 {:ok, user} = Repo.insert(changeset)
347 assert user.info.confirmation_pending
348 assert user.info.confirmation_token
351 test "it creates confirmed user if :confirmed option is given" do
352 changeset = User.register_changeset(%User{}, @full_user_data, confirmed: true)
353 assert changeset.valid?
355 {:ok, user} = Repo.insert(changeset)
357 refute user.info.confirmation_pending
358 refute user.info.confirmation_token
362 describe "get_or_fetch/1" do
363 test "gets an existing user by nickname" do
365 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
367 assert user == fetched_user
370 test "gets an existing user by ap_id" do
371 ap_id = "http://mastodon.example.org/users/admin"
377 nickname: "admin@mastodon.example.org",
382 {:ok, fetched_user} = User.get_or_fetch(ap_id)
383 freshed_user = refresh_record(user)
384 assert freshed_user == fetched_user
388 describe "fetching a user from nickname or trying to build one" do
389 test "gets an existing user" do
391 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
393 assert user == fetched_user
396 test "gets an existing user, case insensitive" do
397 user = insert(:user, nickname: "nick")
398 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
400 assert user == fetched_user
403 test "gets an existing user by fully qualified nickname" do
406 {:ok, fetched_user} =
407 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
409 assert user == fetched_user
412 test "gets an existing user by fully qualified nickname, case insensitive" do
413 user = insert(:user, nickname: "nick")
414 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
416 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
418 assert user == fetched_user
421 test "fetches an external user via ostatus if no user exists" do
422 {:ok, fetched_user} = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
423 assert fetched_user.nickname == "shp@social.heldscal.la"
426 test "returns nil if no user could be fetched" do
427 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
428 assert fetched_user == "not found nonexistant@social.heldscal.la"
431 test "returns nil for nonexistant local user" do
432 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
433 assert fetched_user == "not found nonexistant"
436 test "updates an existing user, if stale" do
437 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
443 nickname: "admin@mastodon.example.org",
444 ap_id: "http://mastodon.example.org/users/admin",
445 last_refreshed_at: a_week_ago,
449 assert orig_user.last_refreshed_at == a_week_ago
451 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
452 assert user.info.source_data["endpoints"]
454 refute user.last_refreshed_at == orig_user.last_refreshed_at
458 test "returns an ap_id for a user" do
461 assert User.ap_id(user) ==
462 Pleroma.Web.Router.Helpers.o_status_url(
463 Pleroma.Web.Endpoint,
469 test "returns an ap_followers link for a user" do
472 assert User.ap_followers(user) ==
473 Pleroma.Web.Router.Helpers.o_status_url(
474 Pleroma.Web.Endpoint,
480 describe "remote user creation changeset" do
486 info: %{some: "info"},
487 avatar: %{some: "avatar"}
490 test "it confirms validity" do
491 cs = User.remote_user_creation(@valid_remote)
495 test "it sets the follower_adress" do
496 cs = User.remote_user_creation(@valid_remote)
497 # remote users get a fake local follower address
498 assert cs.changes.follower_address ==
499 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
502 test "it enforces the fqn format for nicknames" do
503 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
504 assert cs.changes.local == false
505 assert cs.changes.avatar
509 test "it has required fields" do
511 |> Enum.each(fn field ->
512 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
517 test "it restricts some sizes" do
518 [bio: 5000, name: 100]
519 |> Enum.each(fn {field, size} ->
520 string = String.pad_leading(".", size)
521 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
524 string = String.pad_leading(".", size + 1)
525 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
531 describe "followers and friends" do
532 test "gets all followers for a given user" do
534 follower_one = insert(:user)
535 follower_two = insert(:user)
536 not_follower = insert(:user)
538 {:ok, follower_one} = User.follow(follower_one, user)
539 {:ok, follower_two} = User.follow(follower_two, user)
541 {:ok, res} = User.get_followers(user)
543 assert Enum.member?(res, follower_one)
544 assert Enum.member?(res, follower_two)
545 refute Enum.member?(res, not_follower)
548 test "gets all friends (followed users) for a given user" do
550 followed_one = insert(:user)
551 followed_two = insert(:user)
552 not_followed = insert(:user)
554 {:ok, user} = User.follow(user, followed_one)
555 {:ok, user} = User.follow(user, followed_two)
557 {:ok, res} = User.get_friends(user)
559 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
560 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
561 assert Enum.member?(res, followed_one)
562 assert Enum.member?(res, followed_two)
563 refute Enum.member?(res, not_followed)
567 describe "updating note and follower count" do
568 test "it sets the info->note_count property" do
571 user = User.get_cached_by_ap_id(note.data["actor"])
573 assert user.info.note_count == 0
575 {:ok, user} = User.update_note_count(user)
577 assert user.info.note_count == 1
580 test "it increases 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.increase_note_count(user)
588 assert user.info.note_count == 1
590 {:ok, user} = User.increase_note_count(user)
592 assert user.info.note_count == 2
595 test "it decreases the info->note_count property" do
597 user = User.get_cached_by_ap_id(note.data["actor"])
599 assert user.info.note_count == 0
601 {:ok, user} = User.increase_note_count(user)
603 assert user.info.note_count == 1
605 {:ok, user} = User.decrease_note_count(user)
607 assert user.info.note_count == 0
609 {:ok, user} = User.decrease_note_count(user)
611 assert user.info.note_count == 0
614 test "it sets the info->follower_count property" do
616 follower = insert(:user)
618 User.follow(follower, user)
620 assert user.info.follower_count == 0
622 {:ok, user} = User.update_follower_count(user)
624 assert user.info.follower_count == 1
628 describe "follow_import" do
629 test "it imports user followings from list" do
630 [user1, user2, user3] = insert_list(3, :user)
637 result = User.follow_import(user1, identifiers)
638 assert is_list(result)
639 assert result == [user2, user3]
644 test "it mutes people" do
646 muted_user = insert(:user)
648 refute User.mutes?(user, muted_user)
650 {:ok, user} = User.mute(user, muted_user)
652 assert User.mutes?(user, muted_user)
655 test "it unmutes users" do
657 muted_user = insert(:user)
659 {:ok, user} = User.mute(user, muted_user)
660 {:ok, user} = User.unmute(user, muted_user)
662 refute User.mutes?(user, muted_user)
667 test "it blocks people" do
669 blocked_user = insert(:user)
671 refute User.blocks?(user, blocked_user)
673 {:ok, user} = User.block(user, blocked_user)
675 assert User.blocks?(user, blocked_user)
678 test "it unblocks users" do
680 blocked_user = insert(:user)
682 {:ok, user} = User.block(user, blocked_user)
683 {:ok, user} = User.unblock(user, blocked_user)
685 refute User.blocks?(user, blocked_user)
688 test "blocks tear down cyclical follow relationships" do
689 blocker = insert(:user)
690 blocked = insert(:user)
692 {:ok, blocker} = User.follow(blocker, blocked)
693 {:ok, blocked} = User.follow(blocked, blocker)
695 assert User.following?(blocker, blocked)
696 assert User.following?(blocked, blocker)
698 {:ok, blocker} = User.block(blocker, blocked)
699 blocked = User.get_cached_by_id(blocked.id)
701 assert User.blocks?(blocker, blocked)
703 refute User.following?(blocker, blocked)
704 refute User.following?(blocked, blocker)
707 test "blocks tear down blocker->blocked follow relationships" do
708 blocker = insert(:user)
709 blocked = insert(:user)
711 {:ok, blocker} = User.follow(blocker, blocked)
713 assert User.following?(blocker, blocked)
714 refute User.following?(blocked, blocker)
716 {:ok, blocker} = User.block(blocker, blocked)
717 blocked = User.get_cached_by_id(blocked.id)
719 assert User.blocks?(blocker, blocked)
721 refute User.following?(blocker, blocked)
722 refute User.following?(blocked, blocker)
725 test "blocks tear down blocked->blocker follow relationships" do
726 blocker = insert(:user)
727 blocked = insert(:user)
729 {:ok, blocked} = User.follow(blocked, blocker)
731 refute User.following?(blocker, blocked)
732 assert User.following?(blocked, blocker)
734 {:ok, blocker} = User.block(blocker, blocked)
735 blocked = User.get_cached_by_id(blocked.id)
737 assert User.blocks?(blocker, blocked)
739 refute User.following?(blocker, blocked)
740 refute User.following?(blocked, blocker)
743 test "blocks tear down blocked->blocker subscription relationships" do
744 blocker = insert(:user)
745 blocked = insert(:user)
747 {:ok, blocker} = User.subscribe(blocked, blocker)
749 assert User.subscribed_to?(blocked, blocker)
750 refute User.subscribed_to?(blocker, blocked)
752 {:ok, blocker} = User.block(blocker, blocked)
754 assert User.blocks?(blocker, blocked)
755 refute User.subscribed_to?(blocker, blocked)
756 refute User.subscribed_to?(blocked, blocker)
760 describe "domain blocking" do
761 test "blocks domains" do
763 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
765 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
767 assert User.blocks?(user, collateral_user)
770 test "unblocks domains" do
772 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
774 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
775 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
777 refute User.blocks?(user, collateral_user)
781 describe "blocks_import" do
782 test "it imports user blocks from list" do
783 [user1, user2, user3] = insert_list(3, :user)
790 result = User.blocks_import(user1, identifiers)
791 assert is_list(result)
792 assert result == [user2, user3]
796 test "get recipients from activity" do
797 actor = insert(:user)
798 user = insert(:user, local: true)
799 user_two = insert(:user, local: false)
800 addressed = insert(:user, local: true)
801 addressed_remote = insert(:user, local: false)
804 CommonAPI.post(actor, %{
805 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
808 assert Enum.map([actor, addressed], & &1.ap_id) --
809 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
811 {:ok, user} = User.follow(user, actor)
812 {:ok, _user_two} = User.follow(user_two, actor)
813 recipients = User.get_recipients_from_activity(activity)
814 assert length(recipients) == 3
815 assert user in recipients
816 assert addressed in recipients
819 test ".deactivate can de-activate then re-activate a user" do
821 assert false == user.info.deactivated
822 {:ok, user} = User.deactivate(user)
823 assert true == user.info.deactivated
824 {:ok, user} = User.deactivate(user, false)
825 assert false == user.info.deactivated
828 test ".delete_user_activities deletes all create activities" do
831 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
832 {:ok, _} = User.delete_user_activities(user)
834 # TODO: Remove favorites, repeats, delete activities.
835 refute Activity.get_by_id(activity.id)
838 test ".delete deactivates a user, all follow relationships and all create activities" do
840 followed = insert(:user)
841 follower = insert(:user)
843 {:ok, user} = User.follow(user, followed)
844 {:ok, follower} = User.follow(follower, user)
846 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
847 {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
849 {:ok, _, _} = CommonAPI.favorite(activity_two.id, user)
850 {:ok, _, _} = CommonAPI.favorite(activity.id, follower)
851 {:ok, _, _} = CommonAPI.repeat(activity.id, follower)
853 {:ok, _} = User.delete(user)
855 followed = User.get_cached_by_id(followed.id)
856 follower = User.get_cached_by_id(follower.id)
857 user = User.get_cached_by_id(user.id)
859 assert user.info.deactivated
861 refute User.following?(user, followed)
862 refute User.following?(followed, follower)
864 # TODO: Remove favorites, repeats, delete activities.
866 refute Activity.get_by_id(activity.id)
869 test "get_public_key_for_ap_id fetches a user that's not in the db" do
870 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
873 test "insert or update a user from given data" do
874 user = insert(:user, %{nickname: "nick@name.de"})
875 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
877 assert {:ok, %User{}} = User.insert_or_update_user(data)
880 describe "per-user rich-text filtering" do
881 test "html_filter_policy returns default policies, when rich-text is enabled" do
884 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
887 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
888 user = insert(:user, %{info: %{no_rich_text: true}})
890 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
894 describe "caching" do
895 test "invalidate_cache works" do
897 _user_info = User.get_cached_user_info(user)
899 User.invalidate_cache(user)
901 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
902 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
903 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
906 test "User.delete() plugs any possible zombie objects" do
909 {:ok, _} = User.delete(user)
911 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
913 assert cached_user != user
915 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
917 assert cached_user != user
921 describe "User.search" do
922 test "finds a user by full or partial nickname" do
923 user = insert(:user, %{nickname: "john"})
925 Enum.each(["john", "jo", "j"], fn query ->
929 |> Map.put(:search_rank, nil)
930 |> Map.put(:search_type, nil)
934 test "finds a user by full or partial name" do
935 user = insert(:user, %{name: "John Doe"})
937 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
941 |> Map.put(:search_rank, nil)
942 |> Map.put(:search_type, nil)
946 test "finds users, preferring nickname matches over name matches" do
947 u1 = insert(:user, %{name: "lain", nickname: "nick1"})
948 u2 = insert(:user, %{nickname: "lain", name: "nick1"})
950 assert [u2.id, u1.id] == Enum.map(User.search("lain"), & &1.id)
953 test "finds users, considering density of matched tokens" do
954 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
955 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
957 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
960 test "finds users, ranking by similarity" do
961 u1 = insert(:user, %{name: "lain"})
962 _u2 = insert(:user, %{name: "ean"})
963 u3 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
964 u4 = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
966 assert [u4.id, u3.id, u1.id] == Enum.map(User.search("lain@ple"), & &1.id)
969 test "finds users, handling misspelled requests" do
970 u1 = insert(:user, %{name: "lain"})
972 assert [u1.id] == Enum.map(User.search("laiin"), & &1.id)
975 test "finds users, boosting ranks of friends and followers" do
977 u2 = insert(:user, %{name: "Doe"})
978 follower = insert(:user, %{name: "Doe"})
979 friend = insert(:user, %{name: "Doe"})
981 {:ok, follower} = User.follow(follower, u1)
982 {:ok, u1} = User.follow(u1, friend)
984 assert [friend.id, follower.id, u2.id] --
985 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
988 test "finds a user whose name is nil" do
989 _user = insert(:user, %{name: "notamatch", nickname: "testuser@pleroma.amplifie.red"})
990 user_two = insert(:user, %{name: nil, nickname: "lain@pleroma.soykaf.com"})
993 User.search("lain@pleroma.soykaf.com")
995 |> Map.put(:search_rank, nil)
996 |> Map.put(:search_type, nil)
999 test "does not yield false-positive matches" do
1000 insert(:user, %{name: "John Doe"})
1002 Enum.each(["mary", "a", ""], fn query ->
1003 assert [] == User.search(query)
1007 test "works with URIs" do
1008 results = User.search("http://mastodon.example.org/users/admin", resolve: true)
1009 result = results |> List.first()
1011 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
1013 assert length(results) == 1
1014 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
1018 test "auth_active?/1 works correctly" do
1019 Pleroma.Config.put([:instance, :account_activation_required], true)
1021 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
1022 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
1023 remote_user = insert(:user, local: false)
1025 refute User.auth_active?(local_user)
1026 assert User.auth_active?(confirmed_user)
1027 assert User.auth_active?(remote_user)
1029 Pleroma.Config.put([:instance, :account_activation_required], false)
1032 describe "superuser?/1" do
1033 test "returns false for unprivileged users" do
1034 user = insert(:user, local: true)
1036 refute User.superuser?(user)
1039 test "returns false for remote users" do
1040 user = insert(:user, local: false)
1041 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1043 refute User.superuser?(user)
1044 refute User.superuser?(remote_admin_user)
1047 test "returns true for local moderators" do
1048 user = insert(:user, local: true, info: %{is_moderator: true})
1050 assert User.superuser?(user)
1053 test "returns true for local admins" do
1054 user = insert(:user, local: true, info: %{is_admin: true})
1056 assert User.superuser?(user)
1060 describe "visible_for?/2" do
1061 test "returns true when the account is itself" do
1062 user = insert(:user, local: true)
1064 assert User.visible_for?(user, user)
1067 test "returns false when the account is unauthenticated and auth is required" do
1068 Pleroma.Config.put([:instance, :account_activation_required], true)
1070 user = insert(:user, local: true, info: %{confirmation_pending: true})
1071 other_user = insert(:user, local: true)
1073 refute User.visible_for?(user, other_user)
1075 Pleroma.Config.put([:instance, :account_activation_required], false)
1078 test "returns true when the account is unauthenticated and auth is not required" do
1079 user = insert(:user, local: true, info: %{confirmation_pending: true})
1080 other_user = insert(:user, local: true)
1082 assert User.visible_for?(user, other_user)
1085 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1086 Pleroma.Config.put([:instance, :account_activation_required], true)
1088 user = insert(:user, local: true, info: %{confirmation_pending: true})
1089 other_user = insert(:user, local: true, info: %{is_admin: true})
1091 assert User.visible_for?(user, other_user)
1093 Pleroma.Config.put([:instance, :account_activation_required], false)
1097 describe "parse_bio/2" do
1098 test "preserves hosts in user links text" do
1099 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1100 user = insert(:user)
1101 bio = "A.k.a. @nick@domain.com"
1104 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1106 }'>" <> "@<span>nick@domain.com</span></a></span>"
1108 assert expected_text == User.parse_bio(bio, user)
1111 test "Adds rel=me on linkbacked urls" do
1112 user = insert(:user, ap_id: "http://social.example.org/users/lain")
1114 bio = "http://example.org/rel_me/null"
1115 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1116 assert expected_text == User.parse_bio(bio, user)
1118 bio = "http://example.org/rel_me/link"
1119 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1120 assert expected_text == User.parse_bio(bio, user)
1122 bio = "http://example.org/rel_me/anchor"
1123 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1124 assert expected_text == User.parse_bio(bio, user)
1128 test "follower count is updated when a follower is blocked" do
1129 user = insert(:user)
1130 follower = insert(:user)
1131 follower2 = insert(:user)
1132 follower3 = insert(:user)
1134 {:ok, follower} = Pleroma.User.follow(follower, user)
1135 {:ok, _follower2} = Pleroma.User.follow(follower2, user)
1136 {:ok, _follower3} = Pleroma.User.follow(follower3, user)
1138 {:ok, _} = Pleroma.User.block(user, follower)
1140 user_show = Pleroma.Web.TwitterAPI.UserView.render("show.json", %{user: user})
1142 assert Map.get(user_show, "followers_count") == 2