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