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 sets the 'accepts_chat_messages' set to true" do
490 changeset = User.register_changeset(%User{}, @full_user_data)
491 assert changeset.valid?
493 {:ok, user} = Repo.insert(changeset)
495 assert user.accepts_chat_messages
498 test "it creates unconfirmed user" do
499 changeset = User.register_changeset(%User{}, @full_user_data)
500 assert changeset.valid?
502 {:ok, user} = Repo.insert(changeset)
504 assert user.confirmation_pending
505 assert user.confirmation_token
508 test "it creates confirmed user if :confirmed option is given" do
509 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
510 assert changeset.valid?
512 {:ok, user} = Repo.insert(changeset)
514 refute user.confirmation_pending
515 refute user.confirmation_token
519 describe "get_or_fetch/1" do
520 test "gets an existing user by nickname" do
522 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
524 assert user == fetched_user
527 test "gets an existing user by ap_id" do
528 ap_id = "http://mastodon.example.org/users/admin"
534 nickname: "admin@mastodon.example.org",
538 {:ok, fetched_user} = User.get_or_fetch(ap_id)
539 freshed_user = refresh_record(user)
540 assert freshed_user == fetched_user
544 describe "fetching a user from nickname or trying to build one" do
545 test "gets an existing user" do
547 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
549 assert user == fetched_user
552 test "gets an existing user, case insensitive" do
553 user = insert(:user, nickname: "nick")
554 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
556 assert user == fetched_user
559 test "gets an existing user by fully qualified nickname" do
562 {:ok, fetched_user} =
563 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
565 assert user == fetched_user
568 test "gets an existing user by fully qualified nickname, case insensitive" do
569 user = insert(:user, nickname: "nick")
570 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
572 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
574 assert user == fetched_user
577 @tag capture_log: true
578 test "returns nil if no user could be fetched" do
579 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
580 assert fetched_user == "not found nonexistant@social.heldscal.la"
583 test "returns nil for nonexistant local user" do
584 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
585 assert fetched_user == "not found nonexistant"
588 test "updates an existing user, if stale" do
589 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
595 nickname: "admin@mastodon.example.org",
596 ap_id: "http://mastodon.example.org/users/admin",
597 last_refreshed_at: a_week_ago
600 assert orig_user.last_refreshed_at == a_week_ago
602 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
606 refute user.last_refreshed_at == orig_user.last_refreshed_at
609 test "if nicknames clash, the old user gets a prefix with the old id to the nickname" do
610 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
616 nickname: "admin@mastodon.example.org",
617 ap_id: "http://mastodon.example.org/users/harinezumigari",
618 last_refreshed_at: a_week_ago
621 assert orig_user.last_refreshed_at == a_week_ago
623 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
627 refute user.id == orig_user.id
629 orig_user = User.get_by_id(orig_user.id)
631 assert orig_user.nickname == "#{orig_user.id}.admin@mastodon.example.org"
634 @tag capture_log: true
635 test "it returns the old user if stale, but unfetchable" do
636 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
642 nickname: "admin@mastodon.example.org",
643 ap_id: "http://mastodon.example.org/users/raymoo",
644 last_refreshed_at: a_week_ago
647 assert orig_user.last_refreshed_at == a_week_ago
649 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/raymoo")
651 assert user.last_refreshed_at == orig_user.last_refreshed_at
655 test "returns an ap_id for a user" do
658 assert User.ap_id(user) ==
659 Pleroma.Web.Router.Helpers.user_feed_url(
660 Pleroma.Web.Endpoint,
666 test "returns an ap_followers link for a user" do
669 assert User.ap_followers(user) ==
670 Pleroma.Web.Router.Helpers.user_feed_url(
671 Pleroma.Web.Endpoint,
677 describe "remote user changeset" do
683 avatar: %{some: "avatar"}
685 setup do: clear_config([:instance, :user_bio_length])
686 setup do: clear_config([:instance, :user_name_length])
688 test "it confirms validity" do
689 cs = User.remote_user_changeset(@valid_remote)
693 test "it sets the follower_adress" do
694 cs = User.remote_user_changeset(@valid_remote)
695 # remote users get a fake local follower address
696 assert cs.changes.follower_address ==
697 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
700 test "it enforces the fqn format for nicknames" do
701 cs = User.remote_user_changeset(%{@valid_remote | nickname: "bla"})
702 assert Ecto.Changeset.get_field(cs, :local) == false
703 assert cs.changes.avatar
707 test "it has required fields" do
709 |> Enum.each(fn field ->
710 cs = User.remote_user_changeset(Map.delete(@valid_remote, field))
716 describe "followers and friends" do
717 test "gets all followers for a given user" do
719 follower_one = insert(:user)
720 follower_two = insert(:user)
721 not_follower = insert(:user)
723 {:ok, follower_one} = User.follow(follower_one, user)
724 {:ok, follower_two} = User.follow(follower_two, user)
726 res = User.get_followers(user)
728 assert Enum.member?(res, follower_one)
729 assert Enum.member?(res, follower_two)
730 refute Enum.member?(res, not_follower)
733 test "gets all friends (followed users) for a given user" do
735 followed_one = insert(:user)
736 followed_two = insert(:user)
737 not_followed = insert(:user)
739 {:ok, user} = User.follow(user, followed_one)
740 {:ok, user} = User.follow(user, followed_two)
742 res = User.get_friends(user)
744 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
745 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
746 assert Enum.member?(res, followed_one)
747 assert Enum.member?(res, followed_two)
748 refute Enum.member?(res, not_followed)
752 describe "updating note and follower count" do
753 test "it sets the note_count property" do
756 user = User.get_cached_by_ap_id(note.data["actor"])
758 assert user.note_count == 0
760 {:ok, user} = User.update_note_count(user)
762 assert user.note_count == 1
765 test "it increases the note_count property" do
767 user = User.get_cached_by_ap_id(note.data["actor"])
769 assert user.note_count == 0
771 {:ok, user} = User.increase_note_count(user)
773 assert user.note_count == 1
775 {:ok, user} = User.increase_note_count(user)
777 assert user.note_count == 2
780 test "it decreases the note_count property" do
782 user = User.get_cached_by_ap_id(note.data["actor"])
784 assert user.note_count == 0
786 {:ok, user} = User.increase_note_count(user)
788 assert user.note_count == 1
790 {:ok, user} = User.decrease_note_count(user)
792 assert user.note_count == 0
794 {:ok, user} = User.decrease_note_count(user)
796 assert user.note_count == 0
799 test "it sets the follower_count property" do
801 follower = insert(:user)
803 User.follow(follower, user)
805 assert user.follower_count == 0
807 {:ok, user} = User.update_follower_count(user)
809 assert user.follower_count == 1
813 describe "follow_import" do
814 test "it imports user followings from list" do
815 [user1, user2, user3] = insert_list(3, :user)
822 {:ok, job} = User.follow_import(user1, identifiers)
824 assert {:ok, result} = ObanHelpers.perform(job)
825 assert is_list(result)
826 assert result == [user2, user3]
831 test "it mutes people" do
833 muted_user = insert(:user)
835 refute User.mutes?(user, muted_user)
836 refute User.muted_notifications?(user, muted_user)
838 {:ok, _user_relationships} = User.mute(user, muted_user)
840 assert User.mutes?(user, muted_user)
841 assert User.muted_notifications?(user, muted_user)
844 test "it unmutes users" do
846 muted_user = insert(:user)
848 {:ok, _user_relationships} = User.mute(user, muted_user)
849 {:ok, _user_mute} = User.unmute(user, muted_user)
851 refute User.mutes?(user, muted_user)
852 refute User.muted_notifications?(user, muted_user)
855 test "it mutes user without notifications" do
857 muted_user = insert(:user)
859 refute User.mutes?(user, muted_user)
860 refute User.muted_notifications?(user, muted_user)
862 {:ok, _user_relationships} = User.mute(user, muted_user, false)
864 assert User.mutes?(user, muted_user)
865 refute User.muted_notifications?(user, muted_user)
870 test "it blocks people" do
872 blocked_user = insert(:user)
874 refute User.blocks?(user, blocked_user)
876 {:ok, _user_relationship} = User.block(user, blocked_user)
878 assert User.blocks?(user, blocked_user)
881 test "it unblocks users" do
883 blocked_user = insert(:user)
885 {:ok, _user_relationship} = User.block(user, blocked_user)
886 {:ok, _user_block} = User.unblock(user, blocked_user)
888 refute User.blocks?(user, blocked_user)
891 test "blocks tear down cyclical follow relationships" do
892 blocker = insert(:user)
893 blocked = insert(:user)
895 {:ok, blocker} = User.follow(blocker, blocked)
896 {:ok, blocked} = User.follow(blocked, blocker)
898 assert User.following?(blocker, blocked)
899 assert User.following?(blocked, blocker)
901 {:ok, _user_relationship} = User.block(blocker, blocked)
902 blocked = User.get_cached_by_id(blocked.id)
904 assert User.blocks?(blocker, blocked)
906 refute User.following?(blocker, blocked)
907 refute User.following?(blocked, blocker)
910 test "blocks tear down blocker->blocked follow relationships" do
911 blocker = insert(:user)
912 blocked = insert(:user)
914 {:ok, blocker} = User.follow(blocker, blocked)
916 assert User.following?(blocker, blocked)
917 refute User.following?(blocked, blocker)
919 {:ok, _user_relationship} = User.block(blocker, blocked)
920 blocked = User.get_cached_by_id(blocked.id)
922 assert User.blocks?(blocker, blocked)
924 refute User.following?(blocker, blocked)
925 refute User.following?(blocked, blocker)
928 test "blocks tear down blocked->blocker follow relationships" do
929 blocker = insert(:user)
930 blocked = insert(:user)
932 {:ok, blocked} = User.follow(blocked, blocker)
934 refute User.following?(blocker, blocked)
935 assert User.following?(blocked, blocker)
937 {:ok, _user_relationship} = User.block(blocker, blocked)
938 blocked = User.get_cached_by_id(blocked.id)
940 assert User.blocks?(blocker, blocked)
942 refute User.following?(blocker, blocked)
943 refute User.following?(blocked, blocker)
946 test "blocks tear down blocked->blocker subscription relationships" do
947 blocker = insert(:user)
948 blocked = insert(:user)
950 {:ok, _subscription} = User.subscribe(blocked, blocker)
952 assert User.subscribed_to?(blocked, blocker)
953 refute User.subscribed_to?(blocker, blocked)
955 {:ok, _user_relationship} = User.block(blocker, blocked)
957 assert User.blocks?(blocker, blocked)
958 refute User.subscribed_to?(blocker, blocked)
959 refute User.subscribed_to?(blocked, blocker)
963 describe "domain blocking" do
964 test "blocks domains" do
966 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
968 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
970 assert User.blocks?(user, collateral_user)
973 test "does not block domain with same end" do
977 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
979 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
981 refute User.blocks?(user, collateral_user)
984 test "does not block domain with same end if wildcard added" do
988 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
990 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
992 refute User.blocks?(user, collateral_user)
995 test "blocks domain with wildcard for subdomain" do
998 user_from_subdomain =
999 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
1001 user_with_two_subdomains =
1003 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
1006 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
1008 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
1010 assert User.blocks?(user, user_from_subdomain)
1011 assert User.blocks?(user, user_with_two_subdomains)
1012 assert User.blocks?(user, user_domain)
1015 test "unblocks domains" do
1016 user = insert(:user)
1017 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
1019 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
1020 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
1022 refute User.blocks?(user, collateral_user)
1025 test "follows take precedence over domain blocks" do
1026 user = insert(:user)
1027 good_eggo = insert(:user, %{ap_id: "https://meanies.social/user/cuteposter"})
1029 {:ok, user} = User.block_domain(user, "meanies.social")
1030 {:ok, user} = User.follow(user, good_eggo)
1032 refute User.blocks?(user, good_eggo)
1036 describe "blocks_import" do
1037 test "it imports user blocks from list" do
1038 [user1, user2, user3] = insert_list(3, :user)
1045 {:ok, job} = User.blocks_import(user1, identifiers)
1047 assert {:ok, result} = ObanHelpers.perform(job)
1048 assert is_list(result)
1049 assert result == [user2, user3]
1053 describe "get_recipients_from_activity" do
1054 test "works for announces" do
1055 actor = insert(:user)
1056 user = insert(:user, local: true)
1058 {:ok, activity} = CommonAPI.post(actor, %{status: "hello"})
1059 {:ok, announce} = CommonAPI.repeat(activity.id, user)
1061 recipients = User.get_recipients_from_activity(announce)
1063 assert user in recipients
1066 test "get recipients" do
1067 actor = insert(:user)
1068 user = insert(:user, local: true)
1069 user_two = insert(:user, local: false)
1070 addressed = insert(:user, local: true)
1071 addressed_remote = insert(:user, local: false)
1074 CommonAPI.post(actor, %{
1075 status: "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
1078 assert Enum.map([actor, addressed], & &1.ap_id) --
1079 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1081 {:ok, user} = User.follow(user, actor)
1082 {:ok, _user_two} = User.follow(user_two, actor)
1083 recipients = User.get_recipients_from_activity(activity)
1084 assert length(recipients) == 3
1085 assert user in recipients
1086 assert addressed in recipients
1089 test "has following" do
1090 actor = insert(:user)
1091 user = insert(:user)
1092 user_two = insert(:user)
1093 addressed = insert(:user, local: true)
1096 CommonAPI.post(actor, %{
1097 status: "hey @#{addressed.nickname}"
1100 assert Enum.map([actor, addressed], & &1.ap_id) --
1101 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1103 {:ok, _actor} = User.follow(actor, user)
1104 {:ok, _actor} = User.follow(actor, user_two)
1105 recipients = User.get_recipients_from_activity(activity)
1106 assert length(recipients) == 2
1107 assert addressed in recipients
1111 describe ".deactivate" do
1112 test "can de-activate then re-activate a user" do
1113 user = insert(:user)
1114 assert false == user.deactivated
1115 {:ok, user} = User.deactivate(user)
1116 assert true == user.deactivated
1117 {:ok, user} = User.deactivate(user, false)
1118 assert false == user.deactivated
1121 test "hide a user from followers" do
1122 user = insert(:user)
1123 user2 = insert(:user)
1125 {:ok, user} = User.follow(user, user2)
1126 {:ok, _user} = User.deactivate(user)
1128 user2 = User.get_cached_by_id(user2.id)
1130 assert user2.follower_count == 0
1131 assert [] = User.get_followers(user2)
1134 test "hide a user from friends" do
1135 user = insert(:user)
1136 user2 = insert(:user)
1138 {:ok, user2} = User.follow(user2, user)
1139 assert user2.following_count == 1
1140 assert User.following_count(user2) == 1
1142 {:ok, _user} = User.deactivate(user)
1144 user2 = User.get_cached_by_id(user2.id)
1146 assert refresh_record(user2).following_count == 0
1147 assert user2.following_count == 0
1148 assert User.following_count(user2) == 0
1149 assert [] = User.get_friends(user2)
1152 test "hide a user's statuses from timelines and notifications" do
1153 user = insert(:user)
1154 user2 = insert(:user)
1156 {:ok, user2} = User.follow(user2, user)
1158 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{user2.nickname}"})
1160 activity = Repo.preload(activity, :bookmark)
1162 [notification] = Pleroma.Notification.for_user(user2)
1163 assert notification.activity.id == activity.id
1165 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
1167 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
1168 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1172 {:ok, _user} = User.deactivate(user)
1174 assert [] == ActivityPub.fetch_public_activities(%{})
1175 assert [] == Pleroma.Notification.for_user(user2)
1178 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1184 describe "delete" do
1186 {:ok, user} = insert(:user) |> User.set_cache()
1191 setup do: clear_config([:instance, :federating])
1193 test ".delete_user_activities deletes all create activities", %{user: user} do
1194 {:ok, activity} = CommonAPI.post(user, %{status: "2hu"})
1196 User.delete_user_activities(user)
1198 # TODO: Test removal favorites, repeats, delete activities.
1199 refute Activity.get_by_id(activity.id)
1202 test "it deactivates a user, all follow relationships and all activities", %{user: user} do
1203 follower = insert(:user)
1204 {:ok, follower} = User.follow(follower, user)
1206 locked_user = insert(:user, name: "locked", locked: true)
1207 {:ok, _} = User.follow(user, locked_user, :follow_pending)
1209 object = insert(:note, user: user)
1210 activity = insert(:note_activity, user: user, note: object)
1212 object_two = insert(:note, user: follower)
1213 activity_two = insert(:note_activity, user: follower, note: object_two)
1215 {:ok, like} = CommonAPI.favorite(user, activity_two.id)
1216 {:ok, like_two} = CommonAPI.favorite(follower, activity.id)
1217 {:ok, repeat} = CommonAPI.repeat(activity_two.id, user)
1219 {:ok, job} = User.delete(user)
1220 {:ok, _user} = ObanHelpers.perform(job)
1222 follower = User.get_cached_by_id(follower.id)
1224 refute User.following?(follower, user)
1225 assert %{deactivated: true} = User.get_by_id(user.id)
1227 assert [] == User.get_follow_requests(locked_user)
1231 |> Activity.Queries.by_actor()
1233 |> Enum.map(fn act -> act.data["type"] end)
1235 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1237 refute Activity.get_by_id(activity.id)
1238 refute Activity.get_by_id(like.id)
1239 refute Activity.get_by_id(like_two.id)
1240 refute Activity.get_by_id(repeat.id)
1244 describe "delete/1 when confirmation is pending" do
1246 user = insert(:user, confirmation_pending: true)
1250 test "deletes user from database when activation required", %{user: user} do
1251 clear_config([:instance, :account_activation_required], true)
1253 {:ok, job} = User.delete(user)
1254 {:ok, _} = ObanHelpers.perform(job)
1256 refute User.get_cached_by_id(user.id)
1257 refute User.get_by_id(user.id)
1260 test "deactivates user when activation is not required", %{user: user} do
1261 clear_config([:instance, :account_activation_required], false)
1263 {:ok, job} = User.delete(user)
1264 {:ok, _} = ObanHelpers.perform(job)
1266 assert %{deactivated: true} = User.get_cached_by_id(user.id)
1267 assert %{deactivated: true} = User.get_by_id(user.id)
1271 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1272 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1275 describe "per-user rich-text filtering" do
1276 test "html_filter_policy returns default policies, when rich-text is enabled" do
1277 user = insert(:user)
1279 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1282 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1283 user = insert(:user, no_rich_text: true)
1285 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1289 describe "caching" do
1290 test "invalidate_cache works" do
1291 user = insert(:user)
1293 User.set_cache(user)
1294 User.invalidate_cache(user)
1296 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1297 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1300 test "User.delete() plugs any possible zombie objects" do
1301 user = insert(:user)
1303 {:ok, job} = User.delete(user)
1304 {:ok, _} = ObanHelpers.perform(job)
1306 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1308 assert cached_user != user
1310 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1312 assert cached_user != user
1316 describe "account_status/1" do
1317 setup do: clear_config([:instance, :account_activation_required])
1319 test "return confirmation_pending for unconfirm user" do
1320 Pleroma.Config.put([:instance, :account_activation_required], true)
1321 user = insert(:user, confirmation_pending: true)
1322 assert User.account_status(user) == :confirmation_pending
1325 test "return active for confirmed user" do
1326 Pleroma.Config.put([:instance, :account_activation_required], true)
1327 user = insert(:user, confirmation_pending: false)
1328 assert User.account_status(user) == :active
1331 test "return active for remote user" do
1332 user = insert(:user, local: false)
1333 assert User.account_status(user) == :active
1336 test "returns :password_reset_pending for user with reset password" do
1337 user = insert(:user, password_reset_pending: true)
1338 assert User.account_status(user) == :password_reset_pending
1341 test "returns :deactivated for deactivated user" do
1342 user = insert(:user, local: true, confirmation_pending: false, deactivated: true)
1343 assert User.account_status(user) == :deactivated
1347 describe "superuser?/1" do
1348 test "returns false for unprivileged users" do
1349 user = insert(:user, local: true)
1351 refute User.superuser?(user)
1354 test "returns false for remote users" do
1355 user = insert(:user, local: false)
1356 remote_admin_user = insert(:user, local: false, is_admin: true)
1358 refute User.superuser?(user)
1359 refute User.superuser?(remote_admin_user)
1362 test "returns true for local moderators" do
1363 user = insert(:user, local: true, is_moderator: true)
1365 assert User.superuser?(user)
1368 test "returns true for local admins" do
1369 user = insert(:user, local: true, is_admin: true)
1371 assert User.superuser?(user)
1375 describe "invisible?/1" do
1376 test "returns true for an invisible user" do
1377 user = insert(:user, local: true, invisible: true)
1379 assert User.invisible?(user)
1382 test "returns false for a non-invisible user" do
1383 user = insert(:user, local: true)
1385 refute User.invisible?(user)
1389 describe "visible_for/2" do
1390 test "returns true when the account is itself" do
1391 user = insert(:user, local: true)
1393 assert User.visible_for(user, user) == :visible
1396 test "returns false when the account is unauthenticated and auth is required" do
1397 Pleroma.Config.put([:instance, :account_activation_required], true)
1399 user = insert(:user, local: true, confirmation_pending: true)
1400 other_user = insert(:user, local: true)
1402 refute User.visible_for(user, other_user) == :visible
1405 test "returns true when the account is unauthenticated and auth is not required" do
1406 user = insert(:user, local: true, confirmation_pending: true)
1407 other_user = insert(:user, local: true)
1409 assert User.visible_for(user, other_user) == :visible
1412 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1413 Pleroma.Config.put([:instance, :account_activation_required], true)
1415 user = insert(:user, local: true, confirmation_pending: true)
1416 other_user = insert(:user, local: true, is_admin: true)
1418 assert User.visible_for(user, other_user) == :visible
1422 describe "parse_bio/2" do
1423 test "preserves hosts in user links text" do
1424 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1425 user = insert(:user)
1426 bio = "A.k.a. @nick@domain.com"
1429 ~s(A.k.a. <span class="h-card"><a class="u-url mention" data-user="#{remote_user.id}" href="#{
1431 }" rel="ugc">@<span>nick@domain.com</span></a></span>)
1433 assert expected_text == User.parse_bio(bio, user)
1436 test "Adds rel=me on linkbacked urls" do
1437 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1439 bio = "http://example.com/rel_me/null"
1440 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1441 assert expected_text == User.parse_bio(bio, user)
1443 bio = "http://example.com/rel_me/link"
1444 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1445 assert expected_text == User.parse_bio(bio, user)
1447 bio = "http://example.com/rel_me/anchor"
1448 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1449 assert expected_text == User.parse_bio(bio, user)
1453 test "follower count is updated when a follower is blocked" do
1454 user = insert(:user)
1455 follower = insert(:user)
1456 follower2 = insert(:user)
1457 follower3 = insert(:user)
1459 {:ok, follower} = User.follow(follower, user)
1460 {:ok, _follower2} = User.follow(follower2, user)
1461 {:ok, _follower3} = User.follow(follower3, user)
1463 {:ok, _user_relationship} = User.block(user, follower)
1464 user = refresh_record(user)
1466 assert user.follower_count == 2
1469 describe "list_inactive_users_query/1" do
1470 defp days_ago(days) do
1472 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1473 -days * 60 * 60 * 24,
1478 test "Users are inactive by default" do
1482 Enum.map(1..total, fn _ ->
1483 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1486 inactive_users_ids =
1487 Pleroma.User.list_inactive_users_query()
1488 |> Pleroma.Repo.all()
1489 |> Enum.map(& &1.id)
1491 Enum.each(users, fn user ->
1492 assert user.id in inactive_users_ids
1496 test "Only includes users who has no recent activity" do
1500 Enum.map(1..total, fn _ ->
1501 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1504 {inactive, active} = Enum.split(users, trunc(total / 2))
1506 Enum.map(active, fn user ->
1507 to = Enum.random(users -- [user])
1510 CommonAPI.post(user, %{
1511 status: "hey @#{to.nickname}"
1515 inactive_users_ids =
1516 Pleroma.User.list_inactive_users_query()
1517 |> Pleroma.Repo.all()
1518 |> Enum.map(& &1.id)
1520 Enum.each(active, fn user ->
1521 refute user.id in inactive_users_ids
1524 Enum.each(inactive, fn user ->
1525 assert user.id in inactive_users_ids
1529 test "Only includes users with no read notifications" do
1533 Enum.map(1..total, fn _ ->
1534 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1537 [sender | recipients] = users
1538 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1540 Enum.each(recipients, fn to ->
1542 CommonAPI.post(sender, %{
1543 status: "hey @#{to.nickname}"
1547 CommonAPI.post(sender, %{
1548 status: "hey again @#{to.nickname}"
1552 Enum.each(active, fn user ->
1553 [n1, _n2] = Pleroma.Notification.for_user(user)
1554 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1557 inactive_users_ids =
1558 Pleroma.User.list_inactive_users_query()
1559 |> Pleroma.Repo.all()
1560 |> Enum.map(& &1.id)
1562 Enum.each(active, fn user ->
1563 refute user.id in inactive_users_ids
1566 Enum.each(inactive, fn user ->
1567 assert user.id in inactive_users_ids
1572 describe "toggle_confirmation/1" do
1573 test "if user is confirmed" do
1574 user = insert(:user, confirmation_pending: false)
1575 {:ok, user} = User.toggle_confirmation(user)
1577 assert user.confirmation_pending
1578 assert user.confirmation_token
1581 test "if user is unconfirmed" do
1582 user = insert(:user, confirmation_pending: true, confirmation_token: "some token")
1583 {:ok, user} = User.toggle_confirmation(user)
1585 refute user.confirmation_pending
1586 refute user.confirmation_token
1590 describe "ensure_keys_present" do
1591 test "it creates keys for a user and stores them in info" do
1592 user = insert(:user)
1593 refute is_binary(user.keys)
1594 {:ok, user} = User.ensure_keys_present(user)
1595 assert is_binary(user.keys)
1598 test "it doesn't create keys if there already are some" do
1599 user = insert(:user, keys: "xxx")
1600 {:ok, user} = User.ensure_keys_present(user)
1601 assert user.keys == "xxx"
1605 describe "get_ap_ids_by_nicknames" do
1606 test "it returns a list of AP ids for a given set of nicknames" do
1607 user = insert(:user)
1608 user_two = insert(:user)
1610 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1611 assert length(ap_ids) == 2
1612 assert user.ap_id in ap_ids
1613 assert user_two.ap_id in ap_ids
1617 describe "sync followers count" do
1619 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1620 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1621 insert(:user, local: true)
1622 insert(:user, local: false, deactivated: true)
1623 {:ok, user1: user1, user2: user2}
1626 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1627 [fdb_user1] = User.external_users(limit: 1)
1629 assert fdb_user1.ap_id
1630 assert fdb_user1.ap_id == user1.ap_id
1631 assert fdb_user1.id == user1.id
1633 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1635 assert fdb_user2.ap_id
1636 assert fdb_user2.ap_id == user2.ap_id
1637 assert fdb_user2.id == user2.id
1639 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1643 describe "is_internal_user?/1" do
1644 test "non-internal user returns false" do
1645 user = insert(:user)
1646 refute User.is_internal_user?(user)
1649 test "user with no nickname returns true" do
1650 user = insert(:user, %{nickname: nil})
1651 assert User.is_internal_user?(user)
1654 test "user with internal-prefixed nickname returns true" do
1655 user = insert(:user, %{nickname: "internal.test"})
1656 assert User.is_internal_user?(user)
1660 describe "update_and_set_cache/1" do
1661 test "returns error when user is stale instead Ecto.StaleEntryError" do
1662 user = insert(:user)
1664 changeset = Ecto.Changeset.change(user, bio: "test")
1668 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1669 User.update_and_set_cache(changeset)
1672 test "performs update cache if user updated" do
1673 user = insert(:user)
1674 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1676 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1678 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1679 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1680 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1684 describe "following/followers synchronization" do
1685 setup do: clear_config([:instance, :external_user_synchronization])
1687 test "updates the counters normally on following/getting a follow when disabled" do
1688 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1689 user = insert(:user)
1694 follower_address: "http://localhost:4001/users/masto_closed/followers",
1695 following_address: "http://localhost:4001/users/masto_closed/following",
1699 assert other_user.following_count == 0
1700 assert other_user.follower_count == 0
1702 {:ok, user} = Pleroma.User.follow(user, other_user)
1703 other_user = Pleroma.User.get_by_id(other_user.id)
1705 assert user.following_count == 1
1706 assert other_user.follower_count == 1
1709 test "syncronizes the counters with the remote instance for the followed when enabled" do
1710 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1712 user = insert(:user)
1717 follower_address: "http://localhost:4001/users/masto_closed/followers",
1718 following_address: "http://localhost:4001/users/masto_closed/following",
1722 assert other_user.following_count == 0
1723 assert other_user.follower_count == 0
1725 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1726 {:ok, _user} = User.follow(user, other_user)
1727 other_user = User.get_by_id(other_user.id)
1729 assert other_user.follower_count == 437
1732 test "syncronizes the counters with the remote instance for the follower when enabled" do
1733 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1735 user = insert(:user)
1740 follower_address: "http://localhost:4001/users/masto_closed/followers",
1741 following_address: "http://localhost:4001/users/masto_closed/following",
1745 assert other_user.following_count == 0
1746 assert other_user.follower_count == 0
1748 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1749 {:ok, other_user} = User.follow(other_user, user)
1751 assert other_user.following_count == 152
1755 describe "change_email/2" do
1757 [user: insert(:user)]
1760 test "blank email returns error", %{user: user} do
1761 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1762 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1765 test "non unique email returns error", %{user: user} do
1766 %{email: email} = insert(:user)
1768 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1769 User.change_email(user, email)
1772 test "invalid email returns error", %{user: user} do
1773 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1774 User.change_email(user, "cofe")
1777 test "changes email", %{user: user} do
1778 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1782 describe "get_cached_by_nickname_or_id" do
1784 local_user = insert(:user)
1785 remote_user = insert(:user, nickname: "nickname@example.com", local: false)
1787 [local_user: local_user, remote_user: remote_user]
1790 setup do: clear_config([:instance, :limit_to_local_content])
1792 test "allows getting remote users by id no matter what :limit_to_local_content is set to", %{
1793 remote_user: remote_user
1795 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1796 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1798 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1799 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1801 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1802 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1805 test "disallows getting remote users by nickname without authentication when :limit_to_local_content is set to :unauthenticated",
1806 %{remote_user: remote_user} do
1807 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1808 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1811 test "allows getting remote users by nickname with authentication when :limit_to_local_content is set to :unauthenticated",
1812 %{remote_user: remote_user, local_user: local_user} do
1813 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1814 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.nickname, for: local_user)
1817 test "disallows getting remote users by nickname when :limit_to_local_content is set to true",
1818 %{remote_user: remote_user} do
1819 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1820 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1823 test "allows getting local users by nickname no matter what :limit_to_local_content is set to",
1824 %{local_user: local_user} do
1825 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1826 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1828 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1829 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1831 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1832 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1836 describe "update_email_notifications/2" do
1838 user = insert(:user, email_notifications: %{"digest" => true})
1843 test "Notifications are updated", %{user: user} do
1844 true = user.email_notifications["digest"]
1845 assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false})
1846 assert result.email_notifications["digest"] == false
1850 test "avatar fallback" do
1851 user = insert(:user)
1852 assert User.avatar_url(user) =~ "/images/avi.png"
1854 clear_config([:assets, :default_user_avatar], "avatar.png")
1856 user = User.get_cached_by_nickname_or_id(user.nickname)
1857 assert User.avatar_url(user) =~ "avatar.png"
1859 assert User.avatar_url(user, no_default: true) == nil