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