1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.Tests.ObanHelpers
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.CommonAPI
16 use Oban.Testing, repo: Pleroma.Repo
18 import Pleroma.Factory
19 import ExUnit.CaptureLog
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 setup do: clear_config([:instance, :account_activation_required])
28 describe "service actors" do
29 test "returns updated invisible actor" do
30 uri = "#{Pleroma.Web.Endpoint.url()}/relay"
31 followers_uri = "#{uri}/followers"
40 follower_address: followers_uri
44 actor = User.get_or_create_service_actor_by_ap_id(uri, "relay")
45 assert actor.invisible
48 test "returns relay user" do
49 uri = "#{Pleroma.Web.Endpoint.url()}/relay"
50 followers_uri = "#{uri}/followers"
57 follower_address: ^followers_uri
58 } = User.get_or_create_service_actor_by_ap_id(uri, "relay")
60 assert capture_log(fn ->
61 refute User.get_or_create_service_actor_by_ap_id("/relay", "relay")
62 end) =~ "Cannot create service actor:"
65 test "returns invisible actor" do
66 uri = "#{Pleroma.Web.Endpoint.url()}/internal/fetch-test"
67 followers_uri = "#{uri}/followers"
68 user = User.get_or_create_service_actor_by_ap_id(uri, "internal.fetch-test")
71 nickname: "internal.fetch-test",
75 follower_address: ^followers_uri
78 user2 = User.get_or_create_service_actor_by_ap_id(uri, "internal.fetch-test")
79 assert user.id == user2.id
83 describe "AP ID user relationships" do
85 {:ok, user: insert(:user)}
88 test "outgoing_relationships_ap_ids/1", %{user: user} do
89 rel_types = [:block, :mute, :notification_mute, :reblog_mute, :inverse_subscription]
97 insert_list(2, :user_relationship, %{source: user, relationship_type: rel_type})
99 ap_ids = Enum.map(rel_records, fn rr -> Repo.preload(rr, :target).target.ap_id end)
100 {rel_type, Enum.sort(ap_ids)}
104 assert ap_ids_by_rel[:block] == Enum.sort(User.blocked_users_ap_ids(user))
105 assert ap_ids_by_rel[:block] == Enum.sort(Enum.map(User.blocked_users(user), & &1.ap_id))
107 assert ap_ids_by_rel[:mute] == Enum.sort(User.muted_users_ap_ids(user))
108 assert ap_ids_by_rel[:mute] == Enum.sort(Enum.map(User.muted_users(user), & &1.ap_id))
110 assert ap_ids_by_rel[:notification_mute] ==
111 Enum.sort(User.notification_muted_users_ap_ids(user))
113 assert ap_ids_by_rel[:notification_mute] ==
114 Enum.sort(Enum.map(User.notification_muted_users(user), & &1.ap_id))
116 assert ap_ids_by_rel[:reblog_mute] == Enum.sort(User.reblog_muted_users_ap_ids(user))
118 assert ap_ids_by_rel[:reblog_mute] ==
119 Enum.sort(Enum.map(User.reblog_muted_users(user), & &1.ap_id))
121 assert ap_ids_by_rel[:inverse_subscription] == Enum.sort(User.subscriber_users_ap_ids(user))
123 assert ap_ids_by_rel[:inverse_subscription] ==
124 Enum.sort(Enum.map(User.subscriber_users(user), & &1.ap_id))
126 outgoing_relationships_ap_ids = User.outgoing_relationships_ap_ids(user, rel_types)
128 assert ap_ids_by_rel ==
129 Enum.into(outgoing_relationships_ap_ids, %{}, fn {k, v} -> {k, Enum.sort(v)} end)
133 describe "when tags are nil" do
134 test "tagging a user" do
135 user = insert(:user, %{tags: nil})
136 user = User.tag(user, ["cool", "dude"])
138 assert "cool" in user.tags
139 assert "dude" in user.tags
142 test "untagging a user" do
143 user = insert(:user, %{tags: nil})
144 user = User.untag(user, ["cool", "dude"])
146 assert user.tags == []
150 test "ap_id returns the activity pub id for the user" do
151 user = UserBuilder.build()
153 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
155 assert expected_ap_id == User.ap_id(user)
158 test "ap_followers returns the followers collection for the user" do
159 user = UserBuilder.build()
161 expected_followers_collection = "#{User.ap_id(user)}/followers"
163 assert expected_followers_collection == User.ap_followers(user)
166 test "ap_following returns the following collection for the user" do
167 user = UserBuilder.build()
169 expected_followers_collection = "#{User.ap_id(user)}/following"
171 assert expected_followers_collection == User.ap_following(user)
174 test "returns all pending follow requests" do
175 unlocked = insert(:user)
176 locked = insert(:user, locked: true)
177 follower = insert(:user)
179 CommonAPI.follow(follower, unlocked)
180 CommonAPI.follow(follower, locked)
182 assert [] = User.get_follow_requests(unlocked)
183 assert [activity] = User.get_follow_requests(locked)
188 test "doesn't return already accepted or duplicate follow requests" do
189 locked = insert(:user, locked: true)
190 pending_follower = insert(:user)
191 accepted_follower = insert(:user)
193 CommonAPI.follow(pending_follower, locked)
194 CommonAPI.follow(pending_follower, locked)
195 CommonAPI.follow(accepted_follower, locked)
197 Pleroma.FollowingRelationship.update(accepted_follower, locked, :follow_accept)
199 assert [^pending_follower] = User.get_follow_requests(locked)
202 test "doesn't return follow requests for deactivated accounts" do
203 locked = insert(:user, locked: true)
204 pending_follower = insert(:user, %{deactivated: true})
206 CommonAPI.follow(pending_follower, locked)
208 assert true == pending_follower.deactivated
209 assert [] = User.get_follow_requests(locked)
212 test "clears follow requests when requester is blocked" do
213 followed = insert(:user, locked: true)
214 follower = insert(:user)
216 CommonAPI.follow(follower, followed)
217 assert [_activity] = User.get_follow_requests(followed)
219 {:ok, _user_relationship} = User.block(followed, follower)
220 assert [] = User.get_follow_requests(followed)
223 test "follow_all follows mutliple users" do
225 followed_zero = insert(:user)
226 followed_one = insert(:user)
227 followed_two = insert(:user)
228 blocked = insert(:user)
229 not_followed = insert(:user)
230 reverse_blocked = insert(:user)
232 {:ok, _user_relationship} = User.block(user, blocked)
233 {:ok, _user_relationship} = User.block(reverse_blocked, user)
235 {:ok, user} = User.follow(user, followed_zero)
237 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
239 assert User.following?(user, followed_one)
240 assert User.following?(user, followed_two)
241 assert User.following?(user, followed_zero)
242 refute User.following?(user, not_followed)
243 refute User.following?(user, blocked)
244 refute User.following?(user, reverse_blocked)
247 test "follow_all follows mutliple users without duplicating" do
249 followed_zero = insert(:user)
250 followed_one = insert(:user)
251 followed_two = insert(:user)
253 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
254 assert length(User.following(user)) == 3
256 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
257 assert length(User.following(user)) == 4
260 test "follow takes a user and another user" do
262 followed = insert(:user)
264 {:ok, user} = User.follow(user, followed)
266 user = User.get_cached_by_id(user.id)
267 followed = User.get_cached_by_ap_id(followed.ap_id)
269 assert followed.follower_count == 1
270 assert user.following_count == 1
272 assert User.ap_followers(followed) in User.following(user)
275 test "can't follow a deactivated users" do
277 followed = insert(:user, %{deactivated: true})
279 {:error, _} = User.follow(user, followed)
282 test "can't follow a user who blocked us" do
283 blocker = insert(:user)
284 blockee = insert(:user)
286 {:ok, _user_relationship} = User.block(blocker, blockee)
288 {:error, _} = User.follow(blockee, blocker)
291 test "can't subscribe to a user who blocked us" do
292 blocker = insert(:user)
293 blocked = insert(:user)
295 {:ok, _user_relationship} = User.block(blocker, blocked)
297 {:error, _} = User.subscribe(blocked, blocker)
300 test "local users do not automatically follow local locked accounts" do
301 follower = insert(:user, locked: true)
302 followed = insert(:user, locked: true)
304 {:ok, follower} = User.maybe_direct_follow(follower, followed)
306 refute User.following?(follower, followed)
309 describe "unfollow/2" do
310 setup do: clear_config([:instance, :external_user_synchronization])
312 test "unfollow with syncronizes external user" do
313 Pleroma.Config.put([:instance, :external_user_synchronization], true)
318 follower_address: "http://localhost:4001/users/fuser1/followers",
319 following_address: "http://localhost:4001/users/fuser1/following",
320 ap_id: "http://localhost:4001/users/fuser1"
327 ap_id: "http://localhost:4001/users/fuser2",
328 follower_address: "http://localhost:4001/users/fuser2/followers",
329 following_address: "http://localhost:4001/users/fuser2/following"
332 {:ok, user} = User.follow(user, followed, :follow_accept)
334 {:ok, user, _activity} = User.unfollow(user, followed)
336 user = User.get_cached_by_id(user.id)
338 assert User.following(user) == []
341 test "unfollow takes a user and another user" do
342 followed = insert(:user)
345 {:ok, user} = User.follow(user, followed, :follow_accept)
347 assert User.following(user) == [user.follower_address, followed.follower_address]
349 {:ok, user, _activity} = User.unfollow(user, followed)
351 assert User.following(user) == [user.follower_address]
354 test "unfollow doesn't unfollow yourself" do
357 {:error, _} = User.unfollow(user, user)
359 assert User.following(user) == [user.follower_address]
363 test "test if a user is following another user" do
364 followed = insert(:user)
366 User.follow(user, followed, :follow_accept)
368 assert User.following?(user, followed)
369 refute User.following?(followed, user)
372 test "fetches correct profile for nickname beginning with number" do
373 # Use old-style integer ID to try to reproduce the problem
374 user = insert(:user, %{id: 1080})
375 user_with_numbers = insert(:user, %{nickname: "#{user.id}garbage"})
376 assert user_with_numbers == User.get_cached_by_nickname_or_id(user_with_numbers.nickname)
379 describe "user registration" do
385 password_confirmation: "test",
386 email: "email@example.com"
388 setup do: clear_config([:instance, :autofollowed_nicknames])
389 setup do: clear_config([:instance, :welcome_message])
390 setup do: clear_config([:instance, :welcome_user_nickname])
392 test "it autofollows accounts that are set for it" do
394 remote_user = insert(:user, %{local: false})
396 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
401 cng = User.register_changeset(%User{}, @full_user_data)
403 {:ok, registered_user} = User.register(cng)
405 assert User.following?(registered_user, user)
406 refute User.following?(registered_user, remote_user)
409 test "it sends a welcome message if it is set" do
410 welcome_user = insert(:user)
412 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
413 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
415 cng = User.register_changeset(%User{}, @full_user_data)
416 {:ok, registered_user} = User.register(cng)
418 activity = Repo.one(Pleroma.Activity)
419 assert registered_user.ap_id in activity.recipients
420 assert Object.normalize(activity).data["content"] =~ "cool site"
421 assert activity.actor == welcome_user.ap_id
424 setup do: clear_config([:instance, :account_activation_required])
426 test "it requires an email, name, nickname and password, bio is optional when account_activation_required is enabled" do
427 Pleroma.Config.put([:instance, :account_activation_required], true)
431 |> Enum.each(fn key ->
432 params = Map.delete(@full_user_data, key)
433 changeset = User.register_changeset(%User{}, params)
435 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
439 test "it requires an name, nickname and password, bio and email are optional when account_activation_required is disabled" do
440 Pleroma.Config.put([:instance, :account_activation_required], false)
444 |> Enum.each(fn key ->
445 params = Map.delete(@full_user_data, key)
446 changeset = User.register_changeset(%User{}, params)
448 assert if key in [:bio, :email], do: changeset.valid?, else: not changeset.valid?
452 test "it restricts certain nicknames" do
453 [restricted_name | _] = Pleroma.Config.get([User, :restricted_nicknames])
455 assert is_bitstring(restricted_name)
459 |> Map.put(:nickname, restricted_name)
461 changeset = User.register_changeset(%User{}, params)
463 refute changeset.valid?
466 test "it sets the password_hash and ap_id" do
467 changeset = User.register_changeset(%User{}, @full_user_data)
469 assert changeset.valid?
471 assert is_binary(changeset.changes[:password_hash])
472 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
474 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
478 describe "user registration, with :account_activation_required" do
484 password_confirmation: "test",
485 email: "email@example.com"
487 setup do: clear_config([:instance, :account_activation_required], true)
489 test "it creates unconfirmed user" do
490 changeset = User.register_changeset(%User{}, @full_user_data)
491 assert changeset.valid?
493 {:ok, user} = Repo.insert(changeset)
495 assert user.confirmation_pending
496 assert user.confirmation_token
499 test "it creates confirmed user if :confirmed option is given" do
500 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
501 assert changeset.valid?
503 {:ok, user} = Repo.insert(changeset)
505 refute user.confirmation_pending
506 refute user.confirmation_token
510 describe "get_or_fetch/1" do
511 test "gets an existing user by nickname" do
513 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
515 assert user == fetched_user
518 test "gets an existing user by ap_id" do
519 ap_id = "http://mastodon.example.org/users/admin"
525 nickname: "admin@mastodon.example.org",
529 {:ok, fetched_user} = User.get_or_fetch(ap_id)
530 freshed_user = refresh_record(user)
531 assert freshed_user == fetched_user
535 describe "fetching a user from nickname or trying to build one" do
536 test "gets an existing user" do
538 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
540 assert user == fetched_user
543 test "gets an existing user, case insensitive" do
544 user = insert(:user, nickname: "nick")
545 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
547 assert user == fetched_user
550 test "gets an existing user by fully qualified nickname" do
553 {:ok, fetched_user} =
554 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
556 assert user == fetched_user
559 test "gets an existing user by fully qualified nickname, case insensitive" do
560 user = insert(:user, nickname: "nick")
561 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
563 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
565 assert user == fetched_user
568 @tag capture_log: true
569 test "returns nil if no user could be fetched" do
570 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
571 assert fetched_user == "not found nonexistant@social.heldscal.la"
574 test "returns nil for nonexistant local user" do
575 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
576 assert fetched_user == "not found nonexistant"
579 test "updates an existing user, if stale" do
580 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
586 nickname: "admin@mastodon.example.org",
587 ap_id: "http://mastodon.example.org/users/admin",
588 last_refreshed_at: a_week_ago
591 assert orig_user.last_refreshed_at == a_week_ago
593 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
597 refute user.last_refreshed_at == orig_user.last_refreshed_at
600 test "if nicknames clash, the old user gets a prefix with the old id to the nickname" do
601 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
607 nickname: "admin@mastodon.example.org",
608 ap_id: "http://mastodon.example.org/users/harinezumigari",
609 last_refreshed_at: a_week_ago
612 assert orig_user.last_refreshed_at == a_week_ago
614 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
618 refute user.id == orig_user.id
620 orig_user = User.get_by_id(orig_user.id)
622 assert orig_user.nickname == "#{orig_user.id}.admin@mastodon.example.org"
625 @tag capture_log: true
626 test "it returns the old user if stale, but unfetchable" do
627 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
633 nickname: "admin@mastodon.example.org",
634 ap_id: "http://mastodon.example.org/users/raymoo",
635 last_refreshed_at: a_week_ago
638 assert orig_user.last_refreshed_at == a_week_ago
640 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/raymoo")
642 assert user.last_refreshed_at == orig_user.last_refreshed_at
646 test "returns an ap_id for a user" do
649 assert User.ap_id(user) ==
650 Pleroma.Web.Router.Helpers.user_feed_url(
651 Pleroma.Web.Endpoint,
657 test "returns an ap_followers link for a user" do
660 assert User.ap_followers(user) ==
661 Pleroma.Web.Router.Helpers.user_feed_url(
662 Pleroma.Web.Endpoint,
668 describe "remote user changeset" do
674 avatar: %{some: "avatar"}
676 setup do: clear_config([:instance, :user_bio_length])
677 setup do: clear_config([:instance, :user_name_length])
679 test "it confirms validity" do
680 cs = User.remote_user_changeset(@valid_remote)
684 test "it sets the follower_adress" do
685 cs = User.remote_user_changeset(@valid_remote)
686 # remote users get a fake local follower address
687 assert cs.changes.follower_address ==
688 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
691 test "it enforces the fqn format for nicknames" do
692 cs = User.remote_user_changeset(%{@valid_remote | nickname: "bla"})
693 assert Ecto.Changeset.get_field(cs, :local) == false
694 assert cs.changes.avatar
698 test "it has required fields" do
700 |> Enum.each(fn field ->
701 cs = User.remote_user_changeset(Map.delete(@valid_remote, field))
707 describe "followers and friends" do
708 test "gets all followers for a given user" do
710 follower_one = insert(:user)
711 follower_two = insert(:user)
712 not_follower = insert(:user)
714 {:ok, follower_one} = User.follow(follower_one, user)
715 {:ok, follower_two} = User.follow(follower_two, user)
717 res = User.get_followers(user)
719 assert Enum.member?(res, follower_one)
720 assert Enum.member?(res, follower_two)
721 refute Enum.member?(res, not_follower)
724 test "gets all friends (followed users) for a given user" do
726 followed_one = insert(:user)
727 followed_two = insert(:user)
728 not_followed = insert(:user)
730 {:ok, user} = User.follow(user, followed_one)
731 {:ok, user} = User.follow(user, followed_two)
733 res = User.get_friends(user)
735 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
736 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
737 assert Enum.member?(res, followed_one)
738 assert Enum.member?(res, followed_two)
739 refute Enum.member?(res, not_followed)
743 describe "updating note and follower count" do
744 test "it sets the note_count property" do
747 user = User.get_cached_by_ap_id(note.data["actor"])
749 assert user.note_count == 0
751 {:ok, user} = User.update_note_count(user)
753 assert user.note_count == 1
756 test "it increases the note_count property" do
758 user = User.get_cached_by_ap_id(note.data["actor"])
760 assert user.note_count == 0
762 {:ok, user} = User.increase_note_count(user)
764 assert user.note_count == 1
766 {:ok, user} = User.increase_note_count(user)
768 assert user.note_count == 2
771 test "it decreases the note_count property" do
773 user = User.get_cached_by_ap_id(note.data["actor"])
775 assert user.note_count == 0
777 {:ok, user} = User.increase_note_count(user)
779 assert user.note_count == 1
781 {:ok, user} = User.decrease_note_count(user)
783 assert user.note_count == 0
785 {:ok, user} = User.decrease_note_count(user)
787 assert user.note_count == 0
790 test "it sets the follower_count property" do
792 follower = insert(:user)
794 User.follow(follower, user)
796 assert user.follower_count == 0
798 {:ok, user} = User.update_follower_count(user)
800 assert user.follower_count == 1
804 describe "follow_import" do
805 test "it imports user followings from list" do
806 [user1, user2, user3] = insert_list(3, :user)
813 {:ok, job} = User.follow_import(user1, identifiers)
815 assert {:ok, result} = ObanHelpers.perform(job)
816 assert is_list(result)
817 assert result == [user2, user3]
822 test "it mutes people" do
824 muted_user = insert(:user)
826 refute User.mutes?(user, muted_user)
827 refute User.muted_notifications?(user, muted_user)
829 {:ok, _user_relationships} = User.mute(user, muted_user)
831 assert User.mutes?(user, muted_user)
832 assert User.muted_notifications?(user, muted_user)
835 test "it unmutes users" do
837 muted_user = insert(:user)
839 {:ok, _user_relationships} = User.mute(user, muted_user)
840 {:ok, _user_mute} = User.unmute(user, muted_user)
842 refute User.mutes?(user, muted_user)
843 refute User.muted_notifications?(user, muted_user)
846 test "it mutes user without notifications" do
848 muted_user = insert(:user)
850 refute User.mutes?(user, muted_user)
851 refute User.muted_notifications?(user, muted_user)
853 {:ok, _user_relationships} = User.mute(user, muted_user, false)
855 assert User.mutes?(user, muted_user)
856 refute User.muted_notifications?(user, muted_user)
861 test "it blocks people" do
863 blocked_user = insert(:user)
865 refute User.blocks?(user, blocked_user)
867 {:ok, _user_relationship} = User.block(user, blocked_user)
869 assert User.blocks?(user, blocked_user)
872 test "it unblocks users" do
874 blocked_user = insert(:user)
876 {:ok, _user_relationship} = User.block(user, blocked_user)
877 {:ok, _user_block} = User.unblock(user, blocked_user)
879 refute User.blocks?(user, blocked_user)
882 test "blocks tear down cyclical follow relationships" do
883 blocker = insert(:user)
884 blocked = insert(:user)
886 {:ok, blocker} = User.follow(blocker, blocked)
887 {:ok, blocked} = User.follow(blocked, blocker)
889 assert User.following?(blocker, blocked)
890 assert User.following?(blocked, blocker)
892 {:ok, _user_relationship} = User.block(blocker, blocked)
893 blocked = User.get_cached_by_id(blocked.id)
895 assert User.blocks?(blocker, blocked)
897 refute User.following?(blocker, blocked)
898 refute User.following?(blocked, blocker)
901 test "blocks tear down blocker->blocked follow relationships" do
902 blocker = insert(:user)
903 blocked = insert(:user)
905 {:ok, blocker} = User.follow(blocker, blocked)
907 assert User.following?(blocker, blocked)
908 refute User.following?(blocked, blocker)
910 {:ok, _user_relationship} = User.block(blocker, blocked)
911 blocked = User.get_cached_by_id(blocked.id)
913 assert User.blocks?(blocker, blocked)
915 refute User.following?(blocker, blocked)
916 refute User.following?(blocked, blocker)
919 test "blocks tear down blocked->blocker follow relationships" do
920 blocker = insert(:user)
921 blocked = insert(:user)
923 {:ok, blocked} = User.follow(blocked, blocker)
925 refute User.following?(blocker, blocked)
926 assert User.following?(blocked, blocker)
928 {:ok, _user_relationship} = User.block(blocker, blocked)
929 blocked = User.get_cached_by_id(blocked.id)
931 assert User.blocks?(blocker, blocked)
933 refute User.following?(blocker, blocked)
934 refute User.following?(blocked, blocker)
937 test "blocks tear down blocked->blocker subscription relationships" do
938 blocker = insert(:user)
939 blocked = insert(:user)
941 {:ok, _subscription} = User.subscribe(blocked, blocker)
943 assert User.subscribed_to?(blocked, blocker)
944 refute User.subscribed_to?(blocker, blocked)
946 {:ok, _user_relationship} = User.block(blocker, blocked)
948 assert User.blocks?(blocker, blocked)
949 refute User.subscribed_to?(blocker, blocked)
950 refute User.subscribed_to?(blocked, blocker)
954 describe "domain blocking" do
955 test "blocks domains" do
957 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
959 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
961 assert User.blocks?(user, collateral_user)
964 test "does not block domain with same end" do
968 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
970 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
972 refute User.blocks?(user, collateral_user)
975 test "does not block domain with same end if wildcard added" do
979 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
981 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
983 refute User.blocks?(user, collateral_user)
986 test "blocks domain with wildcard for subdomain" do
989 user_from_subdomain =
990 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
992 user_with_two_subdomains =
994 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
997 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
999 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
1001 assert User.blocks?(user, user_from_subdomain)
1002 assert User.blocks?(user, user_with_two_subdomains)
1003 assert User.blocks?(user, user_domain)
1006 test "unblocks domains" do
1007 user = insert(:user)
1008 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
1010 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
1011 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
1013 refute User.blocks?(user, collateral_user)
1016 test "follows take precedence over domain blocks" do
1017 user = insert(:user)
1018 good_eggo = insert(:user, %{ap_id: "https://meanies.social/user/cuteposter"})
1020 {:ok, user} = User.block_domain(user, "meanies.social")
1021 {:ok, user} = User.follow(user, good_eggo)
1023 refute User.blocks?(user, good_eggo)
1027 describe "blocks_import" do
1028 test "it imports user blocks from list" do
1029 [user1, user2, user3] = insert_list(3, :user)
1036 {:ok, job} = User.blocks_import(user1, identifiers)
1038 assert {:ok, result} = ObanHelpers.perform(job)
1039 assert is_list(result)
1040 assert result == [user2, user3]
1044 describe "get_recipients_from_activity" do
1045 test "works for announces" do
1046 actor = insert(:user)
1047 user = insert(:user, local: true)
1049 {:ok, activity} = CommonAPI.post(actor, %{status: "hello"})
1050 {:ok, announce} = CommonAPI.repeat(activity.id, user)
1052 recipients = User.get_recipients_from_activity(announce)
1054 assert user in recipients
1057 test "get recipients" do
1058 actor = insert(:user)
1059 user = insert(:user, local: true)
1060 user_two = insert(:user, local: false)
1061 addressed = insert(:user, local: true)
1062 addressed_remote = insert(:user, local: false)
1065 CommonAPI.post(actor, %{
1066 status: "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
1069 assert Enum.map([actor, addressed], & &1.ap_id) --
1070 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1072 {:ok, user} = User.follow(user, actor)
1073 {:ok, _user_two} = User.follow(user_two, actor)
1074 recipients = User.get_recipients_from_activity(activity)
1075 assert length(recipients) == 3
1076 assert user in recipients
1077 assert addressed in recipients
1080 test "has following" do
1081 actor = insert(:user)
1082 user = insert(:user)
1083 user_two = insert(:user)
1084 addressed = insert(:user, local: true)
1087 CommonAPI.post(actor, %{
1088 status: "hey @#{addressed.nickname}"
1091 assert Enum.map([actor, addressed], & &1.ap_id) --
1092 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1094 {:ok, _actor} = User.follow(actor, user)
1095 {:ok, _actor} = User.follow(actor, user_two)
1096 recipients = User.get_recipients_from_activity(activity)
1097 assert length(recipients) == 2
1098 assert addressed in recipients
1102 describe ".deactivate" do
1103 test "can de-activate then re-activate a user" do
1104 user = insert(:user)
1105 assert false == user.deactivated
1106 {:ok, user} = User.deactivate(user)
1107 assert true == user.deactivated
1108 {:ok, user} = User.deactivate(user, false)
1109 assert false == user.deactivated
1112 test "hide a user from followers" do
1113 user = insert(:user)
1114 user2 = insert(:user)
1116 {:ok, user} = User.follow(user, user2)
1117 {:ok, _user} = User.deactivate(user)
1119 user2 = User.get_cached_by_id(user2.id)
1121 assert user2.follower_count == 0
1122 assert [] = User.get_followers(user2)
1125 test "hide a user from friends" do
1126 user = insert(:user)
1127 user2 = insert(:user)
1129 {:ok, user2} = User.follow(user2, user)
1130 assert user2.following_count == 1
1131 assert User.following_count(user2) == 1
1133 {:ok, _user} = User.deactivate(user)
1135 user2 = User.get_cached_by_id(user2.id)
1137 assert refresh_record(user2).following_count == 0
1138 assert user2.following_count == 0
1139 assert User.following_count(user2) == 0
1140 assert [] = User.get_friends(user2)
1143 test "hide a user's statuses from timelines and notifications" do
1144 user = insert(:user)
1145 user2 = insert(:user)
1147 {:ok, user2} = User.follow(user2, user)
1149 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{user2.nickname}"})
1151 activity = Repo.preload(activity, :bookmark)
1153 [notification] = Pleroma.Notification.for_user(user2)
1154 assert notification.activity.id == activity.id
1156 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
1158 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
1159 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1163 {:ok, _user} = User.deactivate(user)
1165 assert [] == ActivityPub.fetch_public_activities(%{})
1166 assert [] == Pleroma.Notification.for_user(user2)
1169 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1175 describe "delete" do
1177 {:ok, user} = insert(:user) |> User.set_cache()
1182 setup do: clear_config([:instance, :federating])
1184 test ".delete_user_activities deletes all create activities", %{user: user} do
1185 {:ok, activity} = CommonAPI.post(user, %{status: "2hu"})
1187 User.delete_user_activities(user)
1189 # TODO: Test removal favorites, repeats, delete activities.
1190 refute Activity.get_by_id(activity.id)
1193 test "it deactivates a user, all follow relationships and all activities", %{user: user} do
1194 follower = insert(:user)
1195 {:ok, follower} = User.follow(follower, user)
1197 locked_user = insert(:user, name: "locked", locked: true)
1198 {:ok, _} = User.follow(user, locked_user, :follow_pending)
1200 object = insert(:note, user: user)
1201 activity = insert(:note_activity, user: user, note: object)
1203 object_two = insert(:note, user: follower)
1204 activity_two = insert(:note_activity, user: follower, note: object_two)
1206 {:ok, like} = CommonAPI.favorite(user, activity_two.id)
1207 {:ok, like_two} = CommonAPI.favorite(follower, activity.id)
1208 {:ok, repeat} = CommonAPI.repeat(activity_two.id, user)
1210 {:ok, job} = User.delete(user)
1211 {:ok, _user} = ObanHelpers.perform(job)
1213 follower = User.get_cached_by_id(follower.id)
1215 refute User.following?(follower, user)
1216 assert %{deactivated: true} = User.get_by_id(user.id)
1218 assert [] == User.get_follow_requests(locked_user)
1222 |> Activity.Queries.by_actor()
1224 |> Enum.map(fn act -> act.data["type"] end)
1226 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1228 refute Activity.get_by_id(activity.id)
1229 refute Activity.get_by_id(like.id)
1230 refute Activity.get_by_id(like_two.id)
1231 refute Activity.get_by_id(repeat.id)
1235 describe "delete/1 when confirmation is pending" do
1237 user = insert(:user, confirmation_pending: true)
1241 test "deletes user from database when activation required", %{user: user} do
1242 clear_config([:instance, :account_activation_required], true)
1244 {:ok, job} = User.delete(user)
1245 {:ok, _} = ObanHelpers.perform(job)
1247 refute User.get_cached_by_id(user.id)
1248 refute User.get_by_id(user.id)
1251 test "deactivates user when activation is not required", %{user: user} do
1252 clear_config([:instance, :account_activation_required], false)
1254 {:ok, job} = User.delete(user)
1255 {:ok, _} = ObanHelpers.perform(job)
1257 assert %{deactivated: true} = User.get_cached_by_id(user.id)
1258 assert %{deactivated: true} = User.get_by_id(user.id)
1262 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1263 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1266 describe "per-user rich-text filtering" do
1267 test "html_filter_policy returns default policies, when rich-text is enabled" do
1268 user = insert(:user)
1270 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1273 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1274 user = insert(:user, no_rich_text: true)
1276 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1280 describe "caching" do
1281 test "invalidate_cache works" do
1282 user = insert(:user)
1284 User.set_cache(user)
1285 User.invalidate_cache(user)
1287 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1288 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1291 test "User.delete() plugs any possible zombie objects" do
1292 user = insert(:user)
1294 {:ok, job} = User.delete(user)
1295 {:ok, _} = ObanHelpers.perform(job)
1297 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1299 assert cached_user != user
1301 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1303 assert cached_user != user
1307 describe "account_status/1" do
1308 setup do: clear_config([:instance, :account_activation_required])
1310 test "return confirmation_pending for unconfirm user" do
1311 Pleroma.Config.put([:instance, :account_activation_required], true)
1312 user = insert(:user, confirmation_pending: true)
1313 assert User.account_status(user) == :confirmation_pending
1316 test "return active for confirmed user" do
1317 Pleroma.Config.put([:instance, :account_activation_required], true)
1318 user = insert(:user, confirmation_pending: false)
1319 assert User.account_status(user) == :active
1322 test "return active for remote user" do
1323 user = insert(:user, local: false)
1324 assert User.account_status(user) == :active
1327 test "returns :password_reset_pending for user with reset password" do
1328 user = insert(:user, password_reset_pending: true)
1329 assert User.account_status(user) == :password_reset_pending
1332 test "returns :deactivated for deactivated user" do
1333 user = insert(:user, local: true, confirmation_pending: false, deactivated: true)
1334 assert User.account_status(user) == :deactivated
1338 describe "superuser?/1" do
1339 test "returns false for unprivileged users" do
1340 user = insert(:user, local: true)
1342 refute User.superuser?(user)
1345 test "returns false for remote users" do
1346 user = insert(:user, local: false)
1347 remote_admin_user = insert(:user, local: false, is_admin: true)
1349 refute User.superuser?(user)
1350 refute User.superuser?(remote_admin_user)
1353 test "returns true for local moderators" do
1354 user = insert(:user, local: true, is_moderator: true)
1356 assert User.superuser?(user)
1359 test "returns true for local admins" do
1360 user = insert(:user, local: true, is_admin: true)
1362 assert User.superuser?(user)
1366 describe "invisible?/1" do
1367 test "returns true for an invisible user" do
1368 user = insert(:user, local: true, invisible: true)
1370 assert User.invisible?(user)
1373 test "returns false for a non-invisible user" do
1374 user = insert(:user, local: true)
1376 refute User.invisible?(user)
1380 describe "visible_for/2" do
1381 test "returns true when the account is itself" do
1382 user = insert(:user, local: true)
1384 assert User.visible_for(user, user) == :visible
1387 test "returns false when the account is unauthenticated and auth is required" do
1388 Pleroma.Config.put([:instance, :account_activation_required], true)
1390 user = insert(:user, local: true, confirmation_pending: true)
1391 other_user = insert(:user, local: true)
1393 refute User.visible_for(user, other_user) == :visible
1396 test "returns true when the account is unauthenticated and auth is not required" do
1397 user = insert(:user, local: true, confirmation_pending: true)
1398 other_user = insert(:user, local: true)
1400 assert User.visible_for(user, other_user) == :visible
1403 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1404 Pleroma.Config.put([:instance, :account_activation_required], true)
1406 user = insert(:user, local: true, confirmation_pending: true)
1407 other_user = insert(:user, local: true, is_admin: true)
1409 assert User.visible_for(user, other_user) == :visible
1413 describe "parse_bio/2" do
1414 test "preserves hosts in user links text" do
1415 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1416 user = insert(:user)
1417 bio = "A.k.a. @nick@domain.com"
1420 ~s(A.k.a. <span class="h-card"><a class="u-url mention" data-user="#{remote_user.id}" href="#{
1422 }" rel="ugc">@<span>nick@domain.com</span></a></span>)
1424 assert expected_text == User.parse_bio(bio, user)
1427 test "Adds rel=me on linkbacked urls" do
1428 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1430 bio = "http://example.com/rel_me/null"
1431 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1432 assert expected_text == User.parse_bio(bio, user)
1434 bio = "http://example.com/rel_me/link"
1435 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1436 assert expected_text == User.parse_bio(bio, user)
1438 bio = "http://example.com/rel_me/anchor"
1439 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1440 assert expected_text == User.parse_bio(bio, user)
1444 test "follower count is updated when a follower is blocked" do
1445 user = insert(:user)
1446 follower = insert(:user)
1447 follower2 = insert(:user)
1448 follower3 = insert(:user)
1450 {:ok, follower} = User.follow(follower, user)
1451 {:ok, _follower2} = User.follow(follower2, user)
1452 {:ok, _follower3} = User.follow(follower3, user)
1454 {:ok, _user_relationship} = User.block(user, follower)
1455 user = refresh_record(user)
1457 assert user.follower_count == 2
1460 describe "list_inactive_users_query/1" do
1461 defp days_ago(days) do
1463 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1464 -days * 60 * 60 * 24,
1469 test "Users are inactive by default" do
1473 Enum.map(1..total, fn _ ->
1474 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1477 inactive_users_ids =
1478 Pleroma.User.list_inactive_users_query()
1479 |> Pleroma.Repo.all()
1480 |> Enum.map(& &1.id)
1482 Enum.each(users, fn user ->
1483 assert user.id in inactive_users_ids
1487 test "Only includes users who has no recent activity" do
1491 Enum.map(1..total, fn _ ->
1492 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1495 {inactive, active} = Enum.split(users, trunc(total / 2))
1497 Enum.map(active, fn user ->
1498 to = Enum.random(users -- [user])
1501 CommonAPI.post(user, %{
1502 status: "hey @#{to.nickname}"
1506 inactive_users_ids =
1507 Pleroma.User.list_inactive_users_query()
1508 |> Pleroma.Repo.all()
1509 |> Enum.map(& &1.id)
1511 Enum.each(active, fn user ->
1512 refute user.id in inactive_users_ids
1515 Enum.each(inactive, fn user ->
1516 assert user.id in inactive_users_ids
1520 test "Only includes users with no read notifications" do
1524 Enum.map(1..total, fn _ ->
1525 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1528 [sender | recipients] = users
1529 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1531 Enum.each(recipients, fn to ->
1533 CommonAPI.post(sender, %{
1534 status: "hey @#{to.nickname}"
1538 CommonAPI.post(sender, %{
1539 status: "hey again @#{to.nickname}"
1543 Enum.each(active, fn user ->
1544 [n1, _n2] = Pleroma.Notification.for_user(user)
1545 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1548 inactive_users_ids =
1549 Pleroma.User.list_inactive_users_query()
1550 |> Pleroma.Repo.all()
1551 |> Enum.map(& &1.id)
1553 Enum.each(active, fn user ->
1554 refute user.id in inactive_users_ids
1557 Enum.each(inactive, fn user ->
1558 assert user.id in inactive_users_ids
1563 describe "toggle_confirmation/1" do
1564 test "if user is confirmed" do
1565 user = insert(:user, confirmation_pending: false)
1566 {:ok, user} = User.toggle_confirmation(user)
1568 assert user.confirmation_pending
1569 assert user.confirmation_token
1572 test "if user is unconfirmed" do
1573 user = insert(:user, confirmation_pending: true, confirmation_token: "some token")
1574 {:ok, user} = User.toggle_confirmation(user)
1576 refute user.confirmation_pending
1577 refute user.confirmation_token
1581 describe "ensure_keys_present" do
1582 test "it creates keys for a user and stores them in info" do
1583 user = insert(:user)
1584 refute is_binary(user.keys)
1585 {:ok, user} = User.ensure_keys_present(user)
1586 assert is_binary(user.keys)
1589 test "it doesn't create keys if there already are some" do
1590 user = insert(:user, keys: "xxx")
1591 {:ok, user} = User.ensure_keys_present(user)
1592 assert user.keys == "xxx"
1596 describe "get_ap_ids_by_nicknames" do
1597 test "it returns a list of AP ids for a given set of nicknames" do
1598 user = insert(:user)
1599 user_two = insert(:user)
1601 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1602 assert length(ap_ids) == 2
1603 assert user.ap_id in ap_ids
1604 assert user_two.ap_id in ap_ids
1608 describe "sync followers count" do
1610 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1611 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1612 insert(:user, local: true)
1613 insert(:user, local: false, deactivated: true)
1614 {:ok, user1: user1, user2: user2}
1617 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1618 [fdb_user1] = User.external_users(limit: 1)
1620 assert fdb_user1.ap_id
1621 assert fdb_user1.ap_id == user1.ap_id
1622 assert fdb_user1.id == user1.id
1624 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1626 assert fdb_user2.ap_id
1627 assert fdb_user2.ap_id == user2.ap_id
1628 assert fdb_user2.id == user2.id
1630 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1634 describe "is_internal_user?/1" do
1635 test "non-internal user returns false" do
1636 user = insert(:user)
1637 refute User.is_internal_user?(user)
1640 test "user with no nickname returns true" do
1641 user = insert(:user, %{nickname: nil})
1642 assert User.is_internal_user?(user)
1645 test "user with internal-prefixed nickname returns true" do
1646 user = insert(:user, %{nickname: "internal.test"})
1647 assert User.is_internal_user?(user)
1651 describe "update_and_set_cache/1" do
1652 test "returns error when user is stale instead Ecto.StaleEntryError" do
1653 user = insert(:user)
1655 changeset = Ecto.Changeset.change(user, bio: "test")
1659 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1660 User.update_and_set_cache(changeset)
1663 test "performs update cache if user updated" do
1664 user = insert(:user)
1665 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1667 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1669 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1670 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1671 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1675 describe "following/followers synchronization" do
1676 setup do: clear_config([:instance, :external_user_synchronization])
1678 test "updates the counters normally on following/getting a follow when disabled" do
1679 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1680 user = insert(:user)
1685 follower_address: "http://localhost:4001/users/masto_closed/followers",
1686 following_address: "http://localhost:4001/users/masto_closed/following",
1690 assert other_user.following_count == 0
1691 assert other_user.follower_count == 0
1693 {:ok, user} = Pleroma.User.follow(user, other_user)
1694 other_user = Pleroma.User.get_by_id(other_user.id)
1696 assert user.following_count == 1
1697 assert other_user.follower_count == 1
1700 test "syncronizes the counters with the remote instance for the followed when enabled" do
1701 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1703 user = insert(:user)
1708 follower_address: "http://localhost:4001/users/masto_closed/followers",
1709 following_address: "http://localhost:4001/users/masto_closed/following",
1713 assert other_user.following_count == 0
1714 assert other_user.follower_count == 0
1716 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1717 {:ok, _user} = User.follow(user, other_user)
1718 other_user = User.get_by_id(other_user.id)
1720 assert other_user.follower_count == 437
1723 test "syncronizes the counters with the remote instance for the follower when enabled" do
1724 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1726 user = insert(:user)
1731 follower_address: "http://localhost:4001/users/masto_closed/followers",
1732 following_address: "http://localhost:4001/users/masto_closed/following",
1736 assert other_user.following_count == 0
1737 assert other_user.follower_count == 0
1739 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1740 {:ok, other_user} = User.follow(other_user, user)
1742 assert other_user.following_count == 152
1746 describe "change_email/2" do
1748 [user: insert(:user)]
1751 test "blank email returns error", %{user: user} do
1752 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1753 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1756 test "non unique email returns error", %{user: user} do
1757 %{email: email} = insert(:user)
1759 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1760 User.change_email(user, email)
1763 test "invalid email returns error", %{user: user} do
1764 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1765 User.change_email(user, "cofe")
1768 test "changes email", %{user: user} do
1769 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1773 describe "get_cached_by_nickname_or_id" do
1775 local_user = insert(:user)
1776 remote_user = insert(:user, nickname: "nickname@example.com", local: false)
1778 [local_user: local_user, remote_user: remote_user]
1781 setup do: clear_config([:instance, :limit_to_local_content])
1783 test "allows getting remote users by id no matter what :limit_to_local_content is set to", %{
1784 remote_user: remote_user
1786 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1787 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1789 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1790 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1792 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1793 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1796 test "disallows getting remote users by nickname without authentication when :limit_to_local_content is set to :unauthenticated",
1797 %{remote_user: remote_user} do
1798 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1799 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1802 test "allows getting remote users by nickname with authentication when :limit_to_local_content is set to :unauthenticated",
1803 %{remote_user: remote_user, local_user: local_user} do
1804 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1805 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.nickname, for: local_user)
1808 test "disallows getting remote users by nickname when :limit_to_local_content is set to true",
1809 %{remote_user: remote_user} do
1810 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1811 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1814 test "allows getting local users by nickname no matter what :limit_to_local_content is set to",
1815 %{local_user: local_user} do
1816 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1817 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1819 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1820 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1822 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1823 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1827 describe "update_email_notifications/2" do
1829 user = insert(:user, email_notifications: %{"digest" => true})
1834 test "Notifications are updated", %{user: user} do
1835 true = user.email_notifications["digest"]
1836 assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false})
1837 assert result.email_notifications["digest"] == false
1841 test "avatar fallback" do
1842 user = insert(:user)
1843 assert User.avatar_url(user) =~ "/images/avi.png"
1845 clear_config([:assets, :default_user_avatar], "avatar.png")
1847 user = User.get_cached_by_nickname_or_id(user.nickname)
1848 assert User.avatar_url(user) =~ "avatar.png"
1850 assert User.avatar_url(user, no_default: true) == nil