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
10 alias Pleroma.Web.CommonAPI
14 import Pleroma.Factory
17 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 describe "when tags are nil" do
22 test "tagging a user" do
23 user = insert(:user, %{tags: nil})
24 user = User.tag(user, ["cool", "dude"])
26 assert "cool" in user.tags
27 assert "dude" in user.tags
30 test "untagging a user" do
31 user = insert(:user, %{tags: nil})
32 user = User.untag(user, ["cool", "dude"])
34 assert user.tags == []
38 test "ap_id returns the activity pub id for the user" do
39 user = UserBuilder.build()
41 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
43 assert expected_ap_id == User.ap_id(user)
46 test "ap_followers returns the followers collection for the user" do
47 user = UserBuilder.build()
49 expected_followers_collection = "#{User.ap_id(user)}/followers"
51 assert expected_followers_collection == User.ap_followers(user)
54 test "returns all pending follow requests" do
55 unlocked = insert(:user)
56 locked = insert(:user, %{info: %{locked: true}})
57 follower = insert(:user)
59 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => unlocked.id})
60 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => locked.id})
62 assert {:ok, []} = User.get_follow_requests(unlocked)
63 assert {:ok, [activity]} = User.get_follow_requests(locked)
68 test "doesn't return already accepted or duplicate follow requests" do
69 locked = insert(:user, %{info: %{locked: true}})
70 pending_follower = insert(:user)
71 accepted_follower = insert(:user)
73 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
74 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
75 Pleroma.Web.TwitterAPI.TwitterAPI.follow(accepted_follower, %{"user_id" => locked.id})
76 User.maybe_follow(accepted_follower, locked)
78 assert {:ok, [activity]} = User.get_follow_requests(locked)
82 test "follow_all follows mutliple users" do
84 followed_zero = insert(:user)
85 followed_one = insert(:user)
86 followed_two = insert(:user)
87 blocked = insert(:user)
88 not_followed = insert(:user)
89 reverse_blocked = insert(:user)
91 {:ok, user} = User.block(user, blocked)
92 {:ok, reverse_blocked} = User.block(reverse_blocked, user)
94 {:ok, user} = User.follow(user, followed_zero)
96 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
98 assert User.following?(user, followed_one)
99 assert User.following?(user, followed_two)
100 assert User.following?(user, followed_zero)
101 refute User.following?(user, not_followed)
102 refute User.following?(user, blocked)
103 refute User.following?(user, reverse_blocked)
106 test "follow_all follows mutliple users without duplicating" do
108 followed_zero = insert(:user)
109 followed_one = insert(:user)
110 followed_two = insert(:user)
112 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
113 assert length(user.following) == 3
115 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
116 assert length(user.following) == 4
119 test "follow takes a user and another user" do
121 followed = insert(:user)
123 {:ok, user} = User.follow(user, followed)
125 user = User.get_by_id(user.id)
127 followed = User.get_by_ap_id(followed.ap_id)
128 assert followed.info.follower_count == 1
130 assert User.ap_followers(followed) in user.following
133 test "can't follow a deactivated users" do
135 followed = insert(:user, info: %{deactivated: true})
137 {:error, _} = User.follow(user, followed)
140 test "can't follow a user who blocked us" do
141 blocker = insert(:user)
142 blockee = insert(:user)
144 {:ok, blocker} = User.block(blocker, blockee)
146 {:error, _} = User.follow(blockee, blocker)
149 test "local users do not automatically follow local locked accounts" do
150 follower = insert(:user, info: %{locked: true})
151 followed = insert(:user, info: %{locked: true})
153 {:ok, follower} = User.maybe_direct_follow(follower, followed)
155 refute User.following?(follower, followed)
158 # This is a somewhat useless test.
159 # test "following a remote user will ensure a websub subscription is present" do
160 # user = insert(:user)
161 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
163 # assert followed.local == false
165 # {:ok, user} = User.follow(user, followed)
166 # assert User.ap_followers(followed) in user.following
168 # query = from w in WebsubClientSubscription,
169 # where: w.topic == ^followed.info["topic"]
170 # websub = Repo.one(query)
175 test "unfollow takes a user and another user" do
176 followed = insert(:user)
177 user = insert(:user, %{following: [User.ap_followers(followed)]})
179 {:ok, user, _activity} = User.unfollow(user, followed)
181 user = User.get_by_id(user.id)
183 assert user.following == []
186 test "unfollow doesn't unfollow yourself" do
189 {:error, _} = User.unfollow(user, user)
191 user = User.get_by_id(user.id)
192 assert user.following == [user.ap_id]
195 test "test if a user is following another user" do
196 followed = insert(:user)
197 user = insert(:user, %{following: [User.ap_followers(followed)]})
199 assert User.following?(user, followed)
200 refute User.following?(followed, user)
203 test "fetches correct profile for nickname beginning with number" do
204 # Use old-style integer ID to try to reproduce the problem
205 user = insert(:user, %{id: 1080})
206 userwithnumbers = insert(:user, %{nickname: "#{user.id}garbage"})
207 assert userwithnumbers == User.get_cached_by_nickname_or_id(userwithnumbers.nickname)
210 describe "user registration" do
216 password_confirmation: "test",
217 email: "email@example.com"
220 test "it autofollows accounts that are set for it" do
222 remote_user = insert(:user, %{local: false})
224 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
229 cng = User.register_changeset(%User{}, @full_user_data)
231 {:ok, registered_user} = User.register(cng)
233 assert User.following?(registered_user, user)
234 refute User.following?(registered_user, remote_user)
236 Pleroma.Config.put([:instance, :autofollowed_nicknames], [])
239 test "it sends a welcome message if it is set" do
240 welcome_user = insert(:user)
242 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
243 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
245 cng = User.register_changeset(%User{}, @full_user_data)
246 {:ok, registered_user} = User.register(cng)
248 activity = Repo.one(Pleroma.Activity)
249 assert registered_user.ap_id in activity.recipients
250 assert activity.data["object"]["content"] =~ "cool site"
251 assert activity.actor == welcome_user.ap_id
253 Pleroma.Config.put([:instance, :welcome_user_nickname], nil)
254 Pleroma.Config.put([:instance, :welcome_message], nil)
257 test "it requires an email, name, nickname and password, bio is optional" do
260 |> Enum.each(fn key ->
261 params = Map.delete(@full_user_data, key)
262 changeset = User.register_changeset(%User{}, params)
264 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
268 test "it restricts certain nicknames" do
269 [restricted_name | _] = Pleroma.Config.get([Pleroma.User, :restricted_nicknames])
271 assert is_bitstring(restricted_name)
275 |> Map.put(:nickname, restricted_name)
277 changeset = User.register_changeset(%User{}, params)
279 refute changeset.valid?
282 test "it sets the password_hash, ap_id and following fields" do
283 changeset = User.register_changeset(%User{}, @full_user_data)
285 assert changeset.valid?
287 assert is_binary(changeset.changes[:password_hash])
288 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
290 assert changeset.changes[:following] == [
291 User.ap_followers(%User{nickname: @full_user_data.nickname})
294 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
297 test "it ensures info is not nil" do
298 changeset = User.register_changeset(%User{}, @full_user_data)
300 assert changeset.valid?
306 refute is_nil(user.info)
310 describe "user registration, with :account_activation_required" do
316 password_confirmation: "test",
317 email: "email@example.com"
321 setting = Pleroma.Config.get([:instance, :account_activation_required])
324 Pleroma.Config.put([:instance, :account_activation_required], true)
325 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
331 test "it creates unconfirmed user" do
332 changeset = User.register_changeset(%User{}, @full_user_data)
333 assert changeset.valid?
335 {:ok, user} = Repo.insert(changeset)
337 assert user.info.confirmation_pending
338 assert user.info.confirmation_token
341 test "it creates confirmed user if :confirmed option is given" do
342 changeset = User.register_changeset(%User{}, @full_user_data, confirmed: true)
343 assert changeset.valid?
345 {:ok, user} = Repo.insert(changeset)
347 refute user.info.confirmation_pending
348 refute user.info.confirmation_token
352 describe "get_or_fetch/1" do
353 test "gets an existing user by nickname" do
355 fetched_user = User.get_or_fetch(user.nickname)
357 assert user == fetched_user
360 test "gets an existing user by ap_id" do
361 ap_id = "http://mastodon.example.org/users/admin"
367 nickname: "admin@mastodon.example.org",
372 fetched_user = User.get_or_fetch(ap_id)
373 freshed_user = refresh_record(user)
374 assert freshed_user == fetched_user
378 describe "fetching a user from nickname or trying to build one" do
379 test "gets an existing user" do
381 fetched_user = User.get_or_fetch_by_nickname(user.nickname)
383 assert user == fetched_user
386 test "gets an existing user, case insensitive" do
387 user = insert(:user, nickname: "nick")
388 fetched_user = User.get_or_fetch_by_nickname("NICK")
390 assert user == fetched_user
393 test "gets an existing user by fully qualified nickname" do
397 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
399 assert user == fetched_user
402 test "gets an existing user by fully qualified nickname, case insensitive" do
403 user = insert(:user, nickname: "nick")
404 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
406 fetched_user = User.get_or_fetch_by_nickname(casing_altered_fqn)
408 assert user == fetched_user
411 test "fetches an external user via ostatus if no user exists" do
412 fetched_user = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
413 assert fetched_user.nickname == "shp@social.heldscal.la"
416 test "returns nil if no user could be fetched" do
417 fetched_user = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
418 assert fetched_user == nil
421 test "returns nil for nonexistant local user" do
422 fetched_user = User.get_or_fetch_by_nickname("nonexistant")
423 assert fetched_user == nil
426 test "updates an existing user, if stale" do
427 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
433 nickname: "admin@mastodon.example.org",
434 ap_id: "http://mastodon.example.org/users/admin",
435 last_refreshed_at: a_week_ago,
439 assert orig_user.last_refreshed_at == a_week_ago
441 user = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
442 assert user.info.source_data["endpoints"]
444 refute user.last_refreshed_at == orig_user.last_refreshed_at
448 test "returns an ap_id for a user" do
451 assert User.ap_id(user) ==
452 Pleroma.Web.Router.Helpers.o_status_url(
453 Pleroma.Web.Endpoint,
459 test "returns an ap_followers link for a user" do
462 assert User.ap_followers(user) ==
463 Pleroma.Web.Router.Helpers.o_status_url(
464 Pleroma.Web.Endpoint,
470 describe "remote user creation changeset" do
476 info: %{some: "info"},
477 avatar: %{some: "avatar"}
480 test "it confirms validity" do
481 cs = User.remote_user_creation(@valid_remote)
485 test "it sets the follower_adress" do
486 cs = User.remote_user_creation(@valid_remote)
487 # remote users get a fake local follower address
488 assert cs.changes.follower_address ==
489 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
492 test "it enforces the fqn format for nicknames" do
493 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
494 assert cs.changes.local == false
495 assert cs.changes.avatar
499 test "it has required fields" do
501 |> Enum.each(fn field ->
502 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
507 test "it restricts some sizes" do
508 [bio: 5000, name: 100]
509 |> Enum.each(fn {field, size} ->
510 string = String.pad_leading(".", size)
511 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
514 string = String.pad_leading(".", size + 1)
515 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
521 describe "followers and friends" do
522 test "gets all followers for a given user" do
524 follower_one = insert(:user)
525 follower_two = insert(:user)
526 not_follower = insert(:user)
528 {:ok, follower_one} = User.follow(follower_one, user)
529 {:ok, follower_two} = User.follow(follower_two, user)
531 {:ok, res} = User.get_followers(user)
533 assert Enum.member?(res, follower_one)
534 assert Enum.member?(res, follower_two)
535 refute Enum.member?(res, not_follower)
538 test "gets all friends (followed users) for a given user" do
540 followed_one = insert(:user)
541 followed_two = insert(:user)
542 not_followed = insert(:user)
544 {:ok, user} = User.follow(user, followed_one)
545 {:ok, user} = User.follow(user, followed_two)
547 {:ok, res} = User.get_friends(user)
549 followed_one = User.get_by_ap_id(followed_one.ap_id)
550 followed_two = User.get_by_ap_id(followed_two.ap_id)
551 assert Enum.member?(res, followed_one)
552 assert Enum.member?(res, followed_two)
553 refute Enum.member?(res, not_followed)
557 describe "updating note and follower count" do
558 test "it sets the info->note_count property" do
561 user = User.get_by_ap_id(note.data["actor"])
563 assert user.info.note_count == 0
565 {:ok, user} = User.update_note_count(user)
567 assert user.info.note_count == 1
570 test "it increases the info->note_count property" do
572 user = User.get_by_ap_id(note.data["actor"])
574 assert user.info.note_count == 0
576 {:ok, user} = User.increase_note_count(user)
578 assert user.info.note_count == 1
580 {:ok, user} = User.increase_note_count(user)
582 assert user.info.note_count == 2
585 test "it decreases the info->note_count property" do
587 user = User.get_by_ap_id(note.data["actor"])
589 assert user.info.note_count == 0
591 {:ok, user} = User.increase_note_count(user)
593 assert user.info.note_count == 1
595 {:ok, user} = User.decrease_note_count(user)
597 assert user.info.note_count == 0
599 {:ok, user} = User.decrease_note_count(user)
601 assert user.info.note_count == 0
604 test "it sets the info->follower_count property" do
606 follower = insert(:user)
608 User.follow(follower, user)
610 assert user.info.follower_count == 0
612 {:ok, user} = User.update_follower_count(user)
614 assert user.info.follower_count == 1
618 describe "follow_import" do
619 test "it imports user followings from list" do
620 [user1, user2, user3] = insert_list(3, :user)
627 result = User.follow_import(user1, identifiers)
628 assert is_list(result)
629 assert result == [user2, user3]
634 test "it mutes people" do
636 muted_user = insert(:user)
638 refute User.mutes?(user, muted_user)
640 {:ok, user} = User.mute(user, muted_user)
642 assert User.mutes?(user, muted_user)
645 test "it unmutes users" do
647 muted_user = insert(:user)
649 {:ok, user} = User.mute(user, muted_user)
650 {:ok, user} = User.unmute(user, muted_user)
652 refute User.mutes?(user, muted_user)
657 test "it blocks people" do
659 blocked_user = insert(:user)
661 refute User.blocks?(user, blocked_user)
663 {:ok, user} = User.block(user, blocked_user)
665 assert User.blocks?(user, blocked_user)
668 test "it unblocks users" do
670 blocked_user = insert(:user)
672 {:ok, user} = User.block(user, blocked_user)
673 {:ok, user} = User.unblock(user, blocked_user)
675 refute User.blocks?(user, blocked_user)
678 test "blocks tear down cyclical follow relationships" do
679 blocker = insert(:user)
680 blocked = insert(:user)
682 {:ok, blocker} = User.follow(blocker, blocked)
683 {:ok, blocked} = User.follow(blocked, blocker)
685 assert User.following?(blocker, blocked)
686 assert User.following?(blocked, blocker)
688 {:ok, blocker} = User.block(blocker, blocked)
689 blocked = User.get_by_id(blocked.id)
691 assert User.blocks?(blocker, blocked)
693 refute User.following?(blocker, blocked)
694 refute User.following?(blocked, blocker)
697 test "blocks tear down blocker->blocked follow relationships" do
698 blocker = insert(:user)
699 blocked = insert(:user)
701 {:ok, blocker} = User.follow(blocker, blocked)
703 assert User.following?(blocker, blocked)
704 refute User.following?(blocked, blocker)
706 {:ok, blocker} = User.block(blocker, blocked)
707 blocked = User.get_by_id(blocked.id)
709 assert User.blocks?(blocker, blocked)
711 refute User.following?(blocker, blocked)
712 refute User.following?(blocked, blocker)
715 test "blocks tear down blocked->blocker follow relationships" do
716 blocker = insert(:user)
717 blocked = insert(:user)
719 {:ok, blocked} = User.follow(blocked, blocker)
721 refute User.following?(blocker, blocked)
722 assert User.following?(blocked, blocker)
724 {:ok, blocker} = User.block(blocker, blocked)
725 blocked = User.get_by_id(blocked.id)
727 assert User.blocks?(blocker, blocked)
729 refute User.following?(blocker, blocked)
730 refute User.following?(blocked, blocker)
734 describe "domain blocking" do
735 test "blocks domains" do
737 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
739 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
741 assert User.blocks?(user, collateral_user)
744 test "unblocks domains" do
746 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
748 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
749 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
751 refute User.blocks?(user, collateral_user)
755 describe "blocks_import" do
756 test "it imports user blocks from list" do
757 [user1, user2, user3] = insert_list(3, :user)
764 result = User.blocks_import(user1, identifiers)
765 assert is_list(result)
766 assert result == [user2, user3]
770 test "get recipients from activity" do
771 actor = insert(:user)
772 user = insert(:user, local: true)
773 user_two = insert(:user, local: false)
774 addressed = insert(:user, local: true)
775 addressed_remote = insert(:user, local: false)
778 CommonAPI.post(actor, %{
779 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
782 assert Enum.map([actor, addressed], & &1.ap_id) --
783 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
785 {:ok, user} = User.follow(user, actor)
786 {:ok, _user_two} = User.follow(user_two, actor)
787 recipients = User.get_recipients_from_activity(activity)
788 assert length(recipients) == 3
789 assert user in recipients
790 assert addressed in recipients
793 test ".deactivate can de-activate then re-activate a user" do
795 assert false == user.info.deactivated
796 {:ok, user} = User.deactivate(user)
797 assert true == user.info.deactivated
798 {:ok, user} = User.deactivate(user, false)
799 assert false == user.info.deactivated
802 test ".delete deactivates a user, all follow relationships and all create activities" do
804 followed = insert(:user)
805 follower = insert(:user)
807 {:ok, user} = User.follow(user, followed)
808 {:ok, follower} = User.follow(follower, user)
810 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
811 {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
813 {:ok, _, _} = CommonAPI.favorite(activity_two.id, user)
814 {:ok, _, _} = CommonAPI.favorite(activity.id, follower)
815 {:ok, _, _} = CommonAPI.repeat(activity.id, follower)
817 {:ok, _} = User.delete(user)
819 followed = User.get_by_id(followed.id)
820 follower = User.get_by_id(follower.id)
821 user = User.get_by_id(user.id)
823 assert user.info.deactivated
825 refute User.following?(user, followed)
826 refute User.following?(followed, follower)
828 # TODO: Remove favorites, repeats, delete activities.
830 refute Activity.get_by_id(activity.id)
833 test "get_public_key_for_ap_id fetches a user that's not in the db" do
834 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
837 test "insert or update a user from given data" do
838 user = insert(:user, %{nickname: "nick@name.de"})
839 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
841 assert {:ok, %User{}} = User.insert_or_update_user(data)
844 describe "per-user rich-text filtering" do
845 test "html_filter_policy returns default policies, when rich-text is enabled" do
848 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
851 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
852 user = insert(:user, %{info: %{no_rich_text: true}})
854 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
858 describe "caching" do
859 test "invalidate_cache works" do
861 _user_info = User.get_cached_user_info(user)
863 User.invalidate_cache(user)
865 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
866 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
867 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
870 test "User.delete() plugs any possible zombie objects" do
873 {:ok, _} = User.delete(user)
875 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
877 assert cached_user != user
879 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
881 assert cached_user != user
885 describe "User.search" do
886 test "finds a user by full or partial nickname" do
887 user = insert(:user, %{nickname: "john"})
889 Enum.each(["john", "jo", "j"], fn query ->
893 |> Map.put(:search_rank, nil)
894 |> Map.put(:search_type, nil)
898 test "finds a user by full or partial name" do
899 user = insert(:user, %{name: "John Doe"})
901 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
905 |> Map.put(:search_rank, nil)
906 |> Map.put(:search_type, nil)
910 test "finds users, preferring nickname matches over name matches" do
911 u1 = insert(:user, %{name: "lain", nickname: "nick1"})
912 u2 = insert(:user, %{nickname: "lain", name: "nick1"})
914 assert [u2.id, u1.id] == Enum.map(User.search("lain"), & &1.id)
917 test "finds users, considering density of matched tokens" do
918 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
919 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
921 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
924 test "finds users, ranking by similarity" do
925 u1 = insert(:user, %{name: "lain"})
926 _u2 = insert(:user, %{name: "ean"})
927 u3 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
928 u4 = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
930 assert [u4.id, u3.id, u1.id] == Enum.map(User.search("lain@ple"), & &1.id)
933 test "finds users, handling misspelled requests" do
934 u1 = insert(:user, %{name: "lain"})
936 assert [u1.id] == Enum.map(User.search("laiin"), & &1.id)
939 test "finds users, boosting ranks of friends and followers" do
941 u2 = insert(:user, %{name: "Doe"})
942 follower = insert(:user, %{name: "Doe"})
943 friend = insert(:user, %{name: "Doe"})
945 {:ok, follower} = User.follow(follower, u1)
946 {:ok, u1} = User.follow(u1, friend)
948 assert [friend.id, follower.id, u2.id] --
949 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
952 test "finds a user whose name is nil" do
953 _user = insert(:user, %{name: "notamatch", nickname: "testuser@pleroma.amplifie.red"})
954 user_two = insert(:user, %{name: nil, nickname: "lain@pleroma.soykaf.com"})
957 User.search("lain@pleroma.soykaf.com")
959 |> Map.put(:search_rank, nil)
960 |> Map.put(:search_type, nil)
963 test "does not yield false-positive matches" do
964 insert(:user, %{name: "John Doe"})
966 Enum.each(["mary", "a", ""], fn query ->
967 assert [] == User.search(query)
971 test "works with URIs" do
972 results = User.search("http://mastodon.example.org/users/admin", resolve: true)
973 result = results |> List.first()
975 user = User.get_by_ap_id("http://mastodon.example.org/users/admin")
977 assert length(results) == 1
978 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
982 test "auth_active?/1 works correctly" do
983 Pleroma.Config.put([:instance, :account_activation_required], true)
985 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
986 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
987 remote_user = insert(:user, local: false)
989 refute User.auth_active?(local_user)
990 assert User.auth_active?(confirmed_user)
991 assert User.auth_active?(remote_user)
993 Pleroma.Config.put([:instance, :account_activation_required], false)
996 describe "superuser?/1" do
997 test "returns false for unprivileged users" do
998 user = insert(:user, local: true)
1000 refute User.superuser?(user)
1003 test "returns false for remote users" do
1004 user = insert(:user, local: false)
1005 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1007 refute User.superuser?(user)
1008 refute User.superuser?(remote_admin_user)
1011 test "returns true for local moderators" do
1012 user = insert(:user, local: true, info: %{is_moderator: true})
1014 assert User.superuser?(user)
1017 test "returns true for local admins" do
1018 user = insert(:user, local: true, info: %{is_admin: true})
1020 assert User.superuser?(user)
1024 describe "visible_for?/2" do
1025 test "returns true when the account is itself" do
1026 user = insert(:user, local: true)
1028 assert User.visible_for?(user, user)
1031 test "returns false when the account is unauthenticated and auth is required" do
1032 Pleroma.Config.put([:instance, :account_activation_required], true)
1034 user = insert(:user, local: true, info: %{confirmation_pending: true})
1035 other_user = insert(:user, local: true)
1037 refute User.visible_for?(user, other_user)
1039 Pleroma.Config.put([:instance, :account_activation_required], false)
1042 test "returns true when the account is unauthenticated and auth is not required" do
1043 user = insert(:user, local: true, info: %{confirmation_pending: true})
1044 other_user = insert(:user, local: true)
1046 assert User.visible_for?(user, other_user)
1049 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1050 Pleroma.Config.put([:instance, :account_activation_required], true)
1052 user = insert(:user, local: true, info: %{confirmation_pending: true})
1053 other_user = insert(:user, local: true, info: %{is_admin: true})
1055 assert User.visible_for?(user, other_user)
1057 Pleroma.Config.put([:instance, :account_activation_required], false)
1061 describe "parse_bio/2" do
1062 test "preserves hosts in user links text" do
1063 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1064 user = insert(:user)
1065 bio = "A.k.a. @nick@domain.com"
1068 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1070 }'>" <> "@<span>nick@domain.com</span></a></span>"
1072 assert expected_text == User.parse_bio(bio, user)
1075 test "Adds rel=me on linkbacked urls" do
1076 user = insert(:user, ap_id: "http://social.example.org/users/lain")
1078 bio = "http://example.org/rel_me/null"
1079 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1080 assert expected_text == User.parse_bio(bio, user)
1082 bio = "http://example.org/rel_me/link"
1083 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1084 assert expected_text == User.parse_bio(bio, user)
1086 bio = "http://example.org/rel_me/anchor"
1087 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1088 assert expected_text == User.parse_bio(bio, user)
1093 user = insert(:user)
1096 CommonAPI.post(user, %{
1097 "status" => "heweoo!"
1100 id1 = activity1.data["object"]["id"]
1103 CommonAPI.post(user, %{
1104 "status" => "heweoo!"
1107 id2 = activity2.data["object"]["id"]
1109 assert {:ok, user_state1} = User.bookmark(user, id1)
1110 assert user_state1.bookmarks == [id1]
1112 assert {:ok, user_state2} = User.unbookmark(user, id1)
1113 assert user_state2.bookmarks == []
1115 assert {:ok, user_state3} = User.bookmark(user, id2)
1116 assert user_state3.bookmarks == [id2]