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 @tag capture_log: true
601 test "it returns the old user if stale, but unfetchable" do
602 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
608 nickname: "admin@mastodon.example.org",
609 ap_id: "http://mastodon.example.org/users/raymoo",
610 last_refreshed_at: a_week_ago
613 assert orig_user.last_refreshed_at == a_week_ago
615 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/raymoo")
617 assert user.last_refreshed_at == orig_user.last_refreshed_at
621 test "returns an ap_id for a user" do
624 assert User.ap_id(user) ==
625 Pleroma.Web.Router.Helpers.user_feed_url(
626 Pleroma.Web.Endpoint,
632 test "returns an ap_followers link for a user" do
635 assert User.ap_followers(user) ==
636 Pleroma.Web.Router.Helpers.user_feed_url(
637 Pleroma.Web.Endpoint,
643 describe "remote user changeset" do
649 avatar: %{some: "avatar"}
651 setup do: clear_config([:instance, :user_bio_length])
652 setup do: clear_config([:instance, :user_name_length])
654 test "it confirms validity" do
655 cs = User.remote_user_changeset(@valid_remote)
659 test "it sets the follower_adress" do
660 cs = User.remote_user_changeset(@valid_remote)
661 # remote users get a fake local follower address
662 assert cs.changes.follower_address ==
663 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
666 test "it enforces the fqn format for nicknames" do
667 cs = User.remote_user_changeset(%{@valid_remote | nickname: "bla"})
668 assert Ecto.Changeset.get_field(cs, :local) == false
669 assert cs.changes.avatar
673 test "it has required fields" do
675 |> Enum.each(fn field ->
676 cs = User.remote_user_changeset(Map.delete(@valid_remote, field))
682 describe "followers and friends" do
683 test "gets all followers for a given user" do
685 follower_one = insert(:user)
686 follower_two = insert(:user)
687 not_follower = insert(:user)
689 {:ok, follower_one} = User.follow(follower_one, user)
690 {:ok, follower_two} = User.follow(follower_two, user)
692 res = User.get_followers(user)
694 assert Enum.member?(res, follower_one)
695 assert Enum.member?(res, follower_two)
696 refute Enum.member?(res, not_follower)
699 test "gets all friends (followed users) for a given user" do
701 followed_one = insert(:user)
702 followed_two = insert(:user)
703 not_followed = insert(:user)
705 {:ok, user} = User.follow(user, followed_one)
706 {:ok, user} = User.follow(user, followed_two)
708 res = User.get_friends(user)
710 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
711 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
712 assert Enum.member?(res, followed_one)
713 assert Enum.member?(res, followed_two)
714 refute Enum.member?(res, not_followed)
718 describe "updating note and follower count" do
719 test "it sets the note_count property" do
722 user = User.get_cached_by_ap_id(note.data["actor"])
724 assert user.note_count == 0
726 {:ok, user} = User.update_note_count(user)
728 assert user.note_count == 1
731 test "it increases the note_count property" do
733 user = User.get_cached_by_ap_id(note.data["actor"])
735 assert user.note_count == 0
737 {:ok, user} = User.increase_note_count(user)
739 assert user.note_count == 1
741 {:ok, user} = User.increase_note_count(user)
743 assert user.note_count == 2
746 test "it decreases the note_count property" do
748 user = User.get_cached_by_ap_id(note.data["actor"])
750 assert user.note_count == 0
752 {:ok, user} = User.increase_note_count(user)
754 assert user.note_count == 1
756 {:ok, user} = User.decrease_note_count(user)
758 assert user.note_count == 0
760 {:ok, user} = User.decrease_note_count(user)
762 assert user.note_count == 0
765 test "it sets the follower_count property" do
767 follower = insert(:user)
769 User.follow(follower, user)
771 assert user.follower_count == 0
773 {:ok, user} = User.update_follower_count(user)
775 assert user.follower_count == 1
779 describe "follow_import" do
780 test "it imports user followings from list" do
781 [user1, user2, user3] = insert_list(3, :user)
788 {:ok, job} = User.follow_import(user1, identifiers)
790 assert {:ok, result} = ObanHelpers.perform(job)
791 assert is_list(result)
792 assert result == [user2, user3]
797 test "it mutes people" do
799 muted_user = insert(:user)
801 refute User.mutes?(user, muted_user)
802 refute User.muted_notifications?(user, muted_user)
804 {:ok, _user_relationships} = User.mute(user, muted_user)
806 assert User.mutes?(user, muted_user)
807 assert User.muted_notifications?(user, muted_user)
810 test "it unmutes users" do
812 muted_user = insert(:user)
814 {:ok, _user_relationships} = User.mute(user, muted_user)
815 {:ok, _user_mute} = User.unmute(user, muted_user)
817 refute User.mutes?(user, muted_user)
818 refute User.muted_notifications?(user, muted_user)
821 test "it mutes user without notifications" do
823 muted_user = insert(:user)
825 refute User.mutes?(user, muted_user)
826 refute User.muted_notifications?(user, muted_user)
828 {:ok, _user_relationships} = User.mute(user, muted_user, false)
830 assert User.mutes?(user, muted_user)
831 refute User.muted_notifications?(user, muted_user)
836 test "it blocks people" do
838 blocked_user = insert(:user)
840 refute User.blocks?(user, blocked_user)
842 {:ok, _user_relationship} = User.block(user, blocked_user)
844 assert User.blocks?(user, blocked_user)
847 test "it unblocks users" do
849 blocked_user = insert(:user)
851 {:ok, _user_relationship} = User.block(user, blocked_user)
852 {:ok, _user_block} = User.unblock(user, blocked_user)
854 refute User.blocks?(user, blocked_user)
857 test "blocks tear down cyclical follow relationships" do
858 blocker = insert(:user)
859 blocked = insert(:user)
861 {:ok, blocker} = User.follow(blocker, blocked)
862 {:ok, blocked} = User.follow(blocked, blocker)
864 assert User.following?(blocker, blocked)
865 assert User.following?(blocked, blocker)
867 {:ok, _user_relationship} = User.block(blocker, blocked)
868 blocked = User.get_cached_by_id(blocked.id)
870 assert User.blocks?(blocker, blocked)
872 refute User.following?(blocker, blocked)
873 refute User.following?(blocked, blocker)
876 test "blocks tear down blocker->blocked follow relationships" do
877 blocker = insert(:user)
878 blocked = insert(:user)
880 {:ok, blocker} = User.follow(blocker, blocked)
882 assert User.following?(blocker, blocked)
883 refute User.following?(blocked, blocker)
885 {:ok, _user_relationship} = User.block(blocker, blocked)
886 blocked = User.get_cached_by_id(blocked.id)
888 assert User.blocks?(blocker, blocked)
890 refute User.following?(blocker, blocked)
891 refute User.following?(blocked, blocker)
894 test "blocks tear down blocked->blocker follow relationships" do
895 blocker = insert(:user)
896 blocked = insert(:user)
898 {:ok, blocked} = User.follow(blocked, blocker)
900 refute User.following?(blocker, blocked)
901 assert User.following?(blocked, blocker)
903 {:ok, _user_relationship} = User.block(blocker, blocked)
904 blocked = User.get_cached_by_id(blocked.id)
906 assert User.blocks?(blocker, blocked)
908 refute User.following?(blocker, blocked)
909 refute User.following?(blocked, blocker)
912 test "blocks tear down blocked->blocker subscription relationships" do
913 blocker = insert(:user)
914 blocked = insert(:user)
916 {:ok, _subscription} = User.subscribe(blocked, blocker)
918 assert User.subscribed_to?(blocked, blocker)
919 refute User.subscribed_to?(blocker, blocked)
921 {:ok, _user_relationship} = User.block(blocker, blocked)
923 assert User.blocks?(blocker, blocked)
924 refute User.subscribed_to?(blocker, blocked)
925 refute User.subscribed_to?(blocked, blocker)
929 describe "domain blocking" do
930 test "blocks domains" do
932 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
934 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
936 assert User.blocks?(user, collateral_user)
939 test "does not block domain with same end" do
943 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
945 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
947 refute User.blocks?(user, collateral_user)
950 test "does not block domain with same end if wildcard added" do
954 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
956 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
958 refute User.blocks?(user, collateral_user)
961 test "blocks domain with wildcard for subdomain" do
964 user_from_subdomain =
965 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
967 user_with_two_subdomains =
969 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
972 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
974 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
976 assert User.blocks?(user, user_from_subdomain)
977 assert User.blocks?(user, user_with_two_subdomains)
978 assert User.blocks?(user, user_domain)
981 test "unblocks domains" do
983 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
985 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
986 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
988 refute User.blocks?(user, collateral_user)
991 test "follows take precedence over domain blocks" do
993 good_eggo = insert(:user, %{ap_id: "https://meanies.social/user/cuteposter"})
995 {:ok, user} = User.block_domain(user, "meanies.social")
996 {:ok, user} = User.follow(user, good_eggo)
998 refute User.blocks?(user, good_eggo)
1002 describe "blocks_import" do
1003 test "it imports user blocks from list" do
1004 [user1, user2, user3] = insert_list(3, :user)
1011 {:ok, job} = User.blocks_import(user1, identifiers)
1013 assert {:ok, result} = ObanHelpers.perform(job)
1014 assert is_list(result)
1015 assert result == [user2, user3]
1019 describe "get_recipients_from_activity" do
1020 test "works for announces" do
1021 actor = insert(:user)
1022 user = insert(:user, local: true)
1024 {:ok, activity} = CommonAPI.post(actor, %{status: "hello"})
1025 {:ok, announce} = CommonAPI.repeat(activity.id, user)
1027 recipients = User.get_recipients_from_activity(announce)
1029 assert user in recipients
1032 test "get recipients" do
1033 actor = insert(:user)
1034 user = insert(:user, local: true)
1035 user_two = insert(:user, local: false)
1036 addressed = insert(:user, local: true)
1037 addressed_remote = insert(:user, local: false)
1040 CommonAPI.post(actor, %{
1041 status: "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
1044 assert Enum.map([actor, addressed], & &1.ap_id) --
1045 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1047 {:ok, user} = User.follow(user, actor)
1048 {:ok, _user_two} = User.follow(user_two, actor)
1049 recipients = User.get_recipients_from_activity(activity)
1050 assert length(recipients) == 3
1051 assert user in recipients
1052 assert addressed in recipients
1055 test "has following" do
1056 actor = insert(:user)
1057 user = insert(:user)
1058 user_two = insert(:user)
1059 addressed = insert(:user, local: true)
1062 CommonAPI.post(actor, %{
1063 status: "hey @#{addressed.nickname}"
1066 assert Enum.map([actor, addressed], & &1.ap_id) --
1067 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1069 {:ok, _actor} = User.follow(actor, user)
1070 {:ok, _actor} = User.follow(actor, user_two)
1071 recipients = User.get_recipients_from_activity(activity)
1072 assert length(recipients) == 2
1073 assert addressed in recipients
1077 describe ".deactivate" do
1078 test "can de-activate then re-activate a user" do
1079 user = insert(:user)
1080 assert false == user.deactivated
1081 {:ok, user} = User.deactivate(user)
1082 assert true == user.deactivated
1083 {:ok, user} = User.deactivate(user, false)
1084 assert false == user.deactivated
1087 test "hide a user from followers" do
1088 user = insert(:user)
1089 user2 = insert(:user)
1091 {:ok, user} = User.follow(user, user2)
1092 {:ok, _user} = User.deactivate(user)
1094 user2 = User.get_cached_by_id(user2.id)
1096 assert user2.follower_count == 0
1097 assert [] = User.get_followers(user2)
1100 test "hide a user from friends" do
1101 user = insert(:user)
1102 user2 = insert(:user)
1104 {:ok, user2} = User.follow(user2, user)
1105 assert user2.following_count == 1
1106 assert User.following_count(user2) == 1
1108 {:ok, _user} = User.deactivate(user)
1110 user2 = User.get_cached_by_id(user2.id)
1112 assert refresh_record(user2).following_count == 0
1113 assert user2.following_count == 0
1114 assert User.following_count(user2) == 0
1115 assert [] = User.get_friends(user2)
1118 test "hide a user's statuses from timelines and notifications" do
1119 user = insert(:user)
1120 user2 = insert(:user)
1122 {:ok, user2} = User.follow(user2, user)
1124 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{user2.nickname}"})
1126 activity = Repo.preload(activity, :bookmark)
1128 [notification] = Pleroma.Notification.for_user(user2)
1129 assert notification.activity.id == activity.id
1131 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
1133 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
1134 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1138 {:ok, _user} = User.deactivate(user)
1140 assert [] == ActivityPub.fetch_public_activities(%{})
1141 assert [] == Pleroma.Notification.for_user(user2)
1144 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1150 describe "delete" do
1152 {:ok, user} = insert(:user) |> User.set_cache()
1157 setup do: clear_config([:instance, :federating])
1159 test ".delete_user_activities deletes all create activities", %{user: user} do
1160 {:ok, activity} = CommonAPI.post(user, %{status: "2hu"})
1162 User.delete_user_activities(user)
1164 # TODO: Test removal favorites, repeats, delete activities.
1165 refute Activity.get_by_id(activity.id)
1168 test "it deactivates a user, all follow relationships and all activities", %{user: user} do
1169 follower = insert(:user)
1170 {:ok, follower} = User.follow(follower, user)
1172 locked_user = insert(:user, name: "locked", locked: true)
1173 {:ok, _} = User.follow(user, locked_user, :follow_pending)
1175 object = insert(:note, user: user)
1176 activity = insert(:note_activity, user: user, note: object)
1178 object_two = insert(:note, user: follower)
1179 activity_two = insert(:note_activity, user: follower, note: object_two)
1181 {:ok, like} = CommonAPI.favorite(user, activity_two.id)
1182 {:ok, like_two} = CommonAPI.favorite(follower, activity.id)
1183 {:ok, repeat} = CommonAPI.repeat(activity_two.id, user)
1185 {:ok, job} = User.delete(user)
1186 {:ok, _user} = ObanHelpers.perform(job)
1188 follower = User.get_cached_by_id(follower.id)
1190 refute User.following?(follower, user)
1191 assert %{deactivated: true} = User.get_by_id(user.id)
1193 assert [] == User.get_follow_requests(locked_user)
1197 |> Activity.Queries.by_actor()
1199 |> Enum.map(fn act -> act.data["type"] end)
1201 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1203 refute Activity.get_by_id(activity.id)
1204 refute Activity.get_by_id(like.id)
1205 refute Activity.get_by_id(like_two.id)
1206 refute Activity.get_by_id(repeat.id)
1210 describe "delete/1 when confirmation is pending" do
1212 user = insert(:user, confirmation_pending: true)
1216 test "deletes user from database when activation required", %{user: user} do
1217 clear_config([:instance, :account_activation_required], true)
1219 {:ok, job} = User.delete(user)
1220 {:ok, _} = ObanHelpers.perform(job)
1222 refute User.get_cached_by_id(user.id)
1223 refute User.get_by_id(user.id)
1226 test "deactivates user when activation is not required", %{user: user} do
1227 clear_config([:instance, :account_activation_required], false)
1229 {:ok, job} = User.delete(user)
1230 {:ok, _} = ObanHelpers.perform(job)
1232 assert %{deactivated: true} = User.get_cached_by_id(user.id)
1233 assert %{deactivated: true} = User.get_by_id(user.id)
1237 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1238 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1241 describe "per-user rich-text filtering" do
1242 test "html_filter_policy returns default policies, when rich-text is enabled" do
1243 user = insert(:user)
1245 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1248 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1249 user = insert(:user, no_rich_text: true)
1251 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1255 describe "caching" do
1256 test "invalidate_cache works" do
1257 user = insert(:user)
1259 User.set_cache(user)
1260 User.invalidate_cache(user)
1262 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1263 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1266 test "User.delete() plugs any possible zombie objects" do
1267 user = insert(:user)
1269 {:ok, job} = User.delete(user)
1270 {:ok, _} = ObanHelpers.perform(job)
1272 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1274 assert cached_user != user
1276 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1278 assert cached_user != user
1282 describe "account_status/1" do
1283 setup do: clear_config([:instance, :account_activation_required])
1285 test "return confirmation_pending for unconfirm user" do
1286 Pleroma.Config.put([:instance, :account_activation_required], true)
1287 user = insert(:user, confirmation_pending: true)
1288 assert User.account_status(user) == :confirmation_pending
1291 test "return active for confirmed user" do
1292 Pleroma.Config.put([:instance, :account_activation_required], true)
1293 user = insert(:user, confirmation_pending: false)
1294 assert User.account_status(user) == :active
1297 test "return active for remote user" do
1298 user = insert(:user, local: false)
1299 assert User.account_status(user) == :active
1302 test "returns :password_reset_pending for user with reset password" do
1303 user = insert(:user, password_reset_pending: true)
1304 assert User.account_status(user) == :password_reset_pending
1307 test "returns :deactivated for deactivated user" do
1308 user = insert(:user, local: true, confirmation_pending: false, deactivated: true)
1309 assert User.account_status(user) == :deactivated
1313 describe "superuser?/1" do
1314 test "returns false for unprivileged users" do
1315 user = insert(:user, local: true)
1317 refute User.superuser?(user)
1320 test "returns false for remote users" do
1321 user = insert(:user, local: false)
1322 remote_admin_user = insert(:user, local: false, is_admin: true)
1324 refute User.superuser?(user)
1325 refute User.superuser?(remote_admin_user)
1328 test "returns true for local moderators" do
1329 user = insert(:user, local: true, is_moderator: true)
1331 assert User.superuser?(user)
1334 test "returns true for local admins" do
1335 user = insert(:user, local: true, is_admin: true)
1337 assert User.superuser?(user)
1341 describe "invisible?/1" do
1342 test "returns true for an invisible user" do
1343 user = insert(:user, local: true, invisible: true)
1345 assert User.invisible?(user)
1348 test "returns false for a non-invisible user" do
1349 user = insert(:user, local: true)
1351 refute User.invisible?(user)
1355 describe "visible_for/2" do
1356 test "returns true when the account is itself" do
1357 user = insert(:user, local: true)
1359 assert User.visible_for(user, user) == :visible
1362 test "returns false when the account is unauthenticated and auth is required" do
1363 Pleroma.Config.put([:instance, :account_activation_required], true)
1365 user = insert(:user, local: true, confirmation_pending: true)
1366 other_user = insert(:user, local: true)
1368 refute User.visible_for(user, other_user) == :visible
1371 test "returns true when the account is unauthenticated and auth is not required" do
1372 user = insert(:user, local: true, confirmation_pending: true)
1373 other_user = insert(:user, local: true)
1375 assert User.visible_for(user, other_user) == :visible
1378 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1379 Pleroma.Config.put([:instance, :account_activation_required], true)
1381 user = insert(:user, local: true, confirmation_pending: true)
1382 other_user = insert(:user, local: true, is_admin: true)
1384 assert User.visible_for(user, other_user) == :visible
1388 describe "parse_bio/2" do
1389 test "preserves hosts in user links text" do
1390 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1391 user = insert(:user)
1392 bio = "A.k.a. @nick@domain.com"
1395 ~s(A.k.a. <span class="h-card"><a class="u-url mention" data-user="#{remote_user.id}" href="#{
1397 }" rel="ugc">@<span>nick@domain.com</span></a></span>)
1399 assert expected_text == User.parse_bio(bio, user)
1402 test "Adds rel=me on linkbacked urls" do
1403 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1405 bio = "http://example.com/rel_me/null"
1406 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1407 assert expected_text == User.parse_bio(bio, user)
1409 bio = "http://example.com/rel_me/link"
1410 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1411 assert expected_text == User.parse_bio(bio, user)
1413 bio = "http://example.com/rel_me/anchor"
1414 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1415 assert expected_text == User.parse_bio(bio, user)
1419 test "follower count is updated when a follower is blocked" do
1420 user = insert(:user)
1421 follower = insert(:user)
1422 follower2 = insert(:user)
1423 follower3 = insert(:user)
1425 {:ok, follower} = User.follow(follower, user)
1426 {:ok, _follower2} = User.follow(follower2, user)
1427 {:ok, _follower3} = User.follow(follower3, user)
1429 {:ok, _user_relationship} = User.block(user, follower)
1430 user = refresh_record(user)
1432 assert user.follower_count == 2
1435 describe "list_inactive_users_query/1" do
1436 defp days_ago(days) do
1438 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1439 -days * 60 * 60 * 24,
1444 test "Users are inactive by default" do
1448 Enum.map(1..total, fn _ ->
1449 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1452 inactive_users_ids =
1453 Pleroma.User.list_inactive_users_query()
1454 |> Pleroma.Repo.all()
1455 |> Enum.map(& &1.id)
1457 Enum.each(users, fn user ->
1458 assert user.id in inactive_users_ids
1462 test "Only includes users who has no recent activity" do
1466 Enum.map(1..total, fn _ ->
1467 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1470 {inactive, active} = Enum.split(users, trunc(total / 2))
1472 Enum.map(active, fn user ->
1473 to = Enum.random(users -- [user])
1476 CommonAPI.post(user, %{
1477 status: "hey @#{to.nickname}"
1481 inactive_users_ids =
1482 Pleroma.User.list_inactive_users_query()
1483 |> Pleroma.Repo.all()
1484 |> Enum.map(& &1.id)
1486 Enum.each(active, fn user ->
1487 refute user.id in inactive_users_ids
1490 Enum.each(inactive, fn user ->
1491 assert user.id in inactive_users_ids
1495 test "Only includes users with no read notifications" do
1499 Enum.map(1..total, fn _ ->
1500 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1503 [sender | recipients] = users
1504 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1506 Enum.each(recipients, fn to ->
1508 CommonAPI.post(sender, %{
1509 status: "hey @#{to.nickname}"
1513 CommonAPI.post(sender, %{
1514 status: "hey again @#{to.nickname}"
1518 Enum.each(active, fn user ->
1519 [n1, _n2] = Pleroma.Notification.for_user(user)
1520 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1523 inactive_users_ids =
1524 Pleroma.User.list_inactive_users_query()
1525 |> Pleroma.Repo.all()
1526 |> Enum.map(& &1.id)
1528 Enum.each(active, fn user ->
1529 refute user.id in inactive_users_ids
1532 Enum.each(inactive, fn user ->
1533 assert user.id in inactive_users_ids
1538 describe "toggle_confirmation/1" do
1539 test "if user is confirmed" do
1540 user = insert(:user, confirmation_pending: false)
1541 {:ok, user} = User.toggle_confirmation(user)
1543 assert user.confirmation_pending
1544 assert user.confirmation_token
1547 test "if user is unconfirmed" do
1548 user = insert(:user, confirmation_pending: true, confirmation_token: "some token")
1549 {:ok, user} = User.toggle_confirmation(user)
1551 refute user.confirmation_pending
1552 refute user.confirmation_token
1556 describe "ensure_keys_present" do
1557 test "it creates keys for a user and stores them in info" do
1558 user = insert(:user)
1559 refute is_binary(user.keys)
1560 {:ok, user} = User.ensure_keys_present(user)
1561 assert is_binary(user.keys)
1564 test "it doesn't create keys if there already are some" do
1565 user = insert(:user, keys: "xxx")
1566 {:ok, user} = User.ensure_keys_present(user)
1567 assert user.keys == "xxx"
1571 describe "get_ap_ids_by_nicknames" do
1572 test "it returns a list of AP ids for a given set of nicknames" do
1573 user = insert(:user)
1574 user_two = insert(:user)
1576 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1577 assert length(ap_ids) == 2
1578 assert user.ap_id in ap_ids
1579 assert user_two.ap_id in ap_ids
1583 describe "sync followers count" do
1585 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1586 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1587 insert(:user, local: true)
1588 insert(:user, local: false, deactivated: true)
1589 {:ok, user1: user1, user2: user2}
1592 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1593 [fdb_user1] = User.external_users(limit: 1)
1595 assert fdb_user1.ap_id
1596 assert fdb_user1.ap_id == user1.ap_id
1597 assert fdb_user1.id == user1.id
1599 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1601 assert fdb_user2.ap_id
1602 assert fdb_user2.ap_id == user2.ap_id
1603 assert fdb_user2.id == user2.id
1605 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1609 describe "is_internal_user?/1" do
1610 test "non-internal user returns false" do
1611 user = insert(:user)
1612 refute User.is_internal_user?(user)
1615 test "user with no nickname returns true" do
1616 user = insert(:user, %{nickname: nil})
1617 assert User.is_internal_user?(user)
1620 test "user with internal-prefixed nickname returns true" do
1621 user = insert(:user, %{nickname: "internal.test"})
1622 assert User.is_internal_user?(user)
1626 describe "update_and_set_cache/1" do
1627 test "returns error when user is stale instead Ecto.StaleEntryError" do
1628 user = insert(:user)
1630 changeset = Ecto.Changeset.change(user, bio: "test")
1634 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1635 User.update_and_set_cache(changeset)
1638 test "performs update cache if user updated" do
1639 user = insert(:user)
1640 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1642 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1644 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1645 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1646 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1650 describe "following/followers synchronization" do
1651 setup do: clear_config([:instance, :external_user_synchronization])
1653 test "updates the counters normally on following/getting a follow when disabled" do
1654 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1655 user = insert(:user)
1660 follower_address: "http://localhost:4001/users/masto_closed/followers",
1661 following_address: "http://localhost:4001/users/masto_closed/following",
1665 assert other_user.following_count == 0
1666 assert other_user.follower_count == 0
1668 {:ok, user} = Pleroma.User.follow(user, other_user)
1669 other_user = Pleroma.User.get_by_id(other_user.id)
1671 assert user.following_count == 1
1672 assert other_user.follower_count == 1
1675 test "syncronizes the counters with the remote instance for the followed when enabled" do
1676 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1678 user = insert(:user)
1683 follower_address: "http://localhost:4001/users/masto_closed/followers",
1684 following_address: "http://localhost:4001/users/masto_closed/following",
1688 assert other_user.following_count == 0
1689 assert other_user.follower_count == 0
1691 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1692 {:ok, _user} = User.follow(user, other_user)
1693 other_user = User.get_by_id(other_user.id)
1695 assert other_user.follower_count == 437
1698 test "syncronizes the counters with the remote instance for the follower when enabled" do
1699 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1701 user = insert(:user)
1706 follower_address: "http://localhost:4001/users/masto_closed/followers",
1707 following_address: "http://localhost:4001/users/masto_closed/following",
1711 assert other_user.following_count == 0
1712 assert other_user.follower_count == 0
1714 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1715 {:ok, other_user} = User.follow(other_user, user)
1717 assert other_user.following_count == 152
1721 describe "change_email/2" do
1723 [user: insert(:user)]
1726 test "blank email returns error", %{user: user} do
1727 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1728 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1731 test "non unique email returns error", %{user: user} do
1732 %{email: email} = insert(:user)
1734 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1735 User.change_email(user, email)
1738 test "invalid email returns error", %{user: user} do
1739 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1740 User.change_email(user, "cofe")
1743 test "changes email", %{user: user} do
1744 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1748 describe "get_cached_by_nickname_or_id" do
1750 local_user = insert(:user)
1751 remote_user = insert(:user, nickname: "nickname@example.com", local: false)
1753 [local_user: local_user, remote_user: remote_user]
1756 setup do: clear_config([:instance, :limit_to_local_content])
1758 test "allows getting remote users by id no matter what :limit_to_local_content is set to", %{
1759 remote_user: remote_user
1761 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1762 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1764 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1765 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1767 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1768 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1771 test "disallows getting remote users by nickname without authentication when :limit_to_local_content is set to :unauthenticated",
1772 %{remote_user: remote_user} do
1773 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1774 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1777 test "allows getting remote users by nickname with authentication when :limit_to_local_content is set to :unauthenticated",
1778 %{remote_user: remote_user, local_user: local_user} do
1779 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1780 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.nickname, for: local_user)
1783 test "disallows getting remote users by nickname when :limit_to_local_content is set to true",
1784 %{remote_user: remote_user} do
1785 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1786 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1789 test "allows getting local users by nickname no matter what :limit_to_local_content is set to",
1790 %{local_user: local_user} do
1791 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1792 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1794 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1795 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1797 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1798 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1802 describe "update_email_notifications/2" do
1804 user = insert(:user, email_notifications: %{"digest" => true})
1809 test "Notifications are updated", %{user: user} do
1810 true = user.email_notifications["digest"]
1811 assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false})
1812 assert result.email_notifications["digest"] == false
1816 test "avatar fallback" do
1817 user = insert(:user)
1818 assert User.avatar_url(user) =~ "/images/avi.png"
1820 clear_config([:assets, :default_user_avatar], "avatar.png")
1822 user = User.get_cached_by_nickname_or_id(user.nickname)
1823 assert User.avatar_url(user) =~ "avatar.png"
1825 assert User.avatar_url(user, no_default: true) == nil