[#2332] Misc. improvements per code change requests.
[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 Mock
19 import Pleroma.Factory
20 import ExUnit.CaptureLog
21
22 setup_all do
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 :ok
25 end
26
27 setup do: clear_config([:instance, :account_activation_required])
28
29 describe "service actors" do
30 test "returns updated invisible actor" do
31 uri = "#{Pleroma.Web.Endpoint.url()}/relay"
32 followers_uri = "#{uri}/followers"
33
34 insert(
35 :user,
36 %{
37 nickname: "relay",
38 invisible: false,
39 local: true,
40 ap_id: uri,
41 follower_address: followers_uri
42 }
43 )
44
45 actor = User.get_or_create_service_actor_by_ap_id(uri, "relay")
46 assert actor.invisible
47 end
48
49 test "returns relay user" do
50 uri = "#{Pleroma.Web.Endpoint.url()}/relay"
51 followers_uri = "#{uri}/followers"
52
53 assert %User{
54 nickname: "relay",
55 invisible: true,
56 local: true,
57 ap_id: ^uri,
58 follower_address: ^followers_uri
59 } = User.get_or_create_service_actor_by_ap_id(uri, "relay")
60
61 assert capture_log(fn ->
62 refute User.get_or_create_service_actor_by_ap_id("/relay", "relay")
63 end) =~ "Cannot create service actor:"
64 end
65
66 test "returns invisible actor" do
67 uri = "#{Pleroma.Web.Endpoint.url()}/internal/fetch-test"
68 followers_uri = "#{uri}/followers"
69 user = User.get_or_create_service_actor_by_ap_id(uri, "internal.fetch-test")
70
71 assert %User{
72 nickname: "internal.fetch-test",
73 invisible: true,
74 local: true,
75 ap_id: ^uri,
76 follower_address: ^followers_uri
77 } = user
78
79 user2 = User.get_or_create_service_actor_by_ap_id(uri, "internal.fetch-test")
80 assert user.id == user2.id
81 end
82 end
83
84 describe "AP ID user relationships" do
85 setup do
86 {:ok, user: insert(:user)}
87 end
88
89 test "outgoing_relationships_ap_ids/1", %{user: user} do
90 rel_types = [:block, :mute, :notification_mute, :reblog_mute, :inverse_subscription]
91
92 ap_ids_by_rel =
93 Enum.into(
94 rel_types,
95 %{},
96 fn rel_type ->
97 rel_records =
98 insert_list(2, :user_relationship, %{source: user, relationship_type: rel_type})
99
100 ap_ids = Enum.map(rel_records, fn rr -> Repo.preload(rr, :target).target.ap_id end)
101 {rel_type, Enum.sort(ap_ids)}
102 end
103 )
104
105 assert ap_ids_by_rel[:block] == Enum.sort(User.blocked_users_ap_ids(user))
106 assert ap_ids_by_rel[:block] == Enum.sort(Enum.map(User.blocked_users(user), & &1.ap_id))
107
108 assert ap_ids_by_rel[:mute] == Enum.sort(User.muted_users_ap_ids(user))
109 assert ap_ids_by_rel[:mute] == Enum.sort(Enum.map(User.muted_users(user), & &1.ap_id))
110
111 assert ap_ids_by_rel[:notification_mute] ==
112 Enum.sort(User.notification_muted_users_ap_ids(user))
113
114 assert ap_ids_by_rel[:notification_mute] ==
115 Enum.sort(Enum.map(User.notification_muted_users(user), & &1.ap_id))
116
117 assert ap_ids_by_rel[:reblog_mute] == Enum.sort(User.reblog_muted_users_ap_ids(user))
118
119 assert ap_ids_by_rel[:reblog_mute] ==
120 Enum.sort(Enum.map(User.reblog_muted_users(user), & &1.ap_id))
121
122 assert ap_ids_by_rel[:inverse_subscription] == Enum.sort(User.subscriber_users_ap_ids(user))
123
124 assert ap_ids_by_rel[:inverse_subscription] ==
125 Enum.sort(Enum.map(User.subscriber_users(user), & &1.ap_id))
126
127 outgoing_relationships_ap_ids = User.outgoing_relationships_ap_ids(user, rel_types)
128
129 assert ap_ids_by_rel ==
130 Enum.into(outgoing_relationships_ap_ids, %{}, fn {k, v} -> {k, Enum.sort(v)} end)
131 end
132 end
133
134 describe "when tags are nil" do
135 test "tagging a user" do
136 user = insert(:user, %{tags: nil})
137 user = User.tag(user, ["cool", "dude"])
138
139 assert "cool" in user.tags
140 assert "dude" in user.tags
141 end
142
143 test "untagging a user" do
144 user = insert(:user, %{tags: nil})
145 user = User.untag(user, ["cool", "dude"])
146
147 assert user.tags == []
148 end
149 end
150
151 test "ap_id returns the activity pub id for the user" do
152 user = UserBuilder.build()
153
154 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
155
156 assert expected_ap_id == User.ap_id(user)
157 end
158
159 test "ap_followers returns the followers collection for the user" do
160 user = UserBuilder.build()
161
162 expected_followers_collection = "#{User.ap_id(user)}/followers"
163
164 assert expected_followers_collection == User.ap_followers(user)
165 end
166
167 test "ap_following returns the following collection for the user" do
168 user = UserBuilder.build()
169
170 expected_followers_collection = "#{User.ap_id(user)}/following"
171
172 assert expected_followers_collection == User.ap_following(user)
173 end
174
175 test "returns all pending follow requests" do
176 unlocked = insert(:user)
177 locked = insert(:user, locked: true)
178 follower = insert(:user)
179
180 CommonAPI.follow(follower, unlocked)
181 CommonAPI.follow(follower, locked)
182
183 assert [] = User.get_follow_requests(unlocked)
184 assert [activity] = User.get_follow_requests(locked)
185
186 assert activity
187 end
188
189 test "doesn't return already accepted or duplicate follow requests" do
190 locked = insert(:user, locked: true)
191 pending_follower = insert(:user)
192 accepted_follower = insert(:user)
193
194 CommonAPI.follow(pending_follower, locked)
195 CommonAPI.follow(pending_follower, locked)
196 CommonAPI.follow(accepted_follower, locked)
197
198 Pleroma.FollowingRelationship.update(accepted_follower, locked, :follow_accept)
199
200 assert [^pending_follower] = User.get_follow_requests(locked)
201 end
202
203 test "clears follow requests when requester is blocked" do
204 followed = insert(:user, locked: true)
205 follower = insert(:user)
206
207 CommonAPI.follow(follower, followed)
208 assert [_activity] = User.get_follow_requests(followed)
209
210 {:ok, _user_relationship} = User.block(followed, follower)
211 assert [] = User.get_follow_requests(followed)
212 end
213
214 test "follow_all follows mutliple users" do
215 user = insert(:user)
216 followed_zero = insert(:user)
217 followed_one = insert(:user)
218 followed_two = insert(:user)
219 blocked = insert(:user)
220 not_followed = insert(:user)
221 reverse_blocked = insert(:user)
222
223 {:ok, _user_relationship} = User.block(user, blocked)
224 {:ok, _user_relationship} = User.block(reverse_blocked, user)
225
226 {:ok, user} = User.follow(user, followed_zero)
227
228 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
229
230 assert User.following?(user, followed_one)
231 assert User.following?(user, followed_two)
232 assert User.following?(user, followed_zero)
233 refute User.following?(user, not_followed)
234 refute User.following?(user, blocked)
235 refute User.following?(user, reverse_blocked)
236 end
237
238 test "follow_all follows mutliple users without duplicating" do
239 user = insert(:user)
240 followed_zero = insert(:user)
241 followed_one = insert(:user)
242 followed_two = insert(:user)
243
244 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
245 assert length(User.following(user)) == 3
246
247 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
248 assert length(User.following(user)) == 4
249 end
250
251 test "follow takes a user and another user" do
252 user = insert(:user)
253 followed = insert(:user)
254
255 {:ok, user} = User.follow(user, followed)
256
257 user = User.get_cached_by_id(user.id)
258 followed = User.get_cached_by_ap_id(followed.ap_id)
259
260 assert followed.follower_count == 1
261 assert user.following_count == 1
262
263 assert User.ap_followers(followed) in User.following(user)
264 end
265
266 test "can't follow a deactivated users" do
267 user = insert(:user)
268 followed = insert(:user, %{deactivated: true})
269
270 {:error, _} = User.follow(user, followed)
271 end
272
273 test "can't follow a user who blocked us" do
274 blocker = insert(:user)
275 blockee = insert(:user)
276
277 {:ok, _user_relationship} = User.block(blocker, blockee)
278
279 {:error, _} = User.follow(blockee, blocker)
280 end
281
282 test "can't subscribe to a user who blocked us" do
283 blocker = insert(:user)
284 blocked = insert(:user)
285
286 {:ok, _user_relationship} = User.block(blocker, blocked)
287
288 {:error, _} = User.subscribe(blocked, blocker)
289 end
290
291 test "local users do not automatically follow local locked accounts" do
292 follower = insert(:user, locked: true)
293 followed = insert(:user, locked: true)
294
295 {:ok, follower} = User.maybe_direct_follow(follower, followed)
296
297 refute User.following?(follower, followed)
298 end
299
300 describe "unfollow/2" do
301 setup do: clear_config([:instance, :external_user_synchronization])
302
303 test "unfollow with syncronizes external user" do
304 Pleroma.Config.put([:instance, :external_user_synchronization], true)
305
306 followed =
307 insert(:user,
308 nickname: "fuser1",
309 follower_address: "http://localhost:4001/users/fuser1/followers",
310 following_address: "http://localhost:4001/users/fuser1/following",
311 ap_id: "http://localhost:4001/users/fuser1"
312 )
313
314 user =
315 insert(:user, %{
316 local: false,
317 nickname: "fuser2",
318 ap_id: "http://localhost:4001/users/fuser2",
319 follower_address: "http://localhost:4001/users/fuser2/followers",
320 following_address: "http://localhost:4001/users/fuser2/following"
321 })
322
323 {:ok, user} = User.follow(user, followed, :follow_accept)
324
325 {:ok, user, _activity} = User.unfollow(user, followed)
326
327 user = User.get_cached_by_id(user.id)
328
329 assert User.following(user) == []
330 end
331
332 test "unfollow takes a user and another user" do
333 followed = insert(:user)
334 user = insert(:user)
335
336 {:ok, user} = User.follow(user, followed, :follow_accept)
337
338 assert User.following(user) == [user.follower_address, followed.follower_address]
339
340 {:ok, user, _activity} = User.unfollow(user, followed)
341
342 assert User.following(user) == [user.follower_address]
343 end
344
345 test "unfollow doesn't unfollow yourself" do
346 user = insert(:user)
347
348 {:error, _} = User.unfollow(user, user)
349
350 assert User.following(user) == [user.follower_address]
351 end
352 end
353
354 test "test if a user is following another user" do
355 followed = insert(:user)
356 user = insert(:user)
357 User.follow(user, followed, :follow_accept)
358
359 assert User.following?(user, followed)
360 refute User.following?(followed, user)
361 end
362
363 test "fetches correct profile for nickname beginning with number" do
364 # Use old-style integer ID to try to reproduce the problem
365 user = insert(:user, %{id: 1080})
366 user_with_numbers = insert(:user, %{nickname: "#{user.id}garbage"})
367 assert user_with_numbers == User.get_cached_by_nickname_or_id(user_with_numbers.nickname)
368 end
369
370 describe "user registration" do
371 @full_user_data %{
372 bio: "A guy",
373 name: "my name",
374 nickname: "nick",
375 password: "test",
376 password_confirmation: "test",
377 email: "email@example.com"
378 }
379 setup do: clear_config([:instance, :autofollowed_nicknames])
380 setup do: clear_config([:instance, :welcome_message])
381 setup do: clear_config([:instance, :welcome_user_nickname])
382
383 test "it autofollows accounts that are set for it" do
384 user = insert(:user)
385 remote_user = insert(:user, %{local: false})
386
387 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
388 user.nickname,
389 remote_user.nickname
390 ])
391
392 cng = User.register_changeset(%User{}, @full_user_data)
393
394 {:ok, registered_user} = User.register(cng)
395
396 assert User.following?(registered_user, user)
397 refute User.following?(registered_user, remote_user)
398 end
399
400 test "it sends a welcome message if it is set" do
401 welcome_user = insert(:user)
402
403 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
404 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
405
406 cng = User.register_changeset(%User{}, @full_user_data)
407 {:ok, registered_user} = User.register(cng)
408
409 activity = Repo.one(Pleroma.Activity)
410 assert registered_user.ap_id in activity.recipients
411 assert Object.normalize(activity).data["content"] =~ "cool site"
412 assert activity.actor == welcome_user.ap_id
413 end
414
415 setup do: clear_config([:instance, :account_activation_required])
416
417 test "it requires an email, name, nickname and password, bio is optional when account_activation_required is enabled" do
418 Pleroma.Config.put([:instance, :account_activation_required], true)
419
420 @full_user_data
421 |> Map.keys()
422 |> Enum.each(fn key ->
423 params = Map.delete(@full_user_data, key)
424 changeset = User.register_changeset(%User{}, params)
425
426 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
427 end)
428 end
429
430 test "it requires an name, nickname and password, bio and email are optional when account_activation_required is disabled" do
431 Pleroma.Config.put([:instance, :account_activation_required], false)
432
433 @full_user_data
434 |> Map.keys()
435 |> Enum.each(fn key ->
436 params = Map.delete(@full_user_data, key)
437 changeset = User.register_changeset(%User{}, params)
438
439 assert if key in [:bio, :email], do: changeset.valid?, else: not changeset.valid?
440 end)
441 end
442
443 test "it restricts certain nicknames" do
444 [restricted_name | _] = Pleroma.Config.get([User, :restricted_nicknames])
445
446 assert is_bitstring(restricted_name)
447
448 params =
449 @full_user_data
450 |> Map.put(:nickname, restricted_name)
451
452 changeset = User.register_changeset(%User{}, params)
453
454 refute changeset.valid?
455 end
456
457 test "it sets the password_hash and ap_id" do
458 changeset = User.register_changeset(%User{}, @full_user_data)
459
460 assert changeset.valid?
461
462 assert is_binary(changeset.changes[:password_hash])
463 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
464
465 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
466 end
467 end
468
469 describe "user registration, with :account_activation_required" do
470 @full_user_data %{
471 bio: "A guy",
472 name: "my name",
473 nickname: "nick",
474 password: "test",
475 password_confirmation: "test",
476 email: "email@example.com"
477 }
478 setup do: clear_config([:instance, :account_activation_required], true)
479
480 test "it creates unconfirmed user" do
481 changeset = User.register_changeset(%User{}, @full_user_data)
482 assert changeset.valid?
483
484 {:ok, user} = Repo.insert(changeset)
485
486 assert user.confirmation_pending
487 assert user.confirmation_token
488 end
489
490 test "it creates confirmed user if :confirmed option is given" do
491 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
492 assert changeset.valid?
493
494 {:ok, user} = Repo.insert(changeset)
495
496 refute user.confirmation_pending
497 refute user.confirmation_token
498 end
499 end
500
501 describe "get_or_fetch/1" do
502 test "gets an existing user by nickname" do
503 user = insert(:user)
504 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
505
506 assert user == fetched_user
507 end
508
509 test "gets an existing user by ap_id" do
510 ap_id = "http://mastodon.example.org/users/admin"
511
512 user =
513 insert(
514 :user,
515 local: false,
516 nickname: "admin@mastodon.example.org",
517 ap_id: ap_id
518 )
519
520 {:ok, fetched_user} = User.get_or_fetch(ap_id)
521 freshed_user = refresh_record(user)
522 assert freshed_user == fetched_user
523 end
524 end
525
526 describe "fetching a user from nickname or trying to build one" do
527 test "gets an existing user" do
528 user = insert(:user)
529 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
530
531 assert user == fetched_user
532 end
533
534 test "gets an existing user, case insensitive" do
535 user = insert(:user, nickname: "nick")
536 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
537
538 assert user == fetched_user
539 end
540
541 test "gets an existing user by fully qualified nickname" do
542 user = insert(:user)
543
544 {:ok, fetched_user} =
545 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
546
547 assert user == fetched_user
548 end
549
550 test "gets an existing user by fully qualified nickname, case insensitive" do
551 user = insert(:user, nickname: "nick")
552 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
553
554 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
555
556 assert user == fetched_user
557 end
558
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.source_data["endpoints"]
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 creation 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_creation(@valid_remote)
626 assert cs.valid?
627 end
628
629 test "it sets the follower_adress" do
630 cs = User.remote_user_creation(@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_creation(%{@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 [:name, :ap_id]
645 |> Enum.each(fn field ->
646 cs = User.remote_user_creation(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 result = ObanHelpers.perform(job)
760
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 result = ObanHelpers.perform(job)
983
984 assert is_list(result)
985 assert result == [user2, user3]
986 end
987 end
988
989 describe "get_recipients_from_activity" do
990 test "get recipients" do
991 actor = insert(:user)
992 user = insert(:user, local: true)
993 user_two = insert(:user, local: false)
994 addressed = insert(:user, local: true)
995 addressed_remote = insert(:user, local: false)
996
997 {:ok, activity} =
998 CommonAPI.post(actor, %{
999 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
1000 })
1001
1002 assert Enum.map([actor, addressed], & &1.ap_id) --
1003 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1004
1005 {:ok, user} = User.follow(user, actor)
1006 {:ok, _user_two} = User.follow(user_two, actor)
1007 recipients = User.get_recipients_from_activity(activity)
1008 assert length(recipients) == 3
1009 assert user in recipients
1010 assert addressed in recipients
1011 end
1012
1013 test "has following" do
1014 actor = insert(:user)
1015 user = insert(:user)
1016 user_two = insert(:user)
1017 addressed = insert(:user, local: true)
1018
1019 {:ok, activity} =
1020 CommonAPI.post(actor, %{
1021 "status" => "hey @#{addressed.nickname}"
1022 })
1023
1024 assert Enum.map([actor, addressed], & &1.ap_id) --
1025 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1026
1027 {:ok, _actor} = User.follow(actor, user)
1028 {:ok, _actor} = User.follow(actor, user_two)
1029 recipients = User.get_recipients_from_activity(activity)
1030 assert length(recipients) == 2
1031 assert addressed in recipients
1032 end
1033 end
1034
1035 describe ".deactivate" do
1036 test "can de-activate then re-activate a user" do
1037 user = insert(:user)
1038 assert false == user.deactivated
1039 {:ok, user} = User.deactivate(user)
1040 assert true == user.deactivated
1041 {:ok, user} = User.deactivate(user, false)
1042 assert false == user.deactivated
1043 end
1044
1045 test "hide a user from followers" do
1046 user = insert(:user)
1047 user2 = insert(:user)
1048
1049 {:ok, user} = User.follow(user, user2)
1050 {:ok, _user} = User.deactivate(user)
1051
1052 user2 = User.get_cached_by_id(user2.id)
1053
1054 assert user2.follower_count == 0
1055 assert [] = User.get_followers(user2)
1056 end
1057
1058 test "hide a user from friends" do
1059 user = insert(:user)
1060 user2 = insert(:user)
1061
1062 {:ok, user2} = User.follow(user2, user)
1063 assert user2.following_count == 1
1064 assert User.following_count(user2) == 1
1065
1066 {:ok, _user} = User.deactivate(user)
1067
1068 user2 = User.get_cached_by_id(user2.id)
1069
1070 assert refresh_record(user2).following_count == 0
1071 assert user2.following_count == 0
1072 assert User.following_count(user2) == 0
1073 assert [] = User.get_friends(user2)
1074 end
1075
1076 test "hide a user's statuses from timelines and notifications" do
1077 user = insert(:user)
1078 user2 = insert(:user)
1079
1080 {:ok, user2} = User.follow(user2, user)
1081
1082 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
1083
1084 activity = Repo.preload(activity, :bookmark)
1085
1086 [notification] = Pleroma.Notification.for_user(user2)
1087 assert notification.activity.id == activity.id
1088
1089 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
1090
1091 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
1092 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1093 "user" => user2
1094 })
1095
1096 {:ok, _user} = User.deactivate(user)
1097
1098 assert [] == ActivityPub.fetch_public_activities(%{})
1099 assert [] == Pleroma.Notification.for_user(user2)
1100
1101 assert [] ==
1102 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1103 "user" => user2
1104 })
1105 end
1106 end
1107
1108 describe "delete" do
1109 setup do
1110 {:ok, user} = insert(:user) |> User.set_cache()
1111
1112 [user: user]
1113 end
1114
1115 setup do: clear_config([:instance, :federating])
1116
1117 test ".delete_user_activities deletes all create activities", %{user: user} do
1118 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
1119
1120 User.delete_user_activities(user)
1121
1122 # TODO: Remove favorites, repeats, delete activities.
1123 refute Activity.get_by_id(activity.id)
1124 end
1125
1126 test "it deletes deactivated user" do
1127 {:ok, user} = insert(:user, deactivated: true) |> User.set_cache()
1128
1129 {:ok, job} = User.delete(user)
1130 {:ok, _user} = ObanHelpers.perform(job)
1131
1132 refute User.get_by_id(user.id)
1133 end
1134
1135 test "it deletes a user, all follow relationships and all activities", %{user: user} do
1136 follower = insert(:user)
1137 {:ok, follower} = User.follow(follower, user)
1138
1139 object = insert(:note, user: user)
1140 activity = insert(:note_activity, user: user, note: object)
1141
1142 object_two = insert(:note, user: follower)
1143 activity_two = insert(:note_activity, user: follower, note: object_two)
1144
1145 {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
1146 {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
1147 {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
1148
1149 {:ok, job} = User.delete(user)
1150 {:ok, _user} = ObanHelpers.perform(job)
1151
1152 follower = User.get_cached_by_id(follower.id)
1153
1154 refute User.following?(follower, user)
1155 refute User.get_by_id(user.id)
1156 assert {:ok, nil} == Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1157
1158 user_activities =
1159 user.ap_id
1160 |> Activity.Queries.by_actor()
1161 |> Repo.all()
1162 |> Enum.map(fn act -> act.data["type"] end)
1163
1164 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1165
1166 refute Activity.get_by_id(activity.id)
1167 refute Activity.get_by_id(like.id)
1168 refute Activity.get_by_id(like_two.id)
1169 refute Activity.get_by_id(repeat.id)
1170 end
1171
1172 test_with_mock "it sends out User Delete activity",
1173 %{user: user},
1174 Pleroma.Web.ActivityPub.Publisher,
1175 [:passthrough],
1176 [] do
1177 Pleroma.Config.put([:instance, :federating], true)
1178
1179 {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
1180 {:ok, _} = User.follow(follower, user)
1181
1182 {:ok, job} = User.delete(user)
1183 {:ok, _user} = ObanHelpers.perform(job)
1184
1185 assert ObanHelpers.member?(
1186 %{
1187 "op" => "publish_one",
1188 "params" => %{
1189 "inbox" => "http://mastodon.example.org/inbox",
1190 "id" => "pleroma:fakeid"
1191 }
1192 },
1193 all_enqueued(worker: Pleroma.Workers.PublisherWorker)
1194 )
1195 end
1196 end
1197
1198 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1199 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1200 end
1201
1202 describe "insert or update a user from given data" do
1203 test "with normal data" do
1204 user = insert(:user, %{nickname: "nick@name.de"})
1205 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
1206
1207 assert {:ok, %User{}} = User.insert_or_update_user(data)
1208 end
1209
1210 test "with overly long fields" do
1211 current_max_length = Pleroma.Config.get([:instance, :account_field_value_length], 255)
1212 user = insert(:user, nickname: "nickname@supergood.domain")
1213
1214 data = %{
1215 ap_id: user.ap_id,
1216 name: user.name,
1217 nickname: user.nickname,
1218 fields: [
1219 %{"name" => "myfield", "value" => String.duplicate("h", current_max_length + 1)}
1220 ]
1221 }
1222
1223 assert {:ok, %User{}} = User.insert_or_update_user(data)
1224 end
1225
1226 test "with an overly long bio" do
1227 current_max_length = Pleroma.Config.get([:instance, :user_bio_length], 5000)
1228 user = insert(:user, nickname: "nickname@supergood.domain")
1229
1230 data = %{
1231 ap_id: user.ap_id,
1232 name: user.name,
1233 nickname: user.nickname,
1234 bio: String.duplicate("h", current_max_length + 1)
1235 }
1236
1237 assert {:ok, %User{}} = User.insert_or_update_user(data)
1238 end
1239
1240 test "with an overly long display name" do
1241 current_max_length = Pleroma.Config.get([:instance, :user_name_length], 100)
1242 user = insert(:user, nickname: "nickname@supergood.domain")
1243
1244 data = %{
1245 ap_id: user.ap_id,
1246 name: String.duplicate("h", current_max_length + 1),
1247 nickname: user.nickname
1248 }
1249
1250 assert {:ok, %User{}} = User.insert_or_update_user(data)
1251 end
1252 end
1253
1254 describe "per-user rich-text filtering" do
1255 test "html_filter_policy returns default policies, when rich-text is enabled" do
1256 user = insert(:user)
1257
1258 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1259 end
1260
1261 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1262 user = insert(:user, no_rich_text: true)
1263
1264 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1265 end
1266 end
1267
1268 describe "caching" do
1269 test "invalidate_cache works" do
1270 user = insert(:user)
1271
1272 User.set_cache(user)
1273 User.invalidate_cache(user)
1274
1275 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1276 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1277 end
1278
1279 test "User.delete() plugs any possible zombie objects" do
1280 user = insert(:user)
1281
1282 {:ok, job} = User.delete(user)
1283 {:ok, _} = ObanHelpers.perform(job)
1284
1285 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1286
1287 assert cached_user != user
1288
1289 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1290
1291 assert cached_user != user
1292 end
1293 end
1294
1295 describe "account_status/1" do
1296 setup do: clear_config([:instance, :account_activation_required])
1297
1298 test "return confirmation_pending for unconfirm user" do
1299 Pleroma.Config.put([:instance, :account_activation_required], true)
1300 user = insert(:user, confirmation_pending: true)
1301 assert User.account_status(user) == :confirmation_pending
1302 end
1303
1304 test "return active for confirmed user" do
1305 Pleroma.Config.put([:instance, :account_activation_required], true)
1306 user = insert(:user, confirmation_pending: false)
1307 assert User.account_status(user) == :active
1308 end
1309
1310 test "return active for remote user" do
1311 user = insert(:user, local: false)
1312 assert User.account_status(user) == :active
1313 end
1314
1315 test "returns :password_reset_pending for user with reset password" do
1316 user = insert(:user, password_reset_pending: true)
1317 assert User.account_status(user) == :password_reset_pending
1318 end
1319
1320 test "returns :deactivated for deactivated user" do
1321 user = insert(:user, local: true, confirmation_pending: false, deactivated: true)
1322 assert User.account_status(user) == :deactivated
1323 end
1324 end
1325
1326 describe "superuser?/1" do
1327 test "returns false for unprivileged users" do
1328 user = insert(:user, local: true)
1329
1330 refute User.superuser?(user)
1331 end
1332
1333 test "returns false for remote users" do
1334 user = insert(:user, local: false)
1335 remote_admin_user = insert(:user, local: false, is_admin: true)
1336
1337 refute User.superuser?(user)
1338 refute User.superuser?(remote_admin_user)
1339 end
1340
1341 test "returns true for local moderators" do
1342 user = insert(:user, local: true, is_moderator: true)
1343
1344 assert User.superuser?(user)
1345 end
1346
1347 test "returns true for local admins" do
1348 user = insert(:user, local: true, is_admin: true)
1349
1350 assert User.superuser?(user)
1351 end
1352 end
1353
1354 describe "invisible?/1" do
1355 test "returns true for an invisible user" do
1356 user = insert(:user, local: true, invisible: true)
1357
1358 assert User.invisible?(user)
1359 end
1360
1361 test "returns false for a non-invisible user" do
1362 user = insert(:user, local: true)
1363
1364 refute User.invisible?(user)
1365 end
1366 end
1367
1368 describe "visible_for?/2" do
1369 test "returns true when the account is itself" do
1370 user = insert(:user, local: true)
1371
1372 assert User.visible_for?(user, user)
1373 end
1374
1375 test "returns false when the account is unauthenticated and auth is required" do
1376 Pleroma.Config.put([:instance, :account_activation_required], true)
1377
1378 user = insert(:user, local: true, confirmation_pending: true)
1379 other_user = insert(:user, local: true)
1380
1381 refute User.visible_for?(user, other_user)
1382 end
1383
1384 test "returns true when the account is unauthenticated and auth is not required" do
1385 user = insert(:user, local: true, confirmation_pending: true)
1386 other_user = insert(:user, local: true)
1387
1388 assert User.visible_for?(user, other_user)
1389 end
1390
1391 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1392 Pleroma.Config.put([:instance, :account_activation_required], true)
1393
1394 user = insert(:user, local: true, confirmation_pending: true)
1395 other_user = insert(:user, local: true, is_admin: true)
1396
1397 assert User.visible_for?(user, other_user)
1398 end
1399 end
1400
1401 describe "parse_bio/2" do
1402 test "preserves hosts in user links text" do
1403 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1404 user = insert(:user)
1405 bio = "A.k.a. @nick@domain.com"
1406
1407 expected_text =
1408 ~s(A.k.a. <span class="h-card"><a data-user="#{remote_user.id}" class="u-url mention" href="#{
1409 remote_user.ap_id
1410 }" rel="ugc">@<span>nick@domain.com</span></a></span>)
1411
1412 assert expected_text == User.parse_bio(bio, user)
1413 end
1414
1415 test "Adds rel=me on linkbacked urls" do
1416 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1417
1418 bio = "http://example.com/rel_me/null"
1419 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1420 assert expected_text == User.parse_bio(bio, user)
1421
1422 bio = "http://example.com/rel_me/link"
1423 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1424 assert expected_text == User.parse_bio(bio, user)
1425
1426 bio = "http://example.com/rel_me/anchor"
1427 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1428 assert expected_text == User.parse_bio(bio, user)
1429 end
1430 end
1431
1432 test "follower count is updated when a follower is blocked" do
1433 user = insert(:user)
1434 follower = insert(:user)
1435 follower2 = insert(:user)
1436 follower3 = insert(:user)
1437
1438 {:ok, follower} = User.follow(follower, user)
1439 {:ok, _follower2} = User.follow(follower2, user)
1440 {:ok, _follower3} = User.follow(follower3, user)
1441
1442 {:ok, _user_relationship} = User.block(user, follower)
1443 user = refresh_record(user)
1444
1445 assert user.follower_count == 2
1446 end
1447
1448 describe "list_inactive_users_query/1" do
1449 defp days_ago(days) do
1450 NaiveDateTime.add(
1451 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1452 -days * 60 * 60 * 24,
1453 :second
1454 )
1455 end
1456
1457 test "Users are inactive by default" do
1458 total = 10
1459
1460 users =
1461 Enum.map(1..total, fn _ ->
1462 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1463 end)
1464
1465 inactive_users_ids =
1466 Pleroma.User.list_inactive_users_query()
1467 |> Pleroma.Repo.all()
1468 |> Enum.map(& &1.id)
1469
1470 Enum.each(users, fn user ->
1471 assert user.id in inactive_users_ids
1472 end)
1473 end
1474
1475 test "Only includes users who has no recent activity" do
1476 total = 10
1477
1478 users =
1479 Enum.map(1..total, fn _ ->
1480 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1481 end)
1482
1483 {inactive, active} = Enum.split(users, trunc(total / 2))
1484
1485 Enum.map(active, fn user ->
1486 to = Enum.random(users -- [user])
1487
1488 {:ok, _} =
1489 CommonAPI.post(user, %{
1490 "status" => "hey @#{to.nickname}"
1491 })
1492 end)
1493
1494 inactive_users_ids =
1495 Pleroma.User.list_inactive_users_query()
1496 |> Pleroma.Repo.all()
1497 |> Enum.map(& &1.id)
1498
1499 Enum.each(active, fn user ->
1500 refute user.id in inactive_users_ids
1501 end)
1502
1503 Enum.each(inactive, fn user ->
1504 assert user.id in inactive_users_ids
1505 end)
1506 end
1507
1508 test "Only includes users with no read notifications" do
1509 total = 10
1510
1511 users =
1512 Enum.map(1..total, fn _ ->
1513 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1514 end)
1515
1516 [sender | recipients] = users
1517 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1518
1519 Enum.each(recipients, fn to ->
1520 {:ok, _} =
1521 CommonAPI.post(sender, %{
1522 "status" => "hey @#{to.nickname}"
1523 })
1524
1525 {:ok, _} =
1526 CommonAPI.post(sender, %{
1527 "status" => "hey again @#{to.nickname}"
1528 })
1529 end)
1530
1531 Enum.each(active, fn user ->
1532 [n1, _n2] = Pleroma.Notification.for_user(user)
1533 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1534 end)
1535
1536 inactive_users_ids =
1537 Pleroma.User.list_inactive_users_query()
1538 |> Pleroma.Repo.all()
1539 |> Enum.map(& &1.id)
1540
1541 Enum.each(active, fn user ->
1542 refute user.id in inactive_users_ids
1543 end)
1544
1545 Enum.each(inactive, fn user ->
1546 assert user.id in inactive_users_ids
1547 end)
1548 end
1549 end
1550
1551 describe "toggle_confirmation/1" do
1552 test "if user is confirmed" do
1553 user = insert(:user, confirmation_pending: false)
1554 {:ok, user} = User.toggle_confirmation(user)
1555
1556 assert user.confirmation_pending
1557 assert user.confirmation_token
1558 end
1559
1560 test "if user is unconfirmed" do
1561 user = insert(:user, confirmation_pending: true, confirmation_token: "some token")
1562 {:ok, user} = User.toggle_confirmation(user)
1563
1564 refute user.confirmation_pending
1565 refute user.confirmation_token
1566 end
1567 end
1568
1569 describe "ensure_keys_present" do
1570 test "it creates keys for a user and stores them in info" do
1571 user = insert(:user)
1572 refute is_binary(user.keys)
1573 {:ok, user} = User.ensure_keys_present(user)
1574 assert is_binary(user.keys)
1575 end
1576
1577 test "it doesn't create keys if there already are some" do
1578 user = insert(:user, keys: "xxx")
1579 {:ok, user} = User.ensure_keys_present(user)
1580 assert user.keys == "xxx"
1581 end
1582 end
1583
1584 describe "get_ap_ids_by_nicknames" do
1585 test "it returns a list of AP ids for a given set of nicknames" do
1586 user = insert(:user)
1587 user_two = insert(:user)
1588
1589 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1590 assert length(ap_ids) == 2
1591 assert user.ap_id in ap_ids
1592 assert user_two.ap_id in ap_ids
1593 end
1594 end
1595
1596 describe "sync followers count" do
1597 setup do
1598 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1599 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1600 insert(:user, local: true)
1601 insert(:user, local: false, deactivated: true)
1602 {:ok, user1: user1, user2: user2}
1603 end
1604
1605 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1606 [fdb_user1] = User.external_users(limit: 1)
1607
1608 assert fdb_user1.ap_id
1609 assert fdb_user1.ap_id == user1.ap_id
1610 assert fdb_user1.id == user1.id
1611
1612 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1613
1614 assert fdb_user2.ap_id
1615 assert fdb_user2.ap_id == user2.ap_id
1616 assert fdb_user2.id == user2.id
1617
1618 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1619 end
1620 end
1621
1622 describe "is_internal_user?/1" do
1623 test "non-internal user returns false" do
1624 user = insert(:user)
1625 refute User.is_internal_user?(user)
1626 end
1627
1628 test "user with no nickname returns true" do
1629 user = insert(:user, %{nickname: nil})
1630 assert User.is_internal_user?(user)
1631 end
1632
1633 test "user with internal-prefixed nickname returns true" do
1634 user = insert(:user, %{nickname: "internal.test"})
1635 assert User.is_internal_user?(user)
1636 end
1637 end
1638
1639 describe "update_and_set_cache/1" do
1640 test "returns error when user is stale instead Ecto.StaleEntryError" do
1641 user = insert(:user)
1642
1643 changeset = Ecto.Changeset.change(user, bio: "test")
1644
1645 Repo.delete(user)
1646
1647 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1648 User.update_and_set_cache(changeset)
1649 end
1650
1651 test "performs update cache if user updated" do
1652 user = insert(:user)
1653 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1654
1655 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1656
1657 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1658 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1659 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1660 end
1661 end
1662
1663 describe "following/followers synchronization" do
1664 setup do: clear_config([:instance, :external_user_synchronization])
1665
1666 test "updates the counters normally on following/getting a follow when disabled" do
1667 Pleroma.Config.put([:instance, :external_user_synchronization], false)
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 {:ok, user} = Pleroma.User.follow(user, other_user)
1682 other_user = Pleroma.User.get_by_id(other_user.id)
1683
1684 assert user.following_count == 1
1685 assert other_user.follower_count == 1
1686 end
1687
1688 test "syncronizes the counters with the remote instance for the followed 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, _user} = User.follow(user, other_user)
1706 other_user = User.get_by_id(other_user.id)
1707
1708 assert other_user.follower_count == 437
1709 end
1710
1711 test "syncronizes the counters with the remote instance for the follower when enabled" do
1712 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1713
1714 user = insert(:user)
1715
1716 other_user =
1717 insert(:user,
1718 local: false,
1719 follower_address: "http://localhost:4001/users/masto_closed/followers",
1720 following_address: "http://localhost:4001/users/masto_closed/following",
1721 ap_enabled: true
1722 )
1723
1724 assert other_user.following_count == 0
1725 assert other_user.follower_count == 0
1726
1727 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1728 {:ok, other_user} = User.follow(other_user, user)
1729
1730 assert other_user.following_count == 152
1731 end
1732 end
1733
1734 describe "change_email/2" do
1735 setup do
1736 [user: insert(:user)]
1737 end
1738
1739 test "blank email returns error", %{user: user} do
1740 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1741 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1742 end
1743
1744 test "non unique email returns error", %{user: user} do
1745 %{email: email} = insert(:user)
1746
1747 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1748 User.change_email(user, email)
1749 end
1750
1751 test "invalid email returns error", %{user: user} do
1752 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1753 User.change_email(user, "cofe")
1754 end
1755
1756 test "changes email", %{user: user} do
1757 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1758 end
1759 end
1760
1761 describe "get_cached_by_nickname_or_id" do
1762 setup do
1763 local_user = insert(:user)
1764 remote_user = insert(:user, nickname: "nickname@example.com", local: false)
1765
1766 [local_user: local_user, remote_user: remote_user]
1767 end
1768
1769 setup do: clear_config([:instance, :limit_to_local_content])
1770
1771 test "allows getting remote users by id no matter what :limit_to_local_content is set to", %{
1772 remote_user: remote_user
1773 } do
1774 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1775 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1776
1777 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1778 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1779
1780 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1781 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1782 end
1783
1784 test "disallows getting remote users by nickname without authentication when :limit_to_local_content is set to :unauthenticated",
1785 %{remote_user: remote_user} do
1786 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1787 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1788 end
1789
1790 test "allows getting remote users by nickname with authentication when :limit_to_local_content is set to :unauthenticated",
1791 %{remote_user: remote_user, local_user: local_user} do
1792 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1793 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.nickname, for: local_user)
1794 end
1795
1796 test "disallows getting remote users by nickname when :limit_to_local_content is set to true",
1797 %{remote_user: remote_user} do
1798 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1799 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1800 end
1801
1802 test "allows getting local users by nickname no matter what :limit_to_local_content is set to",
1803 %{local_user: local_user} do
1804 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1805 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1806
1807 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1808 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1809
1810 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1811 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1812 end
1813 end
1814
1815 describe "update_email_notifications/2" do
1816 setup do
1817 user = insert(:user, email_notifications: %{"digest" => true})
1818
1819 {:ok, user: user}
1820 end
1821
1822 test "Notifications are updated", %{user: user} do
1823 true = user.email_notifications["digest"]
1824 assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false})
1825 assert result.email_notifications["digest"] == false
1826 end
1827 end
1828 end