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