Added the ability to upload background, logo, default user avatar, instance thumbnail...
[akkoma] / test / user_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.UserTest do
6 alias Pleroma.Activity
7 alias Pleroma.Builders.UserBuilder
8 alias Pleroma.Object
9 alias Pleroma.Repo
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.CommonAPI
14
15 use Pleroma.DataCase
16 use Oban.Testing, repo: Pleroma.Repo
17
18 import Pleroma.Factory
19 import ExUnit.CaptureLog
20
21 setup_all do
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 :ok
24 end
25
26 setup do: clear_config([:instance, :account_activation_required])
27
28 describe "service actors" do
29 test "returns updated invisible actor" do
30 uri = "#{Pleroma.Web.Endpoint.url()}/relay"
31 followers_uri = "#{uri}/followers"
32
33 insert(
34 :user,
35 %{
36 nickname: "relay",
37 invisible: false,
38 local: true,
39 ap_id: uri,
40 follower_address: followers_uri
41 }
42 )
43
44 actor = User.get_or_create_service_actor_by_ap_id(uri, "relay")
45 assert actor.invisible
46 end
47
48 test "returns relay user" do
49 uri = "#{Pleroma.Web.Endpoint.url()}/relay"
50 followers_uri = "#{uri}/followers"
51
52 assert %User{
53 nickname: "relay",
54 invisible: true,
55 local: true,
56 ap_id: ^uri,
57 follower_address: ^followers_uri
58 } = User.get_or_create_service_actor_by_ap_id(uri, "relay")
59
60 assert capture_log(fn ->
61 refute User.get_or_create_service_actor_by_ap_id("/relay", "relay")
62 end) =~ "Cannot create service actor:"
63 end
64
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")
69
70 assert %User{
71 nickname: "internal.fetch-test",
72 invisible: true,
73 local: true,
74 ap_id: ^uri,
75 follower_address: ^followers_uri
76 } = user
77
78 user2 = User.get_or_create_service_actor_by_ap_id(uri, "internal.fetch-test")
79 assert user.id == user2.id
80 end
81 end
82
83 describe "AP ID user relationships" do
84 setup do
85 {:ok, user: insert(:user)}
86 end
87
88 test "outgoing_relationships_ap_ids/1", %{user: user} do
89 rel_types = [:block, :mute, :notification_mute, :reblog_mute, :inverse_subscription]
90
91 ap_ids_by_rel =
92 Enum.into(
93 rel_types,
94 %{},
95 fn rel_type ->
96 rel_records =
97 insert_list(2, :user_relationship, %{source: user, relationship_type: rel_type})
98
99 ap_ids = Enum.map(rel_records, fn rr -> Repo.preload(rr, :target).target.ap_id end)
100 {rel_type, Enum.sort(ap_ids)}
101 end
102 )
103
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))
106
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))
109
110 assert ap_ids_by_rel[:notification_mute] ==
111 Enum.sort(User.notification_muted_users_ap_ids(user))
112
113 assert ap_ids_by_rel[:notification_mute] ==
114 Enum.sort(Enum.map(User.notification_muted_users(user), & &1.ap_id))
115
116 assert ap_ids_by_rel[:reblog_mute] == Enum.sort(User.reblog_muted_users_ap_ids(user))
117
118 assert ap_ids_by_rel[:reblog_mute] ==
119 Enum.sort(Enum.map(User.reblog_muted_users(user), & &1.ap_id))
120
121 assert ap_ids_by_rel[:inverse_subscription] == Enum.sort(User.subscriber_users_ap_ids(user))
122
123 assert ap_ids_by_rel[:inverse_subscription] ==
124 Enum.sort(Enum.map(User.subscriber_users(user), & &1.ap_id))
125
126 outgoing_relationships_ap_ids = User.outgoing_relationships_ap_ids(user, rel_types)
127
128 assert ap_ids_by_rel ==
129 Enum.into(outgoing_relationships_ap_ids, %{}, fn {k, v} -> {k, Enum.sort(v)} end)
130 end
131 end
132
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"])
137
138 assert "cool" in user.tags
139 assert "dude" in user.tags
140 end
141
142 test "untagging a user" do
143 user = insert(:user, %{tags: nil})
144 user = User.untag(user, ["cool", "dude"])
145
146 assert user.tags == []
147 end
148 end
149
150 test "ap_id returns the activity pub id for the user" do
151 user = UserBuilder.build()
152
153 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
154
155 assert expected_ap_id == User.ap_id(user)
156 end
157
158 test "ap_followers returns the followers collection for the user" do
159 user = UserBuilder.build()
160
161 expected_followers_collection = "#{User.ap_id(user)}/followers"
162
163 assert expected_followers_collection == User.ap_followers(user)
164 end
165
166 test "ap_following returns the following collection for the user" do
167 user = UserBuilder.build()
168
169 expected_followers_collection = "#{User.ap_id(user)}/following"
170
171 assert expected_followers_collection == User.ap_following(user)
172 end
173
174 test "returns all pending follow requests" do
175 unlocked = insert(:user)
176 locked = insert(:user, locked: true)
177 follower = insert(:user)
178
179 CommonAPI.follow(follower, unlocked)
180 CommonAPI.follow(follower, locked)
181
182 assert [] = User.get_follow_requests(unlocked)
183 assert [activity] = User.get_follow_requests(locked)
184
185 assert activity
186 end
187
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)
192
193 CommonAPI.follow(pending_follower, locked)
194 CommonAPI.follow(pending_follower, locked)
195 CommonAPI.follow(accepted_follower, locked)
196
197 Pleroma.FollowingRelationship.update(accepted_follower, locked, :follow_accept)
198
199 assert [^pending_follower] = User.get_follow_requests(locked)
200 end
201
202 test "clears follow requests when requester is blocked" do
203 followed = insert(:user, locked: true)
204 follower = insert(:user)
205
206 CommonAPI.follow(follower, followed)
207 assert [_activity] = User.get_follow_requests(followed)
208
209 {:ok, _user_relationship} = User.block(followed, follower)
210 assert [] = User.get_follow_requests(followed)
211 end
212
213 test "follow_all follows mutliple users" do
214 user = insert(:user)
215 followed_zero = insert(:user)
216 followed_one = insert(:user)
217 followed_two = insert(:user)
218 blocked = insert(:user)
219 not_followed = insert(:user)
220 reverse_blocked = insert(:user)
221
222 {:ok, _user_relationship} = User.block(user, blocked)
223 {:ok, _user_relationship} = User.block(reverse_blocked, user)
224
225 {:ok, user} = User.follow(user, followed_zero)
226
227 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
228
229 assert User.following?(user, followed_one)
230 assert User.following?(user, followed_two)
231 assert User.following?(user, followed_zero)
232 refute User.following?(user, not_followed)
233 refute User.following?(user, blocked)
234 refute User.following?(user, reverse_blocked)
235 end
236
237 test "follow_all follows mutliple users without duplicating" do
238 user = insert(:user)
239 followed_zero = insert(:user)
240 followed_one = insert(:user)
241 followed_two = insert(:user)
242
243 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
244 assert length(User.following(user)) == 3
245
246 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
247 assert length(User.following(user)) == 4
248 end
249
250 test "follow takes a user and another user" do
251 user = insert(:user)
252 followed = insert(:user)
253
254 {:ok, user} = User.follow(user, followed)
255
256 user = User.get_cached_by_id(user.id)
257 followed = User.get_cached_by_ap_id(followed.ap_id)
258
259 assert followed.follower_count == 1
260 assert user.following_count == 1
261
262 assert User.ap_followers(followed) in User.following(user)
263 end
264
265 test "can't follow a deactivated users" do
266 user = insert(:user)
267 followed = insert(:user, %{deactivated: true})
268
269 {:error, _} = User.follow(user, followed)
270 end
271
272 test "can't follow a user who blocked us" do
273 blocker = insert(:user)
274 blockee = insert(:user)
275
276 {:ok, _user_relationship} = User.block(blocker, blockee)
277
278 {:error, _} = User.follow(blockee, blocker)
279 end
280
281 test "can't subscribe to a user who blocked us" do
282 blocker = insert(:user)
283 blocked = insert(:user)
284
285 {:ok, _user_relationship} = User.block(blocker, blocked)
286
287 {:error, _} = User.subscribe(blocked, blocker)
288 end
289
290 test "local users do not automatically follow local locked accounts" do
291 follower = insert(:user, locked: true)
292 followed = insert(:user, locked: true)
293
294 {:ok, follower} = User.maybe_direct_follow(follower, followed)
295
296 refute User.following?(follower, followed)
297 end
298
299 describe "unfollow/2" do
300 setup do: clear_config([:instance, :external_user_synchronization])
301
302 test "unfollow with syncronizes external user" do
303 Pleroma.Config.put([:instance, :external_user_synchronization], true)
304
305 followed =
306 insert(:user,
307 nickname: "fuser1",
308 follower_address: "http://localhost:4001/users/fuser1/followers",
309 following_address: "http://localhost:4001/users/fuser1/following",
310 ap_id: "http://localhost:4001/users/fuser1"
311 )
312
313 user =
314 insert(:user, %{
315 local: false,
316 nickname: "fuser2",
317 ap_id: "http://localhost:4001/users/fuser2",
318 follower_address: "http://localhost:4001/users/fuser2/followers",
319 following_address: "http://localhost:4001/users/fuser2/following"
320 })
321
322 {:ok, user} = User.follow(user, followed, :follow_accept)
323
324 {:ok, user, _activity} = User.unfollow(user, followed)
325
326 user = User.get_cached_by_id(user.id)
327
328 assert User.following(user) == []
329 end
330
331 test "unfollow takes a user and another user" do
332 followed = insert(:user)
333 user = insert(:user)
334
335 {:ok, user} = User.follow(user, followed, :follow_accept)
336
337 assert User.following(user) == [user.follower_address, followed.follower_address]
338
339 {:ok, user, _activity} = User.unfollow(user, followed)
340
341 assert User.following(user) == [user.follower_address]
342 end
343
344 test "unfollow doesn't unfollow yourself" do
345 user = insert(:user)
346
347 {:error, _} = User.unfollow(user, user)
348
349 assert User.following(user) == [user.follower_address]
350 end
351 end
352
353 test "test if a user is following another user" do
354 followed = insert(:user)
355 user = insert(:user)
356 User.follow(user, followed, :follow_accept)
357
358 assert User.following?(user, followed)
359 refute User.following?(followed, user)
360 end
361
362 test "fetches correct profile for nickname beginning with number" do
363 # Use old-style integer ID to try to reproduce the problem
364 user = insert(:user, %{id: 1080})
365 user_with_numbers = insert(:user, %{nickname: "#{user.id}garbage"})
366 assert user_with_numbers == User.get_cached_by_nickname_or_id(user_with_numbers.nickname)
367 end
368
369 describe "user registration" do
370 @full_user_data %{
371 bio: "A guy",
372 name: "my name",
373 nickname: "nick",
374 password: "test",
375 password_confirmation: "test",
376 email: "email@example.com"
377 }
378 setup do: clear_config([:instance, :autofollowed_nicknames])
379 setup do: clear_config([:instance, :welcome_message])
380 setup do: clear_config([:instance, :welcome_user_nickname])
381
382 test "it autofollows accounts that are set for it" do
383 user = insert(:user)
384 remote_user = insert(:user, %{local: false})
385
386 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
387 user.nickname,
388 remote_user.nickname
389 ])
390
391 cng = User.register_changeset(%User{}, @full_user_data)
392
393 {:ok, registered_user} = User.register(cng)
394
395 assert User.following?(registered_user, user)
396 refute User.following?(registered_user, remote_user)
397 end
398
399 test "it sends a welcome message if it is set" do
400 welcome_user = insert(:user)
401
402 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
403 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
404
405 cng = User.register_changeset(%User{}, @full_user_data)
406 {:ok, registered_user} = User.register(cng)
407
408 activity = Repo.one(Pleroma.Activity)
409 assert registered_user.ap_id in activity.recipients
410 assert Object.normalize(activity).data["content"] =~ "cool site"
411 assert activity.actor == welcome_user.ap_id
412 end
413
414 setup do: clear_config([:instance, :account_activation_required])
415
416 test "it requires an email, name, nickname and password, bio is optional when account_activation_required is enabled" do
417 Pleroma.Config.put([:instance, :account_activation_required], true)
418
419 @full_user_data
420 |> Map.keys()
421 |> Enum.each(fn key ->
422 params = Map.delete(@full_user_data, key)
423 changeset = User.register_changeset(%User{}, params)
424
425 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
426 end)
427 end
428
429 test "it requires an name, nickname and password, bio and email are optional when account_activation_required is disabled" do
430 Pleroma.Config.put([:instance, :account_activation_required], false)
431
432 @full_user_data
433 |> Map.keys()
434 |> Enum.each(fn key ->
435 params = Map.delete(@full_user_data, key)
436 changeset = User.register_changeset(%User{}, params)
437
438 assert if key in [:bio, :email], do: changeset.valid?, else: not changeset.valid?
439 end)
440 end
441
442 test "it restricts certain nicknames" do
443 [restricted_name | _] = Pleroma.Config.get([User, :restricted_nicknames])
444
445 assert is_bitstring(restricted_name)
446
447 params =
448 @full_user_data
449 |> Map.put(:nickname, restricted_name)
450
451 changeset = User.register_changeset(%User{}, params)
452
453 refute changeset.valid?
454 end
455
456 test "it sets the password_hash and ap_id" do
457 changeset = User.register_changeset(%User{}, @full_user_data)
458
459 assert changeset.valid?
460
461 assert is_binary(changeset.changes[:password_hash])
462 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
463
464 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
465 end
466 end
467
468 describe "user registration, with :account_activation_required" do
469 @full_user_data %{
470 bio: "A guy",
471 name: "my name",
472 nickname: "nick",
473 password: "test",
474 password_confirmation: "test",
475 email: "email@example.com"
476 }
477 setup do: clear_config([:instance, :account_activation_required], true)
478
479 test "it creates unconfirmed user" do
480 changeset = User.register_changeset(%User{}, @full_user_data)
481 assert changeset.valid?
482
483 {:ok, user} = Repo.insert(changeset)
484
485 assert user.confirmation_pending
486 assert user.confirmation_token
487 end
488
489 test "it creates confirmed user if :confirmed option is given" do
490 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
491 assert changeset.valid?
492
493 {:ok, user} = Repo.insert(changeset)
494
495 refute user.confirmation_pending
496 refute user.confirmation_token
497 end
498 end
499
500 describe "get_or_fetch/1" do
501 test "gets an existing user by nickname" do
502 user = insert(:user)
503 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
504
505 assert user == fetched_user
506 end
507
508 test "gets an existing user by ap_id" do
509 ap_id = "http://mastodon.example.org/users/admin"
510
511 user =
512 insert(
513 :user,
514 local: false,
515 nickname: "admin@mastodon.example.org",
516 ap_id: ap_id
517 )
518
519 {:ok, fetched_user} = User.get_or_fetch(ap_id)
520 freshed_user = refresh_record(user)
521 assert freshed_user == fetched_user
522 end
523 end
524
525 describe "fetching a user from nickname or trying to build one" do
526 test "gets an existing user" do
527 user = insert(:user)
528 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
529
530 assert user == fetched_user
531 end
532
533 test "gets an existing user, case insensitive" do
534 user = insert(:user, nickname: "nick")
535 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
536
537 assert user == fetched_user
538 end
539
540 test "gets an existing user by fully qualified nickname" do
541 user = insert(:user)
542
543 {:ok, fetched_user} =
544 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
545
546 assert user == fetched_user
547 end
548
549 test "gets an existing user by fully qualified nickname, case insensitive" do
550 user = insert(:user, nickname: "nick")
551 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
552
553 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
554
555 assert user == fetched_user
556 end
557
558 @tag capture_log: true
559 test "returns nil if no user could be fetched" do
560 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
561 assert fetched_user == "not found nonexistant@social.heldscal.la"
562 end
563
564 test "returns nil for nonexistant local user" do
565 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
566 assert fetched_user == "not found nonexistant"
567 end
568
569 test "updates an existing user, if stale" do
570 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
571
572 orig_user =
573 insert(
574 :user,
575 local: false,
576 nickname: "admin@mastodon.example.org",
577 ap_id: "http://mastodon.example.org/users/admin",
578 last_refreshed_at: a_week_ago
579 )
580
581 assert orig_user.last_refreshed_at == a_week_ago
582
583 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
584
585 assert user.inbox
586
587 refute user.last_refreshed_at == orig_user.last_refreshed_at
588 end
589 end
590
591 test "returns an ap_id for a user" do
592 user = insert(:user)
593
594 assert User.ap_id(user) ==
595 Pleroma.Web.Router.Helpers.user_feed_url(
596 Pleroma.Web.Endpoint,
597 :feed_redirect,
598 user.nickname
599 )
600 end
601
602 test "returns an ap_followers link for a user" do
603 user = insert(:user)
604
605 assert User.ap_followers(user) ==
606 Pleroma.Web.Router.Helpers.user_feed_url(
607 Pleroma.Web.Endpoint,
608 :feed_redirect,
609 user.nickname
610 ) <> "/followers"
611 end
612
613 describe "remote user changeset" do
614 @valid_remote %{
615 bio: "hello",
616 name: "Someone",
617 nickname: "a@b.de",
618 ap_id: "http...",
619 avatar: %{some: "avatar"}
620 }
621 setup do: clear_config([:instance, :user_bio_length])
622 setup do: clear_config([:instance, :user_name_length])
623
624 test "it confirms validity" do
625 cs = User.remote_user_changeset(@valid_remote)
626 assert cs.valid?
627 end
628
629 test "it sets the follower_adress" do
630 cs = User.remote_user_changeset(@valid_remote)
631 # remote users get a fake local follower address
632 assert cs.changes.follower_address ==
633 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
634 end
635
636 test "it enforces the fqn format for nicknames" do
637 cs = User.remote_user_changeset(%{@valid_remote | nickname: "bla"})
638 assert Ecto.Changeset.get_field(cs, :local) == false
639 assert cs.changes.avatar
640 refute cs.valid?
641 end
642
643 test "it has required fields" do
644 [:ap_id]
645 |> Enum.each(fn field ->
646 cs = User.remote_user_changeset(Map.delete(@valid_remote, field))
647 refute cs.valid?
648 end)
649 end
650 end
651
652 describe "followers and friends" do
653 test "gets all followers for a given user" do
654 user = insert(:user)
655 follower_one = insert(:user)
656 follower_two = insert(:user)
657 not_follower = insert(:user)
658
659 {:ok, follower_one} = User.follow(follower_one, user)
660 {:ok, follower_two} = User.follow(follower_two, user)
661
662 res = User.get_followers(user)
663
664 assert Enum.member?(res, follower_one)
665 assert Enum.member?(res, follower_two)
666 refute Enum.member?(res, not_follower)
667 end
668
669 test "gets all friends (followed users) for a given user" do
670 user = insert(:user)
671 followed_one = insert(:user)
672 followed_two = insert(:user)
673 not_followed = insert(:user)
674
675 {:ok, user} = User.follow(user, followed_one)
676 {:ok, user} = User.follow(user, followed_two)
677
678 res = User.get_friends(user)
679
680 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
681 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
682 assert Enum.member?(res, followed_one)
683 assert Enum.member?(res, followed_two)
684 refute Enum.member?(res, not_followed)
685 end
686 end
687
688 describe "updating note and follower count" do
689 test "it sets the note_count property" do
690 note = insert(:note)
691
692 user = User.get_cached_by_ap_id(note.data["actor"])
693
694 assert user.note_count == 0
695
696 {:ok, user} = User.update_note_count(user)
697
698 assert user.note_count == 1
699 end
700
701 test "it increases the note_count property" do
702 note = insert(:note)
703 user = User.get_cached_by_ap_id(note.data["actor"])
704
705 assert user.note_count == 0
706
707 {:ok, user} = User.increase_note_count(user)
708
709 assert user.note_count == 1
710
711 {:ok, user} = User.increase_note_count(user)
712
713 assert user.note_count == 2
714 end
715
716 test "it decreases the note_count property" do
717 note = insert(:note)
718 user = User.get_cached_by_ap_id(note.data["actor"])
719
720 assert user.note_count == 0
721
722 {:ok, user} = User.increase_note_count(user)
723
724 assert user.note_count == 1
725
726 {:ok, user} = User.decrease_note_count(user)
727
728 assert user.note_count == 0
729
730 {:ok, user} = User.decrease_note_count(user)
731
732 assert user.note_count == 0
733 end
734
735 test "it sets the follower_count property" do
736 user = insert(:user)
737 follower = insert(:user)
738
739 User.follow(follower, user)
740
741 assert user.follower_count == 0
742
743 {:ok, user} = User.update_follower_count(user)
744
745 assert user.follower_count == 1
746 end
747 end
748
749 describe "follow_import" do
750 test "it imports user followings from list" do
751 [user1, user2, user3] = insert_list(3, :user)
752
753 identifiers = [
754 user2.ap_id,
755 user3.nickname
756 ]
757
758 {:ok, job} = User.follow_import(user1, identifiers)
759
760 assert {:ok, result} = ObanHelpers.perform(job)
761 assert is_list(result)
762 assert result == [user2, user3]
763 end
764 end
765
766 describe "mutes" do
767 test "it mutes people" do
768 user = insert(:user)
769 muted_user = insert(:user)
770
771 refute User.mutes?(user, muted_user)
772 refute User.muted_notifications?(user, muted_user)
773
774 {:ok, _user_relationships} = User.mute(user, muted_user)
775
776 assert User.mutes?(user, muted_user)
777 assert User.muted_notifications?(user, muted_user)
778 end
779
780 test "it unmutes users" do
781 user = insert(:user)
782 muted_user = insert(:user)
783
784 {:ok, _user_relationships} = User.mute(user, muted_user)
785 {:ok, _user_mute} = User.unmute(user, muted_user)
786
787 refute User.mutes?(user, muted_user)
788 refute User.muted_notifications?(user, muted_user)
789 end
790
791 test "it mutes user without notifications" do
792 user = insert(:user)
793 muted_user = insert(:user)
794
795 refute User.mutes?(user, muted_user)
796 refute User.muted_notifications?(user, muted_user)
797
798 {:ok, _user_relationships} = User.mute(user, muted_user, false)
799
800 assert User.mutes?(user, muted_user)
801 refute User.muted_notifications?(user, muted_user)
802 end
803 end
804
805 describe "blocks" do
806 test "it blocks people" do
807 user = insert(:user)
808 blocked_user = insert(:user)
809
810 refute User.blocks?(user, blocked_user)
811
812 {:ok, _user_relationship} = User.block(user, blocked_user)
813
814 assert User.blocks?(user, blocked_user)
815 end
816
817 test "it unblocks users" do
818 user = insert(:user)
819 blocked_user = insert(:user)
820
821 {:ok, _user_relationship} = User.block(user, blocked_user)
822 {:ok, _user_block} = User.unblock(user, blocked_user)
823
824 refute User.blocks?(user, blocked_user)
825 end
826
827 test "blocks tear down cyclical follow relationships" do
828 blocker = insert(:user)
829 blocked = insert(:user)
830
831 {:ok, blocker} = User.follow(blocker, blocked)
832 {:ok, blocked} = User.follow(blocked, blocker)
833
834 assert User.following?(blocker, blocked)
835 assert User.following?(blocked, blocker)
836
837 {:ok, _user_relationship} = User.block(blocker, blocked)
838 blocked = User.get_cached_by_id(blocked.id)
839
840 assert User.blocks?(blocker, blocked)
841
842 refute User.following?(blocker, blocked)
843 refute User.following?(blocked, blocker)
844 end
845
846 test "blocks tear down blocker->blocked follow relationships" do
847 blocker = insert(:user)
848 blocked = insert(:user)
849
850 {:ok, blocker} = User.follow(blocker, blocked)
851
852 assert User.following?(blocker, blocked)
853 refute User.following?(blocked, blocker)
854
855 {:ok, _user_relationship} = User.block(blocker, blocked)
856 blocked = User.get_cached_by_id(blocked.id)
857
858 assert User.blocks?(blocker, blocked)
859
860 refute User.following?(blocker, blocked)
861 refute User.following?(blocked, blocker)
862 end
863
864 test "blocks tear down blocked->blocker follow relationships" do
865 blocker = insert(:user)
866 blocked = insert(:user)
867
868 {:ok, blocked} = User.follow(blocked, blocker)
869
870 refute User.following?(blocker, blocked)
871 assert User.following?(blocked, blocker)
872
873 {:ok, _user_relationship} = User.block(blocker, blocked)
874 blocked = User.get_cached_by_id(blocked.id)
875
876 assert User.blocks?(blocker, blocked)
877
878 refute User.following?(blocker, blocked)
879 refute User.following?(blocked, blocker)
880 end
881
882 test "blocks tear down blocked->blocker subscription relationships" do
883 blocker = insert(:user)
884 blocked = insert(:user)
885
886 {:ok, _subscription} = User.subscribe(blocked, blocker)
887
888 assert User.subscribed_to?(blocked, blocker)
889 refute User.subscribed_to?(blocker, blocked)
890
891 {:ok, _user_relationship} = User.block(blocker, blocked)
892
893 assert User.blocks?(blocker, blocked)
894 refute User.subscribed_to?(blocker, blocked)
895 refute User.subscribed_to?(blocked, blocker)
896 end
897 end
898
899 describe "domain blocking" do
900 test "blocks domains" do
901 user = insert(:user)
902 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
903
904 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
905
906 assert User.blocks?(user, collateral_user)
907 end
908
909 test "does not block domain with same end" do
910 user = insert(:user)
911
912 collateral_user =
913 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
914
915 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
916
917 refute User.blocks?(user, collateral_user)
918 end
919
920 test "does not block domain with same end if wildcard added" do
921 user = insert(:user)
922
923 collateral_user =
924 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
925
926 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
927
928 refute User.blocks?(user, collateral_user)
929 end
930
931 test "blocks domain with wildcard for subdomain" do
932 user = insert(:user)
933
934 user_from_subdomain =
935 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
936
937 user_with_two_subdomains =
938 insert(:user, %{
939 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
940 })
941
942 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
943
944 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
945
946 assert User.blocks?(user, user_from_subdomain)
947 assert User.blocks?(user, user_with_two_subdomains)
948 assert User.blocks?(user, user_domain)
949 end
950
951 test "unblocks domains" do
952 user = insert(:user)
953 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
954
955 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
956 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
957
958 refute User.blocks?(user, collateral_user)
959 end
960
961 test "follows take precedence over domain blocks" do
962 user = insert(:user)
963 good_eggo = insert(:user, %{ap_id: "https://meanies.social/user/cuteposter"})
964
965 {:ok, user} = User.block_domain(user, "meanies.social")
966 {:ok, user} = User.follow(user, good_eggo)
967
968 refute User.blocks?(user, good_eggo)
969 end
970 end
971
972 describe "blocks_import" do
973 test "it imports user blocks from list" do
974 [user1, user2, user3] = insert_list(3, :user)
975
976 identifiers = [
977 user2.ap_id,
978 user3.nickname
979 ]
980
981 {:ok, job} = User.blocks_import(user1, identifiers)
982
983 assert {:ok, result} = ObanHelpers.perform(job)
984 assert is_list(result)
985 assert result == [user2, user3]
986 end
987 end
988
989 describe "get_recipients_from_activity" do
990 test "works for announces" do
991 actor = insert(:user)
992 user = insert(:user, local: true)
993
994 {:ok, activity} = CommonAPI.post(actor, %{status: "hello"})
995 {:ok, announce, _} = CommonAPI.repeat(activity.id, user)
996
997 recipients = User.get_recipients_from_activity(announce)
998
999 assert user in recipients
1000 end
1001
1002 test "get recipients" do
1003 actor = insert(:user)
1004 user = insert(:user, local: true)
1005 user_two = insert(:user, local: false)
1006 addressed = insert(:user, local: true)
1007 addressed_remote = insert(:user, local: false)
1008
1009 {:ok, activity} =
1010 CommonAPI.post(actor, %{
1011 status: "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
1012 })
1013
1014 assert Enum.map([actor, addressed], & &1.ap_id) --
1015 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1016
1017 {:ok, user} = User.follow(user, actor)
1018 {:ok, _user_two} = User.follow(user_two, actor)
1019 recipients = User.get_recipients_from_activity(activity)
1020 assert length(recipients) == 3
1021 assert user in recipients
1022 assert addressed in recipients
1023 end
1024
1025 test "has following" do
1026 actor = insert(:user)
1027 user = insert(:user)
1028 user_two = insert(:user)
1029 addressed = insert(:user, local: true)
1030
1031 {:ok, activity} =
1032 CommonAPI.post(actor, %{
1033 status: "hey @#{addressed.nickname}"
1034 })
1035
1036 assert Enum.map([actor, addressed], & &1.ap_id) --
1037 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1038
1039 {:ok, _actor} = User.follow(actor, user)
1040 {:ok, _actor} = User.follow(actor, user_two)
1041 recipients = User.get_recipients_from_activity(activity)
1042 assert length(recipients) == 2
1043 assert addressed in recipients
1044 end
1045 end
1046
1047 describe ".deactivate" do
1048 test "can de-activate then re-activate a user" do
1049 user = insert(:user)
1050 assert false == user.deactivated
1051 {:ok, user} = User.deactivate(user)
1052 assert true == user.deactivated
1053 {:ok, user} = User.deactivate(user, false)
1054 assert false == user.deactivated
1055 end
1056
1057 test "hide a user from followers" do
1058 user = insert(:user)
1059 user2 = insert(:user)
1060
1061 {:ok, user} = User.follow(user, user2)
1062 {:ok, _user} = User.deactivate(user)
1063
1064 user2 = User.get_cached_by_id(user2.id)
1065
1066 assert user2.follower_count == 0
1067 assert [] = User.get_followers(user2)
1068 end
1069
1070 test "hide a user from friends" do
1071 user = insert(:user)
1072 user2 = insert(:user)
1073
1074 {:ok, user2} = User.follow(user2, user)
1075 assert user2.following_count == 1
1076 assert User.following_count(user2) == 1
1077
1078 {:ok, _user} = User.deactivate(user)
1079
1080 user2 = User.get_cached_by_id(user2.id)
1081
1082 assert refresh_record(user2).following_count == 0
1083 assert user2.following_count == 0
1084 assert User.following_count(user2) == 0
1085 assert [] = User.get_friends(user2)
1086 end
1087
1088 test "hide a user's statuses from timelines and notifications" do
1089 user = insert(:user)
1090 user2 = insert(:user)
1091
1092 {:ok, user2} = User.follow(user2, user)
1093
1094 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{user2.nickname}"})
1095
1096 activity = Repo.preload(activity, :bookmark)
1097
1098 [notification] = Pleroma.Notification.for_user(user2)
1099 assert notification.activity.id == activity.id
1100
1101 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
1102
1103 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
1104 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1105 "user" => user2
1106 })
1107
1108 {:ok, _user} = User.deactivate(user)
1109
1110 assert [] == ActivityPub.fetch_public_activities(%{})
1111 assert [] == Pleroma.Notification.for_user(user2)
1112
1113 assert [] ==
1114 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1115 "user" => user2
1116 })
1117 end
1118 end
1119
1120 describe "delete" do
1121 setup do
1122 {:ok, user} = insert(:user) |> User.set_cache()
1123
1124 [user: user]
1125 end
1126
1127 setup do: clear_config([:instance, :federating])
1128
1129 test ".delete_user_activities deletes all create activities", %{user: user} do
1130 {:ok, activity} = CommonAPI.post(user, %{status: "2hu"})
1131
1132 User.delete_user_activities(user)
1133
1134 # TODO: Test removal favorites, repeats, delete activities.
1135 refute Activity.get_by_id(activity.id)
1136 end
1137
1138 test "it deactivates a user, all follow relationships and all activities", %{user: user} do
1139 follower = insert(:user)
1140 {:ok, follower} = User.follow(follower, user)
1141
1142 object = insert(:note, user: user)
1143 activity = insert(:note_activity, user: user, note: object)
1144
1145 object_two = insert(:note, user: follower)
1146 activity_two = insert(:note_activity, user: follower, note: object_two)
1147
1148 {:ok, like} = CommonAPI.favorite(user, activity_two.id)
1149 {:ok, like_two} = CommonAPI.favorite(follower, activity.id)
1150 {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
1151
1152 {:ok, job} = User.delete(user)
1153 {:ok, _user} = ObanHelpers.perform(job)
1154
1155 follower = User.get_cached_by_id(follower.id)
1156
1157 refute User.following?(follower, user)
1158 assert %{deactivated: true} = User.get_by_id(user.id)
1159
1160 user_activities =
1161 user.ap_id
1162 |> Activity.Queries.by_actor()
1163 |> Repo.all()
1164 |> Enum.map(fn act -> act.data["type"] end)
1165
1166 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1167
1168 refute Activity.get_by_id(activity.id)
1169 refute Activity.get_by_id(like.id)
1170 refute Activity.get_by_id(like_two.id)
1171 refute Activity.get_by_id(repeat.id)
1172 end
1173 end
1174
1175 describe "delete/1 when confirmation is pending" do
1176 setup do
1177 user = insert(:user, confirmation_pending: true)
1178 {:ok, user: user}
1179 end
1180
1181 test "deletes user from database when activation required", %{user: user} do
1182 clear_config([:instance, :account_activation_required], true)
1183
1184 {:ok, job} = User.delete(user)
1185 {:ok, _} = ObanHelpers.perform(job)
1186
1187 refute User.get_cached_by_id(user.id)
1188 refute User.get_by_id(user.id)
1189 end
1190
1191 test "deactivates user when activation is not required", %{user: user} do
1192 clear_config([:instance, :account_activation_required], false)
1193
1194 {:ok, job} = User.delete(user)
1195 {:ok, _} = ObanHelpers.perform(job)
1196
1197 assert %{deactivated: true} = User.get_cached_by_id(user.id)
1198 assert %{deactivated: true} = User.get_by_id(user.id)
1199 end
1200 end
1201
1202 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1203 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1204 end
1205
1206 describe "per-user rich-text filtering" do
1207 test "html_filter_policy returns default policies, when rich-text is enabled" do
1208 user = insert(:user)
1209
1210 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1211 end
1212
1213 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1214 user = insert(:user, no_rich_text: true)
1215
1216 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1217 end
1218 end
1219
1220 describe "caching" do
1221 test "invalidate_cache works" do
1222 user = insert(:user)
1223
1224 User.set_cache(user)
1225 User.invalidate_cache(user)
1226
1227 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1228 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1229 end
1230
1231 test "User.delete() plugs any possible zombie objects" do
1232 user = insert(:user)
1233
1234 {:ok, job} = User.delete(user)
1235 {:ok, _} = ObanHelpers.perform(job)
1236
1237 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1238
1239 assert cached_user != user
1240
1241 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1242
1243 assert cached_user != user
1244 end
1245 end
1246
1247 describe "account_status/1" do
1248 setup do: clear_config([:instance, :account_activation_required])
1249
1250 test "return confirmation_pending for unconfirm user" do
1251 Pleroma.Config.put([:instance, :account_activation_required], true)
1252 user = insert(:user, confirmation_pending: true)
1253 assert User.account_status(user) == :confirmation_pending
1254 end
1255
1256 test "return active for confirmed user" do
1257 Pleroma.Config.put([:instance, :account_activation_required], true)
1258 user = insert(:user, confirmation_pending: false)
1259 assert User.account_status(user) == :active
1260 end
1261
1262 test "return active for remote user" do
1263 user = insert(:user, local: false)
1264 assert User.account_status(user) == :active
1265 end
1266
1267 test "returns :password_reset_pending for user with reset password" do
1268 user = insert(:user, password_reset_pending: true)
1269 assert User.account_status(user) == :password_reset_pending
1270 end
1271
1272 test "returns :deactivated for deactivated user" do
1273 user = insert(:user, local: true, confirmation_pending: false, deactivated: true)
1274 assert User.account_status(user) == :deactivated
1275 end
1276 end
1277
1278 describe "superuser?/1" do
1279 test "returns false for unprivileged users" do
1280 user = insert(:user, local: true)
1281
1282 refute User.superuser?(user)
1283 end
1284
1285 test "returns false for remote users" do
1286 user = insert(:user, local: false)
1287 remote_admin_user = insert(:user, local: false, is_admin: true)
1288
1289 refute User.superuser?(user)
1290 refute User.superuser?(remote_admin_user)
1291 end
1292
1293 test "returns true for local moderators" do
1294 user = insert(:user, local: true, is_moderator: true)
1295
1296 assert User.superuser?(user)
1297 end
1298
1299 test "returns true for local admins" do
1300 user = insert(:user, local: true, is_admin: true)
1301
1302 assert User.superuser?(user)
1303 end
1304 end
1305
1306 describe "invisible?/1" do
1307 test "returns true for an invisible user" do
1308 user = insert(:user, local: true, invisible: true)
1309
1310 assert User.invisible?(user)
1311 end
1312
1313 test "returns false for a non-invisible user" do
1314 user = insert(:user, local: true)
1315
1316 refute User.invisible?(user)
1317 end
1318 end
1319
1320 describe "visible_for?/2" do
1321 test "returns true when the account is itself" do
1322 user = insert(:user, local: true)
1323
1324 assert User.visible_for?(user, user)
1325 end
1326
1327 test "returns false when the account is unauthenticated and auth is required" do
1328 Pleroma.Config.put([:instance, :account_activation_required], true)
1329
1330 user = insert(:user, local: true, confirmation_pending: true)
1331 other_user = insert(:user, local: true)
1332
1333 refute User.visible_for?(user, other_user)
1334 end
1335
1336 test "returns true when the account is unauthenticated and auth is not required" do
1337 user = insert(:user, local: true, confirmation_pending: true)
1338 other_user = insert(:user, local: true)
1339
1340 assert User.visible_for?(user, other_user)
1341 end
1342
1343 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1344 Pleroma.Config.put([:instance, :account_activation_required], true)
1345
1346 user = insert(:user, local: true, confirmation_pending: true)
1347 other_user = insert(:user, local: true, is_admin: true)
1348
1349 assert User.visible_for?(user, other_user)
1350 end
1351 end
1352
1353 describe "parse_bio/2" do
1354 test "preserves hosts in user links text" do
1355 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1356 user = insert(:user)
1357 bio = "A.k.a. @nick@domain.com"
1358
1359 expected_text =
1360 ~s(A.k.a. <span class="h-card"><a class="u-url mention" data-user="#{remote_user.id}" href="#{
1361 remote_user.ap_id
1362 }" rel="ugc">@<span>nick@domain.com</span></a></span>)
1363
1364 assert expected_text == User.parse_bio(bio, user)
1365 end
1366
1367 test "Adds rel=me on linkbacked urls" do
1368 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1369
1370 bio = "http://example.com/rel_me/null"
1371 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1372 assert expected_text == User.parse_bio(bio, user)
1373
1374 bio = "http://example.com/rel_me/link"
1375 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1376 assert expected_text == User.parse_bio(bio, user)
1377
1378 bio = "http://example.com/rel_me/anchor"
1379 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1380 assert expected_text == User.parse_bio(bio, user)
1381 end
1382 end
1383
1384 test "follower count is updated when a follower is blocked" do
1385 user = insert(:user)
1386 follower = insert(:user)
1387 follower2 = insert(:user)
1388 follower3 = insert(:user)
1389
1390 {:ok, follower} = User.follow(follower, user)
1391 {:ok, _follower2} = User.follow(follower2, user)
1392 {:ok, _follower3} = User.follow(follower3, user)
1393
1394 {:ok, _user_relationship} = User.block(user, follower)
1395 user = refresh_record(user)
1396
1397 assert user.follower_count == 2
1398 end
1399
1400 describe "list_inactive_users_query/1" do
1401 defp days_ago(days) do
1402 NaiveDateTime.add(
1403 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1404 -days * 60 * 60 * 24,
1405 :second
1406 )
1407 end
1408
1409 test "Users are inactive by default" do
1410 total = 10
1411
1412 users =
1413 Enum.map(1..total, fn _ ->
1414 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1415 end)
1416
1417 inactive_users_ids =
1418 Pleroma.User.list_inactive_users_query()
1419 |> Pleroma.Repo.all()
1420 |> Enum.map(& &1.id)
1421
1422 Enum.each(users, fn user ->
1423 assert user.id in inactive_users_ids
1424 end)
1425 end
1426
1427 test "Only includes users who has no recent activity" do
1428 total = 10
1429
1430 users =
1431 Enum.map(1..total, fn _ ->
1432 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1433 end)
1434
1435 {inactive, active} = Enum.split(users, trunc(total / 2))
1436
1437 Enum.map(active, fn user ->
1438 to = Enum.random(users -- [user])
1439
1440 {:ok, _} =
1441 CommonAPI.post(user, %{
1442 status: "hey @#{to.nickname}"
1443 })
1444 end)
1445
1446 inactive_users_ids =
1447 Pleroma.User.list_inactive_users_query()
1448 |> Pleroma.Repo.all()
1449 |> Enum.map(& &1.id)
1450
1451 Enum.each(active, fn user ->
1452 refute user.id in inactive_users_ids
1453 end)
1454
1455 Enum.each(inactive, fn user ->
1456 assert user.id in inactive_users_ids
1457 end)
1458 end
1459
1460 test "Only includes users with no read notifications" do
1461 total = 10
1462
1463 users =
1464 Enum.map(1..total, fn _ ->
1465 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1466 end)
1467
1468 [sender | recipients] = users
1469 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1470
1471 Enum.each(recipients, fn to ->
1472 {:ok, _} =
1473 CommonAPI.post(sender, %{
1474 status: "hey @#{to.nickname}"
1475 })
1476
1477 {:ok, _} =
1478 CommonAPI.post(sender, %{
1479 status: "hey again @#{to.nickname}"
1480 })
1481 end)
1482
1483 Enum.each(active, fn user ->
1484 [n1, _n2] = Pleroma.Notification.for_user(user)
1485 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1486 end)
1487
1488 inactive_users_ids =
1489 Pleroma.User.list_inactive_users_query()
1490 |> Pleroma.Repo.all()
1491 |> Enum.map(& &1.id)
1492
1493 Enum.each(active, fn user ->
1494 refute user.id in inactive_users_ids
1495 end)
1496
1497 Enum.each(inactive, fn user ->
1498 assert user.id in inactive_users_ids
1499 end)
1500 end
1501 end
1502
1503 describe "toggle_confirmation/1" do
1504 test "if user is confirmed" do
1505 user = insert(:user, confirmation_pending: false)
1506 {:ok, user} = User.toggle_confirmation(user)
1507
1508 assert user.confirmation_pending
1509 assert user.confirmation_token
1510 end
1511
1512 test "if user is unconfirmed" do
1513 user = insert(:user, confirmation_pending: true, confirmation_token: "some token")
1514 {:ok, user} = User.toggle_confirmation(user)
1515
1516 refute user.confirmation_pending
1517 refute user.confirmation_token
1518 end
1519 end
1520
1521 describe "ensure_keys_present" do
1522 test "it creates keys for a user and stores them in info" do
1523 user = insert(:user)
1524 refute is_binary(user.keys)
1525 {:ok, user} = User.ensure_keys_present(user)
1526 assert is_binary(user.keys)
1527 end
1528
1529 test "it doesn't create keys if there already are some" do
1530 user = insert(:user, keys: "xxx")
1531 {:ok, user} = User.ensure_keys_present(user)
1532 assert user.keys == "xxx"
1533 end
1534 end
1535
1536 describe "get_ap_ids_by_nicknames" do
1537 test "it returns a list of AP ids for a given set of nicknames" do
1538 user = insert(:user)
1539 user_two = insert(:user)
1540
1541 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1542 assert length(ap_ids) == 2
1543 assert user.ap_id in ap_ids
1544 assert user_two.ap_id in ap_ids
1545 end
1546 end
1547
1548 describe "sync followers count" do
1549 setup do
1550 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1551 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1552 insert(:user, local: true)
1553 insert(:user, local: false, deactivated: true)
1554 {:ok, user1: user1, user2: user2}
1555 end
1556
1557 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1558 [fdb_user1] = User.external_users(limit: 1)
1559
1560 assert fdb_user1.ap_id
1561 assert fdb_user1.ap_id == user1.ap_id
1562 assert fdb_user1.id == user1.id
1563
1564 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1565
1566 assert fdb_user2.ap_id
1567 assert fdb_user2.ap_id == user2.ap_id
1568 assert fdb_user2.id == user2.id
1569
1570 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1571 end
1572 end
1573
1574 describe "is_internal_user?/1" do
1575 test "non-internal user returns false" do
1576 user = insert(:user)
1577 refute User.is_internal_user?(user)
1578 end
1579
1580 test "user with no nickname returns true" do
1581 user = insert(:user, %{nickname: nil})
1582 assert User.is_internal_user?(user)
1583 end
1584
1585 test "user with internal-prefixed nickname returns true" do
1586 user = insert(:user, %{nickname: "internal.test"})
1587 assert User.is_internal_user?(user)
1588 end
1589 end
1590
1591 describe "update_and_set_cache/1" do
1592 test "returns error when user is stale instead Ecto.StaleEntryError" do
1593 user = insert(:user)
1594
1595 changeset = Ecto.Changeset.change(user, bio: "test")
1596
1597 Repo.delete(user)
1598
1599 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1600 User.update_and_set_cache(changeset)
1601 end
1602
1603 test "performs update cache if user updated" do
1604 user = insert(:user)
1605 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1606
1607 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1608
1609 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1610 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1611 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1612 end
1613 end
1614
1615 describe "following/followers synchronization" do
1616 setup do: clear_config([:instance, :external_user_synchronization])
1617
1618 test "updates the counters normally on following/getting a follow when disabled" do
1619 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1620 user = insert(:user)
1621
1622 other_user =
1623 insert(:user,
1624 local: false,
1625 follower_address: "http://localhost:4001/users/masto_closed/followers",
1626 following_address: "http://localhost:4001/users/masto_closed/following",
1627 ap_enabled: true
1628 )
1629
1630 assert other_user.following_count == 0
1631 assert other_user.follower_count == 0
1632
1633 {:ok, user} = Pleroma.User.follow(user, other_user)
1634 other_user = Pleroma.User.get_by_id(other_user.id)
1635
1636 assert user.following_count == 1
1637 assert other_user.follower_count == 1
1638 end
1639
1640 test "syncronizes the counters with the remote instance for the followed when enabled" do
1641 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1642
1643 user = insert(:user)
1644
1645 other_user =
1646 insert(:user,
1647 local: false,
1648 follower_address: "http://localhost:4001/users/masto_closed/followers",
1649 following_address: "http://localhost:4001/users/masto_closed/following",
1650 ap_enabled: true
1651 )
1652
1653 assert other_user.following_count == 0
1654 assert other_user.follower_count == 0
1655
1656 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1657 {:ok, _user} = User.follow(user, other_user)
1658 other_user = User.get_by_id(other_user.id)
1659
1660 assert other_user.follower_count == 437
1661 end
1662
1663 test "syncronizes the counters with the remote instance for the follower when enabled" do
1664 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1665
1666 user = insert(:user)
1667
1668 other_user =
1669 insert(:user,
1670 local: false,
1671 follower_address: "http://localhost:4001/users/masto_closed/followers",
1672 following_address: "http://localhost:4001/users/masto_closed/following",
1673 ap_enabled: true
1674 )
1675
1676 assert other_user.following_count == 0
1677 assert other_user.follower_count == 0
1678
1679 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1680 {:ok, other_user} = User.follow(other_user, user)
1681
1682 assert other_user.following_count == 152
1683 end
1684 end
1685
1686 describe "change_email/2" do
1687 setup do
1688 [user: insert(:user)]
1689 end
1690
1691 test "blank email returns error", %{user: user} do
1692 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1693 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1694 end
1695
1696 test "non unique email returns error", %{user: user} do
1697 %{email: email} = insert(:user)
1698
1699 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1700 User.change_email(user, email)
1701 end
1702
1703 test "invalid email returns error", %{user: user} do
1704 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1705 User.change_email(user, "cofe")
1706 end
1707
1708 test "changes email", %{user: user} do
1709 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1710 end
1711 end
1712
1713 describe "get_cached_by_nickname_or_id" do
1714 setup do
1715 local_user = insert(:user)
1716 remote_user = insert(:user, nickname: "nickname@example.com", local: false)
1717
1718 [local_user: local_user, remote_user: remote_user]
1719 end
1720
1721 setup do: clear_config([:instance, :limit_to_local_content])
1722
1723 test "allows getting remote users by id no matter what :limit_to_local_content is set to", %{
1724 remote_user: remote_user
1725 } do
1726 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1727 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1728
1729 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1730 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1731
1732 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1733 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1734 end
1735
1736 test "disallows getting remote users by nickname without authentication when :limit_to_local_content is set to :unauthenticated",
1737 %{remote_user: remote_user} do
1738 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1739 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1740 end
1741
1742 test "allows getting remote users by nickname with authentication when :limit_to_local_content is set to :unauthenticated",
1743 %{remote_user: remote_user, local_user: local_user} do
1744 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1745 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.nickname, for: local_user)
1746 end
1747
1748 test "disallows getting remote users by nickname when :limit_to_local_content is set to true",
1749 %{remote_user: remote_user} do
1750 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1751 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1752 end
1753
1754 test "allows getting local users by nickname no matter what :limit_to_local_content is set to",
1755 %{local_user: local_user} do
1756 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1757 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1758
1759 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1760 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1761
1762 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1763 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1764 end
1765 end
1766
1767 describe "update_email_notifications/2" do
1768 setup do
1769 user = insert(:user, email_notifications: %{"digest" => true})
1770
1771 {:ok, user: user}
1772 end
1773
1774 test "Notifications are updated", %{user: user} do
1775 true = user.email_notifications["digest"]
1776 assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false})
1777 assert result.email_notifications["digest"] == false
1778 end
1779 end
1780
1781 test "avatar fallback" do
1782 user = insert(:user)
1783 assert User.avatar_url(user) =~ "/images/avi.png"
1784
1785 Pleroma.Config.put([:assets, :default_user_avatar], "avatar.png")
1786
1787 user = User.get_cached_by_nickname_or_id(user.nickname)
1788 assert User.avatar_url(user) =~ "avatar.png"
1789
1790 assert User.avatar_url(user, no_default: true) == nil
1791 end
1792 end