108883ba33dbdeefb9a0c880091187eef75e6b6a
[akkoma] / test / user_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.UserTest do
6 alias Pleroma.Activity
7 alias Pleroma.Builders.UserBuilder
8 alias Pleroma.Object
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.CommonAPI
13
14 use Pleroma.DataCase
15
16 import Pleroma.Factory
17
18 setup_all do
19 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 :ok
21 end
22
23 describe "when tags are nil" do
24 test "tagging a user" do
25 user = insert(:user, %{tags: nil})
26 user = User.tag(user, ["cool", "dude"])
27
28 assert "cool" in user.tags
29 assert "dude" in user.tags
30 end
31
32 test "untagging a user" do
33 user = insert(:user, %{tags: nil})
34 user = User.untag(user, ["cool", "dude"])
35
36 assert user.tags == []
37 end
38 end
39
40 test "ap_id returns the activity pub id for the user" do
41 user = UserBuilder.build()
42
43 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
44
45 assert expected_ap_id == User.ap_id(user)
46 end
47
48 test "ap_followers returns the followers collection for the user" do
49 user = UserBuilder.build()
50
51 expected_followers_collection = "#{User.ap_id(user)}/followers"
52
53 assert expected_followers_collection == User.ap_followers(user)
54 end
55
56 test "returns all pending follow requests" do
57 unlocked = insert(:user)
58 locked = insert(:user, %{info: %{locked: true}})
59 follower = insert(:user)
60
61 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => unlocked.id})
62 Pleroma.Web.TwitterAPI.TwitterAPI.follow(follower, %{"user_id" => locked.id})
63
64 assert {:ok, []} = User.get_follow_requests(unlocked)
65 assert {:ok, [activity]} = User.get_follow_requests(locked)
66
67 assert activity
68 end
69
70 test "doesn't return already accepted or duplicate follow requests" do
71 locked = insert(:user, %{info: %{locked: true}})
72 pending_follower = insert(:user)
73 accepted_follower = insert(:user)
74
75 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
76 Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id})
77 Pleroma.Web.TwitterAPI.TwitterAPI.follow(accepted_follower, %{"user_id" => locked.id})
78 User.follow(accepted_follower, locked)
79
80 assert {:ok, [activity]} = User.get_follow_requests(locked)
81 assert activity
82 end
83
84 test "follow_all follows mutliple users" do
85 user = insert(:user)
86 followed_zero = insert(:user)
87 followed_one = insert(:user)
88 followed_two = insert(:user)
89 blocked = insert(:user)
90 not_followed = insert(:user)
91 reverse_blocked = insert(:user)
92
93 {:ok, user} = User.block(user, blocked)
94 {:ok, reverse_blocked} = User.block(reverse_blocked, user)
95
96 {:ok, user} = User.follow(user, followed_zero)
97
98 {:ok, user} = User.follow_all(user, [followed_one, followed_two, blocked, reverse_blocked])
99
100 assert User.following?(user, followed_one)
101 assert User.following?(user, followed_two)
102 assert User.following?(user, followed_zero)
103 refute User.following?(user, not_followed)
104 refute User.following?(user, blocked)
105 refute User.following?(user, reverse_blocked)
106 end
107
108 test "follow_all follows mutliple users without duplicating" do
109 user = insert(:user)
110 followed_zero = insert(:user)
111 followed_one = insert(:user)
112 followed_two = insert(:user)
113
114 {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
115 assert length(user.following) == 3
116
117 {:ok, user} = User.follow_all(user, [followed_one, followed_two])
118 assert length(user.following) == 4
119 end
120
121 test "follow takes a user and another user" do
122 user = insert(:user)
123 followed = insert(:user)
124
125 {:ok, user} = User.follow(user, followed)
126
127 user = User.get_cached_by_id(user.id)
128
129 followed = User.get_cached_by_ap_id(followed.ap_id)
130 assert followed.info.follower_count == 1
131
132 assert User.ap_followers(followed) in user.following
133 end
134
135 test "can't follow a deactivated users" do
136 user = insert(:user)
137 followed = insert(:user, info: %{deactivated: true})
138
139 {:error, _} = User.follow(user, followed)
140 end
141
142 test "can't follow a user who blocked us" do
143 blocker = insert(:user)
144 blockee = insert(:user)
145
146 {:ok, blocker} = User.block(blocker, blockee)
147
148 {:error, _} = User.follow(blockee, blocker)
149 end
150
151 test "can't subscribe to a user who blocked us" do
152 blocker = insert(:user)
153 blocked = insert(:user)
154
155 {:ok, blocker} = User.block(blocker, blocked)
156
157 {:error, _} = User.subscribe(blocked, blocker)
158 end
159
160 test "local users do not automatically follow local locked accounts" do
161 follower = insert(:user, info: %{locked: true})
162 followed = insert(:user, info: %{locked: true})
163
164 {:ok, follower} = User.maybe_direct_follow(follower, followed)
165
166 refute User.following?(follower, followed)
167 end
168
169 # This is a somewhat useless test.
170 # test "following a remote user will ensure a websub subscription is present" do
171 # user = insert(:user)
172 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
173
174 # assert followed.local == false
175
176 # {:ok, user} = User.follow(user, followed)
177 # assert User.ap_followers(followed) in user.following
178
179 # query = from w in WebsubClientSubscription,
180 # where: w.topic == ^followed.info["topic"]
181 # websub = Repo.one(query)
182
183 # assert websub
184 # end
185
186 test "unfollow takes a user and another user" do
187 followed = insert(:user)
188 user = insert(:user, %{following: [User.ap_followers(followed)]})
189
190 {:ok, user, _activity} = User.unfollow(user, followed)
191
192 user = User.get_cached_by_id(user.id)
193
194 assert user.following == []
195 end
196
197 test "unfollow doesn't unfollow yourself" do
198 user = insert(:user)
199
200 {:error, _} = User.unfollow(user, user)
201
202 user = User.get_cached_by_id(user.id)
203 assert user.following == [user.ap_id]
204 end
205
206 test "test if a user is following another user" do
207 followed = insert(:user)
208 user = insert(:user, %{following: [User.ap_followers(followed)]})
209
210 assert User.following?(user, followed)
211 refute User.following?(followed, user)
212 end
213
214 test "fetches correct profile for nickname beginning with number" do
215 # Use old-style integer ID to try to reproduce the problem
216 user = insert(:user, %{id: 1080})
217 user_with_numbers = insert(:user, %{nickname: "#{user.id}garbage"})
218 assert user_with_numbers == User.get_cached_by_nickname_or_id(user_with_numbers.nickname)
219 end
220
221 describe "user registration" do
222 @full_user_data %{
223 bio: "A guy",
224 name: "my name",
225 nickname: "nick",
226 password: "test",
227 password_confirmation: "test",
228 email: "email@example.com"
229 }
230
231 test "it autofollows accounts that are set for it" do
232 user = insert(:user)
233 remote_user = insert(:user, %{local: false})
234
235 Pleroma.Config.put([:instance, :autofollowed_nicknames], [
236 user.nickname,
237 remote_user.nickname
238 ])
239
240 cng = User.register_changeset(%User{}, @full_user_data)
241
242 {:ok, registered_user} = User.register(cng)
243
244 assert User.following?(registered_user, user)
245 refute User.following?(registered_user, remote_user)
246
247 Pleroma.Config.put([:instance, :autofollowed_nicknames], [])
248 end
249
250 test "it sends a welcome message if it is set" do
251 welcome_user = insert(:user)
252
253 Pleroma.Config.put([:instance, :welcome_user_nickname], welcome_user.nickname)
254 Pleroma.Config.put([:instance, :welcome_message], "Hello, this is a cool site")
255
256 cng = User.register_changeset(%User{}, @full_user_data)
257 {:ok, registered_user} = User.register(cng)
258
259 activity = Repo.one(Pleroma.Activity)
260 assert registered_user.ap_id in activity.recipients
261 assert Object.normalize(activity).data["content"] =~ "cool site"
262 assert activity.actor == welcome_user.ap_id
263
264 Pleroma.Config.put([:instance, :welcome_user_nickname], nil)
265 Pleroma.Config.put([:instance, :welcome_message], nil)
266 end
267
268 test "it requires an email, name, nickname and password, bio is optional" do
269 @full_user_data
270 |> Map.keys()
271 |> Enum.each(fn key ->
272 params = Map.delete(@full_user_data, key)
273 changeset = User.register_changeset(%User{}, params)
274
275 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
276 end)
277 end
278
279 test "it restricts certain nicknames" do
280 [restricted_name | _] = Pleroma.Config.get([User, :restricted_nicknames])
281
282 assert is_bitstring(restricted_name)
283
284 params =
285 @full_user_data
286 |> Map.put(:nickname, restricted_name)
287
288 changeset = User.register_changeset(%User{}, params)
289
290 refute changeset.valid?
291 end
292
293 test "it sets the password_hash, ap_id and following fields" do
294 changeset = User.register_changeset(%User{}, @full_user_data)
295
296 assert changeset.valid?
297
298 assert is_binary(changeset.changes[:password_hash])
299 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
300
301 assert changeset.changes[:following] == [
302 User.ap_followers(%User{nickname: @full_user_data.nickname})
303 ]
304
305 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
306 end
307
308 test "it ensures info is not nil" do
309 changeset = User.register_changeset(%User{}, @full_user_data)
310
311 assert changeset.valid?
312
313 {:ok, user} =
314 changeset
315 |> Repo.insert()
316
317 refute is_nil(user.info)
318 end
319 end
320
321 describe "user registration, with :account_activation_required" do
322 @full_user_data %{
323 bio: "A guy",
324 name: "my name",
325 nickname: "nick",
326 password: "test",
327 password_confirmation: "test",
328 email: "email@example.com"
329 }
330
331 setup do
332 setting = Pleroma.Config.get([:instance, :account_activation_required])
333
334 unless setting do
335 Pleroma.Config.put([:instance, :account_activation_required], true)
336 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
337 end
338
339 :ok
340 end
341
342 test "it creates unconfirmed user" do
343 changeset = User.register_changeset(%User{}, @full_user_data)
344 assert changeset.valid?
345
346 {:ok, user} = Repo.insert(changeset)
347
348 assert user.info.confirmation_pending
349 assert user.info.confirmation_token
350 end
351
352 test "it creates confirmed user if :confirmed option is given" do
353 changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)
354 assert changeset.valid?
355
356 {:ok, user} = Repo.insert(changeset)
357
358 refute user.info.confirmation_pending
359 refute user.info.confirmation_token
360 end
361 end
362
363 describe "get_or_fetch/1" do
364 test "gets an existing user by nickname" do
365 user = insert(:user)
366 {:ok, fetched_user} = User.get_or_fetch(user.nickname)
367
368 assert user == fetched_user
369 end
370
371 test "gets an existing user by ap_id" do
372 ap_id = "http://mastodon.example.org/users/admin"
373
374 user =
375 insert(
376 :user,
377 local: false,
378 nickname: "admin@mastodon.example.org",
379 ap_id: ap_id,
380 info: %{}
381 )
382
383 {:ok, fetched_user} = User.get_or_fetch(ap_id)
384 freshed_user = refresh_record(user)
385 assert freshed_user == fetched_user
386 end
387 end
388
389 describe "fetching a user from nickname or trying to build one" do
390 test "gets an existing user" do
391 user = insert(:user)
392 {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)
393
394 assert user == fetched_user
395 end
396
397 test "gets an existing user, case insensitive" do
398 user = insert(:user, nickname: "nick")
399 {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")
400
401 assert user == fetched_user
402 end
403
404 test "gets an existing user by fully qualified nickname" do
405 user = insert(:user)
406
407 {:ok, fetched_user} =
408 User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
409
410 assert user == fetched_user
411 end
412
413 test "gets an existing user by fully qualified nickname, case insensitive" do
414 user = insert(:user, nickname: "nick")
415 casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())
416
417 {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)
418
419 assert user == fetched_user
420 end
421
422 test "fetches an external user via ostatus if no user exists" do
423 {:ok, fetched_user} = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
424 assert fetched_user.nickname == "shp@social.heldscal.la"
425 end
426
427 test "returns nil if no user could be fetched" do
428 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
429 assert fetched_user == "not found nonexistant@social.heldscal.la"
430 end
431
432 test "returns nil for nonexistant local user" do
433 {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant")
434 assert fetched_user == "not found nonexistant"
435 end
436
437 test "updates an existing user, if stale" do
438 a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
439
440 orig_user =
441 insert(
442 :user,
443 local: false,
444 nickname: "admin@mastodon.example.org",
445 ap_id: "http://mastodon.example.org/users/admin",
446 last_refreshed_at: a_week_ago,
447 info: %{}
448 )
449
450 assert orig_user.last_refreshed_at == a_week_ago
451
452 {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
453 assert user.info.source_data["endpoints"]
454
455 refute user.last_refreshed_at == orig_user.last_refreshed_at
456 end
457 end
458
459 test "returns an ap_id for a user" do
460 user = insert(:user)
461
462 assert User.ap_id(user) ==
463 Pleroma.Web.Router.Helpers.o_status_url(
464 Pleroma.Web.Endpoint,
465 :feed_redirect,
466 user.nickname
467 )
468 end
469
470 test "returns an ap_followers link for a user" do
471 user = insert(:user)
472
473 assert User.ap_followers(user) ==
474 Pleroma.Web.Router.Helpers.o_status_url(
475 Pleroma.Web.Endpoint,
476 :feed_redirect,
477 user.nickname
478 ) <> "/followers"
479 end
480
481 describe "remote user creation changeset" do
482 @valid_remote %{
483 bio: "hello",
484 name: "Someone",
485 nickname: "a@b.de",
486 ap_id: "http...",
487 info: %{some: "info"},
488 avatar: %{some: "avatar"}
489 }
490
491 test "it confirms validity" do
492 cs = User.remote_user_creation(@valid_remote)
493 assert cs.valid?
494 end
495
496 test "it sets the follower_adress" do
497 cs = User.remote_user_creation(@valid_remote)
498 # remote users get a fake local follower address
499 assert cs.changes.follower_address ==
500 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
501 end
502
503 test "it enforces the fqn format for nicknames" do
504 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
505 assert cs.changes.local == false
506 assert cs.changes.avatar
507 refute cs.valid?
508 end
509
510 test "it has required fields" do
511 [:name, :ap_id]
512 |> Enum.each(fn field ->
513 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
514 refute cs.valid?
515 end)
516 end
517
518 test "it restricts some sizes" do
519 [bio: 5000, name: 100]
520 |> Enum.each(fn {field, size} ->
521 string = String.pad_leading(".", size)
522 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
523 assert cs.valid?
524
525 string = String.pad_leading(".", size + 1)
526 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
527 refute cs.valid?
528 end)
529 end
530 end
531
532 describe "followers and friends" do
533 test "gets all followers for a given user" do
534 user = insert(:user)
535 follower_one = insert(:user)
536 follower_two = insert(:user)
537 not_follower = insert(:user)
538
539 {:ok, follower_one} = User.follow(follower_one, user)
540 {:ok, follower_two} = User.follow(follower_two, user)
541
542 {:ok, res} = User.get_followers(user)
543
544 assert Enum.member?(res, follower_one)
545 assert Enum.member?(res, follower_two)
546 refute Enum.member?(res, not_follower)
547 end
548
549 test "gets all friends (followed users) for a given user" do
550 user = insert(:user)
551 followed_one = insert(:user)
552 followed_two = insert(:user)
553 not_followed = insert(:user)
554
555 {:ok, user} = User.follow(user, followed_one)
556 {:ok, user} = User.follow(user, followed_two)
557
558 {:ok, res} = User.get_friends(user)
559
560 followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
561 followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
562 assert Enum.member?(res, followed_one)
563 assert Enum.member?(res, followed_two)
564 refute Enum.member?(res, not_followed)
565 end
566 end
567
568 describe "updating note and follower count" do
569 test "it sets the info->note_count property" do
570 note = insert(:note)
571
572 user = User.get_cached_by_ap_id(note.data["actor"])
573
574 assert user.info.note_count == 0
575
576 {:ok, user} = User.update_note_count(user)
577
578 assert user.info.note_count == 1
579 end
580
581 test "it increases the info->note_count property" do
582 note = insert(:note)
583 user = User.get_cached_by_ap_id(note.data["actor"])
584
585 assert user.info.note_count == 0
586
587 {:ok, user} = User.increase_note_count(user)
588
589 assert user.info.note_count == 1
590
591 {:ok, user} = User.increase_note_count(user)
592
593 assert user.info.note_count == 2
594 end
595
596 test "it decreases the info->note_count property" do
597 note = insert(:note)
598 user = User.get_cached_by_ap_id(note.data["actor"])
599
600 assert user.info.note_count == 0
601
602 {:ok, user} = User.increase_note_count(user)
603
604 assert user.info.note_count == 1
605
606 {:ok, user} = User.decrease_note_count(user)
607
608 assert user.info.note_count == 0
609
610 {:ok, user} = User.decrease_note_count(user)
611
612 assert user.info.note_count == 0
613 end
614
615 test "it sets the info->follower_count property" do
616 user = insert(:user)
617 follower = insert(:user)
618
619 User.follow(follower, user)
620
621 assert user.info.follower_count == 0
622
623 {:ok, user} = User.update_follower_count(user)
624
625 assert user.info.follower_count == 1
626 end
627 end
628
629 describe "remove duplicates from following list" do
630 test "it removes duplicates" do
631 user = insert(:user)
632 follower = insert(:user)
633
634 {:ok, %User{following: following} = follower} = User.follow(follower, user)
635 assert length(following) == 2
636
637 {:ok, follower} =
638 follower
639 |> User.update_changeset(%{following: following ++ following})
640 |> Repo.update()
641
642 assert length(follower.following) == 4
643
644 {:ok, follower} = User.remove_duplicated_following(follower)
645 assert length(follower.following) == 2
646 end
647
648 test "it does nothing when following is uniq" do
649 user = insert(:user)
650 follower = insert(:user)
651
652 {:ok, follower} = User.follow(follower, user)
653 assert length(follower.following) == 2
654
655 {:ok, follower} = User.remove_duplicated_following(follower)
656 assert length(follower.following) == 2
657 end
658 end
659
660 describe "follow_import" do
661 test "it imports user followings from list" do
662 [user1, user2, user3] = insert_list(3, :user)
663
664 identifiers = [
665 user2.ap_id,
666 user3.nickname
667 ]
668
669 result = User.follow_import(user1, identifiers)
670 assert is_list(result)
671 assert result == [user2, user3]
672 end
673 end
674
675 describe "mutes" do
676 test "it mutes people" do
677 user = insert(:user)
678 muted_user = insert(:user)
679
680 refute User.mutes?(user, muted_user)
681
682 {:ok, user} = User.mute(user, muted_user)
683
684 assert User.mutes?(user, muted_user)
685 end
686
687 test "it unmutes users" do
688 user = insert(:user)
689 muted_user = insert(:user)
690
691 {:ok, user} = User.mute(user, muted_user)
692 {:ok, user} = User.unmute(user, muted_user)
693
694 refute User.mutes?(user, muted_user)
695 end
696 end
697
698 describe "blocks" do
699 test "it blocks people" do
700 user = insert(:user)
701 blocked_user = insert(:user)
702
703 refute User.blocks?(user, blocked_user)
704
705 {:ok, user} = User.block(user, blocked_user)
706
707 assert User.blocks?(user, blocked_user)
708 end
709
710 test "it unblocks users" do
711 user = insert(:user)
712 blocked_user = insert(:user)
713
714 {:ok, user} = User.block(user, blocked_user)
715 {:ok, user} = User.unblock(user, blocked_user)
716
717 refute User.blocks?(user, blocked_user)
718 end
719
720 test "blocks tear down cyclical follow relationships" do
721 blocker = insert(:user)
722 blocked = insert(:user)
723
724 {:ok, blocker} = User.follow(blocker, blocked)
725 {:ok, blocked} = User.follow(blocked, blocker)
726
727 assert User.following?(blocker, blocked)
728 assert User.following?(blocked, blocker)
729
730 {:ok, blocker} = User.block(blocker, blocked)
731 blocked = User.get_cached_by_id(blocked.id)
732
733 assert User.blocks?(blocker, blocked)
734
735 refute User.following?(blocker, blocked)
736 refute User.following?(blocked, blocker)
737 end
738
739 test "blocks tear down blocker->blocked follow relationships" do
740 blocker = insert(:user)
741 blocked = insert(:user)
742
743 {:ok, blocker} = User.follow(blocker, blocked)
744
745 assert User.following?(blocker, blocked)
746 refute User.following?(blocked, blocker)
747
748 {:ok, blocker} = User.block(blocker, blocked)
749 blocked = User.get_cached_by_id(blocked.id)
750
751 assert User.blocks?(blocker, blocked)
752
753 refute User.following?(blocker, blocked)
754 refute User.following?(blocked, blocker)
755 end
756
757 test "blocks tear down blocked->blocker follow relationships" do
758 blocker = insert(:user)
759 blocked = insert(:user)
760
761 {:ok, blocked} = User.follow(blocked, blocker)
762
763 refute User.following?(blocker, blocked)
764 assert User.following?(blocked, blocker)
765
766 {:ok, blocker} = User.block(blocker, blocked)
767 blocked = User.get_cached_by_id(blocked.id)
768
769 assert User.blocks?(blocker, blocked)
770
771 refute User.following?(blocker, blocked)
772 refute User.following?(blocked, blocker)
773 end
774
775 test "blocks tear down blocked->blocker subscription relationships" do
776 blocker = insert(:user)
777 blocked = insert(:user)
778
779 {:ok, blocker} = User.subscribe(blocked, blocker)
780
781 assert User.subscribed_to?(blocked, blocker)
782 refute User.subscribed_to?(blocker, blocked)
783
784 {:ok, blocker} = User.block(blocker, blocked)
785
786 assert User.blocks?(blocker, blocked)
787 refute User.subscribed_to?(blocker, blocked)
788 refute User.subscribed_to?(blocked, blocker)
789 end
790 end
791
792 describe "domain blocking" do
793 test "blocks domains" do
794 user = insert(:user)
795 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
796
797 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
798
799 assert User.blocks?(user, collateral_user)
800 end
801
802 test "unblocks domains" do
803 user = insert(:user)
804 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
805
806 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
807 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
808
809 refute User.blocks?(user, collateral_user)
810 end
811 end
812
813 describe "blocks_import" do
814 test "it imports user blocks from list" do
815 [user1, user2, user3] = insert_list(3, :user)
816
817 identifiers = [
818 user2.ap_id,
819 user3.nickname
820 ]
821
822 result = User.blocks_import(user1, identifiers)
823 assert is_list(result)
824 assert result == [user2, user3]
825 end
826 end
827
828 test "get recipients from activity" do
829 actor = insert(:user)
830 user = insert(:user, local: true)
831 user_two = insert(:user, local: false)
832 addressed = insert(:user, local: true)
833 addressed_remote = insert(:user, local: false)
834
835 {:ok, activity} =
836 CommonAPI.post(actor, %{
837 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
838 })
839
840 assert Enum.map([actor, addressed], & &1.ap_id) --
841 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
842
843 {:ok, user} = User.follow(user, actor)
844 {:ok, _user_two} = User.follow(user_two, actor)
845 recipients = User.get_recipients_from_activity(activity)
846 assert length(recipients) == 3
847 assert user in recipients
848 assert addressed in recipients
849 end
850
851 describe ".deactivate" do
852 test "can de-activate then re-activate a user" do
853 user = insert(:user)
854 assert false == user.info.deactivated
855 {:ok, user} = User.deactivate(user)
856 assert true == user.info.deactivated
857 {:ok, user} = User.deactivate(user, false)
858 assert false == user.info.deactivated
859 end
860
861 test "hide a user from followers " do
862 user = insert(:user)
863 user2 = insert(:user)
864
865 {:ok, user} = User.follow(user, user2)
866 {:ok, _user} = User.deactivate(user)
867
868 info = User.get_cached_user_info(user2)
869
870 assert info.follower_count == 0
871 assert {:ok, []} = User.get_followers(user2)
872 end
873
874 test "hide a user from friends" do
875 user = insert(:user)
876 user2 = insert(:user)
877
878 {:ok, user2} = User.follow(user2, user)
879 assert User.following_count(user2) == 1
880
881 {:ok, _user} = User.deactivate(user)
882
883 info = User.get_cached_user_info(user2)
884
885 assert info.following_count == 0
886 assert User.following_count(user2) == 0
887 assert {:ok, []} = User.get_friends(user2)
888 end
889
890 test "hide a user's statuses from timelines and notifications" do
891 user = insert(:user)
892 user2 = insert(:user)
893
894 {:ok, user2} = User.follow(user2, user)
895
896 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
897
898 activity = Repo.preload(activity, :bookmark)
899
900 [notification] = Pleroma.Notification.for_user(user2)
901 assert notification.activity.id == activity.id
902
903 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
904
905 assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
906 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
907
908 {:ok, _user} = User.deactivate(user)
909
910 assert [] == ActivityPub.fetch_public_activities(%{})
911 assert [] == Pleroma.Notification.for_user(user2)
912
913 assert [] ==
914 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
915 end
916 end
917
918 test ".delete_user_activities deletes all create activities" do
919 user = insert(:user)
920
921 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
922
923 Ecto.Adapters.SQL.Sandbox.unboxed_run(Repo, fn ->
924 {:ok, _} = User.delete_user_activities(user)
925 # TODO: Remove favorites, repeats, delete activities.
926 refute Activity.get_by_id(activity.id)
927 end)
928 end
929
930 test ".delete deactivates a user, all follow relationships and all create activities" do
931 user = insert(:user)
932 followed = insert(:user)
933 follower = insert(:user)
934
935 {:ok, user} = User.follow(user, followed)
936 {:ok, follower} = User.follow(follower, user)
937
938 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
939 {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
940
941 {:ok, _, _} = CommonAPI.favorite(activity_two.id, user)
942 {:ok, _, _} = CommonAPI.favorite(activity.id, follower)
943 {:ok, _, _} = CommonAPI.repeat(activity.id, follower)
944
945 {:ok, _} = User.delete(user)
946
947 followed = User.get_cached_by_id(followed.id)
948 follower = User.get_cached_by_id(follower.id)
949 user = User.get_cached_by_id(user.id)
950
951 assert user.info.deactivated
952
953 refute User.following?(user, followed)
954 refute User.following?(followed, follower)
955
956 # TODO: Remove favorites, repeats, delete activities.
957
958 refute Activity.get_by_id(activity.id)
959 end
960
961 test "get_public_key_for_ap_id fetches a user that's not in the db" do
962 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
963 end
964
965 test "insert or update a user from given data" do
966 user = insert(:user, %{nickname: "nick@name.de"})
967 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
968
969 assert {:ok, %User{}} = User.insert_or_update_user(data)
970 end
971
972 describe "per-user rich-text filtering" do
973 test "html_filter_policy returns default policies, when rich-text is enabled" do
974 user = insert(:user)
975
976 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
977 end
978
979 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
980 user = insert(:user, %{info: %{no_rich_text: true}})
981
982 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
983 end
984 end
985
986 describe "caching" do
987 test "invalidate_cache works" do
988 user = insert(:user)
989 _user_info = User.get_cached_user_info(user)
990
991 User.invalidate_cache(user)
992
993 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
994 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
995 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
996 end
997
998 test "User.delete() plugs any possible zombie objects" do
999 user = insert(:user)
1000
1001 {:ok, _} = User.delete(user)
1002
1003 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
1004
1005 assert cached_user != user
1006
1007 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
1008
1009 assert cached_user != user
1010 end
1011 end
1012
1013 describe "User.search" do
1014 test "finds a user by full or partial nickname" do
1015 user = insert(:user, %{nickname: "john"})
1016
1017 Enum.each(["john", "jo", "j"], fn query ->
1018 assert user ==
1019 User.search(query)
1020 |> List.first()
1021 |> Map.put(:search_rank, nil)
1022 |> Map.put(:search_type, nil)
1023 end)
1024 end
1025
1026 test "finds a user by full or partial name" do
1027 user = insert(:user, %{name: "John Doe"})
1028
1029 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
1030 assert user ==
1031 User.search(query)
1032 |> List.first()
1033 |> Map.put(:search_rank, nil)
1034 |> Map.put(:search_type, nil)
1035 end)
1036 end
1037
1038 test "finds users, preferring nickname matches over name matches" do
1039 u1 = insert(:user, %{name: "lain", nickname: "nick1"})
1040 u2 = insert(:user, %{nickname: "lain", name: "nick1"})
1041
1042 assert [u2.id, u1.id] == Enum.map(User.search("lain"), & &1.id)
1043 end
1044
1045 test "finds users, considering density of matched tokens" do
1046 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
1047 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
1048
1049 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
1050 end
1051
1052 test "finds users, ranking by similarity" do
1053 u1 = insert(:user, %{name: "lain"})
1054 _u2 = insert(:user, %{name: "ean"})
1055 u3 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
1056 u4 = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
1057
1058 assert [u4.id, u3.id, u1.id] == Enum.map(User.search("lain@ple", for_user: u1), & &1.id)
1059 end
1060
1061 test "finds users, handling misspelled requests" do
1062 u1 = insert(:user, %{name: "lain"})
1063
1064 assert [u1.id] == Enum.map(User.search("laiin"), & &1.id)
1065 end
1066
1067 test "finds users, boosting ranks of friends and followers" do
1068 u1 = insert(:user)
1069 u2 = insert(:user, %{name: "Doe"})
1070 follower = insert(:user, %{name: "Doe"})
1071 friend = insert(:user, %{name: "Doe"})
1072
1073 {:ok, follower} = User.follow(follower, u1)
1074 {:ok, u1} = User.follow(u1, friend)
1075
1076 assert [friend.id, follower.id, u2.id] --
1077 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
1078 end
1079
1080 test "find local and remote statuses for authenticated users" do
1081 u1 = insert(:user, %{name: "lain"})
1082 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
1083 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
1084
1085 results =
1086 "lain"
1087 |> User.search(for_user: u1)
1088 |> Enum.map(& &1.id)
1089 |> Enum.sort()
1090
1091 assert [u1.id, u2.id, u3.id] == results
1092 end
1093
1094 test "find only local statuses for unauthenticated users" do
1095 %{id: id} = insert(:user, %{name: "lain"})
1096 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
1097 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
1098
1099 assert [%{id: ^id}] = User.search("lain")
1100 end
1101
1102 test "finds a user whose name is nil" do
1103 _user = insert(:user, %{name: "notamatch", nickname: "testuser@pleroma.amplifie.red"})
1104 user_two = insert(:user, %{name: nil, nickname: "lain@pleroma.soykaf.com"})
1105
1106 assert user_two ==
1107 User.search("lain@pleroma.soykaf.com")
1108 |> List.first()
1109 |> Map.put(:search_rank, nil)
1110 |> Map.put(:search_type, nil)
1111 end
1112
1113 test "does not yield false-positive matches" do
1114 insert(:user, %{name: "John Doe"})
1115
1116 Enum.each(["mary", "a", ""], fn query ->
1117 assert [] == User.search(query)
1118 end)
1119 end
1120
1121 test "works with URIs" do
1122 user = insert(:user)
1123
1124 results =
1125 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
1126
1127 result = results |> List.first()
1128
1129 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
1130
1131 assert length(results) == 1
1132 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
1133 end
1134 end
1135
1136 test "auth_active?/1 works correctly" do
1137 Pleroma.Config.put([:instance, :account_activation_required], true)
1138
1139 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
1140 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
1141 remote_user = insert(:user, local: false)
1142
1143 refute User.auth_active?(local_user)
1144 assert User.auth_active?(confirmed_user)
1145 assert User.auth_active?(remote_user)
1146
1147 Pleroma.Config.put([:instance, :account_activation_required], false)
1148 end
1149
1150 describe "superuser?/1" do
1151 test "returns false for unprivileged users" do
1152 user = insert(:user, local: true)
1153
1154 refute User.superuser?(user)
1155 end
1156
1157 test "returns false for remote users" do
1158 user = insert(:user, local: false)
1159 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1160
1161 refute User.superuser?(user)
1162 refute User.superuser?(remote_admin_user)
1163 end
1164
1165 test "returns true for local moderators" do
1166 user = insert(:user, local: true, info: %{is_moderator: true})
1167
1168 assert User.superuser?(user)
1169 end
1170
1171 test "returns true for local admins" do
1172 user = insert(:user, local: true, info: %{is_admin: true})
1173
1174 assert User.superuser?(user)
1175 end
1176 end
1177
1178 describe "visible_for?/2" do
1179 test "returns true when the account is itself" do
1180 user = insert(:user, local: true)
1181
1182 assert User.visible_for?(user, user)
1183 end
1184
1185 test "returns false when the account is unauthenticated and auth is required" do
1186 Pleroma.Config.put([:instance, :account_activation_required], true)
1187
1188 user = insert(:user, local: true, info: %{confirmation_pending: true})
1189 other_user = insert(:user, local: true)
1190
1191 refute User.visible_for?(user, other_user)
1192
1193 Pleroma.Config.put([:instance, :account_activation_required], false)
1194 end
1195
1196 test "returns true when the account is unauthenticated and auth is not required" do
1197 user = insert(:user, local: true, info: %{confirmation_pending: true})
1198 other_user = insert(:user, local: true)
1199
1200 assert User.visible_for?(user, other_user)
1201 end
1202
1203 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1204 Pleroma.Config.put([:instance, :account_activation_required], true)
1205
1206 user = insert(:user, local: true, info: %{confirmation_pending: true})
1207 other_user = insert(:user, local: true, info: %{is_admin: true})
1208
1209 assert User.visible_for?(user, other_user)
1210
1211 Pleroma.Config.put([:instance, :account_activation_required], false)
1212 end
1213 end
1214
1215 describe "parse_bio/2" do
1216 test "preserves hosts in user links text" do
1217 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1218 user = insert(:user)
1219 bio = "A.k.a. @nick@domain.com"
1220
1221 expected_text =
1222 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1223 remote_user.ap_id
1224 }'>@<span>nick@domain.com</span></a></span>"
1225
1226 assert expected_text == User.parse_bio(bio, user)
1227 end
1228
1229 test "Adds rel=me on linkbacked urls" do
1230 user = insert(:user, ap_id: "http://social.example.org/users/lain")
1231
1232 bio = "http://example.org/rel_me/null"
1233 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1234 assert expected_text == User.parse_bio(bio, user)
1235
1236 bio = "http://example.org/rel_me/link"
1237 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1238 assert expected_text == User.parse_bio(bio, user)
1239
1240 bio = "http://example.org/rel_me/anchor"
1241 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1242 assert expected_text == User.parse_bio(bio, user)
1243 end
1244 end
1245
1246 test "follower count is updated when a follower is blocked" do
1247 user = insert(:user)
1248 follower = insert(:user)
1249 follower2 = insert(:user)
1250 follower3 = insert(:user)
1251
1252 {:ok, follower} = User.follow(follower, user)
1253 {:ok, _follower2} = User.follow(follower2, user)
1254 {:ok, _follower3} = User.follow(follower3, user)
1255
1256 {:ok, _} = User.block(user, follower)
1257
1258 user_show = Pleroma.Web.TwitterAPI.UserView.render("show.json", %{user: user})
1259
1260 assert Map.get(user_show, "followers_count") == 2
1261 end
1262
1263 describe "toggle_confirmation/1" do
1264 test "if user is confirmed" do
1265 user = insert(:user, info: %{confirmation_pending: false})
1266 {:ok, user} = User.toggle_confirmation(user)
1267
1268 assert user.info.confirmation_pending
1269 assert user.info.confirmation_token
1270 end
1271
1272 test "if user is unconfirmed" do
1273 user = insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
1274 {:ok, user} = User.toggle_confirmation(user)
1275
1276 refute user.info.confirmation_pending
1277 refute user.info.confirmation_token
1278 end
1279 end
1280
1281 describe "ensure_keys_present" do
1282 test "it creates keys for a user and stores them in info" do
1283 user = insert(:user)
1284 refute is_binary(user.info.keys)
1285 {:ok, user} = User.ensure_keys_present(user)
1286 assert is_binary(user.info.keys)
1287 end
1288
1289 test "it doesn't create keys if there already are some" do
1290 user = insert(:user, %{info: %{keys: "xxx"}})
1291 {:ok, user} = User.ensure_keys_present(user)
1292 assert user.info.keys == "xxx"
1293 end
1294 end
1295
1296 describe "get_ap_ids_by_nicknames" do
1297 test "it returns a list of AP ids for a given set of nicknames" do
1298 user = insert(:user)
1299 user_two = insert(:user)
1300
1301 ap_ids = User.get_ap_ids_by_nicknames([user.nickname, user_two.nickname, "nonexistent"])
1302 assert length(ap_ids) == 2
1303 assert user.ap_id in ap_ids
1304 assert user_two.ap_id in ap_ids
1305 end
1306 end
1307 end