Merge branch '1427-oauth-graceful-admin-scope' into 'develop'
[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
918 test "follows take precedence over domain blocks" do
919 user = insert(:user)
920 good_eggo = insert(:user, %{ap_id: "https://meanies.social/user/cuteposter"})
921
922 {:ok, user} = User.block_domain(user, "meanies.social")
923 {:ok, user} = User.follow(user, good_eggo)
924
925 refute User.blocks?(user, good_eggo)
926 end
927 end
928
929 describe "blocks_import" do
930 test "it imports user blocks from list" do
931 [user1, user2, user3] = insert_list(3, :user)
932
933 identifiers = [
934 user2.ap_id,
935 user3.nickname
936 ]
937
938 {:ok, job} = User.blocks_import(user1, identifiers)
939 result = ObanHelpers.perform(job)
940
941 assert is_list(result)
942 assert result == [user2, user3]
943 end
944 end
945
946 describe "get_recipients_from_activity" do
947 test "get recipients" do
948 actor = insert(:user)
949 user = insert(:user, local: true)
950 user_two = insert(:user, local: false)
951 addressed = insert(:user, local: true)
952 addressed_remote = insert(:user, local: false)
953
954 {:ok, activity} =
955 CommonAPI.post(actor, %{
956 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
957 })
958
959 assert Enum.map([actor, addressed], & &1.ap_id) --
960 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
961
962 {:ok, user} = User.follow(user, actor)
963 {:ok, _user_two} = User.follow(user_two, actor)
964 recipients = User.get_recipients_from_activity(activity)
965 assert length(recipients) == 3
966 assert user in recipients
967 assert addressed in recipients
968 end
969
970 test "has following" do
971 actor = insert(:user)
972 user = insert(:user)
973 user_two = insert(:user)
974 addressed = insert(:user, local: true)
975
976 {:ok, activity} =
977 CommonAPI.post(actor, %{
978 "status" => "hey @#{addressed.nickname}"
979 })
980
981 assert Enum.map([actor, addressed], & &1.ap_id) --
982 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
983
984 {:ok, _actor} = User.follow(actor, user)
985 {:ok, _actor} = User.follow(actor, user_two)
986 recipients = User.get_recipients_from_activity(activity)
987 assert length(recipients) == 2
988 assert addressed in recipients
989 end
990 end
991
992 describe ".deactivate" do
993 test "can de-activate then re-activate a user" do
994 user = insert(:user)
995 assert false == user.deactivated
996 {:ok, user} = User.deactivate(user)
997 assert true == user.deactivated
998 {:ok, user} = User.deactivate(user, false)
999 assert false == user.deactivated
1000 end
1001
1002 test "hide a user from followers" do
1003 user = insert(:user)
1004 user2 = insert(:user)
1005
1006 {:ok, user} = User.follow(user, user2)
1007 {:ok, _user} = User.deactivate(user)
1008
1009 user2 = User.get_cached_by_id(user2.id)
1010
1011 assert user2.follower_count == 0
1012 assert [] = User.get_followers(user2)
1013 end
1014
1015 test "hide a user from friends" do
1016 user = insert(:user)
1017 user2 = insert(:user)
1018
1019 {:ok, user2} = User.follow(user2, user)
1020 assert user2.following_count == 1
1021 assert User.following_count(user2) == 1
1022
1023 {:ok, _user} = User.deactivate(user)
1024
1025 user2 = User.get_cached_by_id(user2.id)
1026
1027 assert refresh_record(user2).following_count == 0
1028 assert user2.following_count == 0
1029 assert User.following_count(user2) == 0
1030 assert [] = User.get_friends(user2)
1031 end
1032
1033 test "hide a user's statuses from timelines and notifications" do
1034 user = insert(:user)
1035 user2 = insert(:user)
1036
1037 {:ok, user2} = User.follow(user2, user)
1038
1039 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
1040
1041 activity = Repo.preload(activity, :bookmark)
1042
1043 [notification] = Pleroma.Notification.for_user(user2)
1044 assert notification.activity.id == activity.id
1045
1046 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
1047
1048 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
1049 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1050 "user" => user2
1051 })
1052
1053 {:ok, _user} = User.deactivate(user)
1054
1055 assert [] == ActivityPub.fetch_public_activities(%{})
1056 assert [] == Pleroma.Notification.for_user(user2)
1057
1058 assert [] ==
1059 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1060 "user" => user2
1061 })
1062 end
1063 end
1064
1065 describe "delete" do
1066 setup do
1067 {:ok, user} = insert(:user) |> User.set_cache()
1068
1069 [user: user]
1070 end
1071
1072 clear_config([:instance, :federating])
1073
1074 test ".delete_user_activities deletes all create activities", %{user: user} do
1075 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
1076
1077 User.delete_user_activities(user)
1078
1079 # TODO: Remove favorites, repeats, delete activities.
1080 refute Activity.get_by_id(activity.id)
1081 end
1082
1083 test "it deletes deactivated user" do
1084 {:ok, user} = insert(:user, deactivated: true) |> User.set_cache()
1085
1086 {:ok, job} = User.delete(user)
1087 {:ok, _user} = ObanHelpers.perform(job)
1088
1089 refute User.get_by_id(user.id)
1090 end
1091
1092 test "it deletes a user, all follow relationships and all activities", %{user: user} do
1093 follower = insert(:user)
1094 {:ok, follower} = User.follow(follower, user)
1095
1096 object = insert(:note, user: user)
1097 activity = insert(:note_activity, user: user, note: object)
1098
1099 object_two = insert(:note, user: follower)
1100 activity_two = insert(:note_activity, user: follower, note: object_two)
1101
1102 {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
1103 {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
1104 {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
1105
1106 {:ok, job} = User.delete(user)
1107 {:ok, _user} = ObanHelpers.perform(job)
1108
1109 follower = User.get_cached_by_id(follower.id)
1110
1111 refute User.following?(follower, user)
1112 refute User.get_by_id(user.id)
1113 assert {:ok, nil} == Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1114
1115 user_activities =
1116 user.ap_id
1117 |> Activity.Queries.by_actor()
1118 |> Repo.all()
1119 |> Enum.map(fn act -> act.data["type"] end)
1120
1121 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1122
1123 refute Activity.get_by_id(activity.id)
1124 refute Activity.get_by_id(like.id)
1125 refute Activity.get_by_id(like_two.id)
1126 refute Activity.get_by_id(repeat.id)
1127 end
1128
1129 test_with_mock "it sends out User Delete activity",
1130 %{user: user},
1131 Pleroma.Web.ActivityPub.Publisher,
1132 [:passthrough],
1133 [] do
1134 Pleroma.Config.put([:instance, :federating], true)
1135
1136 {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
1137 {:ok, _} = User.follow(follower, user)
1138
1139 {:ok, job} = User.delete(user)
1140 {:ok, _user} = ObanHelpers.perform(job)
1141
1142 assert ObanHelpers.member?(
1143 %{
1144 "op" => "publish_one",
1145 "params" => %{
1146 "inbox" => "http://mastodon.example.org/inbox",
1147 "id" => "pleroma:fakeid"
1148 }
1149 },
1150 all_enqueued(worker: Pleroma.Workers.PublisherWorker)
1151 )
1152 end
1153 end
1154
1155 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1156 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1157 end
1158
1159 describe "insert or update a user from given data" do
1160 test "with normal data" do
1161 user = insert(:user, %{nickname: "nick@name.de"})
1162 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
1163
1164 assert {:ok, %User{}} = User.insert_or_update_user(data)
1165 end
1166
1167 test "with overly long fields" do
1168 current_max_length = Pleroma.Config.get([:instance, :account_field_value_length], 255)
1169 user = insert(:user, nickname: "nickname@supergood.domain")
1170
1171 data = %{
1172 ap_id: user.ap_id,
1173 name: user.name,
1174 nickname: user.nickname,
1175 fields: [
1176 %{"name" => "myfield", "value" => String.duplicate("h", current_max_length + 1)}
1177 ]
1178 }
1179
1180 assert {:ok, %User{}} = User.insert_or_update_user(data)
1181 end
1182
1183 test "with an overly long bio" do
1184 current_max_length = Pleroma.Config.get([:instance, :user_bio_length], 5000)
1185 user = insert(:user, nickname: "nickname@supergood.domain")
1186
1187 data = %{
1188 ap_id: user.ap_id,
1189 name: user.name,
1190 nickname: user.nickname,
1191 bio: String.duplicate("h", current_max_length + 1)
1192 }
1193
1194 assert {:ok, %User{}} = User.insert_or_update_user(data)
1195 end
1196
1197 test "with an overly long display name" do
1198 current_max_length = Pleroma.Config.get([:instance, :user_name_length], 100)
1199 user = insert(:user, nickname: "nickname@supergood.domain")
1200
1201 data = %{
1202 ap_id: user.ap_id,
1203 name: String.duplicate("h", current_max_length + 1),
1204 nickname: user.nickname
1205 }
1206
1207 assert {:ok, %User{}} = User.insert_or_update_user(data)
1208 end
1209 end
1210
1211 describe "per-user rich-text filtering" do
1212 test "html_filter_policy returns default policies, when rich-text is enabled" do
1213 user = insert(:user)
1214
1215 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1216 end
1217
1218 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1219 user = insert(:user, no_rich_text: true)
1220
1221 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1222 end
1223 end
1224
1225 describe "caching" do
1226 test "invalidate_cache works" do
1227 user = insert(:user)
1228
1229 User.set_cache(user)
1230 User.invalidate_cache(user)
1231
1232 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1233 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1234 end
1235
1236 test "User.delete() plugs any possible zombie objects" do
1237 user = insert(:user)
1238
1239 {:ok, job} = User.delete(user)
1240 {:ok, _} = ObanHelpers.perform(job)
1241
1242 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1243
1244 assert cached_user != user
1245
1246 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1247
1248 assert cached_user != user
1249 end
1250 end
1251
1252 test "auth_active?/1 works correctly" do
1253 Pleroma.Config.put([:instance, :account_activation_required], true)
1254
1255 local_user = insert(:user, local: true, confirmation_pending: true)
1256 confirmed_user = insert(:user, local: true, confirmation_pending: false)
1257 remote_user = insert(:user, local: false)
1258
1259 refute User.auth_active?(local_user)
1260 assert User.auth_active?(confirmed_user)
1261 assert User.auth_active?(remote_user)
1262
1263 # also shows unactive for deactivated users
1264
1265 deactivated_but_confirmed =
1266 insert(:user, local: true, confirmation_pending: false, deactivated: true)
1267
1268 refute User.auth_active?(deactivated_but_confirmed)
1269 end
1270
1271 describe "superuser?/1" do
1272 test "returns false for unprivileged users" do
1273 user = insert(:user, local: true)
1274
1275 refute User.superuser?(user)
1276 end
1277
1278 test "returns false for remote users" do
1279 user = insert(:user, local: false)
1280 remote_admin_user = insert(:user, local: false, is_admin: true)
1281
1282 refute User.superuser?(user)
1283 refute User.superuser?(remote_admin_user)
1284 end
1285
1286 test "returns true for local moderators" do
1287 user = insert(:user, local: true, is_moderator: true)
1288
1289 assert User.superuser?(user)
1290 end
1291
1292 test "returns true for local admins" do
1293 user = insert(:user, local: true, is_admin: true)
1294
1295 assert User.superuser?(user)
1296 end
1297 end
1298
1299 describe "invisible?/1" do
1300 test "returns true for an invisible user" do
1301 user = insert(:user, local: true, invisible: true)
1302
1303 assert User.invisible?(user)
1304 end
1305
1306 test "returns false for a non-invisible user" do
1307 user = insert(:user, local: true)
1308
1309 refute User.invisible?(user)
1310 end
1311 end
1312
1313 describe "visible_for?/2" do
1314 test "returns true when the account is itself" do
1315 user = insert(:user, local: true)
1316
1317 assert User.visible_for?(user, user)
1318 end
1319
1320 test "returns false when the account is unauthenticated and auth is required" do
1321 Pleroma.Config.put([:instance, :account_activation_required], true)
1322
1323 user = insert(:user, local: true, confirmation_pending: true)
1324 other_user = insert(:user, local: true)
1325
1326 refute User.visible_for?(user, other_user)
1327 end
1328
1329 test "returns true when the account is unauthenticated and auth is not required" do
1330 user = insert(:user, local: true, confirmation_pending: true)
1331 other_user = insert(:user, local: true)
1332
1333 assert User.visible_for?(user, other_user)
1334 end
1335
1336 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1337 Pleroma.Config.put([:instance, :account_activation_required], true)
1338
1339 user = insert(:user, local: true, confirmation_pending: true)
1340 other_user = insert(:user, local: true, is_admin: true)
1341
1342 assert User.visible_for?(user, other_user)
1343 end
1344 end
1345
1346 describe "parse_bio/2" do
1347 test "preserves hosts in user links text" do
1348 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1349 user = insert(:user)
1350 bio = "A.k.a. @nick@domain.com"
1351
1352 expected_text =
1353 ~s(A.k.a. <span class="h-card"><a data-user="#{remote_user.id}" class="u-url mention" href="#{
1354 remote_user.ap_id
1355 }" rel="ugc">@<span>nick@domain.com</span></a></span>)
1356
1357 assert expected_text == User.parse_bio(bio, user)
1358 end
1359
1360 test "Adds rel=me on linkbacked urls" do
1361 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1362
1363 bio = "http://example.com/rel_me/null"
1364 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1365 assert expected_text == User.parse_bio(bio, user)
1366
1367 bio = "http://example.com/rel_me/link"
1368 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1369 assert expected_text == User.parse_bio(bio, user)
1370
1371 bio = "http://example.com/rel_me/anchor"
1372 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1373 assert expected_text == User.parse_bio(bio, user)
1374 end
1375 end
1376
1377 test "follower count is updated when a follower is blocked" do
1378 user = insert(:user)
1379 follower = insert(:user)
1380 follower2 = insert(:user)
1381 follower3 = insert(:user)
1382
1383 {:ok, follower} = User.follow(follower, user)
1384 {:ok, _follower2} = User.follow(follower2, user)
1385 {:ok, _follower3} = User.follow(follower3, user)
1386
1387 {:ok, _user_relationship} = User.block(user, follower)
1388 user = refresh_record(user)
1389
1390 assert user.follower_count == 2
1391 end
1392
1393 describe "list_inactive_users_query/1" do
1394 defp days_ago(days) do
1395 NaiveDateTime.add(
1396 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1397 -days * 60 * 60 * 24,
1398 :second
1399 )
1400 end
1401
1402 test "Users are inactive by default" do
1403 total = 10
1404
1405 users =
1406 Enum.map(1..total, fn _ ->
1407 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1408 end)
1409
1410 inactive_users_ids =
1411 Pleroma.User.list_inactive_users_query()
1412 |> Pleroma.Repo.all()
1413 |> Enum.map(& &1.id)
1414
1415 Enum.each(users, fn user ->
1416 assert user.id in inactive_users_ids
1417 end)
1418 end
1419
1420 test "Only includes users who has no recent activity" do
1421 total = 10
1422
1423 users =
1424 Enum.map(1..total, fn _ ->
1425 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1426 end)
1427
1428 {inactive, active} = Enum.split(users, trunc(total / 2))
1429
1430 Enum.map(active, fn user ->
1431 to = Enum.random(users -- [user])
1432
1433 {:ok, _} =
1434 CommonAPI.post(user, %{
1435 "status" => "hey @#{to.nickname}"
1436 })
1437 end)
1438
1439 inactive_users_ids =
1440 Pleroma.User.list_inactive_users_query()
1441 |> Pleroma.Repo.all()
1442 |> Enum.map(& &1.id)
1443
1444 Enum.each(active, fn user ->
1445 refute user.id in inactive_users_ids
1446 end)
1447
1448 Enum.each(inactive, fn user ->
1449 assert user.id in inactive_users_ids
1450 end)
1451 end
1452
1453 test "Only includes users with no read notifications" do
1454 total = 10
1455
1456 users =
1457 Enum.map(1..total, fn _ ->
1458 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1459 end)
1460
1461 [sender | recipients] = users
1462 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1463
1464 Enum.each(recipients, fn to ->
1465 {:ok, _} =
1466 CommonAPI.post(sender, %{
1467 "status" => "hey @#{to.nickname}"
1468 })
1469
1470 {:ok, _} =
1471 CommonAPI.post(sender, %{
1472 "status" => "hey again @#{to.nickname}"
1473 })
1474 end)
1475
1476 Enum.each(active, fn user ->
1477 [n1, _n2] = Pleroma.Notification.for_user(user)
1478 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1479 end)
1480
1481 inactive_users_ids =
1482 Pleroma.User.list_inactive_users_query()
1483 |> Pleroma.Repo.all()
1484 |> Enum.map(& &1.id)
1485
1486 Enum.each(active, fn user ->
1487 refute user.id in inactive_users_ids
1488 end)
1489
1490 Enum.each(inactive, fn user ->
1491 assert user.id in inactive_users_ids
1492 end)
1493 end
1494 end
1495
1496 describe "toggle_confirmation/1" do
1497 test "if user is confirmed" do
1498 user = insert(:user, confirmation_pending: false)
1499 {:ok, user} = User.toggle_confirmation(user)
1500
1501 assert user.confirmation_pending
1502 assert user.confirmation_token
1503 end
1504
1505 test "if user is unconfirmed" do
1506 user = insert(:user, confirmation_pending: true, confirmation_token: "some token")
1507 {:ok, user} = User.toggle_confirmation(user)
1508
1509 refute user.confirmation_pending
1510 refute user.confirmation_token
1511 end
1512 end
1513
1514 describe "ensure_keys_present" do
1515 test "it creates keys for a user and stores them in info" do
1516 user = insert(:user)
1517 refute is_binary(user.keys)
1518 {:ok, user} = User.ensure_keys_present(user)
1519 assert is_binary(user.keys)
1520 end
1521
1522 test "it doesn't create keys if there already are some" do
1523 user = insert(:user, keys: "xxx")
1524 {:ok, user} = User.ensure_keys_present(user)
1525 assert user.keys == "xxx"
1526 end
1527 end
1528
1529 describe "get_ap_ids_by_nicknames" do
1530 test "it returns a list of AP ids for a given set of nicknames" do
1531 user = insert(:user)
1532 user_two = insert(:user)
1533
1534 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1535 assert length(ap_ids) == 2
1536 assert user.ap_id in ap_ids
1537 assert user_two.ap_id in ap_ids
1538 end
1539 end
1540
1541 describe "sync followers count" do
1542 setup do
1543 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1544 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1545 insert(:user, local: true)
1546 insert(:user, local: false, deactivated: true)
1547 {:ok, user1: user1, user2: user2}
1548 end
1549
1550 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1551 [fdb_user1] = User.external_users(limit: 1)
1552
1553 assert fdb_user1.ap_id
1554 assert fdb_user1.ap_id == user1.ap_id
1555 assert fdb_user1.id == user1.id
1556
1557 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1558
1559 assert fdb_user2.ap_id
1560 assert fdb_user2.ap_id == user2.ap_id
1561 assert fdb_user2.id == user2.id
1562
1563 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1564 end
1565 end
1566
1567 describe "is_internal_user?/1" do
1568 test "non-internal user returns false" do
1569 user = insert(:user)
1570 refute User.is_internal_user?(user)
1571 end
1572
1573 test "user with no nickname returns true" do
1574 user = insert(:user, %{nickname: nil})
1575 assert User.is_internal_user?(user)
1576 end
1577
1578 test "user with internal-prefixed nickname returns true" do
1579 user = insert(:user, %{nickname: "internal.test"})
1580 assert User.is_internal_user?(user)
1581 end
1582 end
1583
1584 describe "update_and_set_cache/1" do
1585 test "returns error when user is stale instead Ecto.StaleEntryError" do
1586 user = insert(:user)
1587
1588 changeset = Ecto.Changeset.change(user, bio: "test")
1589
1590 Repo.delete(user)
1591
1592 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1593 User.update_and_set_cache(changeset)
1594 end
1595
1596 test "performs update cache if user updated" do
1597 user = insert(:user)
1598 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1599
1600 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1601
1602 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1603 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1604 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1605 end
1606 end
1607
1608 describe "following/followers synchronization" do
1609 clear_config([:instance, :external_user_synchronization])
1610
1611 test "updates the counters normally on following/getting a follow when disabled" do
1612 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1613 user = insert(:user)
1614
1615 other_user =
1616 insert(:user,
1617 local: false,
1618 follower_address: "http://localhost:4001/users/masto_closed/followers",
1619 following_address: "http://localhost:4001/users/masto_closed/following",
1620 ap_enabled: true
1621 )
1622
1623 assert other_user.following_count == 0
1624 assert other_user.follower_count == 0
1625
1626 {:ok, user} = Pleroma.User.follow(user, other_user)
1627 other_user = Pleroma.User.get_by_id(other_user.id)
1628
1629 assert user.following_count == 1
1630 assert other_user.follower_count == 1
1631 end
1632
1633 test "syncronizes the counters with the remote instance for the followed when enabled" do
1634 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1635
1636 user = insert(:user)
1637
1638 other_user =
1639 insert(:user,
1640 local: false,
1641 follower_address: "http://localhost:4001/users/masto_closed/followers",
1642 following_address: "http://localhost:4001/users/masto_closed/following",
1643 ap_enabled: true
1644 )
1645
1646 assert other_user.following_count == 0
1647 assert other_user.follower_count == 0
1648
1649 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1650 {:ok, _user} = User.follow(user, other_user)
1651 other_user = User.get_by_id(other_user.id)
1652
1653 assert other_user.follower_count == 437
1654 end
1655
1656 test "syncronizes the counters with the remote instance for the follower when enabled" do
1657 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1658
1659 user = insert(:user)
1660
1661 other_user =
1662 insert(:user,
1663 local: false,
1664 follower_address: "http://localhost:4001/users/masto_closed/followers",
1665 following_address: "http://localhost:4001/users/masto_closed/following",
1666 ap_enabled: true
1667 )
1668
1669 assert other_user.following_count == 0
1670 assert other_user.follower_count == 0
1671
1672 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1673 {:ok, other_user} = User.follow(other_user, user)
1674
1675 assert other_user.following_count == 152
1676 end
1677 end
1678
1679 describe "change_email/2" do
1680 setup do
1681 [user: insert(:user)]
1682 end
1683
1684 test "blank email returns error", %{user: user} do
1685 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1686 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1687 end
1688
1689 test "non unique email returns error", %{user: user} do
1690 %{email: email} = insert(:user)
1691
1692 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1693 User.change_email(user, email)
1694 end
1695
1696 test "invalid email returns error", %{user: user} do
1697 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1698 User.change_email(user, "cofe")
1699 end
1700
1701 test "changes email", %{user: user} do
1702 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1703 end
1704 end
1705
1706 describe "get_cached_by_nickname_or_id" do
1707 setup do
1708 limit_to_local_content = Pleroma.Config.get([:instance, :limit_to_local_content])
1709 local_user = insert(:user)
1710 remote_user = insert(:user, nickname: "nickname@example.com", local: false)
1711
1712 on_exit(fn ->
1713 Pleroma.Config.put([:instance, :limit_to_local_content], limit_to_local_content)
1714 end)
1715
1716 [local_user: local_user, remote_user: remote_user]
1717 end
1718
1719 test "allows getting remote users by id no matter what :limit_to_local_content is set to", %{
1720 remote_user: remote_user
1721 } do
1722 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1723 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1724
1725 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1726 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1727
1728 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1729 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1730 end
1731
1732 test "disallows getting remote users by nickname without authentication when :limit_to_local_content is set to :unauthenticated",
1733 %{remote_user: remote_user} do
1734 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1735 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1736 end
1737
1738 test "allows getting remote users by nickname with authentication when :limit_to_local_content is set to :unauthenticated",
1739 %{remote_user: remote_user, local_user: local_user} do
1740 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1741 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.nickname, for: local_user)
1742 end
1743
1744 test "disallows getting remote users by nickname when :limit_to_local_content is set to true",
1745 %{remote_user: remote_user} do
1746 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1747 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1748 end
1749
1750 test "allows getting local users by nickname no matter what :limit_to_local_content is set to",
1751 %{local_user: local_user} do
1752 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1753 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1754
1755 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1756 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1757
1758 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1759 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1760 end
1761 end
1762
1763 describe "update_email_notifications/2" do
1764 setup do
1765 user = insert(:user, email_notifications: %{"digest" => true})
1766
1767 {:ok, user: user}
1768 end
1769
1770 test "Notifications are updated", %{user: user} do
1771 true = user.email_notifications["digest"]
1772 assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false})
1773 assert result.email_notifications["digest"] == false
1774 end
1775 end
1776 end