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
6 alias Pleroma.Builders.UserBuilder
10 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
16 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 describe "when tags are nil" do
21 test "tagging a user" do
22 user = insert(:user, %{tags: nil})
23 user = User.tag(user, ["cool", "dude"])
25 assert "cool" in user.tags
26 assert "dude" in user.tags
29 test "untagging a user" do
30 user = insert(:user, %{tags: nil})
31 user = User.untag(user, ["cool", "dude"])
33 assert user.tags == []
37 test "ap_id returns the activity pub id for the user" do
38 user = UserBuilder.build()
40 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
42 assert expected_ap_id == User.ap_id(user)
45 test "ap_followers returns the followers collection for the user" do
46 user = UserBuilder.build()
48 expected_followers_collection = "#{User.ap_id(user)}/followers"
50 assert expected_followers_collection == User.ap_followers(user)
53 test "returns all pending follow requests" do
54 unlocked = insert(:user)
55 locked = insert(:user, %{info: %{locked: true}})
56 follower = insert(:user)
58 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => unlocked.id})
59 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => locked.id})
61 assert {:ok, []} = User.get_follow_requests(unlocked)
62 assert {:ok, [activity]} = User.get_follow_requests(locked)
67 test "doesn't return already accepted or duplicate follow requests" do
68 locked = insert(:user, %{info: %{locked: true}})
69 pending_follower = insert(:user)
70 accepted_follower = insert(:user)
72 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
73 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
74 Pleroma.Web.TwitterAPI.TwitterAPI.follow(accepted_follower, %{"user_id" => locked.id})
75 User.maybe_follow(accepted_follower, locked)
77 assert {:ok, [activity]} = User.get_follow_requests(locked)
81 test "follow_all follows mutliple users" do
83 followed_zero = insert(:user)
84 followed_one = insert(:user)
85 followed_two = insert(:user)
86 blocked = insert(:user)
87 not_followed = insert(:user)
88 reverse_blocked = insert(:user)
90 {:ok, user} = User.block(user, blocked)
91 {:ok, reverse_blocked} = User.block(reverse_blocked, user)
93 {:ok, user} = User.follow(user, followed_zero)
95 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
97 assert User.following?(user, followed_one)
98 assert User.following?(user, followed_two)
99 assert User.following?(user, followed_zero)
100 refute User.following?(user, not_followed)
101 refute User.following?(user, blocked)
102 refute User.following?(user, reverse_blocked)
105 test "follow_all follows mutliple users without duplicating" do
107 followed_zero = insert(:user)
108 followed_one = insert(:user)
109 followed_two = insert(:user)
111 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
112 assert length(user.following) == 3
114 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
115 assert length(user.following) == 4
118 test "follow takes a user and another user" do
120 followed = insert(:user)
122 {:ok, user} = User.follow(user, followed)
124 user = Repo.get(User, user.id)
126 followed = User.get_by_ap_id(followed.ap_id)
127 assert followed.info.follower_count == 1
129 assert User.ap_followers(followed) in user.following
132 test "can't follow a deactivated users" do
134 followed = insert(:user, info: %{deactivated: true})
136 {:error, _} = User.follow(user, followed)
139 test "can't follow a user who blocked us" do
140 blocker = insert(:user)
141 blockee = insert(:user)
143 {:ok, blocker} = User.block(blocker, blockee)
145 {:error, _} = User.follow(blockee, blocker)
148 test "local users do not automatically follow local locked accounts" do
149 follower = insert(:user, info: %{locked: true})
150 followed = insert(:user, info: %{locked: true})
152 {:ok, follower} = User.maybe_direct_follow(follower, followed)
154 refute User.following?(follower, followed)
157 # This is a somewhat useless test.
158 # test "following a remote user will ensure a websub subscription is present" do
159 # user = insert(:user)
160 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
162 # assert followed.local == false
164 # {:ok, user} = User.follow(user, followed)
165 # assert User.ap_followers(followed) in user.following
167 # query = from w in WebsubClientSubscription,
168 # where: w.topic == ^followed.info["topic"]
169 # websub = Repo.one(query)
174 test "unfollow takes a user and another user" do
175 followed = insert(:user)
176 user = insert(:user, %{following: [User.ap_followers(followed)]})
178 {:ok, user, _activity} = User.unfollow(user, followed)
180 user = Repo.get(User, user.id)
182 assert user.following == []
185 test "unfollow doesn't unfollow yourself" do
188 {:error, _} = User.unfollow(user, user)
190 user = Repo.get(User, user.id)
191 assert user.following == [user.ap_id]
194 test "test if a user is following another user" do
195 followed = insert(:user)
196 user = insert(:user, %{following: [User.ap_followers(followed)]})
198 assert User.following?(user, followed)
199 refute User.following?(followed, user)
202 describe "user registration" do
208 password_confirmation: "test",
209 email: "email@example.com"
212 test "it autofollows accounts that are set for it" do
214 remote_user = insert(:user, %{local: false})
216 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
221 cng = User.register_changeset(%User{}, @full_user_data)
223 {:ok, registered_user} = User.register(cng)
225 assert User.following?(registered_user, user)
226 refute User.following?(registered_user, remote_user)
228 Pleroma.Config.put([:instance, :autofollowed_nicknames], [])
231 test "it sends a welcome message if it is set" do
232 welcome_user = insert(:user)
234 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
235 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
237 cng = User.register_changeset(%User{}, @full_user_data)
238 {:ok, registered_user} = User.register(cng)
240 activity = Repo.one(Pleroma.Activity)
241 assert registered_user.ap_id in activity.recipients
242 assert activity.data["object"]["content"] =~ "cool site"
243 assert activity.actor == welcome_user.ap_id
245 Pleroma.Config.put([:instance, :welcome_user_nickname], nil)
246 Pleroma.Config.put([:instance, :welcome_message], nil)
249 test "it requires an email, name, nickname and password, bio is optional" do
252 |> Enum.each(fn key ->
253 params = Map.delete(@full_user_data, key)
254 changeset = User.register_changeset(%User{}, params)
256 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
260 test "it restricts certain nicknames" do
261 [restricted_name | _] = Pleroma.Config.get([Pleroma.User, :restricted_nicknames])
263 assert is_bitstring(restricted_name)
267 |> Map.put(:nickname, restricted_name)
269 changeset = User.register_changeset(%User{}, params)
271 refute changeset.valid?
274 test "it sets the password_hash, ap_id and following fields" do
275 changeset = User.register_changeset(%User{}, @full_user_data)
277 assert changeset.valid?
279 assert is_binary(changeset.changes[:password_hash])
280 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
282 assert changeset.changes[:following] == [
283 User.ap_followers(%User{nickname: @full_user_data.nickname})
286 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
289 test "it ensures info is not nil" do
290 changeset = User.register_changeset(%User{}, @full_user_data)
292 assert changeset.valid?
298 refute is_nil(user.info)
302 describe "user registration, with :account_activation_required" do
308 password_confirmation: "test",
309 email: "email@example.com"
313 setting = Pleroma.Config.get([:instance, :account_activation_required])
316 Pleroma.Config.put([:instance, :account_activation_required], true)
317 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
323 test "it creates unconfirmed user" do
324 changeset = User.register_changeset(%User{}, @full_user_data)
325 assert changeset.valid?
327 {:ok, user} = Repo.insert(changeset)
329 assert user.info.confirmation_pending
330 assert user.info.confirmation_token
333 test "it creates confirmed user if :confirmed option is given" do
334 changeset = User.register_changeset(%User{}, @full_user_data, confirmed: true)
335 assert changeset.valid?
337 {:ok, user} = Repo.insert(changeset)
339 refute user.info.confirmation_pending
340 refute user.info.confirmation_token
344 describe "get_or_fetch/1" do
345 test "gets an existing user by nickname" do
347 fetched_user = User.get_or_fetch(user.nickname)
349 assert user == fetched_user
352 test "gets an existing user by ap_id" do
353 ap_id = "http://mastodon.example.org/users/admin"
359 nickname: "admin@mastodon.example.org",
364 fetched_user = User.get_or_fetch(ap_id)
365 freshed_user = refresh_record(user)
366 assert freshed_user == fetched_user
370 describe "fetching a user from nickname or trying to build one" do
371 test "gets an existing user" do
373 fetched_user = User.get_or_fetch_by_nickname(user.nickname)
375 assert user == fetched_user
378 test "gets an existing user, case insensitive" do
379 user = insert(:user, nickname: "nick")
380 fetched_user = User.get_or_fetch_by_nickname("NICK")
382 assert user == fetched_user
385 test "gets an existing user by fully qualified nickname" do
389 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
391 assert user == fetched_user
394 test "gets an existing user by fully qualified nickname, case insensitive" do
395 user = insert(:user, nickname: "nick")
396 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
398 fetched_user = User.get_or_fetch_by_nickname(casing_altered_fqn)
400 assert user == fetched_user
403 test "fetches an external user via ostatus if no user exists" do
404 fetched_user = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
405 assert fetched_user.nickname == "shp@social.heldscal.la"
408 test "returns nil if no user could be fetched" do
409 fetched_user = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
410 assert fetched_user == nil
413 test "returns nil for nonexistant local user" do
414 fetched_user = User.get_or_fetch_by_nickname("nonexistant")
415 assert fetched_user == nil
418 test "updates an existing user, if stale" do
419 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
425 nickname: "admin@mastodon.example.org",
426 ap_id: "http://mastodon.example.org/users/admin",
427 last_refreshed_at: a_week_ago,
431 assert orig_user.last_refreshed_at == a_week_ago
433 user = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
434 assert user.info.source_data["endpoints"]
436 refute user.last_refreshed_at == orig_user.last_refreshed_at
440 test "returns an ap_id for a user" do
443 assert User.ap_id(user) ==
444 Pleroma.Web.Router.Helpers.o_status_url(
445 Pleroma.Web.Endpoint,
451 test "returns an ap_followers link for a user" do
454 assert User.ap_followers(user) ==
455 Pleroma.Web.Router.Helpers.o_status_url(
456 Pleroma.Web.Endpoint,
462 describe "remote user creation changeset" do
468 info: %{some: "info"},
469 avatar: %{some: "avatar"}
472 test "it confirms validity" do
473 cs = User.remote_user_creation(@valid_remote)
477 test "it sets the follower_adress" do
478 cs = User.remote_user_creation(@valid_remote)
479 # remote users get a fake local follower address
480 assert cs.changes.follower_address ==
481 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
484 test "it enforces the fqn format for nicknames" do
485 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
486 assert cs.changes.local == false
487 assert cs.changes.avatar
491 test "it has required fields" do
493 |> Enum.each(fn field ->
494 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
499 test "it restricts some sizes" do
500 [bio: 5000, name: 100]
501 |> Enum.each(fn {field, size} ->
502 string = String.pad_leading(".", size)
503 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
506 string = String.pad_leading(".", size + 1)
507 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
513 describe "followers and friends" do
514 test "gets all followers for a given user" do
516 follower_one = insert(:user)
517 follower_two = insert(:user)
518 not_follower = insert(:user)
520 {:ok, follower_one} = User.follow(follower_one, user)
521 {:ok, follower_two} = User.follow(follower_two, user)
523 {:ok, res} = User.get_followers(user)
525 assert Enum.member?(res, follower_one)
526 assert Enum.member?(res, follower_two)
527 refute Enum.member?(res, not_follower)
530 test "gets all friends (followed users) for a given user" do
532 followed_one = insert(:user)
533 followed_two = insert(:user)
534 not_followed = insert(:user)
536 {:ok, user} = User.follow(user, followed_one)
537 {:ok, user} = User.follow(user, followed_two)
539 {:ok, res} = User.get_friends(user)
541 followed_one = User.get_by_ap_id(followed_one.ap_id)
542 followed_two = User.get_by_ap_id(followed_two.ap_id)
543 assert Enum.member?(res, followed_one)
544 assert Enum.member?(res, followed_two)
545 refute Enum.member?(res, not_followed)
549 describe "updating note and follower count" do
550 test "it sets the info->note_count property" do
553 user = User.get_by_ap_id(note.data["actor"])
555 assert user.info.note_count == 0
557 {:ok, user} = User.update_note_count(user)
559 assert user.info.note_count == 1
562 test "it increases the info->note_count property" do
564 user = User.get_by_ap_id(note.data["actor"])
566 assert user.info.note_count == 0
568 {:ok, user} = User.increase_note_count(user)
570 assert user.info.note_count == 1
572 {:ok, user} = User.increase_note_count(user)
574 assert user.info.note_count == 2
577 test "it decreases the info->note_count property" do
579 user = User.get_by_ap_id(note.data["actor"])
581 assert user.info.note_count == 0
583 {:ok, user} = User.increase_note_count(user)
585 assert user.info.note_count == 1
587 {:ok, user} = User.decrease_note_count(user)
589 assert user.info.note_count == 0
591 {:ok, user} = User.decrease_note_count(user)
593 assert user.info.note_count == 0
596 test "it sets the info->follower_count property" do
598 follower = insert(:user)
600 User.follow(follower, user)
602 assert user.info.follower_count == 0
604 {:ok, user} = User.update_follower_count(user)
606 assert user.info.follower_count == 1
610 describe "follow_import" do
611 test "it imports user followings from list" do
612 [user1, user2, user3] = insert_list(3, :user)
619 result = User.follow_import(user1, identifiers)
620 assert is_list(result)
621 assert result == [user2, user3]
626 test "it mutes people" do
628 muted_user = insert(:user)
630 refute User.mutes?(user, muted_user)
632 {:ok, user} = User.mute(user, muted_user)
634 assert User.mutes?(user, muted_user)
637 test "it unmutes users" do
639 muted_user = insert(:user)
641 {:ok, user} = User.mute(user, muted_user)
642 {:ok, user} = User.unmute(user, muted_user)
644 refute User.mutes?(user, muted_user)
649 test "it blocks people" do
651 blocked_user = insert(:user)
653 refute User.blocks?(user, blocked_user)
655 {:ok, user} = User.block(user, blocked_user)
657 assert User.blocks?(user, blocked_user)
660 test "it unblocks users" do
662 blocked_user = insert(:user)
664 {:ok, user} = User.block(user, blocked_user)
665 {:ok, user} = User.unblock(user, blocked_user)
667 refute User.blocks?(user, blocked_user)
670 test "blocks tear down cyclical follow relationships" do
671 blocker = insert(:user)
672 blocked = insert(:user)
674 {:ok, blocker} = User.follow(blocker, blocked)
675 {:ok, blocked} = User.follow(blocked, blocker)
677 assert User.following?(blocker, blocked)
678 assert User.following?(blocked, blocker)
680 {:ok, blocker} = User.block(blocker, blocked)
681 blocked = Repo.get(User, blocked.id)
683 assert User.blocks?(blocker, blocked)
685 refute User.following?(blocker, blocked)
686 refute User.following?(blocked, blocker)
689 test "blocks tear down blocker->blocked follow relationships" do
690 blocker = insert(:user)
691 blocked = insert(:user)
693 {:ok, blocker} = User.follow(blocker, blocked)
695 assert User.following?(blocker, blocked)
696 refute User.following?(blocked, blocker)
698 {:ok, blocker} = User.block(blocker, blocked)
699 blocked = Repo.get(User, 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 blocked->blocker follow relationships" do
708 blocker = insert(:user)
709 blocked = insert(:user)
711 {:ok, blocked} = User.follow(blocked, blocker)
713 refute User.following?(blocker, blocked)
714 assert User.following?(blocked, blocker)
716 {:ok, blocker} = User.block(blocker, blocked)
717 blocked = Repo.get(User, blocked.id)
719 assert User.blocks?(blocker, blocked)
721 refute User.following?(blocker, blocked)
722 refute User.following?(blocked, blocker)
726 describe "domain blocking" do
727 test "blocks domains" do
729 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
731 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
733 assert User.blocks?(user, collateral_user)
736 test "unblocks domains" do
738 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
740 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
741 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
743 refute User.blocks?(user, collateral_user)
747 describe "blocks_import" do
748 test "it imports user blocks from list" do
749 [user1, user2, user3] = insert_list(3, :user)
756 result = User.blocks_import(user1, identifiers)
757 assert is_list(result)
758 assert result == [user2, user3]
762 test "get recipients from activity" do
763 actor = insert(:user)
764 user = insert(:user, local: true)
765 user_two = insert(:user, local: false)
766 addressed = insert(:user, local: true)
767 addressed_remote = insert(:user, local: false)
770 CommonAPI.post(actor, %{
771 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
774 assert Enum.map([actor, addressed], & &1.ap_id) --
775 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
777 {:ok, user} = User.follow(user, actor)
778 {:ok, _user_two} = User.follow(user_two, actor)
779 recipients = User.get_recipients_from_activity(activity)
780 assert length(recipients) == 3
781 assert user in recipients
782 assert addressed in recipients
785 test ".deactivate can de-activate then re-activate a user" do
787 assert false == user.info.deactivated
788 {:ok, user} = User.deactivate(user)
789 assert true == user.info.deactivated
790 {:ok, user} = User.deactivate(user, false)
791 assert false == user.info.deactivated
794 test ".delete deactivates a user, all follow relationships and all create activities" do
796 followed = insert(:user)
797 follower = insert(:user)
799 {:ok, user} = User.follow(user, followed)
800 {:ok, follower} = User.follow(follower, user)
802 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
803 {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
805 {:ok, _, _} = CommonAPI.favorite(activity_two.id, user)
806 {:ok, _, _} = CommonAPI.favorite(activity.id, follower)
807 {:ok, _, _} = CommonAPI.repeat(activity.id, follower)
809 {:ok, _} = User.delete(user)
811 followed = Repo.get(User, followed.id)
812 follower = Repo.get(User, follower.id)
813 user = Repo.get(User, user.id)
815 assert user.info.deactivated
817 refute User.following?(user, followed)
818 refute User.following?(followed, follower)
820 # TODO: Remove favorites, repeats, delete activities.
822 refute Repo.get(Activity, activity.id)
825 test "get_public_key_for_ap_id fetches a user that's not in the db" do
826 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
829 test "insert or update a user from given data" do
830 user = insert(:user, %{nickname: "nick@name.de"})
831 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
833 assert {:ok, %User{}} = User.insert_or_update_user(data)
836 describe "per-user rich-text filtering" do
837 test "html_filter_policy returns default policies, when rich-text is enabled" do
840 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
843 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
844 user = insert(:user, %{info: %{no_rich_text: true}})
846 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
850 describe "caching" do
851 test "invalidate_cache works" do
853 _user_info = User.get_cached_user_info(user)
855 User.invalidate_cache(user)
857 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
858 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
859 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
862 test "User.delete() plugs any possible zombie objects" do
865 {:ok, _} = User.delete(user)
867 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
869 assert cached_user != user
871 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
873 assert cached_user != user
877 describe "User.search" do
878 test "finds a user by full or partial nickname" do
879 user = insert(:user, %{nickname: "john"})
881 Enum.each(["john", "jo", "j"], fn query ->
882 assert user == User.search(query) |> List.first() |> Map.put(:search_rank, nil)
886 test "finds a user by full or partial name" do
887 user = insert(:user, %{name: "John Doe"})
889 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
890 assert user == User.search(query) |> List.first() |> Map.put(:search_rank, nil)
894 test "finds users, preferring nickname matches over name matches" do
895 u1 = insert(:user, %{name: "lain", nickname: "nick1"})
896 u2 = insert(:user, %{nickname: "lain", name: "nick1"})
898 assert [u2.id, u1.id] == Enum.map(User.search("lain"), & &1.id)
901 test "finds users, considering density of matched tokens" do
902 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
903 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
905 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
908 test "finds users, ranking by similarity" do
909 u1 = insert(:user, %{name: "lain"})
910 _u2 = insert(:user, %{name: "ean"})
911 u3 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
912 u4 = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
914 assert [u4.id, u3.id, u1.id] == Enum.map(User.search("lain@ple"), & &1.id)
917 test "finds users, handling misspelled requests" do
918 u1 = insert(:user, %{name: "lain"})
920 assert [u1.id] == Enum.map(User.search("laiin"), & &1.id)
923 test "finds users, boosting ranks of friends and followers" do
925 u2 = insert(:user, %{name: "Doe"})
926 follower = insert(:user, %{name: "Doe"})
927 friend = insert(:user, %{name: "Doe"})
929 {:ok, follower} = User.follow(follower, u1)
930 {:ok, u1} = User.follow(u1, friend)
932 assert [friend.id, follower.id, u2.id] --
933 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
936 test "finds a user whose name is nil" do
937 _user = insert(:user, %{name: "notamatch", nickname: "testuser@pleroma.amplifie.red"})
938 user_two = insert(:user, %{name: nil, nickname: "lain@pleroma.soykaf.com"})
941 User.search("lain@pleroma.soykaf.com")
943 |> Map.put(:search_rank, nil)
946 test "does not yield false-positive matches" do
947 insert(:user, %{name: "John Doe"})
949 Enum.each(["mary", "a", ""], fn query ->
950 assert [] == User.search(query)
954 test "works with URIs" do
955 results = User.search("http://mastodon.example.org/users/admin", resolve: true)
956 result = results |> List.first()
958 user = User.get_by_ap_id("http://mastodon.example.org/users/admin")
960 assert length(results) == 1
961 assert user == result |> Map.put(:search_rank, nil)
965 test "auth_active?/1 works correctly" do
966 Pleroma.Config.put([:instance, :account_activation_required], true)
968 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
969 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
970 remote_user = insert(:user, local: false)
972 refute User.auth_active?(local_user)
973 assert User.auth_active?(confirmed_user)
974 assert User.auth_active?(remote_user)
976 Pleroma.Config.put([:instance, :account_activation_required], false)
979 describe "superuser?/1" do
980 test "returns false for unprivileged users" do
981 user = insert(:user, local: true)
983 refute User.superuser?(user)
986 test "returns false for remote users" do
987 user = insert(:user, local: false)
988 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
990 refute User.superuser?(user)
991 refute User.superuser?(remote_admin_user)
994 test "returns true for local moderators" do
995 user = insert(:user, local: true, info: %{is_moderator: true})
997 assert User.superuser?(user)
1000 test "returns true for local admins" do
1001 user = insert(:user, local: true, info: %{is_admin: true})
1003 assert User.superuser?(user)
1007 describe "visible_for?/2" do
1008 test "returns true when the account is itself" do
1009 user = insert(:user, local: true)
1011 assert User.visible_for?(user, user)
1014 test "returns false when the account is unauthenticated and auth is required" do
1015 Pleroma.Config.put([:instance, :account_activation_required], true)
1017 user = insert(:user, local: true, info: %{confirmation_pending: true})
1018 other_user = insert(:user, local: true)
1020 refute User.visible_for?(user, other_user)
1022 Pleroma.Config.put([:instance, :account_activation_required], false)
1025 test "returns true when the account is unauthenticated and auth is not required" do
1026 user = insert(:user, local: true, info: %{confirmation_pending: true})
1027 other_user = insert(:user, local: true)
1029 assert User.visible_for?(user, other_user)
1032 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1033 Pleroma.Config.put([:instance, :account_activation_required], true)
1035 user = insert(:user, local: true, info: %{confirmation_pending: true})
1036 other_user = insert(:user, local: true, info: %{is_admin: true})
1038 assert User.visible_for?(user, other_user)
1040 Pleroma.Config.put([:instance, :account_activation_required], false)
1044 describe "parse_bio/2" do
1045 test "preserves hosts in user links text" do
1046 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1047 user = insert(:user)
1048 bio = "A.k.a. @nick@domain.com"
1051 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1053 }'>" <> "@<span>nick@domain.com</span></a></span>"
1055 assert expected_text == User.parse_bio(bio, user)
1060 user = insert(:user)
1063 CommonAPI.post(user, %{
1064 "status" => "heweoo!"
1067 id1 = activity1.data["object"]["id"]
1070 CommonAPI.post(user, %{
1071 "status" => "heweoo!"
1074 id2 = activity2.data["object"]["id"]
1076 assert {:ok, user_state1} = User.bookmark(user, id1)
1077 assert user_state1.bookmarks == [id1]
1079 assert {:ok, user_state2} = User.unbookmark(user, id1)
1080 assert user_state2.bookmarks == []
1082 assert {:ok, user_state3} = User.bookmark(user, id2)
1083 assert user_state3.bookmarks == [id2]