User: On registration, set `accepts_chat_messages` to true.
[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 sets the 'accepts_chat_messages' set to true" 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.accepts_chat_messages
496 end
497
498 test "it creates unconfirmed user" do
499 changeset = User.register_changeset(%User{}, @full_user_data)
500 assert changeset.valid?
501
502 {:ok, user} = Repo.insert(changeset)
503
504 assert user.confirmation_pending
505 assert user.confirmation_token
506 end
507
508 test "it creates confirmed user if :confirmed option is given" do
509 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
510 assert changeset.valid?
511
512 {:ok, user} = Repo.insert(changeset)
513
514 refute user.confirmation_pending
515 refute user.confirmation_token
516 end
517 end
518
519 describe "get_or_fetch/1" do
520 test "gets an existing user by nickname" do
521 user = insert(:user)
522 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
523
524 assert user == fetched_user
525 end
526
527 test "gets an existing user by ap_id" do
528 ap_id = "http://mastodon.example.org/users/admin"
529
530 user =
531 insert(
532 :user,
533 local: false,
534 nickname: "admin@mastodon.example.org",
535 ap_id: ap_id
536 )
537
538 {:ok, fetched_user} = User.get_or_fetch(ap_id)
539 freshed_user = refresh_record(user)
540 assert freshed_user == fetched_user
541 end
542 end
543
544 describe "fetching a user from nickname or trying to build one" do
545 test "gets an existing user" do
546 user = insert(:user)
547 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
548
549 assert user == fetched_user
550 end
551
552 test "gets an existing user, case insensitive" do
553 user = insert(:user, nickname: "nick")
554 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
555
556 assert user == fetched_user
557 end
558
559 test "gets an existing user by fully qualified nickname" do
560 user = insert(:user)
561
562 {:ok, fetched_user} =
563 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
564
565 assert user == fetched_user
566 end
567
568 test "gets an existing user by fully qualified nickname, case insensitive" do
569 user = insert(:user, nickname: "nick")
570 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
571
572 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
573
574 assert user == fetched_user
575 end
576
577 @tag capture_log: true
578 test "returns nil if no user could be fetched" do
579 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
580 assert fetched_user == "not found nonexistant@social.heldscal.la"
581 end
582
583 test "returns nil for nonexistant local user" do
584 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
585 assert fetched_user == "not found nonexistant"
586 end
587
588 test "updates an existing user, if stale" do
589 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
590
591 orig_user =
592 insert(
593 :user,
594 local: false,
595 nickname: "admin@mastodon.example.org",
596 ap_id: "http://mastodon.example.org/users/admin",
597 last_refreshed_at: a_week_ago
598 )
599
600 assert orig_user.last_refreshed_at == a_week_ago
601
602 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
603
604 assert user.inbox
605
606 refute user.last_refreshed_at == orig_user.last_refreshed_at
607 end
608
609 test "if nicknames clash, the old user gets a prefix with the old id to the nickname" do
610 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
611
612 orig_user =
613 insert(
614 :user,
615 local: false,
616 nickname: "admin@mastodon.example.org",
617 ap_id: "http://mastodon.example.org/users/harinezumigari",
618 last_refreshed_at: a_week_ago
619 )
620
621 assert orig_user.last_refreshed_at == a_week_ago
622
623 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
624
625 assert user.inbox
626
627 refute user.id == orig_user.id
628
629 orig_user = User.get_by_id(orig_user.id)
630
631 assert orig_user.nickname == "#{orig_user.id}.admin@mastodon.example.org"
632 end
633
634 @tag capture_log: true
635 test "it returns the old user if stale, but unfetchable" do
636 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
637
638 orig_user =
639 insert(
640 :user,
641 local: false,
642 nickname: "admin@mastodon.example.org",
643 ap_id: "http://mastodon.example.org/users/raymoo",
644 last_refreshed_at: a_week_ago
645 )
646
647 assert orig_user.last_refreshed_at == a_week_ago
648
649 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/raymoo")
650
651 assert user.last_refreshed_at == orig_user.last_refreshed_at
652 end
653 end
654
655 test "returns an ap_id for a user" do
656 user = insert(:user)
657
658 assert User.ap_id(user) ==
659 Pleroma.Web.Router.Helpers.user_feed_url(
660 Pleroma.Web.Endpoint,
661 :feed_redirect,
662 user.nickname
663 )
664 end
665
666 test "returns an ap_followers link for a user" do
667 user = insert(:user)
668
669 assert User.ap_followers(user) ==
670 Pleroma.Web.Router.Helpers.user_feed_url(
671 Pleroma.Web.Endpoint,
672 :feed_redirect,
673 user.nickname
674 ) <> "/followers"
675 end
676
677 describe "remote user changeset" do
678 @valid_remote %{
679 bio: "hello",
680 name: "Someone",
681 nickname: "a@b.de",
682 ap_id: "http...",
683 avatar: %{some: "avatar"}
684 }
685 setup do: clear_config([:instance, :user_bio_length])
686 setup do: clear_config([:instance, :user_name_length])
687
688 test "it confirms validity" do
689 cs = User.remote_user_changeset(@valid_remote)
690 assert cs.valid?
691 end
692
693 test "it sets the follower_adress" do
694 cs = User.remote_user_changeset(@valid_remote)
695 # remote users get a fake local follower address
696 assert cs.changes.follower_address ==
697 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
698 end
699
700 test "it enforces the fqn format for nicknames" do
701 cs = User.remote_user_changeset(%{@valid_remote | nickname: "bla"})
702 assert Ecto.Changeset.get_field(cs, :local) == false
703 assert cs.changes.avatar
704 refute cs.valid?
705 end
706
707 test "it has required fields" do
708 [:ap_id]
709 |> Enum.each(fn field ->
710 cs = User.remote_user_changeset(Map.delete(@valid_remote, field))
711 refute cs.valid?
712 end)
713 end
714 end
715
716 describe "followers and friends" do
717 test "gets all followers for a given user" do
718 user = insert(:user)
719 follower_one = insert(:user)
720 follower_two = insert(:user)
721 not_follower = insert(:user)
722
723 {:ok, follower_one} = User.follow(follower_one, user)
724 {:ok, follower_two} = User.follow(follower_two, user)
725
726 res = User.get_followers(user)
727
728 assert Enum.member?(res, follower_one)
729 assert Enum.member?(res, follower_two)
730 refute Enum.member?(res, not_follower)
731 end
732
733 test "gets all friends (followed users) for a given user" do
734 user = insert(:user)
735 followed_one = insert(:user)
736 followed_two = insert(:user)
737 not_followed = insert(:user)
738
739 {:ok, user} = User.follow(user, followed_one)
740 {:ok, user} = User.follow(user, followed_two)
741
742 res = User.get_friends(user)
743
744 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
745 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
746 assert Enum.member?(res, followed_one)
747 assert Enum.member?(res, followed_two)
748 refute Enum.member?(res, not_followed)
749 end
750 end
751
752 describe "updating note and follower count" do
753 test "it sets the note_count property" do
754 note = insert(:note)
755
756 user = User.get_cached_by_ap_id(note.data["actor"])
757
758 assert user.note_count == 0
759
760 {:ok, user} = User.update_note_count(user)
761
762 assert user.note_count == 1
763 end
764
765 test "it increases the note_count property" do
766 note = insert(:note)
767 user = User.get_cached_by_ap_id(note.data["actor"])
768
769 assert user.note_count == 0
770
771 {:ok, user} = User.increase_note_count(user)
772
773 assert user.note_count == 1
774
775 {:ok, user} = User.increase_note_count(user)
776
777 assert user.note_count == 2
778 end
779
780 test "it decreases the note_count property" do
781 note = insert(:note)
782 user = User.get_cached_by_ap_id(note.data["actor"])
783
784 assert user.note_count == 0
785
786 {:ok, user} = User.increase_note_count(user)
787
788 assert user.note_count == 1
789
790 {:ok, user} = User.decrease_note_count(user)
791
792 assert user.note_count == 0
793
794 {:ok, user} = User.decrease_note_count(user)
795
796 assert user.note_count == 0
797 end
798
799 test "it sets the follower_count property" do
800 user = insert(:user)
801 follower = insert(:user)
802
803 User.follow(follower, user)
804
805 assert user.follower_count == 0
806
807 {:ok, user} = User.update_follower_count(user)
808
809 assert user.follower_count == 1
810 end
811 end
812
813 describe "follow_import" do
814 test "it imports user followings from list" do
815 [user1, user2, user3] = insert_list(3, :user)
816
817 identifiers = [
818 user2.ap_id,
819 user3.nickname
820 ]
821
822 {:ok, job} = User.follow_import(user1, identifiers)
823
824 assert {:ok, result} = ObanHelpers.perform(job)
825 assert is_list(result)
826 assert result == [user2, user3]
827 end
828 end
829
830 describe "mutes" do
831 test "it mutes people" do
832 user = insert(:user)
833 muted_user = insert(:user)
834
835 refute User.mutes?(user, muted_user)
836 refute User.muted_notifications?(user, muted_user)
837
838 {:ok, _user_relationships} = User.mute(user, muted_user)
839
840 assert User.mutes?(user, muted_user)
841 assert User.muted_notifications?(user, muted_user)
842 end
843
844 test "it unmutes users" do
845 user = insert(:user)
846 muted_user = insert(:user)
847
848 {:ok, _user_relationships} = User.mute(user, muted_user)
849 {:ok, _user_mute} = User.unmute(user, muted_user)
850
851 refute User.mutes?(user, muted_user)
852 refute User.muted_notifications?(user, muted_user)
853 end
854
855 test "it mutes user without notifications" do
856 user = insert(:user)
857 muted_user = insert(:user)
858
859 refute User.mutes?(user, muted_user)
860 refute User.muted_notifications?(user, muted_user)
861
862 {:ok, _user_relationships} = User.mute(user, muted_user, false)
863
864 assert User.mutes?(user, muted_user)
865 refute User.muted_notifications?(user, muted_user)
866 end
867 end
868
869 describe "blocks" do
870 test "it blocks people" do
871 user = insert(:user)
872 blocked_user = insert(:user)
873
874 refute User.blocks?(user, blocked_user)
875
876 {:ok, _user_relationship} = User.block(user, blocked_user)
877
878 assert User.blocks?(user, blocked_user)
879 end
880
881 test "it unblocks users" do
882 user = insert(:user)
883 blocked_user = insert(:user)
884
885 {:ok, _user_relationship} = User.block(user, blocked_user)
886 {:ok, _user_block} = User.unblock(user, blocked_user)
887
888 refute User.blocks?(user, blocked_user)
889 end
890
891 test "blocks tear down cyclical follow relationships" do
892 blocker = insert(:user)
893 blocked = insert(:user)
894
895 {:ok, blocker} = User.follow(blocker, blocked)
896 {:ok, blocked} = User.follow(blocked, blocker)
897
898 assert User.following?(blocker, blocked)
899 assert User.following?(blocked, blocker)
900
901 {:ok, _user_relationship} = User.block(blocker, blocked)
902 blocked = User.get_cached_by_id(blocked.id)
903
904 assert User.blocks?(blocker, blocked)
905
906 refute User.following?(blocker, blocked)
907 refute User.following?(blocked, blocker)
908 end
909
910 test "blocks tear down blocker->blocked follow relationships" do
911 blocker = insert(:user)
912 blocked = insert(:user)
913
914 {:ok, blocker} = User.follow(blocker, blocked)
915
916 assert User.following?(blocker, blocked)
917 refute User.following?(blocked, blocker)
918
919 {:ok, _user_relationship} = User.block(blocker, blocked)
920 blocked = User.get_cached_by_id(blocked.id)
921
922 assert User.blocks?(blocker, blocked)
923
924 refute User.following?(blocker, blocked)
925 refute User.following?(blocked, blocker)
926 end
927
928 test "blocks tear down blocked->blocker follow relationships" do
929 blocker = insert(:user)
930 blocked = insert(:user)
931
932 {:ok, blocked} = User.follow(blocked, blocker)
933
934 refute User.following?(blocker, blocked)
935 assert User.following?(blocked, blocker)
936
937 {:ok, _user_relationship} = User.block(blocker, blocked)
938 blocked = User.get_cached_by_id(blocked.id)
939
940 assert User.blocks?(blocker, blocked)
941
942 refute User.following?(blocker, blocked)
943 refute User.following?(blocked, blocker)
944 end
945
946 test "blocks tear down blocked->blocker subscription relationships" do
947 blocker = insert(:user)
948 blocked = insert(:user)
949
950 {:ok, _subscription} = User.subscribe(blocked, blocker)
951
952 assert User.subscribed_to?(blocked, blocker)
953 refute User.subscribed_to?(blocker, blocked)
954
955 {:ok, _user_relationship} = User.block(blocker, blocked)
956
957 assert User.blocks?(blocker, blocked)
958 refute User.subscribed_to?(blocker, blocked)
959 refute User.subscribed_to?(blocked, blocker)
960 end
961 end
962
963 describe "domain blocking" do
964 test "blocks domains" do
965 user = insert(:user)
966 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
967
968 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
969
970 assert User.blocks?(user, collateral_user)
971 end
972
973 test "does not block domain with same end" do
974 user = insert(:user)
975
976 collateral_user =
977 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
978
979 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
980
981 refute User.blocks?(user, collateral_user)
982 end
983
984 test "does not block domain with same end if wildcard added" do
985 user = insert(:user)
986
987 collateral_user =
988 insert(:user, %{ap_id: "https://another-awful-and-rude-instance.com/user/bully"})
989
990 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
991
992 refute User.blocks?(user, collateral_user)
993 end
994
995 test "blocks domain with wildcard for subdomain" do
996 user = insert(:user)
997
998 user_from_subdomain =
999 insert(:user, %{ap_id: "https://subdomain.awful-and-rude-instance.com/user/bully"})
1000
1001 user_with_two_subdomains =
1002 insert(:user, %{
1003 ap_id: "https://subdomain.second_subdomain.awful-and-rude-instance.com/user/bully"
1004 })
1005
1006 user_domain = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
1007
1008 {:ok, user} = User.block_domain(user, "*.awful-and-rude-instance.com")
1009
1010 assert User.blocks?(user, user_from_subdomain)
1011 assert User.blocks?(user, user_with_two_subdomains)
1012 assert User.blocks?(user, user_domain)
1013 end
1014
1015 test "unblocks domains" do
1016 user = insert(:user)
1017 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
1018
1019 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
1020 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
1021
1022 refute User.blocks?(user, collateral_user)
1023 end
1024
1025 test "follows take precedence over domain blocks" do
1026 user = insert(:user)
1027 good_eggo = insert(:user, %{ap_id: "https://meanies.social/user/cuteposter"})
1028
1029 {:ok, user} = User.block_domain(user, "meanies.social")
1030 {:ok, user} = User.follow(user, good_eggo)
1031
1032 refute User.blocks?(user, good_eggo)
1033 end
1034 end
1035
1036 describe "blocks_import" do
1037 test "it imports user blocks from list" do
1038 [user1, user2, user3] = insert_list(3, :user)
1039
1040 identifiers = [
1041 user2.ap_id,
1042 user3.nickname
1043 ]
1044
1045 {:ok, job} = User.blocks_import(user1, identifiers)
1046
1047 assert {:ok, result} = ObanHelpers.perform(job)
1048 assert is_list(result)
1049 assert result == [user2, user3]
1050 end
1051 end
1052
1053 describe "get_recipients_from_activity" do
1054 test "works for announces" do
1055 actor = insert(:user)
1056 user = insert(:user, local: true)
1057
1058 {:ok, activity} = CommonAPI.post(actor, %{status: "hello"})
1059 {:ok, announce} = CommonAPI.repeat(activity.id, user)
1060
1061 recipients = User.get_recipients_from_activity(announce)
1062
1063 assert user in recipients
1064 end
1065
1066 test "get recipients" do
1067 actor = insert(:user)
1068 user = insert(:user, local: true)
1069 user_two = insert(:user, local: false)
1070 addressed = insert(:user, local: true)
1071 addressed_remote = insert(:user, local: false)
1072
1073 {:ok, activity} =
1074 CommonAPI.post(actor, %{
1075 status: "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
1076 })
1077
1078 assert Enum.map([actor, addressed], & &1.ap_id) --
1079 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1080
1081 {:ok, user} = User.follow(user, actor)
1082 {:ok, _user_two} = User.follow(user_two, actor)
1083 recipients = User.get_recipients_from_activity(activity)
1084 assert length(recipients) == 3
1085 assert user in recipients
1086 assert addressed in recipients
1087 end
1088
1089 test "has following" do
1090 actor = insert(:user)
1091 user = insert(:user)
1092 user_two = insert(:user)
1093 addressed = insert(:user, local: true)
1094
1095 {:ok, activity} =
1096 CommonAPI.post(actor, %{
1097 status: "hey @#{addressed.nickname}"
1098 })
1099
1100 assert Enum.map([actor, addressed], & &1.ap_id) --
1101 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
1102
1103 {:ok, _actor} = User.follow(actor, user)
1104 {:ok, _actor} = User.follow(actor, user_two)
1105 recipients = User.get_recipients_from_activity(activity)
1106 assert length(recipients) == 2
1107 assert addressed in recipients
1108 end
1109 end
1110
1111 describe ".deactivate" do
1112 test "can de-activate then re-activate a user" do
1113 user = insert(:user)
1114 assert false == user.deactivated
1115 {:ok, user} = User.deactivate(user)
1116 assert true == user.deactivated
1117 {:ok, user} = User.deactivate(user, false)
1118 assert false == user.deactivated
1119 end
1120
1121 test "hide a user from followers" do
1122 user = insert(:user)
1123 user2 = insert(:user)
1124
1125 {:ok, user} = User.follow(user, user2)
1126 {:ok, _user} = User.deactivate(user)
1127
1128 user2 = User.get_cached_by_id(user2.id)
1129
1130 assert user2.follower_count == 0
1131 assert [] = User.get_followers(user2)
1132 end
1133
1134 test "hide a user from friends" do
1135 user = insert(:user)
1136 user2 = insert(:user)
1137
1138 {:ok, user2} = User.follow(user2, user)
1139 assert user2.following_count == 1
1140 assert User.following_count(user2) == 1
1141
1142 {:ok, _user} = User.deactivate(user)
1143
1144 user2 = User.get_cached_by_id(user2.id)
1145
1146 assert refresh_record(user2).following_count == 0
1147 assert user2.following_count == 0
1148 assert User.following_count(user2) == 0
1149 assert [] = User.get_friends(user2)
1150 end
1151
1152 test "hide a user's statuses from timelines and notifications" do
1153 user = insert(:user)
1154 user2 = insert(:user)
1155
1156 {:ok, user2} = User.follow(user2, user)
1157
1158 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{user2.nickname}"})
1159
1160 activity = Repo.preload(activity, :bookmark)
1161
1162 [notification] = Pleroma.Notification.for_user(user2)
1163 assert notification.activity.id == activity.id
1164
1165 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
1166
1167 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
1168 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1169 user: user2
1170 })
1171
1172 {:ok, _user} = User.deactivate(user)
1173
1174 assert [] == ActivityPub.fetch_public_activities(%{})
1175 assert [] == Pleroma.Notification.for_user(user2)
1176
1177 assert [] ==
1178 ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
1179 user: user2
1180 })
1181 end
1182 end
1183
1184 describe "delete" do
1185 setup do
1186 {:ok, user} = insert(:user) |> User.set_cache()
1187
1188 [user: user]
1189 end
1190
1191 setup do: clear_config([:instance, :federating])
1192
1193 test ".delete_user_activities deletes all create activities", %{user: user} do
1194 {:ok, activity} = CommonAPI.post(user, %{status: "2hu"})
1195
1196 User.delete_user_activities(user)
1197
1198 # TODO: Test removal favorites, repeats, delete activities.
1199 refute Activity.get_by_id(activity.id)
1200 end
1201
1202 test "it deactivates a user, all follow relationships and all activities", %{user: user} do
1203 follower = insert(:user)
1204 {:ok, follower} = User.follow(follower, user)
1205
1206 locked_user = insert(:user, name: "locked", locked: true)
1207 {:ok, _} = User.follow(user, locked_user, :follow_pending)
1208
1209 object = insert(:note, user: user)
1210 activity = insert(:note_activity, user: user, note: object)
1211
1212 object_two = insert(:note, user: follower)
1213 activity_two = insert(:note_activity, user: follower, note: object_two)
1214
1215 {:ok, like} = CommonAPI.favorite(user, activity_two.id)
1216 {:ok, like_two} = CommonAPI.favorite(follower, activity.id)
1217 {:ok, repeat} = CommonAPI.repeat(activity_two.id, user)
1218
1219 {:ok, job} = User.delete(user)
1220 {:ok, _user} = ObanHelpers.perform(job)
1221
1222 follower = User.get_cached_by_id(follower.id)
1223
1224 refute User.following?(follower, user)
1225 assert %{deactivated: true} = User.get_by_id(user.id)
1226
1227 assert [] == User.get_follow_requests(locked_user)
1228
1229 user_activities =
1230 user.ap_id
1231 |> Activity.Queries.by_actor()
1232 |> Repo.all()
1233 |> Enum.map(fn act -> act.data["type"] end)
1234
1235 assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
1236
1237 refute Activity.get_by_id(activity.id)
1238 refute Activity.get_by_id(like.id)
1239 refute Activity.get_by_id(like_two.id)
1240 refute Activity.get_by_id(repeat.id)
1241 end
1242 end
1243
1244 describe "delete/1 when confirmation is pending" do
1245 setup do
1246 user = insert(:user, confirmation_pending: true)
1247 {:ok, user: user}
1248 end
1249
1250 test "deletes user from database when activation required", %{user: user} do
1251 clear_config([:instance, :account_activation_required], true)
1252
1253 {:ok, job} = User.delete(user)
1254 {:ok, _} = ObanHelpers.perform(job)
1255
1256 refute User.get_cached_by_id(user.id)
1257 refute User.get_by_id(user.id)
1258 end
1259
1260 test "deactivates user when activation is not required", %{user: user} do
1261 clear_config([:instance, :account_activation_required], false)
1262
1263 {:ok, job} = User.delete(user)
1264 {:ok, _} = ObanHelpers.perform(job)
1265
1266 assert %{deactivated: true} = User.get_cached_by_id(user.id)
1267 assert %{deactivated: true} = User.get_by_id(user.id)
1268 end
1269 end
1270
1271 test "get_public_key_for_ap_id fetches a user that's not in the db" do
1272 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
1273 end
1274
1275 describe "per-user rich-text filtering" do
1276 test "html_filter_policy returns default policies, when rich-text is enabled" do
1277 user = insert(:user)
1278
1279 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
1280 end
1281
1282 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
1283 user = insert(:user, no_rich_text: true)
1284
1285 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
1286 end
1287 end
1288
1289 describe "caching" do
1290 test "invalidate_cache works" do
1291 user = insert(:user)
1292
1293 User.set_cache(user)
1294 User.invalidate_cache(user)
1295
1296 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1297 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
1298 end
1299
1300 test "User.delete() plugs any possible zombie objects" do
1301 user = insert(:user)
1302
1303 {:ok, job} = User.delete(user)
1304 {:ok, _} = ObanHelpers.perform(job)
1305
1306 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1307
1308 assert cached_user != user
1309
1310 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1311
1312 assert cached_user != user
1313 end
1314 end
1315
1316 describe "account_status/1" do
1317 setup do: clear_config([:instance, :account_activation_required])
1318
1319 test "return confirmation_pending for unconfirm user" do
1320 Pleroma.Config.put([:instance, :account_activation_required], true)
1321 user = insert(:user, confirmation_pending: true)
1322 assert User.account_status(user) == :confirmation_pending
1323 end
1324
1325 test "return active for confirmed user" do
1326 Pleroma.Config.put([:instance, :account_activation_required], true)
1327 user = insert(:user, confirmation_pending: false)
1328 assert User.account_status(user) == :active
1329 end
1330
1331 test "return active for remote user" do
1332 user = insert(:user, local: false)
1333 assert User.account_status(user) == :active
1334 end
1335
1336 test "returns :password_reset_pending for user with reset password" do
1337 user = insert(:user, password_reset_pending: true)
1338 assert User.account_status(user) == :password_reset_pending
1339 end
1340
1341 test "returns :deactivated for deactivated user" do
1342 user = insert(:user, local: true, confirmation_pending: false, deactivated: true)
1343 assert User.account_status(user) == :deactivated
1344 end
1345 end
1346
1347 describe "superuser?/1" do
1348 test "returns false for unprivileged users" do
1349 user = insert(:user, local: true)
1350
1351 refute User.superuser?(user)
1352 end
1353
1354 test "returns false for remote users" do
1355 user = insert(:user, local: false)
1356 remote_admin_user = insert(:user, local: false, is_admin: true)
1357
1358 refute User.superuser?(user)
1359 refute User.superuser?(remote_admin_user)
1360 end
1361
1362 test "returns true for local moderators" do
1363 user = insert(:user, local: true, is_moderator: true)
1364
1365 assert User.superuser?(user)
1366 end
1367
1368 test "returns true for local admins" do
1369 user = insert(:user, local: true, is_admin: true)
1370
1371 assert User.superuser?(user)
1372 end
1373 end
1374
1375 describe "invisible?/1" do
1376 test "returns true for an invisible user" do
1377 user = insert(:user, local: true, invisible: true)
1378
1379 assert User.invisible?(user)
1380 end
1381
1382 test "returns false for a non-invisible user" do
1383 user = insert(:user, local: true)
1384
1385 refute User.invisible?(user)
1386 end
1387 end
1388
1389 describe "visible_for/2" do
1390 test "returns true when the account is itself" do
1391 user = insert(:user, local: true)
1392
1393 assert User.visible_for(user, user) == :visible
1394 end
1395
1396 test "returns false when the account is unauthenticated and auth is required" do
1397 Pleroma.Config.put([:instance, :account_activation_required], true)
1398
1399 user = insert(:user, local: true, confirmation_pending: true)
1400 other_user = insert(:user, local: true)
1401
1402 refute User.visible_for(user, other_user) == :visible
1403 end
1404
1405 test "returns true when the account is unauthenticated and auth is not required" do
1406 user = insert(:user, local: true, confirmation_pending: true)
1407 other_user = insert(:user, local: true)
1408
1409 assert User.visible_for(user, other_user) == :visible
1410 end
1411
1412 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1413 Pleroma.Config.put([:instance, :account_activation_required], true)
1414
1415 user = insert(:user, local: true, confirmation_pending: true)
1416 other_user = insert(:user, local: true, is_admin: true)
1417
1418 assert User.visible_for(user, other_user) == :visible
1419 end
1420 end
1421
1422 describe "parse_bio/2" do
1423 test "preserves hosts in user links text" do
1424 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1425 user = insert(:user)
1426 bio = "A.k.a. @nick@domain.com"
1427
1428 expected_text =
1429 ~s(A.k.a. <span class="h-card"><a class="u-url mention" data-user="#{remote_user.id}" href="#{
1430 remote_user.ap_id
1431 }" rel="ugc">@<span>nick@domain.com</span></a></span>)
1432
1433 assert expected_text == User.parse_bio(bio, user)
1434 end
1435
1436 test "Adds rel=me on linkbacked urls" do
1437 user = insert(:user, ap_id: "https://social.example.org/users/lain")
1438
1439 bio = "http://example.com/rel_me/null"
1440 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1441 assert expected_text == User.parse_bio(bio, user)
1442
1443 bio = "http://example.com/rel_me/link"
1444 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1445 assert expected_text == User.parse_bio(bio, user)
1446
1447 bio = "http://example.com/rel_me/anchor"
1448 expected_text = "<a href=\"#{bio}\" rel=\"me\">#{bio}</a>"
1449 assert expected_text == User.parse_bio(bio, user)
1450 end
1451 end
1452
1453 test "follower count is updated when a follower is blocked" do
1454 user = insert(:user)
1455 follower = insert(:user)
1456 follower2 = insert(:user)
1457 follower3 = insert(:user)
1458
1459 {:ok, follower} = User.follow(follower, user)
1460 {:ok, _follower2} = User.follow(follower2, user)
1461 {:ok, _follower3} = User.follow(follower3, user)
1462
1463 {:ok, _user_relationship} = User.block(user, follower)
1464 user = refresh_record(user)
1465
1466 assert user.follower_count == 2
1467 end
1468
1469 describe "list_inactive_users_query/1" do
1470 defp days_ago(days) do
1471 NaiveDateTime.add(
1472 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1473 -days * 60 * 60 * 24,
1474 :second
1475 )
1476 end
1477
1478 test "Users are inactive by default" do
1479 total = 10
1480
1481 users =
1482 Enum.map(1..total, fn _ ->
1483 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1484 end)
1485
1486 inactive_users_ids =
1487 Pleroma.User.list_inactive_users_query()
1488 |> Pleroma.Repo.all()
1489 |> Enum.map(& &1.id)
1490
1491 Enum.each(users, fn user ->
1492 assert user.id in inactive_users_ids
1493 end)
1494 end
1495
1496 test "Only includes users who has no recent activity" do
1497 total = 10
1498
1499 users =
1500 Enum.map(1..total, fn _ ->
1501 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1502 end)
1503
1504 {inactive, active} = Enum.split(users, trunc(total / 2))
1505
1506 Enum.map(active, fn user ->
1507 to = Enum.random(users -- [user])
1508
1509 {:ok, _} =
1510 CommonAPI.post(user, %{
1511 status: "hey @#{to.nickname}"
1512 })
1513 end)
1514
1515 inactive_users_ids =
1516 Pleroma.User.list_inactive_users_query()
1517 |> Pleroma.Repo.all()
1518 |> Enum.map(& &1.id)
1519
1520 Enum.each(active, fn user ->
1521 refute user.id in inactive_users_ids
1522 end)
1523
1524 Enum.each(inactive, fn user ->
1525 assert user.id in inactive_users_ids
1526 end)
1527 end
1528
1529 test "Only includes users with no read notifications" do
1530 total = 10
1531
1532 users =
1533 Enum.map(1..total, fn _ ->
1534 insert(:user, last_digest_emailed_at: days_ago(20), deactivated: false)
1535 end)
1536
1537 [sender | recipients] = users
1538 {inactive, active} = Enum.split(recipients, trunc(total / 2))
1539
1540 Enum.each(recipients, fn to ->
1541 {:ok, _} =
1542 CommonAPI.post(sender, %{
1543 status: "hey @#{to.nickname}"
1544 })
1545
1546 {:ok, _} =
1547 CommonAPI.post(sender, %{
1548 status: "hey again @#{to.nickname}"
1549 })
1550 end)
1551
1552 Enum.each(active, fn user ->
1553 [n1, _n2] = Pleroma.Notification.for_user(user)
1554 {:ok, _} = Pleroma.Notification.read_one(user, n1.id)
1555 end)
1556
1557 inactive_users_ids =
1558 Pleroma.User.list_inactive_users_query()
1559 |> Pleroma.Repo.all()
1560 |> Enum.map(& &1.id)
1561
1562 Enum.each(active, fn user ->
1563 refute user.id in inactive_users_ids
1564 end)
1565
1566 Enum.each(inactive, fn user ->
1567 assert user.id in inactive_users_ids
1568 end)
1569 end
1570 end
1571
1572 describe "toggle_confirmation/1" do
1573 test "if user is confirmed" do
1574 user = insert(:user, confirmation_pending: false)
1575 {:ok, user} = User.toggle_confirmation(user)
1576
1577 assert user.confirmation_pending
1578 assert user.confirmation_token
1579 end
1580
1581 test "if user is unconfirmed" do
1582 user = insert(:user, confirmation_pending: true, confirmation_token: "some token")
1583 {:ok, user} = User.toggle_confirmation(user)
1584
1585 refute user.confirmation_pending
1586 refute user.confirmation_token
1587 end
1588 end
1589
1590 describe "ensure_keys_present" do
1591 test "it creates keys for a user and stores them in info" do
1592 user = insert(:user)
1593 refute is_binary(user.keys)
1594 {:ok, user} = User.ensure_keys_present(user)
1595 assert is_binary(user.keys)
1596 end
1597
1598 test "it doesn't create keys if there already are some" do
1599 user = insert(:user, keys: "xxx")
1600 {:ok, user} = User.ensure_keys_present(user)
1601 assert user.keys == "xxx"
1602 end
1603 end
1604
1605 describe "get_ap_ids_by_nicknames" do
1606 test "it returns a list of AP ids for a given set of nicknames" do
1607 user = insert(:user)
1608 user_two = insert(:user)
1609
1610 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1611 assert length(ap_ids) == 2
1612 assert user.ap_id in ap_ids
1613 assert user_two.ap_id in ap_ids
1614 end
1615 end
1616
1617 describe "sync followers count" do
1618 setup do
1619 user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
1620 user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
1621 insert(:user, local: true)
1622 insert(:user, local: false, deactivated: true)
1623 {:ok, user1: user1, user2: user2}
1624 end
1625
1626 test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
1627 [fdb_user1] = User.external_users(limit: 1)
1628
1629 assert fdb_user1.ap_id
1630 assert fdb_user1.ap_id == user1.ap_id
1631 assert fdb_user1.id == user1.id
1632
1633 [fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
1634
1635 assert fdb_user2.ap_id
1636 assert fdb_user2.ap_id == user2.ap_id
1637 assert fdb_user2.id == user2.id
1638
1639 assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
1640 end
1641 end
1642
1643 describe "is_internal_user?/1" do
1644 test "non-internal user returns false" do
1645 user = insert(:user)
1646 refute User.is_internal_user?(user)
1647 end
1648
1649 test "user with no nickname returns true" do
1650 user = insert(:user, %{nickname: nil})
1651 assert User.is_internal_user?(user)
1652 end
1653
1654 test "user with internal-prefixed nickname returns true" do
1655 user = insert(:user, %{nickname: "internal.test"})
1656 assert User.is_internal_user?(user)
1657 end
1658 end
1659
1660 describe "update_and_set_cache/1" do
1661 test "returns error when user is stale instead Ecto.StaleEntryError" do
1662 user = insert(:user)
1663
1664 changeset = Ecto.Changeset.change(user, bio: "test")
1665
1666 Repo.delete(user)
1667
1668 assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
1669 User.update_and_set_cache(changeset)
1670 end
1671
1672 test "performs update cache if user updated" do
1673 user = insert(:user)
1674 assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1675
1676 changeset = Ecto.Changeset.change(user, bio: "test-bio")
1677
1678 assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
1679 assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1680 assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
1681 end
1682 end
1683
1684 describe "following/followers synchronization" do
1685 setup do: clear_config([:instance, :external_user_synchronization])
1686
1687 test "updates the counters normally on following/getting a follow when disabled" do
1688 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1689 user = insert(:user)
1690
1691 other_user =
1692 insert(:user,
1693 local: false,
1694 follower_address: "http://localhost:4001/users/masto_closed/followers",
1695 following_address: "http://localhost:4001/users/masto_closed/following",
1696 ap_enabled: true
1697 )
1698
1699 assert other_user.following_count == 0
1700 assert other_user.follower_count == 0
1701
1702 {:ok, user} = Pleroma.User.follow(user, other_user)
1703 other_user = Pleroma.User.get_by_id(other_user.id)
1704
1705 assert user.following_count == 1
1706 assert other_user.follower_count == 1
1707 end
1708
1709 test "syncronizes the counters with the remote instance for the followed when enabled" do
1710 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1711
1712 user = insert(:user)
1713
1714 other_user =
1715 insert(:user,
1716 local: false,
1717 follower_address: "http://localhost:4001/users/masto_closed/followers",
1718 following_address: "http://localhost:4001/users/masto_closed/following",
1719 ap_enabled: true
1720 )
1721
1722 assert other_user.following_count == 0
1723 assert other_user.follower_count == 0
1724
1725 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1726 {:ok, _user} = User.follow(user, other_user)
1727 other_user = User.get_by_id(other_user.id)
1728
1729 assert other_user.follower_count == 437
1730 end
1731
1732 test "syncronizes the counters with the remote instance for the follower when enabled" do
1733 Pleroma.Config.put([:instance, :external_user_synchronization], false)
1734
1735 user = insert(:user)
1736
1737 other_user =
1738 insert(:user,
1739 local: false,
1740 follower_address: "http://localhost:4001/users/masto_closed/followers",
1741 following_address: "http://localhost:4001/users/masto_closed/following",
1742 ap_enabled: true
1743 )
1744
1745 assert other_user.following_count == 0
1746 assert other_user.follower_count == 0
1747
1748 Pleroma.Config.put([:instance, :external_user_synchronization], true)
1749 {:ok, other_user} = User.follow(other_user, user)
1750
1751 assert other_user.following_count == 152
1752 end
1753 end
1754
1755 describe "change_email/2" do
1756 setup do
1757 [user: insert(:user)]
1758 end
1759
1760 test "blank email returns error", %{user: user} do
1761 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
1762 assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
1763 end
1764
1765 test "non unique email returns error", %{user: user} do
1766 %{email: email} = insert(:user)
1767
1768 assert {:error, %{errors: [email: {"has already been taken", _}]}} =
1769 User.change_email(user, email)
1770 end
1771
1772 test "invalid email returns error", %{user: user} do
1773 assert {:error, %{errors: [email: {"has invalid format", _}]}} =
1774 User.change_email(user, "cofe")
1775 end
1776
1777 test "changes email", %{user: user} do
1778 assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
1779 end
1780 end
1781
1782 describe "get_cached_by_nickname_or_id" do
1783 setup do
1784 local_user = insert(:user)
1785 remote_user = insert(:user, nickname: "nickname@example.com", local: false)
1786
1787 [local_user: local_user, remote_user: remote_user]
1788 end
1789
1790 setup do: clear_config([:instance, :limit_to_local_content])
1791
1792 test "allows getting remote users by id no matter what :limit_to_local_content is set to", %{
1793 remote_user: remote_user
1794 } do
1795 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1796 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1797
1798 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1799 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1800
1801 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1802 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.id)
1803 end
1804
1805 test "disallows getting remote users by nickname without authentication when :limit_to_local_content is set to :unauthenticated",
1806 %{remote_user: remote_user} do
1807 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1808 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1809 end
1810
1811 test "allows getting remote users by nickname with authentication when :limit_to_local_content is set to :unauthenticated",
1812 %{remote_user: remote_user, local_user: local_user} do
1813 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1814 assert %User{} = User.get_cached_by_nickname_or_id(remote_user.nickname, for: local_user)
1815 end
1816
1817 test "disallows getting remote users by nickname when :limit_to_local_content is set to true",
1818 %{remote_user: remote_user} do
1819 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1820 assert nil == User.get_cached_by_nickname_or_id(remote_user.nickname)
1821 end
1822
1823 test "allows getting local users by nickname no matter what :limit_to_local_content is set to",
1824 %{local_user: local_user} do
1825 Pleroma.Config.put([:instance, :limit_to_local_content], false)
1826 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1827
1828 Pleroma.Config.put([:instance, :limit_to_local_content], true)
1829 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1830
1831 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
1832 assert %User{} = User.get_cached_by_nickname_or_id(local_user.nickname)
1833 end
1834 end
1835
1836 describe "update_email_notifications/2" do
1837 setup do
1838 user = insert(:user, email_notifications: %{"digest" => true})
1839
1840 {:ok, user: user}
1841 end
1842
1843 test "Notifications are updated", %{user: user} do
1844 true = user.email_notifications["digest"]
1845 assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false})
1846 assert result.email_notifications["digest"] == false
1847 end
1848 end
1849
1850 test "avatar fallback" do
1851 user = insert(:user)
1852 assert User.avatar_url(user) =~ "/images/avi.png"
1853
1854 clear_config([:assets, :default_user_avatar], "avatar.png")
1855
1856 user = User.get_cached_by_nickname_or_id(user.nickname)
1857 assert User.avatar_url(user) =~ "avatar.png"
1858
1859 assert User.avatar_url(user, no_default: true) == nil
1860 end
1861 end