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.maybe_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([Pleroma.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 "follow_import" do
630 test "it imports user followings from list" do
631 [user1, user2, user3] = insert_list(3, :user)
638 result = User.follow_import(user1, identifiers)
639 assert is_list(result)
640 assert result == [user2, user3]
645 test "it mutes people" do
647 muted_user = insert(:user)
649 refute User.mutes?(user, muted_user)
651 {:ok, user} = User.mute(user, muted_user)
653 assert User.mutes?(user, muted_user)
656 test "it unmutes users" do
658 muted_user = insert(:user)
660 {:ok, user} = User.mute(user, muted_user)
661 {:ok, user} = User.unmute(user, muted_user)
663 refute User.mutes?(user, muted_user)
668 test "it blocks people" do
670 blocked_user = insert(:user)
672 refute User.blocks?(user, blocked_user)
674 {:ok, user} = User.block(user, blocked_user)
676 assert User.blocks?(user, blocked_user)
679 test "it unblocks users" do
681 blocked_user = insert(:user)
683 {:ok, user} = User.block(user, blocked_user)
684 {:ok, user} = User.unblock(user, blocked_user)
686 refute User.blocks?(user, blocked_user)
689 test "blocks tear down cyclical follow relationships" do
690 blocker = insert(:user)
691 blocked = insert(:user)
693 {:ok, blocker} = User.follow(blocker, blocked)
694 {:ok, blocked} = User.follow(blocked, blocker)
696 assert User.following?(blocker, blocked)
697 assert User.following?(blocked, blocker)
699 {:ok, blocker} = User.block(blocker, blocked)
700 blocked = User.get_cached_by_id(blocked.id)
702 assert User.blocks?(blocker, blocked)
704 refute User.following?(blocker, blocked)
705 refute User.following?(blocked, blocker)
708 test "blocks tear down blocker->blocked follow relationships" do
709 blocker = insert(:user)
710 blocked = insert(:user)
712 {:ok, blocker} = User.follow(blocker, blocked)
714 assert User.following?(blocker, blocked)
715 refute User.following?(blocked, blocker)
717 {:ok, blocker} = User.block(blocker, blocked)
718 blocked = User.get_cached_by_id(blocked.id)
720 assert User.blocks?(blocker, blocked)
722 refute User.following?(blocker, blocked)
723 refute User.following?(blocked, blocker)
726 test "blocks tear down blocked->blocker follow relationships" do
727 blocker = insert(:user)
728 blocked = insert(:user)
730 {:ok, blocked} = User.follow(blocked, blocker)
732 refute User.following?(blocker, blocked)
733 assert User.following?(blocked, blocker)
735 {:ok, blocker} = User.block(blocker, blocked)
736 blocked = User.get_cached_by_id(blocked.id)
738 assert User.blocks?(blocker, blocked)
740 refute User.following?(blocker, blocked)
741 refute User.following?(blocked, blocker)
744 test "blocks tear down blocked->blocker subscription relationships" do
745 blocker = insert(:user)
746 blocked = insert(:user)
748 {:ok, blocker} = User.subscribe(blocked, blocker)
750 assert User.subscribed_to?(blocked, blocker)
751 refute User.subscribed_to?(blocker, blocked)
753 {:ok, blocker} = User.block(blocker, blocked)
755 assert User.blocks?(blocker, blocked)
756 refute User.subscribed_to?(blocker, blocked)
757 refute User.subscribed_to?(blocked, blocker)
761 describe "domain blocking" do
762 test "blocks domains" do
764 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
766 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
768 assert User.blocks?(user, collateral_user)
771 test "unblocks domains" do
773 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
775 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
776 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
778 refute User.blocks?(user, collateral_user)
782 describe "blocks_import" do
783 test "it imports user blocks from list" do
784 [user1, user2, user3] = insert_list(3, :user)
791 result = User.blocks_import(user1, identifiers)
792 assert is_list(result)
793 assert result == [user2, user3]
797 test "get recipients from activity" do
798 actor = insert(:user)
799 user = insert(:user, local: true)
800 user_two = insert(:user, local: false)
801 addressed = insert(:user, local: true)
802 addressed_remote = insert(:user, local: false)
805 CommonAPI.post(actor, %{
806 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
809 assert Enum.map([actor, addressed], & &1.ap_id) --
810 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
812 {:ok, user} = User.follow(user, actor)
813 {:ok, _user_two} = User.follow(user_two, actor)
814 recipients = User.get_recipients_from_activity(activity)
815 assert length(recipients) == 3
816 assert user in recipients
817 assert addressed in recipients
820 describe ".deactivate" do
821 test "can de-activate then re-activate a user" do
823 assert false == user.info.deactivated
824 {:ok, user} = User.deactivate(user)
825 assert true == user.info.deactivated
826 {:ok, user} = User.deactivate(user, false)
827 assert false == user.info.deactivated
830 test "hide a user from followers " do
832 user2 = insert(:user)
834 {:ok, user} = User.follow(user, user2)
835 {:ok, _user} = User.deactivate(user)
837 info = User.get_cached_user_info(user2)
839 assert info.follower_count == 0
840 assert {:ok, []} = User.get_followers(user2)
843 test "hide a user from friends" do
845 user2 = insert(:user)
847 {:ok, user2} = User.follow(user2, user)
848 assert User.following_count(user2) == 1
850 {:ok, _user} = User.deactivate(user)
852 info = User.get_cached_user_info(user2)
854 assert info.following_count == 0
855 assert User.following_count(user2) == 0
856 assert {:ok, []} = User.get_friends(user2)
859 test "hide a user's statuses from timelines and notifications" do
861 user2 = insert(:user)
863 {:ok, user2} = User.follow(user2, user)
865 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
867 activity = Repo.preload(activity, :bookmark)
869 [notification] = Pleroma.Notification.for_user(user2)
870 assert notification.activity.id == activity.id
872 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
875 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
876 |> ActivityPub.contain_timeline(user2)
878 {:ok, _user} = User.deactivate(user)
880 assert [] == ActivityPub.fetch_public_activities(%{})
881 assert [] == Pleroma.Notification.for_user(user2)
884 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
885 |> ActivityPub.contain_timeline(user2)
889 test ".delete_user_activities deletes all create activities" do
892 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
894 Ecto.Adapters.SQL.Sandbox.unboxed_run(Repo, fn ->
895 {:ok, _} = User.delete_user_activities(user)
896 # TODO: Remove favorites, repeats, delete activities.
897 refute Activity.get_by_id(activity.id)
901 test ".delete deactivates a user, all follow relationships and all create activities" do
903 followed = insert(:user)
904 follower = insert(:user)
906 {:ok, user} = User.follow(user, followed)
907 {:ok, follower} = User.follow(follower, user)
909 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
910 {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
912 {:ok, _, _} = CommonAPI.favorite(activity_two.id, user)
913 {:ok, _, _} = CommonAPI.favorite(activity.id, follower)
914 {:ok, _, _} = CommonAPI.repeat(activity.id, follower)
916 {:ok, _} = User.delete(user)
918 followed = User.get_cached_by_id(followed.id)
919 follower = User.get_cached_by_id(follower.id)
920 user = User.get_cached_by_id(user.id)
922 assert user.info.deactivated
924 refute User.following?(user, followed)
925 refute User.following?(followed, follower)
927 # TODO: Remove favorites, repeats, delete activities.
929 refute Activity.get_by_id(activity.id)
932 test "get_public_key_for_ap_id fetches a user that's not in the db" do
933 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
936 test "insert or update a user from given data" do
937 user = insert(:user, %{nickname: "nick@name.de"})
938 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
940 assert {:ok, %User{}} = User.insert_or_update_user(data)
943 describe "per-user rich-text filtering" do
944 test "html_filter_policy returns default policies, when rich-text is enabled" do
947 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
950 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
951 user = insert(:user, %{info: %{no_rich_text: true}})
953 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
957 describe "caching" do
958 test "invalidate_cache works" do
960 _user_info = User.get_cached_user_info(user)
962 User.invalidate_cache(user)
964 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
965 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
966 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
969 test "User.delete() plugs any possible zombie objects" do
972 {:ok, _} = User.delete(user)
974 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
976 assert cached_user != user
978 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
980 assert cached_user != user
984 describe "User.search" do
985 test "finds a user by full or partial nickname" do
986 user = insert(:user, %{nickname: "john"})
988 Enum.each(["john", "jo", "j"], fn query ->
992 |> Map.put(:search_rank, nil)
993 |> Map.put(:search_type, nil)
997 test "finds a user by full or partial name" do
998 user = insert(:user, %{name: "John Doe"})
1000 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
1004 |> Map.put(:search_rank, nil)
1005 |> Map.put(:search_type, nil)
1009 test "finds users, preferring nickname matches over name matches" do
1010 u1 = insert(:user, %{name: "lain", nickname: "nick1"})
1011 u2 = insert(:user, %{nickname: "lain", name: "nick1"})
1013 assert [u2.id, u1.id] == Enum.map(User.search("lain"), & &1.id)
1016 test "finds users, considering density of matched tokens" do
1017 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
1018 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
1020 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
1023 test "finds users, ranking by similarity" do
1024 u1 = insert(:user, %{name: "lain"})
1025 _u2 = insert(:user, %{name: "ean"})
1026 u3 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
1027 u4 = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
1029 assert [u4.id, u3.id, u1.id] == Enum.map(User.search("lain@ple"), & &1.id)
1032 test "finds users, handling misspelled requests" do
1033 u1 = insert(:user, %{name: "lain"})
1035 assert [u1.id] == Enum.map(User.search("laiin"), & &1.id)
1038 test "finds users, boosting ranks of friends and followers" do
1040 u2 = insert(:user, %{name: "Doe"})
1041 follower = insert(:user, %{name: "Doe"})
1042 friend = insert(:user, %{name: "Doe"})
1044 {:ok, follower} = User.follow(follower, u1)
1045 {:ok, u1} = User.follow(u1, friend)
1047 assert [friend.id, follower.id, u2.id] --
1048 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
1051 test "finds a user whose name is nil" do
1052 _user = insert(:user, %{name: "notamatch", nickname: "testuser@pleroma.amplifie.red"})
1053 user_two = insert(:user, %{name: nil, nickname: "lain@pleroma.soykaf.com"})
1056 User.search("lain@pleroma.soykaf.com")
1058 |> Map.put(:search_rank, nil)
1059 |> Map.put(:search_type, nil)
1062 test "does not yield false-positive matches" do
1063 insert(:user, %{name: "John Doe"})
1065 Enum.each(["mary", "a", ""], fn query ->
1066 assert [] == User.search(query)
1070 test "works with URIs" do
1071 results = User.search("http://mastodon.example.org/users/admin", resolve: true)
1072 result = results |> List.first()
1074 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
1076 assert length(results) == 1
1077 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
1081 test "auth_active?/1 works correctly" do
1082 Pleroma.Config.put([:instance, :account_activation_required], true)
1084 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
1085 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
1086 remote_user = insert(:user, local: false)
1088 refute User.auth_active?(local_user)
1089 assert User.auth_active?(confirmed_user)
1090 assert User.auth_active?(remote_user)
1092 Pleroma.Config.put([:instance, :account_activation_required], false)
1095 describe "superuser?/1" do
1096 test "returns false for unprivileged users" do
1097 user = insert(:user, local: true)
1099 refute User.superuser?(user)
1102 test "returns false for remote users" do
1103 user = insert(:user, local: false)
1104 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1106 refute User.superuser?(user)
1107 refute User.superuser?(remote_admin_user)
1110 test "returns true for local moderators" do
1111 user = insert(:user, local: true, info: %{is_moderator: true})
1113 assert User.superuser?(user)
1116 test "returns true for local admins" do
1117 user = insert(:user, local: true, info: %{is_admin: true})
1119 assert User.superuser?(user)
1123 describe "visible_for?/2" do
1124 test "returns true when the account is itself" do
1125 user = insert(:user, local: true)
1127 assert User.visible_for?(user, user)
1130 test "returns false when the account is unauthenticated and auth is required" do
1131 Pleroma.Config.put([:instance, :account_activation_required], true)
1133 user = insert(:user, local: true, info: %{confirmation_pending: true})
1134 other_user = insert(:user, local: true)
1136 refute User.visible_for?(user, other_user)
1138 Pleroma.Config.put([:instance, :account_activation_required], false)
1141 test "returns true when the account is unauthenticated and auth is not required" do
1142 user = insert(:user, local: true, info: %{confirmation_pending: true})
1143 other_user = insert(:user, local: true)
1145 assert User.visible_for?(user, other_user)
1148 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1149 Pleroma.Config.put([:instance, :account_activation_required], true)
1151 user = insert(:user, local: true, info: %{confirmation_pending: true})
1152 other_user = insert(:user, local: true, info: %{is_admin: true})
1154 assert User.visible_for?(user, other_user)
1156 Pleroma.Config.put([:instance, :account_activation_required], false)
1160 describe "parse_bio/2" do
1161 test "preserves hosts in user links text" do
1162 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1163 user = insert(:user)
1164 bio = "A.k.a. @nick@domain.com"
1167 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1169 }'>@<span>nick@domain.com</span></a></span>"
1171 assert expected_text == User.parse_bio(bio, user)
1174 test "Adds rel=me on linkbacked urls" do
1175 user = insert(:user, ap_id: "http://social.example.org/users/lain")
1177 bio = "http://example.org/rel_me/null"
1178 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1179 assert expected_text == User.parse_bio(bio, user)
1181 bio = "http://example.org/rel_me/link"
1182 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1183 assert expected_text == User.parse_bio(bio, user)
1185 bio = "http://example.org/rel_me/anchor"
1186 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1187 assert expected_text == User.parse_bio(bio, user)
1191 test "follower count is updated when a follower is blocked" do
1192 user = insert(:user)
1193 follower = insert(:user)
1194 follower2 = insert(:user)
1195 follower3 = insert(:user)
1197 {:ok, follower} = Pleroma.User.follow(follower, user)
1198 {:ok, _follower2} = Pleroma.User.follow(follower2, user)
1199 {:ok, _follower3} = Pleroma.User.follow(follower3, user)
1201 {:ok, _} = Pleroma.User.block(user, follower)
1203 user_show = Pleroma.Web.TwitterAPI.UserView.render("show.json", %{user: user})
1205 assert Map.get(user_show, "followers_count") == 2
1208 describe "toggle_confirmation/1" do
1209 test "if user is confirmed" do
1210 user = insert(:user, info: %{confirmation_pending: false})
1211 {:ok, user} = User.toggle_confirmation(user)
1213 assert user.info.confirmation_pending
1214 assert user.info.confirmation_token
1217 test "if user is unconfirmed" do
1218 user = insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
1219 {:ok, user} = User.toggle_confirmation(user)
1221 refute user.info.confirmation_pending
1222 refute user.info.confirmation_token