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