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