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