Merge branch 'feature/1854-emoji-pagination' into 'develop'
[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
590 @tag capture_log: true
591 test "it returns the old user if stale, but unfetchable" do
592 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
593
594 orig_user =
595 insert(
596 :user,
597 local: false,
598 nickname: "admin@mastodon.example.org",
599 ap_id: "http://mastodon.example.org/users/raymoo",
600 last_refreshed_at: a_week_ago
601 )
602
603 assert orig_user.last_refreshed_at == a_week_ago
604
605 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/raymoo")
606
607 assert user.last_refreshed_at == orig_user.last_refreshed_at
608 end
609 end
610
611 test "returns an ap_id for a user" do
612 user = insert(:user)
613
614 assert User.ap_id(user) ==
615 Pleroma.Web.Router.Helpers.user_feed_url(
616 Pleroma.Web.Endpoint,
617 :feed_redirect,
618 user.nickname
619 )
620 end
621
622 test "returns an ap_followers link for a user" do
623 user = insert(:user)
624
625 assert User.ap_followers(user) ==
626 Pleroma.Web.Router.Helpers.user_feed_url(
627 Pleroma.Web.Endpoint,
628 :feed_redirect,
629 user.nickname
630 ) <> "/followers"
631 end
632
633 describe "remote user changeset" do
634 @valid_remote %{
635 bio: "hello",
636 name: "Someone",
637 nickname: "a@b.de",
638 ap_id: "http...",
639 avatar: %{some: "avatar"}
640 }
641 setup do: clear_config([:instance, :user_bio_length])
642 setup do: clear_config([:instance, :user_name_length])
643
644 test "it confirms validity" do
645 cs = User.remote_user_changeset(@valid_remote)
646 assert cs.valid?
647 end
648
649 test "it sets the follower_adress" do
650 cs = User.remote_user_changeset(@valid_remote)
651 # remote users get a fake local follower address
652 assert cs.changes.follower_address ==
653 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
654 end
655
656 test "it enforces the fqn format for nicknames" do
657 cs = User.remote_user_changeset(%{@valid_remote | nickname: "bla"})
658 assert Ecto.Changeset.get_field(cs, :local) == false
659 assert cs.changes.avatar
660 refute cs.valid?
661 end
662
663 test "it has required fields" do
664 [:ap_id]
665 |> Enum.each(fn field ->
666 cs = User.remote_user_changeset(Map.delete(@valid_remote, field))
667 refute cs.valid?
668 end)
669 end
670 end
671
672 describe "followers and friends" do
673 test "gets all followers for a given user" do
674 user = insert(:user)
675 follower_one = insert(:user)
676 follower_two = insert(:user)
677 not_follower = insert(:user)
678
679 {:ok, follower_one} = User.follow(follower_one, user)
680 {:ok, follower_two} = User.follow(follower_two, user)
681
682 res = User.get_followers(user)
683
684 assert Enum.member?(res, follower_one)
685 assert Enum.member?(res, follower_two)
686 refute Enum.member?(res, not_follower)
687 end
688
689 test "gets all friends (followed users) for a given user" do
690 user = insert(:user)
691 followed_one = insert(:user)
692 followed_two = insert(:user)
693 not_followed = insert(:user)
694
695 {:ok, user} = User.follow(user, followed_one)
696 {:ok, user} = User.follow(user, followed_two)
697
698 res = User.get_friends(user)
699
700 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
701 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
702 assert Enum.member?(res, followed_one)
703 assert Enum.member?(res, followed_two)
704 refute Enum.member?(res, not_followed)
705 end
706 end
707
708 describe "updating note and follower count" do
709 test "it sets the note_count property" do
710 note = insert(:note)
711
712 user = User.get_cached_by_ap_id(note.data["actor"])
713
714 assert user.note_count == 0
715
716 {:ok, user} = User.update_note_count(user)
717
718 assert user.note_count == 1
719 end
720
721 test "it increases the note_count property" do
722 note = insert(:note)
723 user = User.get_cached_by_ap_id(note.data["actor"])
724
725 assert user.note_count == 0
726
727 {:ok, user} = User.increase_note_count(user)
728
729 assert user.note_count == 1
730
731 {:ok, user} = User.increase_note_count(user)
732
733 assert user.note_count == 2
734 end
735
736 test "it decreases the note_count property" do
737 note = insert(:note)
738 user = User.get_cached_by_ap_id(note.data["actor"])
739
740 assert user.note_count == 0
741
742 {:ok, user} = User.increase_note_count(user)
743
744 assert user.note_count == 1
745
746 {:ok, user} = User.decrease_note_count(user)
747
748 assert user.note_count == 0
749
750 {:ok, user} = User.decrease_note_count(user)
751
752 assert user.note_count == 0
753 end
754
755 test "it sets the follower_count property" do
756 user = insert(:user)
757 follower = insert(:user)
758
759 User.follow(follower, user)
760
761 assert user.follower_count == 0
762
763 {:ok, user} = User.update_follower_count(user)
764
765 assert user.follower_count == 1
766 end
767 end
768
769 describe "follow_import" do
770 test "it imports user followings from list" do
771 [user1, user2, user3] = insert_list(3, :user)
772
773 identifiers = [
774 user2.ap_id,
775 user3.nickname
776 ]
777
778 {:ok, job} = User.follow_import(user1, identifiers)
779
780 assert {:ok, result} = ObanHelpers.perform(job)
781 assert is_list(result)
782 assert result == [user2, user3]
783 end
784 end
785
786 describe "mutes" do
787 test "it mutes people" do
788 user = insert(:user)
789 muted_user = insert(:user)
790
791 refute User.mutes?(user, muted_user)
792 refute User.muted_notifications?(user, muted_user)
793
794 {:ok, _user_relationships} = User.mute(user, muted_user)
795
796 assert User.mutes?(user, muted_user)
797 assert User.muted_notifications?(user, muted_user)
798 end
799
800 test "it unmutes users" do
801 user = insert(:user)
802 muted_user = insert(:user)
803
804 {:ok, _user_relationships} = User.mute(user, muted_user)
805 {:ok, _user_mute} = User.unmute(user, muted_user)
806
807 refute User.mutes?(user, muted_user)
808 refute User.muted_notifications?(user, muted_user)
809 end
810
811 test "it mutes user without notifications" do
812 user = insert(:user)
813 muted_user = insert(:user)
814
815 refute User.mutes?(user, muted_user)
816 refute User.muted_notifications?(user, muted_user)
817
818 {:ok, _user_relationships} = User.mute(user, muted_user, false)
819
820 assert User.mutes?(user, muted_user)
821 refute User.muted_notifications?(user, muted_user)
822 end
823 end
824
825 describe "blocks" do
826 test "it blocks people" do
827 user = insert(:user)
828 blocked_user = insert(:user)
829
830 refute User.blocks?(user, blocked_user)
831
832 {:ok, _user_relationship} = User.block(user, blocked_user)
833
834 assert User.blocks?(user, blocked_user)
835 end
836
837 test "it unblocks users" do
838 user = insert(:user)
839 blocked_user = insert(:user)
840
841 {:ok, _user_relationship} = User.block(user, blocked_user)
842 {:ok, _user_block} = User.unblock(user, blocked_user)
843
844 refute User.blocks?(user, blocked_user)
845 end
846
847 test "blocks tear down cyclical follow relationships" do
848 blocker = insert(:user)
849 blocked = insert(:user)
850
851 {:ok, blocker} = User.follow(blocker, blocked)
852 {:ok, blocked} = User.follow(blocked, blocker)
853
854 assert User.following?(blocker, blocked)
855 assert User.following?(blocked, blocker)
856
857 {:ok, _user_relationship} = User.block(blocker, blocked)
858 blocked = User.get_cached_by_id(blocked.id)
859
860 assert User.blocks?(blocker, blocked)
861
862 refute User.following?(blocker, blocked)
863 refute User.following?(blocked, blocker)
864 end
865
866 test "blocks tear down blocker->blocked follow relationships" do
867 blocker = insert(:user)
868 blocked = insert(:user)
869
870 {:ok, blocker} = User.follow(blocker, blocked)
871
872 assert User.following?(blocker, blocked)
873 refute User.following?(blocked, blocker)
874
875 {:ok, _user_relationship} = User.block(blocker, blocked)
876 blocked = User.get_cached_by_id(blocked.id)
877
878 assert User.blocks?(blocker, blocked)
879
880 refute User.following?(blocker, blocked)
881 refute User.following?(blocked, blocker)
882 end
883
884 test "blocks tear down blocked->blocker follow relationships" do
885 blocker = insert(:user)
886 blocked = insert(:user)
887
888 {:ok, blocked} = User.follow(blocked, blocker)
889
890 refute User.following?(blocker, blocked)
891 assert User.following?(blocked, blocker)
892
893 {:ok, _user_relationship} = User.block(blocker, blocked)
894 blocked = User.get_cached_by_id(blocked.id)
895
896 assert User.blocks?(blocker, blocked)
897
898 refute User.following?(blocker, blocked)
899 refute User.following?(blocked, blocker)
900 end
901
902 test "blocks tear down blocked->blocker subscription relationships" do
903 blocker = insert(:user)
904 blocked = insert(:user)
905
906 {:ok, _subscription} = User.subscribe(blocked, blocker)
907
908 assert User.subscribed_to?(blocked, blocker)
909 refute User.subscribed_to?(blocker, blocked)
910
911 {:ok, _user_relationship} = User.block(blocker, blocked)
912
913 assert User.blocks?(blocker, blocked)
914 refute User.subscribed_to?(blocker, blocked)
915 refute User.subscribed_to?(blocked, blocker)
916 end
917 end
918
919 describe "domain blocking" do
920 test "blocks domains" do
921 user = insert(:user)
922 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
923
924 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
925
926 assert User.blocks?(user, collateral_user)
927 end
928
929 test "does not block domain with same end" do
930 user = insert(:user)
931
932 collateral_user =
933 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
934
935 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
936
937 refute User.blocks?(user, collateral_user)
938 end
939
940 test "does not block domain with same end if wildcard added" do
941 user = insert(:user)
942
943 collateral_user =
944 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
945
946 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
947
948 refute User.blocks?(user, collateral_user)
949 end
950
951 test "blocks domain with wildcard for subdomain" do
952 user = insert(:user)
953
954 user_from_subdomain =
955 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
956
957 user_with_two_subdomains =
958 insert(:user, %{
959 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
960 })
961
962 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
963
964 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
965
966 assert User.blocks?(user, user_from_subdomain)
967 assert User.blocks?(user, user_with_two_subdomains)
968 assert User.blocks?(user, user_domain)
969 end
970
971 test "unblocks domains" do
972 user = insert(:user)
973 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
974
975 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
976 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
977
978 refute User.blocks?(user, collateral_user)
979 end
980
981 test "follows take precedence over domain blocks" do
982 user = insert(:user)
983 good_eggo = insert(:user, %{ap_id: "https://meanies.social/user/cuteposter"})
984
985 {:ok, user} = User.block_domain(user, "meanies.social")
986 {:ok, user} = User.follow(user, good_eggo)
987
988 refute User.blocks?(user, good_eggo)
989 end
990 end
991
992 describe "blocks_import" do
993 test "it imports user blocks from list" do
994 [user1, user2, user3] = insert_list(3, :user)
995
996 identifiers = [
997 user2.ap_id,
998 user3.nickname
999 ]
1000
1001 {:ok, job} = User.blocks_import(user1, identifiers)
1002
1003 assert {:ok, result} = ObanHelpers.perform(job)
1004 assert is_list(result)
1005 assert result == [user2, user3]
1006 end
1007 end
1008
1009 describe "get_recipients_from_activity" do
1010 test "works for announces" do
1011 actor = insert(:user)
1012 user = insert(:user, local: true)
1013
1014 {:ok, activity} = CommonAPI.post(actor, %{status: "hello"})
1015 {:ok, announce} = CommonAPI.repeat(activity.id, user)
1016
1017 recipients = User.get_recipients_from_activity(announce)
1018
1019 assert user in recipients
1020 end
1021
1022 test "get recipients" do
1023 actor = insert(:user)
1024 user = insert(:user, local: true)
1025 user_two = insert(:user, local: false)
1026 addressed = insert(:user, local: true)
1027 addressed_remote = insert(:user, local: false)
1028
1029 {:ok, activity} =
1030 CommonAPI.post(actor, %{
1031 status: "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
1032 })
1033
1034 assert Enum.map([actor, addressed], & &1.ap_id) --
1035 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1036
1037 {:ok, user} = User.follow(user, actor)
1038 {:ok, _user_two} = User.follow(user_two, actor)
1039 recipients = User.get_recipients_from_activity(activity)
1040 assert length(recipients) == 3
1041 assert user in recipients
1042 assert addressed in recipients
1043 end
1044
1045 test "has following" do
1046 actor = insert(:user)
1047 user = insert(:user)
1048 user_two = insert(:user)
1049 addressed = insert(:user, local: true)
1050
1051 {:ok, activity} =
1052 CommonAPI.post(actor, %{
1053 status: "hey @#{addressed.nickname}"
1054 })
1055
1056 assert Enum.map([actor, addressed], & &1.ap_id) --
1057 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1058
1059 {:ok, _actor} = User.follow(actor, user)
1060 {:ok, _actor} = User.follow(actor, user_two)
1061 recipients = User.get_recipients_from_activity(activity)
1062 assert length(recipients) == 2
1063 assert addressed in recipients
1064 end
1065 end
1066
1067 describe ".deactivate" do
1068 test "can de-activate then re-activate a user" do
1069 user = insert(:user)
1070 assert false == user.deactivated
1071 {:ok, user} = User.deactivate(user)
1072 assert true == user.deactivated
1073 {:ok, user} = User.deactivate(user, false)
1074 assert false == user.deactivated
1075 end
1076
1077 test "hide a user from followers" do
1078 user = insert(:user)
1079 user2 = insert(:user)
1080
1081 {:ok, user} = User.follow(user, user2)
1082 {:ok, _user} = User.deactivate(user)
1083
1084 user2 = User.get_cached_by_id(user2.id)
1085
1086 assert user2.follower_count == 0
1087 assert [] = User.get_followers(user2)
1088 end
1089
1090 test "hide a user from friends" do
1091 user = insert(:user)
1092 user2 = insert(:user)
1093
1094 {:ok, user2} = User.follow(user2, user)
1095 assert user2.following_count == 1
1096 assert User.following_count(user2) == 1
1097
1098 {:ok, _user} = User.deactivate(user)
1099
1100 user2 = User.get_cached_by_id(user2.id)
1101
1102 assert refresh_record(user2).following_count == 0
1103 assert user2.following_count == 0
1104 assert User.following_count(user2) == 0
1105 assert [] = User.get_friends(user2)
1106 end
1107
1108 test "hide a user's statuses from timelines and notifications" do
1109 user = insert(:user)
1110 user2 = insert(:user)
1111
1112 {:ok, user2} = User.follow(user2, user)
1113
1114 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{user2.nickname}"})
1115
1116 activity = Repo.preload(activity, :bookmark)
1117
1118 [notification] = Pleroma.Notification.for_user(user2)
1119 assert notification.activity.id == activity.id
1120
1121 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
1122
1123 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
1124 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1125 user: user2
1126 })
1127
1128 {:ok, _user} = User.deactivate(user)
1129
1130 assert [] == ActivityPub.fetch_public_activities(%{})
1131 assert [] == Pleroma.Notification.for_user(user2)
1132
1133 assert [] ==
1134 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1135 user: user2
1136 })
1137 end
1138 end
1139
1140 describe "delete" do
1141 setup do
1142 {:ok, user} = insert(:user) |> User.set_cache()
1143
1144 [user: user]
1145 end
1146
1147 setup do: clear_config([:instance, :federating])
1148
1149 test ".delete_user_activities deletes all create activities", %{user: user} do
1150 {:ok, activity} = CommonAPI.post(user, %{status: "2hu"})
1151
1152 User.delete_user_activities(user)
1153
1154 # TODO: Test removal favorites, repeats, delete activities.
1155 refute Activity.get_by_id(activity.id)
1156 end
1157
1158 test "it deactivates a user, all follow relationships and all activities", %{user: user} do
1159 follower = insert(:user)
1160 {:ok, follower} = User.follow(follower, user)
1161
1162 locked_user = insert(:user, name: "locked", locked: true)
1163 {:ok, _} = User.follow(user, locked_user, :follow_pending)
1164
1165 object = insert(:note, user: user)
1166 activity = insert(:note_activity, user: user, note: object)
1167
1168 object_two = insert(:note, user: follower)
1169 activity_two = insert(:note_activity, user: follower, note: object_two)
1170
1171 {:ok, like} = CommonAPI.favorite(user, activity_two.id)
1172 {:ok, like_two} = CommonAPI.favorite(follower, activity.id)
1173 {:ok, repeat} = CommonAPI.repeat(activity_two.id, user)
1174
1175 {:ok, job} = User.delete(user)
1176 {:ok, _user} = ObanHelpers.perform(job)
1177
1178 follower = User.get_cached_by_id(follower.id)
1179
1180 refute User.following?(follower, user)
1181 assert %{deactivated: true} = User.get_by_id(user.id)
1182
1183 assert [] == User.get_follow_requests(locked_user)
1184
1185 user_activities =
1186 user.ap_id
1187 |> Activity.Queries.by_actor()
1188 |> Repo.all()
1189 |> Enum.map(fn act -> act.data["type"] end)
1190
1191 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1192
1193 refute Activity.get_by_id(activity.id)
1194 refute Activity.get_by_id(like.id)
1195 refute Activity.get_by_id(like_two.id)
1196 refute Activity.get_by_id(repeat.id)
1197 end
1198 end
1199
1200 describe "delete/1 when confirmation is pending" do
1201 setup do
1202 user = insert(:user, confirmation_pending: true)
1203 {:ok, user: user}
1204 end
1205
1206 test "deletes user from database when activation required", %{user: user} do
1207 clear_config([:instance, :account_activation_required], true)
1208
1209 {:ok, job} = User.delete(user)
1210 {:ok, _} = ObanHelpers.perform(job)
1211
1212 refute User.get_cached_by_id(user.id)
1213 refute User.get_by_id(user.id)
1214 end
1215
1216 test "deactivates user when activation is not required", %{user: user} do
1217 clear_config([:instance, :account_activation_required], false)
1218
1219 {:ok, job} = User.delete(user)
1220 {:ok, _} = ObanHelpers.perform(job)
1221
1222 assert %{deactivated: true} = User.get_cached_by_id(user.id)
1223 assert %{deactivated: true} = User.get_by_id(user.id)
1224 end
1225 end
1226
1227 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1228 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1229 end
1230
1231 describe "per-user rich-text filtering" do
1232 test "html_filter_policy returns default policies, when rich-text is enabled" do
1233 user = insert(:user)
1234
1235 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1236 end
1237
1238 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1239 user = insert(:user, no_rich_text: true)
1240
1241 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1242 end
1243 end
1244
1245 describe "caching" do
1246 test "invalidate_cache works" do
1247 user = insert(:user)
1248
1249 User.set_cache(user)
1250 User.invalidate_cache(user)
1251
1252 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1253 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1254 end
1255
1256 test "User.delete() plugs any possible zombie objects" do
1257 user = insert(:user)
1258
1259 {:ok, job} = User.delete(user)
1260 {:ok, _} = ObanHelpers.perform(job)
1261
1262 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1263
1264 assert cached_user != user
1265
1266 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1267
1268 assert cached_user != user
1269 end
1270 end
1271
1272 describe "account_status/1" do
1273 setup do: clear_config([:instance, :account_activation_required])
1274
1275 test "return confirmation_pending for unconfirm user" do
1276 Pleroma.Config.put([:instance, :account_activation_required], true)
1277 user = insert(:user, confirmation_pending: true)
1278 assert User.account_status(user) == :confirmation_pending
1279 end
1280
1281 test "return active for confirmed user" do
1282 Pleroma.Config.put([:instance, :account_activation_required], true)
1283 user = insert(:user, confirmation_pending: false)
1284 assert User.account_status(user) == :active
1285 end
1286
1287 test "return active for remote user" do
1288 user = insert(:user, local: false)
1289 assert User.account_status(user) == :active
1290 end
1291
1292 test "returns :password_reset_pending for user with reset password" do
1293 user = insert(:user, password_reset_pending: true)
1294 assert User.account_status(user) == :password_reset_pending
1295 end
1296
1297 test "returns :deactivated for deactivated user" do
1298 user = insert(:user, local: true, confirmation_pending: false, deactivated: true)
1299 assert User.account_status(user) == :deactivated
1300 end
1301 end
1302
1303 describe "superuser?/1" do
1304 test "returns false for unprivileged users" do
1305 user = insert(:user, local: true)
1306
1307 refute User.superuser?(user)
1308 end
1309
1310 test "returns false for remote users" do
1311 user = insert(:user, local: false)
1312 remote_admin_user = insert(:user, local: false, is_admin: true)
1313
1314 refute User.superuser?(user)
1315 refute User.superuser?(remote_admin_user)
1316 end
1317
1318 test "returns true for local moderators" do
1319 user = insert(:user, local: true, is_moderator: true)
1320
1321 assert User.superuser?(user)
1322 end
1323
1324 test "returns true for local admins" do
1325 user = insert(:user, local: true, is_admin: true)
1326
1327 assert User.superuser?(user)
1328 end
1329 end
1330
1331 describe "invisible?/1" do
1332 test "returns true for an invisible user" do
1333 user = insert(:user, local: true, invisible: true)
1334
1335 assert User.invisible?(user)
1336 end
1337
1338 test "returns false for a non-invisible user" do
1339 user = insert(:user, local: true)
1340
1341 refute User.invisible?(user)
1342 end
1343 end
1344
1345 describe "visible_for?/2" do
1346 test "returns true when the account is itself" do
1347 user = insert(:user, local: true)
1348
1349 assert User.visible_for?(user, user)
1350 end
1351
1352 test "returns false when the account is unauthenticated and auth is required" do
1353 Pleroma.Config.put([:instance, :account_activation_required], true)
1354
1355 user = insert(:user, local: true, confirmation_pending: true)
1356 other_user = insert(:user, local: true)
1357
1358 refute User.visible_for?(user, other_user)
1359 end
1360
1361 test "returns true when the account is unauthenticated and auth is not required" do
1362 user = insert(:user, local: true, confirmation_pending: true)
1363 other_user = insert(:user, local: true)
1364
1365 assert User.visible_for?(user, other_user)
1366 end
1367
1368 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1369 Pleroma.Config.put([:instance, :account_activation_required], true)
1370
1371 user = insert(:user, local: true, confirmation_pending: true)
1372 other_user = insert(:user, local: true, is_admin: true)
1373
1374 assert User.visible_for?(user, other_user)
1375 end
1376 end
1377
1378 describe "parse_bio/2" do
1379 test "preserves hosts in user links text" do
1380 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1381 user = insert(:user)
1382 bio = "A.k.a. @nick@domain.com"
1383
1384 expected_text =
1385 ~s(A.k.a. <span class="h-card"><a class="u-url mention" data-user="#{remote_user.id}" href="#{
1386 remote_user.ap_id
1387 }" rel="ugc">@<span>nick@domain.com</span></a></span>)
1388
1389 assert expected_text == User.parse_bio(bio, user)
1390 end
1391
1392 test "Adds rel=me on linkbacked urls" do
1393 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1394
1395 bio = "http://example.com/rel_me/null"
1396 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1397 assert expected_text == User.parse_bio(bio, user)
1398
1399 bio = "http://example.com/rel_me/link"
1400 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1401 assert expected_text == User.parse_bio(bio, user)
1402
1403 bio = "http://example.com/rel_me/anchor"
1404 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1405 assert expected_text == User.parse_bio(bio, user)
1406 end
1407 end
1408
1409 test "follower count is updated when a follower is blocked" do
1410 user = insert(:user)
1411 follower = insert(:user)
1412 follower2 = insert(:user)
1413 follower3 = insert(:user)
1414
1415 {:ok, follower} = User.follow(follower, user)
1416 {:ok, _follower2} = User.follow(follower2, user)
1417 {:ok, _follower3} = User.follow(follower3, user)
1418
1419 {:ok, _user_relationship} = User.block(user, follower)
1420 user = refresh_record(user)
1421
1422 assert user.follower_count == 2
1423 end
1424
1425 describe "list_inactive_users_query/1" do
1426 defp days_ago(days) do
1427 NaiveDateTime.add(
1428 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1429 -days * 60 * 60 * 24,
1430 :second
1431 )
1432 end
1433
1434 test "Users are inactive by default" do
1435 total = 10
1436
1437 users =
1438 Enum.map(1..total, fn _ ->
1439 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1440 end)
1441
1442 inactive_users_ids =
1443 Pleroma.User.list_inactive_users_query()
1444 |> Pleroma.Repo.all()
1445 |> Enum.map(& &1.id)
1446
1447 Enum.each(users, fn user ->
1448 assert user.id in inactive_users_ids
1449 end)
1450 end
1451
1452 test "Only includes users who has no recent activity" do
1453 total = 10
1454
1455 users =
1456 Enum.map(1..total, fn _ ->
1457 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1458 end)
1459
1460 {inactive, active} = Enum.split(users, trunc(total / 2))
1461
1462 Enum.map(active, fn user ->
1463 to = Enum.random(users -- [user])
1464
1465 {:ok, _} =
1466 CommonAPI.post(user, %{
1467 status: "hey @#{to.nickname}"
1468 })
1469 end)
1470
1471 inactive_users_ids =
1472 Pleroma.User.list_inactive_users_query()
1473 |> Pleroma.Repo.all()
1474 |> Enum.map(& &1.id)
1475
1476 Enum.each(active, fn user ->
1477 refute user.id in inactive_users_ids
1478 end)
1479
1480 Enum.each(inactive, fn user ->
1481 assert user.id in inactive_users_ids
1482 end)
1483 end
1484
1485 test "Only includes users with no read notifications" do
1486 total = 10
1487
1488 users =
1489 Enum.map(1..total, fn _ ->
1490 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1491 end)
1492
1493 [sender | recipients] = users
1494 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1495
1496 Enum.each(recipients, fn to ->
1497 {:ok, _} =
1498 CommonAPI.post(sender, %{
1499 status: "hey @#{to.nickname}"
1500 })
1501
1502 {:ok, _} =
1503 CommonAPI.post(sender, %{
1504 status: "hey again @#{to.nickname}"
1505 })
1506 end)
1507
1508 Enum.each(active, fn user ->
1509 [n1, _n2] = Pleroma.Notification.for_user(user)
1510 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1511 end)
1512
1513 inactive_users_ids =
1514 Pleroma.User.list_inactive_users_query()
1515 |> Pleroma.Repo.all()
1516 |> Enum.map(& &1.id)
1517
1518 Enum.each(active, fn user ->
1519 refute user.id in inactive_users_ids
1520 end)
1521
1522 Enum.each(inactive, fn user ->
1523 assert user.id in inactive_users_ids
1524 end)
1525 end
1526 end
1527
1528 describe "toggle_confirmation/1" do
1529 test "if user is confirmed" do
1530 user = insert(:user, confirmation_pending: false)
1531 {:ok, user} = User.toggle_confirmation(user)
1532
1533 assert user.confirmation_pending
1534 assert user.confirmation_token
1535 end
1536
1537 test "if user is unconfirmed" do
1538 user = insert(:user, confirmation_pending: true, confirmation_token: "some token")
1539 {:ok, user} = User.toggle_confirmation(user)
1540
1541 refute user.confirmation_pending
1542 refute user.confirmation_token
1543 end
1544 end
1545
1546 describe "ensure_keys_present" do
1547 test "it creates keys for a user and stores them in info" do
1548 user = insert(:user)
1549 refute is_binary(user.keys)
1550 {:ok, user} = User.ensure_keys_present(user)
1551 assert is_binary(user.keys)
1552 end
1553
1554 test "it doesn't create keys if there already are some" do
1555 user = insert(:user, keys: "xxx")
1556 {:ok, user} = User.ensure_keys_present(user)
1557 assert user.keys == "xxx"
1558 end
1559 end
1560
1561 describe "get_ap_ids_by_nicknames" do
1562 test "it returns a list of AP ids for a given set of nicknames" do
1563 user = insert(:user)
1564 user_two = insert(:user)
1565
1566 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1567 assert length(ap_ids) == 2
1568 assert user.ap_id in ap_ids
1569 assert user_two.ap_id in ap_ids
1570 end
1571 end
1572
1573 describe "sync followers count" do
1574 setup do
1575 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1576 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1577 insert(:user, local: true)
1578 insert(:user, local: false, deactivated: true)
1579 {:ok, user1: user1, user2: user2}
1580 end
1581
1582 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1583 [fdb_user1] = User.external_users(limit: 1)
1584
1585 assert fdb_user1.ap_id
1586 assert fdb_user1.ap_id == user1.ap_id
1587 assert fdb_user1.id == user1.id
1588
1589 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1590
1591 assert fdb_user2.ap_id
1592 assert fdb_user2.ap_id == user2.ap_id
1593 assert fdb_user2.id == user2.id
1594
1595 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1596 end
1597 end
1598
1599 describe "is_internal_user?/1" do
1600 test "non-internal user returns false" do
1601 user = insert(:user)
1602 refute User.is_internal_user?(user)
1603 end
1604
1605 test "user with no nickname returns true" do
1606 user = insert(:user, %{nickname: nil})
1607 assert User.is_internal_user?(user)
1608 end
1609
1610 test "user with internal-prefixed nickname returns true" do
1611 user = insert(:user, %{nickname: "internal.test"})
1612 assert User.is_internal_user?(user)
1613 end
1614 end
1615
1616 describe "update_and_set_cache/1" do
1617 test "returns error when user is stale instead Ecto.StaleEntryError" do
1618 user = insert(:user)
1619
1620 changeset = Ecto.Changeset.change(user, bio: "test")
1621
1622 Repo.delete(user)
1623
1624 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1625 User.update_and_set_cache(changeset)
1626 end
1627
1628 test "performs update cache if user updated" do
1629 user = insert(:user)
1630 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1631
1632 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1633
1634 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1635 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1636 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1637 end
1638 end
1639
1640 describe "following/followers synchronization" do
1641 setup do: clear_config([:instance, :external_user_synchronization])
1642
1643 test "updates the counters normally on following/getting a follow when disabled" do
1644 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1645 user = insert(:user)
1646
1647 other_user =
1648 insert(:user,
1649 local: false,
1650 follower_address: "http://localhost:4001/users/masto_closed/followers",
1651 following_address: "http://localhost:4001/users/masto_closed/following",
1652 ap_enabled: true
1653 )
1654
1655 assert other_user.following_count == 0
1656 assert other_user.follower_count == 0
1657
1658 {:ok, user} = Pleroma.User.follow(user, other_user)
1659 other_user = Pleroma.User.get_by_id(other_user.id)
1660
1661 assert user.following_count == 1
1662 assert other_user.follower_count == 1
1663 end
1664
1665 test "syncronizes the counters with the remote instance for the followed when enabled" do
1666 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1667
1668 user = insert(:user)
1669
1670 other_user =
1671 insert(:user,
1672 local: false,
1673 follower_address: "http://localhost:4001/users/masto_closed/followers",
1674 following_address: "http://localhost:4001/users/masto_closed/following",
1675 ap_enabled: true
1676 )
1677
1678 assert other_user.following_count == 0
1679 assert other_user.follower_count == 0
1680
1681 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1682 {:ok, _user} = User.follow(user, other_user)
1683 other_user = User.get_by_id(other_user.id)
1684
1685 assert other_user.follower_count == 437
1686 end
1687
1688 test "syncronizes the counters with the remote instance for the follower when enabled" do
1689 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1690
1691 user = insert(:user)
1692
1693 other_user =
1694 insert(:user,
1695 local: false,
1696 follower_address: "http://localhost:4001/users/masto_closed/followers",
1697 following_address: "http://localhost:4001/users/masto_closed/following",
1698 ap_enabled: true
1699 )
1700
1701 assert other_user.following_count == 0
1702 assert other_user.follower_count == 0
1703
1704 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1705 {:ok, other_user} = User.follow(other_user, user)
1706
1707 assert other_user.following_count == 152
1708 end
1709 end
1710
1711 describe "change_email/2" do
1712 setup do
1713 [user: insert(:user)]
1714 end
1715
1716 test "blank email returns error", %{user: user} do
1717 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1718 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1719 end
1720
1721 test "non unique email returns error", %{user: user} do
1722 %{email: email} = insert(:user)
1723
1724 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1725 User.change_email(user, email)
1726 end
1727
1728 test "invalid email returns error", %{user: user} do
1729 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1730 User.change_email(user, "cofe")
1731 end
1732
1733 test "changes email", %{user: user} do
1734 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1735 end
1736 end
1737
1738 describe "get_cached_by_nickname_or_id" do
1739 setup do
1740 local_user = insert(:user)
1741 remote_user = insert(:user, nickname: "nickname@example.com", local: false)
1742
1743 [local_user: local_user, remote_user: remote_user]
1744 end
1745
1746 setup do: clear_config([:instance, :limit_to_local_content])
1747
1748 test "allows getting remote users by id no matter what :limit_to_local_content is set to", %{
1749 remote_user: remote_user
1750 } do
1751 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1752 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1753
1754 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1755 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1756
1757 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1758 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1759 end
1760
1761 test "disallows getting remote users by nickname without authentication when :limit_to_local_content is set to :unauthenticated",
1762 %{remote_user: remote_user} do
1763 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1764 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1765 end
1766
1767 test "allows getting remote users by nickname with authentication when :limit_to_local_content is set to :unauthenticated",
1768 %{remote_user: remote_user, local_user: local_user} do
1769 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1770 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.nickname, for: local_user)
1771 end
1772
1773 test "disallows getting remote users by nickname when :limit_to_local_content is set to true",
1774 %{remote_user: remote_user} do
1775 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1776 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1777 end
1778
1779 test "allows getting local users by nickname no matter what :limit_to_local_content is set to",
1780 %{local_user: local_user} do
1781 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1782 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1783
1784 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1785 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1786
1787 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1788 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1789 end
1790 end
1791
1792 describe "update_email_notifications/2" do
1793 setup do
1794 user = insert(:user, email_notifications: %{"digest" => true})
1795
1796 {:ok, user: user}
1797 end
1798
1799 test "Notifications are updated", %{user: user} do
1800 true = user.email_notifications["digest"]
1801 assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false})
1802 assert result.email_notifications["digest"] == false
1803 end
1804 end
1805
1806 test "avatar fallback" do
1807 user = insert(:user)
1808 assert User.avatar_url(user) =~ "/images/avi.png"
1809
1810 clear_config([:assets, :default_user_avatar], "avatar.png")
1811
1812 user = User.get_cached_by_nickname_or_id(user.nickname)
1813 assert User.avatar_url(user) =~ "avatar.png"
1814
1815 assert User.avatar_url(user, no_default: true) == nil
1816 end
1817 end