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