Feature/896 toggling confirmation
[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.maybe_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([Pleroma.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 "follow_import" do
630 test "it imports user followings from list" do
631 [user1, user2, user3] = insert_list(3, :user)
632
633 identifiers = [
634 user2.ap_id,
635 user3.nickname
636 ]
637
638 result = User.follow_import(user1, identifiers)
639 assert is_list(result)
640 assert result == [user2, user3]
641 end
642 end
643
644 describe "mutes" do
645 test "it mutes people" do
646 user = insert(:user)
647 muted_user = insert(:user)
648
649 refute User.mutes?(user, muted_user)
650
651 {:ok, user} = User.mute(user, muted_user)
652
653 assert User.mutes?(user, muted_user)
654 end
655
656 test "it unmutes users" do
657 user = insert(:user)
658 muted_user = insert(:user)
659
660 {:ok, user} = User.mute(user, muted_user)
661 {:ok, user} = User.unmute(user, muted_user)
662
663 refute User.mutes?(user, muted_user)
664 end
665 end
666
667 describe "blocks" do
668 test "it blocks people" do
669 user = insert(:user)
670 blocked_user = insert(:user)
671
672 refute User.blocks?(user, blocked_user)
673
674 {:ok, user} = User.block(user, blocked_user)
675
676 assert User.blocks?(user, blocked_user)
677 end
678
679 test "it unblocks users" do
680 user = insert(:user)
681 blocked_user = insert(:user)
682
683 {:ok, user} = User.block(user, blocked_user)
684 {:ok, user} = User.unblock(user, blocked_user)
685
686 refute User.blocks?(user, blocked_user)
687 end
688
689 test "blocks tear down cyclical follow relationships" do
690 blocker = insert(:user)
691 blocked = insert(:user)
692
693 {:ok, blocker} = User.follow(blocker, blocked)
694 {:ok, blocked} = User.follow(blocked, blocker)
695
696 assert User.following?(blocker, blocked)
697 assert User.following?(blocked, blocker)
698
699 {:ok, blocker} = User.block(blocker, blocked)
700 blocked = User.get_cached_by_id(blocked.id)
701
702 assert User.blocks?(blocker, blocked)
703
704 refute User.following?(blocker, blocked)
705 refute User.following?(blocked, blocker)
706 end
707
708 test "blocks tear down blocker->blocked follow relationships" do
709 blocker = insert(:user)
710 blocked = insert(:user)
711
712 {:ok, blocker} = User.follow(blocker, blocked)
713
714 assert User.following?(blocker, blocked)
715 refute User.following?(blocked, blocker)
716
717 {:ok, blocker} = User.block(blocker, blocked)
718 blocked = User.get_cached_by_id(blocked.id)
719
720 assert User.blocks?(blocker, blocked)
721
722 refute User.following?(blocker, blocked)
723 refute User.following?(blocked, blocker)
724 end
725
726 test "blocks tear down blocked->blocker follow relationships" do
727 blocker = insert(:user)
728 blocked = insert(:user)
729
730 {:ok, blocked} = User.follow(blocked, blocker)
731
732 refute User.following?(blocker, blocked)
733 assert User.following?(blocked, blocker)
734
735 {:ok, blocker} = User.block(blocker, blocked)
736 blocked = User.get_cached_by_id(blocked.id)
737
738 assert User.blocks?(blocker, blocked)
739
740 refute User.following?(blocker, blocked)
741 refute User.following?(blocked, blocker)
742 end
743
744 test "blocks tear down blocked->blocker subscription relationships" do
745 blocker = insert(:user)
746 blocked = insert(:user)
747
748 {:ok, blocker} = User.subscribe(blocked, blocker)
749
750 assert User.subscribed_to?(blocked, blocker)
751 refute User.subscribed_to?(blocker, blocked)
752
753 {:ok, blocker} = User.block(blocker, blocked)
754
755 assert User.blocks?(blocker, blocked)
756 refute User.subscribed_to?(blocker, blocked)
757 refute User.subscribed_to?(blocked, blocker)
758 end
759 end
760
761 describe "domain blocking" do
762 test "blocks domains" do
763 user = insert(:user)
764 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
765
766 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
767
768 assert User.blocks?(user, collateral_user)
769 end
770
771 test "unblocks domains" do
772 user = insert(:user)
773 collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
774
775 {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
776 {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
777
778 refute User.blocks?(user, collateral_user)
779 end
780 end
781
782 describe "blocks_import" do
783 test "it imports user blocks from list" do
784 [user1, user2, user3] = insert_list(3, :user)
785
786 identifiers = [
787 user2.ap_id,
788 user3.nickname
789 ]
790
791 result = User.blocks_import(user1, identifiers)
792 assert is_list(result)
793 assert result == [user2, user3]
794 end
795 end
796
797 test "get recipients from activity" do
798 actor = insert(:user)
799 user = insert(:user, local: true)
800 user_two = insert(:user, local: false)
801 addressed = insert(:user, local: true)
802 addressed_remote = insert(:user, local: false)
803
804 {:ok, activity} =
805 CommonAPI.post(actor, %{
806 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
807 })
808
809 assert Enum.map([actor, addressed], & &1.ap_id) --
810 Enum.map(User.get_recipients_from_activity(activity), & &1.ap_id) == []
811
812 {:ok, user} = User.follow(user, actor)
813 {:ok, _user_two} = User.follow(user_two, actor)
814 recipients = User.get_recipients_from_activity(activity)
815 assert length(recipients) == 3
816 assert user in recipients
817 assert addressed in recipients
818 end
819
820 describe ".deactivate" do
821 test "can de-activate then re-activate a user" do
822 user = insert(:user)
823 assert false == user.info.deactivated
824 {:ok, user} = User.deactivate(user)
825 assert true == user.info.deactivated
826 {:ok, user} = User.deactivate(user, false)
827 assert false == user.info.deactivated
828 end
829
830 test "hide a user from followers " do
831 user = insert(:user)
832 user2 = insert(:user)
833
834 {:ok, user} = User.follow(user, user2)
835 {:ok, _user} = User.deactivate(user)
836
837 info = User.get_cached_user_info(user2)
838
839 assert info.follower_count == 0
840 assert {:ok, []} = User.get_followers(user2)
841 end
842
843 test "hide a user from friends" do
844 user = insert(:user)
845 user2 = insert(:user)
846
847 {:ok, user2} = User.follow(user2, user)
848 assert User.following_count(user2) == 1
849
850 {:ok, _user} = User.deactivate(user)
851
852 info = User.get_cached_user_info(user2)
853
854 assert info.following_count == 0
855 assert User.following_count(user2) == 0
856 assert {:ok, []} = User.get_friends(user2)
857 end
858
859 test "hide a user's statuses from timelines and notifications" do
860 user = insert(:user)
861 user2 = insert(:user)
862
863 {:ok, user2} = User.follow(user2, user)
864
865 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
866
867 activity = Repo.preload(activity, :bookmark)
868
869 [notification] = Pleroma.Notification.for_user(user2)
870 assert notification.activity.id == activity.id
871
872 assert [activity] == ActivityPub.fetch_public_activities(%{}) |> Repo.preload(:bookmark)
873
874 assert [activity] ==
875 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
876 |> ActivityPub.contain_timeline(user2)
877
878 {:ok, _user} = User.deactivate(user)
879
880 assert [] == ActivityPub.fetch_public_activities(%{})
881 assert [] == Pleroma.Notification.for_user(user2)
882
883 assert [] ==
884 ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
885 |> ActivityPub.contain_timeline(user2)
886 end
887 end
888
889 test ".delete_user_activities deletes all create activities" do
890 user = insert(:user)
891
892 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
893
894 Ecto.Adapters.SQL.Sandbox.unboxed_run(Repo, fn ->
895 {:ok, _} = User.delete_user_activities(user)
896 # TODO: Remove favorites, repeats, delete activities.
897 refute Activity.get_by_id(activity.id)
898 end)
899 end
900
901 test ".delete deactivates a user, all follow relationships and all create activities" do
902 user = insert(:user)
903 followed = insert(:user)
904 follower = insert(:user)
905
906 {:ok, user} = User.follow(user, followed)
907 {:ok, follower} = User.follow(follower, user)
908
909 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
910 {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
911
912 {:ok, _, _} = CommonAPI.favorite(activity_two.id, user)
913 {:ok, _, _} = CommonAPI.favorite(activity.id, follower)
914 {:ok, _, _} = CommonAPI.repeat(activity.id, follower)
915
916 {:ok, _} = User.delete(user)
917
918 followed = User.get_cached_by_id(followed.id)
919 follower = User.get_cached_by_id(follower.id)
920 user = User.get_cached_by_id(user.id)
921
922 assert user.info.deactivated
923
924 refute User.following?(user, followed)
925 refute User.following?(followed, follower)
926
927 # TODO: Remove favorites, repeats, delete activities.
928
929 refute Activity.get_by_id(activity.id)
930 end
931
932 test "get_public_key_for_ap_id fetches a user that's not in the db" do
933 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
934 end
935
936 test "insert or update a user from given data" do
937 user = insert(:user, %{nickname: "nick@name.de"})
938 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
939
940 assert {:ok, %User{}} = User.insert_or_update_user(data)
941 end
942
943 describe "per-user rich-text filtering" do
944 test "html_filter_policy returns default policies, when rich-text is enabled" do
945 user = insert(:user)
946
947 assert Pleroma.Config.get([:markup, :scrub_policy]) == User.html_filter_policy(user)
948 end
949
950 test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
951 user = insert(:user, %{info: %{no_rich_text: true}})
952
953 assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
954 end
955 end
956
957 describe "caching" do
958 test "invalidate_cache works" do
959 user = insert(:user)
960 _user_info = User.get_cached_user_info(user)
961
962 User.invalidate_cache(user)
963
964 {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
965 {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
966 {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
967 end
968
969 test "User.delete() plugs any possible zombie objects" do
970 user = insert(:user)
971
972 {:ok, _} = User.delete(user)
973
974 {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
975
976 assert cached_user != user
977
978 {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
979
980 assert cached_user != user
981 end
982 end
983
984 describe "User.search" do
985 test "finds a user by full or partial nickname" do
986 user = insert(:user, %{nickname: "john"})
987
988 Enum.each(["john", "jo", "j"], fn query ->
989 assert user ==
990 User.search(query)
991 |> List.first()
992 |> Map.put(:search_rank, nil)
993 |> Map.put(:search_type, nil)
994 end)
995 end
996
997 test "finds a user by full or partial name" do
998 user = insert(:user, %{name: "John Doe"})
999
1000 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
1001 assert user ==
1002 User.search(query)
1003 |> List.first()
1004 |> Map.put(:search_rank, nil)
1005 |> Map.put(:search_type, nil)
1006 end)
1007 end
1008
1009 test "finds users, preferring nickname matches over name matches" do
1010 u1 = insert(:user, %{name: "lain", nickname: "nick1"})
1011 u2 = insert(:user, %{nickname: "lain", name: "nick1"})
1012
1013 assert [u2.id, u1.id] == Enum.map(User.search("lain"), & &1.id)
1014 end
1015
1016 test "finds users, considering density of matched tokens" do
1017 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
1018 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
1019
1020 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
1021 end
1022
1023 test "finds users, ranking by similarity" do
1024 u1 = insert(:user, %{name: "lain"})
1025 _u2 = insert(:user, %{name: "ean"})
1026 u3 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
1027 u4 = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
1028
1029 assert [u4.id, u3.id, u1.id] == Enum.map(User.search("lain@ple"), & &1.id)
1030 end
1031
1032 test "finds users, handling misspelled requests" do
1033 u1 = insert(:user, %{name: "lain"})
1034
1035 assert [u1.id] == Enum.map(User.search("laiin"), & &1.id)
1036 end
1037
1038 test "finds users, boosting ranks of friends and followers" do
1039 u1 = insert(:user)
1040 u2 = insert(:user, %{name: "Doe"})
1041 follower = insert(:user, %{name: "Doe"})
1042 friend = insert(:user, %{name: "Doe"})
1043
1044 {:ok, follower} = User.follow(follower, u1)
1045 {:ok, u1} = User.follow(u1, friend)
1046
1047 assert [friend.id, follower.id, u2.id] --
1048 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
1049 end
1050
1051 test "finds a user whose name is nil" do
1052 _user = insert(:user, %{name: "notamatch", nickname: "testuser@pleroma.amplifie.red"})
1053 user_two = insert(:user, %{name: nil, nickname: "lain@pleroma.soykaf.com"})
1054
1055 assert user_two ==
1056 User.search("lain@pleroma.soykaf.com")
1057 |> List.first()
1058 |> Map.put(:search_rank, nil)
1059 |> Map.put(:search_type, nil)
1060 end
1061
1062 test "does not yield false-positive matches" do
1063 insert(:user, %{name: "John Doe"})
1064
1065 Enum.each(["mary", "a", ""], fn query ->
1066 assert [] == User.search(query)
1067 end)
1068 end
1069
1070 test "works with URIs" do
1071 results = User.search("http://mastodon.example.org/users/admin", resolve: true)
1072 result = results |> List.first()
1073
1074 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
1075
1076 assert length(results) == 1
1077 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
1078 end
1079 end
1080
1081 test "auth_active?/1 works correctly" do
1082 Pleroma.Config.put([:instance, :account_activation_required], true)
1083
1084 local_user = insert(:user, local: true, info: %{confirmation_pending: true})
1085 confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
1086 remote_user = insert(:user, local: false)
1087
1088 refute User.auth_active?(local_user)
1089 assert User.auth_active?(confirmed_user)
1090 assert User.auth_active?(remote_user)
1091
1092 Pleroma.Config.put([:instance, :account_activation_required], false)
1093 end
1094
1095 describe "superuser?/1" do
1096 test "returns false for unprivileged users" do
1097 user = insert(:user, local: true)
1098
1099 refute User.superuser?(user)
1100 end
1101
1102 test "returns false for remote users" do
1103 user = insert(:user, local: false)
1104 remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
1105
1106 refute User.superuser?(user)
1107 refute User.superuser?(remote_admin_user)
1108 end
1109
1110 test "returns true for local moderators" do
1111 user = insert(:user, local: true, info: %{is_moderator: true})
1112
1113 assert User.superuser?(user)
1114 end
1115
1116 test "returns true for local admins" do
1117 user = insert(:user, local: true, info: %{is_admin: true})
1118
1119 assert User.superuser?(user)
1120 end
1121 end
1122
1123 describe "visible_for?/2" do
1124 test "returns true when the account is itself" do
1125 user = insert(:user, local: true)
1126
1127 assert User.visible_for?(user, user)
1128 end
1129
1130 test "returns false when the account is unauthenticated and auth is required" do
1131 Pleroma.Config.put([:instance, :account_activation_required], true)
1132
1133 user = insert(:user, local: true, info: %{confirmation_pending: true})
1134 other_user = insert(:user, local: true)
1135
1136 refute User.visible_for?(user, other_user)
1137
1138 Pleroma.Config.put([:instance, :account_activation_required], false)
1139 end
1140
1141 test "returns true when the account is unauthenticated and auth is not required" do
1142 user = insert(:user, local: true, info: %{confirmation_pending: true})
1143 other_user = insert(:user, local: true)
1144
1145 assert User.visible_for?(user, other_user)
1146 end
1147
1148 test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
1149 Pleroma.Config.put([:instance, :account_activation_required], true)
1150
1151 user = insert(:user, local: true, info: %{confirmation_pending: true})
1152 other_user = insert(:user, local: true, info: %{is_admin: true})
1153
1154 assert User.visible_for?(user, other_user)
1155
1156 Pleroma.Config.put([:instance, :account_activation_required], false)
1157 end
1158 end
1159
1160 describe "parse_bio/2" do
1161 test "preserves hosts in user links text" do
1162 remote_user = insert(:user, local: false, nickname: "nick@domain.com")
1163 user = insert(:user)
1164 bio = "A.k.a. @nick@domain.com"
1165
1166 expected_text =
1167 "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
1168 remote_user.ap_id
1169 }'>@<span>nick@domain.com</span></a></span>"
1170
1171 assert expected_text == User.parse_bio(bio, user)
1172 end
1173
1174 test "Adds rel=me on linkbacked urls" do
1175 user = insert(:user, ap_id: "http://social.example.org/users/lain")
1176
1177 bio = "http://example.org/rel_me/null"
1178 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1179 assert expected_text == User.parse_bio(bio, user)
1180
1181 bio = "http://example.org/rel_me/link"
1182 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1183 assert expected_text == User.parse_bio(bio, user)
1184
1185 bio = "http://example.org/rel_me/anchor"
1186 expected_text = "<a href=\"#{bio}\">#{bio}</a>"
1187 assert expected_text == User.parse_bio(bio, user)
1188 end
1189 end
1190
1191 test "follower count is updated when a follower is blocked" do
1192 user = insert(:user)
1193 follower = insert(:user)
1194 follower2 = insert(:user)
1195 follower3 = insert(:user)
1196
1197 {:ok, follower} = Pleroma.User.follow(follower, user)
1198 {:ok, _follower2} = Pleroma.User.follow(follower2, user)
1199 {:ok, _follower3} = Pleroma.User.follow(follower3, user)
1200
1201 {:ok, _} = Pleroma.User.block(user, follower)
1202
1203 user_show = Pleroma.Web.TwitterAPI.UserView.render("show.json", %{user: user})
1204
1205 assert Map.get(user_show, "followers_count") == 2
1206 end
1207
1208 describe "toggle_confirmation/1" do
1209 test "if user is confirmed" do
1210 user = insert(:user, info: %{confirmation_pending: false})
1211 {:ok, user} = User.toggle_confirmation(user)
1212
1213 assert user.info.confirmation_pending
1214 assert user.info.confirmation_token
1215 end
1216
1217 test "if user is unconfirmed" do
1218 user = insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
1219 {:ok, user} = User.toggle_confirmation(user)
1220
1221 refute user.info.confirmation_pending
1222 refute user.info.confirmation_token
1223 end
1224 end
1225 end