use correct versions for oban migrations
[akkoma] / test / mix / tasks / pleroma / user_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.UserTest do
6 alias Pleroma.Activity
7 alias Pleroma.MFA
8 alias Pleroma.Object
9 alias Pleroma.Repo
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.OAuth.Authorization
14 alias Pleroma.Web.OAuth.Token
15
16 use Pleroma.DataCase
17 use Oban.Testing, repo: Pleroma.Repo
18
19 import ExUnit.CaptureIO
20 import Mock
21 import Pleroma.Factory
22
23 setup_all do
24 Mix.shell(Mix.Shell.Process)
25
26 on_exit(fn ->
27 Mix.shell(Mix.Shell.IO)
28 end)
29
30 :ok
31 end
32
33 describe "running new" do
34 test "user is created" do
35 # just get random data
36 unsaved = build(:user)
37
38 # prepare to answer yes
39 send(self(), {:mix_shell_input, :prompt, "Y"})
40
41 Mix.Tasks.Pleroma.User.run([
42 "new",
43 unsaved.nickname,
44 unsaved.email,
45 "--name",
46 unsaved.name,
47 "--bio",
48 unsaved.bio,
49 "--password",
50 "test",
51 "--moderator",
52 "--admin"
53 ])
54
55 assert_received {:mix_shell, :info, [message]}
56 assert message =~ "user will be created"
57
58 assert_received {:mix_shell, :prompt, [message]}
59 assert message =~ "Continue"
60
61 assert_received {:mix_shell, :info, [message]}
62 assert message =~ "created"
63
64 user = User.get_cached_by_nickname(unsaved.nickname)
65 assert user.name == unsaved.name
66 assert user.email == unsaved.email
67 assert user.bio == unsaved.bio
68 assert user.is_moderator
69 assert user.is_admin
70 end
71
72 test "user is not created" do
73 unsaved = build(:user)
74
75 # prepare to answer no
76 send(self(), {:mix_shell_input, :prompt, "N"})
77
78 Mix.Tasks.Pleroma.User.run(["new", unsaved.nickname, unsaved.email])
79
80 assert_received {:mix_shell, :info, [message]}
81 assert message =~ "user will be created"
82
83 assert_received {:mix_shell, :prompt, [message]}
84 assert message =~ "Continue"
85
86 assert_received {:mix_shell, :info, [message]}
87 assert message =~ "will not be created"
88
89 refute User.get_cached_by_nickname(unsaved.nickname)
90 end
91 end
92
93 describe "running rm" do
94 test "user is deleted" do
95 clear_config([:instance, :federating], true)
96 user = insert(:user)
97
98 with_mock Pleroma.Web.Federator,
99 publish: fn _ -> nil end do
100 Mix.Tasks.Pleroma.User.run(["rm", user.nickname])
101 ObanHelpers.perform_all()
102
103 assert_received {:mix_shell, :info, [message]}
104 assert message =~ " deleted"
105 assert %{deactivated: true} = User.get_by_nickname(user.nickname)
106
107 assert called(Pleroma.Web.Federator.publish(:_))
108 end
109 end
110
111 test "a remote user's create activity is deleted when the object has been pruned" do
112 user = insert(:user)
113 user2 = insert(:user)
114
115 {:ok, post} = CommonAPI.post(user, %{status: "uguu"})
116 {:ok, post2} = CommonAPI.post(user2, %{status: "test"})
117 obj = Object.normalize(post2, fetch: false)
118
119 {:ok, like_object, meta} = Pleroma.Web.ActivityPub.Builder.like(user, obj)
120
121 {:ok, like_activity, _meta} =
122 Pleroma.Web.ActivityPub.Pipeline.common_pipeline(
123 like_object,
124 Keyword.put(meta, :local, true)
125 )
126
127 like_activity.data["object"]
128 |> Pleroma.Object.get_by_ap_id()
129 |> Repo.delete()
130
131 clear_config([:instance, :federating], true)
132
133 object = Object.normalize(post, fetch: false)
134 Object.prune(object)
135
136 with_mock Pleroma.Web.Federator,
137 publish: fn _ -> nil end do
138 Mix.Tasks.Pleroma.User.run(["rm", user.nickname])
139 ObanHelpers.perform_all()
140
141 assert_received {:mix_shell, :info, [message]}
142 assert message =~ " deleted"
143 assert %{deactivated: true} = User.get_by_nickname(user.nickname)
144
145 assert called(Pleroma.Web.Federator.publish(:_))
146 refute Pleroma.Repo.get(Pleroma.Activity, like_activity.id)
147 end
148
149 refute Activity.get_by_id(post.id)
150 end
151
152 test "no user to delete" do
153 Mix.Tasks.Pleroma.User.run(["rm", "nonexistent"])
154
155 assert_received {:mix_shell, :error, [message]}
156 assert message =~ "No local user"
157 end
158 end
159
160 describe "running toggle_activated" do
161 test "user is deactivated" do
162 user = insert(:user)
163
164 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
165
166 assert_received {:mix_shell, :info, [message]}
167 assert message =~ " deactivated"
168
169 user = User.get_cached_by_nickname(user.nickname)
170 assert user.deactivated
171 end
172
173 test "user is activated" do
174 user = insert(:user, deactivated: true)
175
176 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
177
178 assert_received {:mix_shell, :info, [message]}
179 assert message =~ " activated"
180
181 user = User.get_cached_by_nickname(user.nickname)
182 refute user.deactivated
183 end
184
185 test "no user to toggle" do
186 Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistent"])
187
188 assert_received {:mix_shell, :error, [message]}
189 assert message =~ "No user"
190 end
191 end
192
193 describe "running deactivate" do
194 test "user is unsubscribed" do
195 followed = insert(:user)
196 remote_followed = insert(:user, local: false)
197 user = insert(:user)
198
199 User.follow(user, followed, :follow_accept)
200 User.follow(user, remote_followed, :follow_accept)
201
202 Mix.Tasks.Pleroma.User.run(["deactivate", user.nickname])
203
204 assert_received {:mix_shell, :info, [message]}
205 assert message =~ "Deactivating"
206
207 # Note that the task has delay :timer.sleep(500)
208 assert_received {:mix_shell, :info, [message]}
209 assert message =~ "Successfully unsubscribed"
210
211 user = User.get_cached_by_nickname(user.nickname)
212 assert Enum.empty?(Enum.filter(User.get_friends(user), & &1.local))
213 assert user.deactivated
214 end
215
216 test "no user to deactivate" do
217 Mix.Tasks.Pleroma.User.run(["deactivate", "nonexistent"])
218
219 assert_received {:mix_shell, :error, [message]}
220 assert message =~ "No user"
221 end
222 end
223
224 describe "running set" do
225 test "All statuses set" do
226 user = insert(:user)
227
228 Mix.Tasks.Pleroma.User.run([
229 "set",
230 user.nickname,
231 "--admin",
232 "--confirmed",
233 "--locked",
234 "--moderator"
235 ])
236
237 assert_received {:mix_shell, :info, [message]}
238 assert message =~ ~r/Admin status .* true/
239
240 assert_received {:mix_shell, :info, [message]}
241 assert message =~ ~r/Confirmation pending .* false/
242
243 assert_received {:mix_shell, :info, [message]}
244 assert message =~ ~r/Locked status .* true/
245
246 assert_received {:mix_shell, :info, [message]}
247 assert message =~ ~r/Moderator status .* true/
248
249 user = User.get_cached_by_nickname(user.nickname)
250 assert user.is_moderator
251 assert user.is_locked
252 assert user.is_admin
253 refute user.confirmation_pending
254 end
255
256 test "All statuses unset" do
257 user =
258 insert(:user,
259 is_locked: true,
260 is_moderator: true,
261 is_admin: true,
262 confirmation_pending: true
263 )
264
265 Mix.Tasks.Pleroma.User.run([
266 "set",
267 user.nickname,
268 "--no-admin",
269 "--no-confirmed",
270 "--no-locked",
271 "--no-moderator"
272 ])
273
274 assert_received {:mix_shell, :info, [message]}
275 assert message =~ ~r/Admin status .* false/
276
277 assert_received {:mix_shell, :info, [message]}
278 assert message =~ ~r/Confirmation pending .* true/
279
280 assert_received {:mix_shell, :info, [message]}
281 assert message =~ ~r/Locked status .* false/
282
283 assert_received {:mix_shell, :info, [message]}
284 assert message =~ ~r/Moderator status .* false/
285
286 user = User.get_cached_by_nickname(user.nickname)
287 refute user.is_moderator
288 refute user.is_locked
289 refute user.is_admin
290 assert user.confirmation_pending
291 end
292
293 test "no user to set status" do
294 Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
295
296 assert_received {:mix_shell, :error, [message]}
297 assert message =~ "No local user"
298 end
299 end
300
301 describe "running reset_password" do
302 test "password reset token is generated" do
303 user = insert(:user)
304
305 assert capture_io(fn ->
306 Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname])
307 end) =~ "URL:"
308
309 assert_received {:mix_shell, :info, [message]}
310 assert message =~ "Generated"
311 end
312
313 test "no user to reset password" do
314 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
315
316 assert_received {:mix_shell, :error, [message]}
317 assert message =~ "No local user"
318 end
319 end
320
321 describe "running reset_mfa" do
322 test "disables MFA" do
323 user =
324 insert(:user,
325 multi_factor_authentication_settings: %MFA.Settings{
326 enabled: true,
327 totp: %MFA.Settings.TOTP{secret: "xx", confirmed: true}
328 }
329 )
330
331 Mix.Tasks.Pleroma.User.run(["reset_mfa", user.nickname])
332
333 assert_received {:mix_shell, :info, [message]}
334 assert message == "Multi-Factor Authentication disabled for #{user.nickname}"
335
336 assert %{enabled: false, totp: false} ==
337 user.nickname
338 |> User.get_cached_by_nickname()
339 |> MFA.mfa_settings()
340 end
341
342 test "no user to reset MFA" do
343 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
344
345 assert_received {:mix_shell, :error, [message]}
346 assert message =~ "No local user"
347 end
348 end
349
350 describe "running invite" do
351 test "invite token is generated" do
352 assert capture_io(fn ->
353 Mix.Tasks.Pleroma.User.run(["invite"])
354 end) =~ "http"
355
356 assert_received {:mix_shell, :info, [message]}
357 assert message =~ "Generated user invite token one time"
358 end
359
360 test "token is generated with expires_at" do
361 assert capture_io(fn ->
362 Mix.Tasks.Pleroma.User.run([
363 "invite",
364 "--expires-at",
365 Date.to_string(Date.utc_today())
366 ])
367 end)
368
369 assert_received {:mix_shell, :info, [message]}
370 assert message =~ "Generated user invite token date limited"
371 end
372
373 test "token is generated with max use" do
374 assert capture_io(fn ->
375 Mix.Tasks.Pleroma.User.run([
376 "invite",
377 "--max-use",
378 "5"
379 ])
380 end)
381
382 assert_received {:mix_shell, :info, [message]}
383 assert message =~ "Generated user invite token reusable"
384 end
385
386 test "token is generated with max use and expires date" do
387 assert capture_io(fn ->
388 Mix.Tasks.Pleroma.User.run([
389 "invite",
390 "--max-use",
391 "5",
392 "--expires-at",
393 Date.to_string(Date.utc_today())
394 ])
395 end)
396
397 assert_received {:mix_shell, :info, [message]}
398 assert message =~ "Generated user invite token reusable date limited"
399 end
400 end
401
402 describe "running invites" do
403 test "invites are listed" do
404 {:ok, invite} = Pleroma.UserInviteToken.create_invite()
405
406 {:ok, invite2} =
407 Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
408
409 # assert capture_io(fn ->
410 Mix.Tasks.Pleroma.User.run([
411 "invites"
412 ])
413
414 # end)
415
416 assert_received {:mix_shell, :info, [message]}
417 assert_received {:mix_shell, :info, [message2]}
418 assert_received {:mix_shell, :info, [message3]}
419 assert message =~ "Invites list:"
420 assert message2 =~ invite.invite_type
421 assert message3 =~ invite2.invite_type
422 end
423 end
424
425 describe "running revoke_invite" do
426 test "invite is revoked" do
427 {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
428
429 assert capture_io(fn ->
430 Mix.Tasks.Pleroma.User.run([
431 "revoke_invite",
432 invite.token
433 ])
434 end)
435
436 assert_received {:mix_shell, :info, [message]}
437 assert message =~ "Invite for token #{invite.token} was revoked."
438 end
439
440 test "it prints an error message when invite is not exist" do
441 Mix.Tasks.Pleroma.User.run(["revoke_invite", "foo"])
442
443 assert_received {:mix_shell, :error, [message]}
444 assert message =~ "No invite found"
445 end
446 end
447
448 describe "running delete_activities" do
449 test "activities are deleted" do
450 %{nickname: nickname} = insert(:user)
451
452 assert :ok == Mix.Tasks.Pleroma.User.run(["delete_activities", nickname])
453 assert_received {:mix_shell, :info, [message]}
454 assert message == "User #{nickname} statuses deleted."
455 end
456
457 test "it prints an error message when user is not exist" do
458 Mix.Tasks.Pleroma.User.run(["delete_activities", "foo"])
459
460 assert_received {:mix_shell, :error, [message]}
461 assert message =~ "No local user"
462 end
463 end
464
465 describe "running confirm" do
466 test "user is confirmed" do
467 %{id: id, nickname: nickname} = insert(:user, confirmation_pending: false)
468
469 assert :ok = Mix.Tasks.Pleroma.User.run(["confirm", nickname])
470 assert_received {:mix_shell, :info, [message]}
471 assert message == "#{nickname} doesn't need confirmation."
472
473 user = Repo.get(User, id)
474 refute user.confirmation_pending
475 refute user.confirmation_token
476 end
477
478 test "user is not confirmed" do
479 %{id: id, nickname: nickname} =
480 insert(:user, confirmation_pending: true, confirmation_token: "some token")
481
482 assert :ok = Mix.Tasks.Pleroma.User.run(["confirm", nickname])
483 assert_received {:mix_shell, :info, [message]}
484 assert message == "#{nickname} doesn't need confirmation."
485
486 user = Repo.get(User, id)
487 refute user.confirmation_pending
488 refute user.confirmation_token
489 end
490
491 test "it prints an error message when user is not exist" do
492 Mix.Tasks.Pleroma.User.run(["confirm", "foo"])
493
494 assert_received {:mix_shell, :error, [message]}
495 assert message =~ "No local user"
496 end
497 end
498
499 describe "search" do
500 test "it returns users matching" do
501 user = insert(:user)
502 moon = insert(:user, nickname: "moon", name: "fediverse expert moon")
503 moot = insert(:user, nickname: "moot")
504 kawen = insert(:user, nickname: "kawen", name: "fediverse expert moon")
505
506 {:ok, user, moon} = User.follow(user, moon)
507
508 assert [moon.id, kawen.id] == User.Search.search("moon") |> Enum.map(& &1.id)
509
510 res = User.search("moo") |> Enum.map(& &1.id)
511 assert Enum.sort([moon.id, moot.id, kawen.id]) == Enum.sort(res)
512
513 assert [kawen.id, moon.id] == User.Search.search("expert fediverse") |> Enum.map(& &1.id)
514
515 assert [moon.id, kawen.id] ==
516 User.Search.search("expert fediverse", for_user: user) |> Enum.map(& &1.id)
517 end
518 end
519
520 describe "signing out" do
521 test "it deletes all user's tokens and authorizations" do
522 user = insert(:user)
523 insert(:oauth_token, user: user)
524 insert(:oauth_authorization, user: user)
525
526 assert Repo.get_by(Token, user_id: user.id)
527 assert Repo.get_by(Authorization, user_id: user.id)
528
529 :ok = Mix.Tasks.Pleroma.User.run(["sign_out", user.nickname])
530
531 refute Repo.get_by(Token, user_id: user.id)
532 refute Repo.get_by(Authorization, user_id: user.id)
533 end
534
535 test "it prints an error message when user is not exist" do
536 Mix.Tasks.Pleroma.User.run(["sign_out", "foo"])
537
538 assert_received {:mix_shell, :error, [message]}
539 assert message =~ "No local user"
540 end
541 end
542
543 describe "tagging" do
544 test "it add tags to a user" do
545 user = insert(:user)
546
547 :ok = Mix.Tasks.Pleroma.User.run(["tag", user.nickname, "pleroma"])
548
549 user = User.get_cached_by_nickname(user.nickname)
550 assert "pleroma" in user.tags
551 end
552
553 test "it prints an error message when user is not exist" do
554 Mix.Tasks.Pleroma.User.run(["tag", "foo"])
555
556 assert_received {:mix_shell, :error, [message]}
557 assert message =~ "Could not change user tags"
558 end
559 end
560
561 describe "untagging" do
562 test "it deletes tags from a user" do
563 user = insert(:user, tags: ["pleroma"])
564 assert "pleroma" in user.tags
565
566 :ok = Mix.Tasks.Pleroma.User.run(["untag", user.nickname, "pleroma"])
567
568 user = User.get_cached_by_nickname(user.nickname)
569 assert Enum.empty?(user.tags)
570 end
571
572 test "it prints an error message when user is not exist" do
573 Mix.Tasks.Pleroma.User.run(["untag", "foo"])
574
575 assert_received {:mix_shell, :error, [message]}
576 assert message =~ "Could not change user tags"
577 end
578 end
579
580 describe "bulk confirm and unconfirm" do
581 test "confirm all" do
582 user1 = insert(:user, confirmation_pending: true)
583 user2 = insert(:user, confirmation_pending: true)
584
585 assert user1.confirmation_pending
586 assert user2.confirmation_pending
587
588 Mix.Tasks.Pleroma.User.run(["confirm_all"])
589
590 user1 = User.get_cached_by_nickname(user1.nickname)
591 user2 = User.get_cached_by_nickname(user2.nickname)
592
593 refute user1.confirmation_pending
594 refute user2.confirmation_pending
595 end
596
597 test "unconfirm all" do
598 user1 = insert(:user, confirmation_pending: false)
599 user2 = insert(:user, confirmation_pending: false)
600 admin = insert(:user, is_admin: true, confirmation_pending: false)
601 mod = insert(:user, is_moderator: true, confirmation_pending: false)
602
603 refute user1.confirmation_pending
604 refute user2.confirmation_pending
605
606 Mix.Tasks.Pleroma.User.run(["unconfirm_all"])
607
608 user1 = User.get_cached_by_nickname(user1.nickname)
609 user2 = User.get_cached_by_nickname(user2.nickname)
610 admin = User.get_cached_by_nickname(admin.nickname)
611 mod = User.get_cached_by_nickname(mod.nickname)
612
613 assert user1.confirmation_pending
614 assert user2.confirmation_pending
615 refute admin.confirmation_pending
616 refute mod.confirmation_pending
617 end
618 end
619 end