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