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