mix tasks consistency
[akkoma] / test / mix / tasks / pleroma / user_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule 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, :yes?, true})
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, :yes?, [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, :yes?, false})
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, :yes?, [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)
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)
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.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, locked: true, is_moderator: true, is_admin: true, confirmation_pending: true)
259
260 Mix.Tasks.Pleroma.User.run([
261 "set",
262 user.nickname,
263 "--no-admin",
264 "--no-confirmed",
265 "--no-locked",
266 "--no-moderator"
267 ])
268
269 assert_received {:mix_shell, :info, [message]}
270 assert message =~ ~r/Admin status .* false/
271
272 assert_received {:mix_shell, :info, [message]}
273 assert message =~ ~r/Confirmation pending .* true/
274
275 assert_received {:mix_shell, :info, [message]}
276 assert message =~ ~r/Locked status .* false/
277
278 assert_received {:mix_shell, :info, [message]}
279 assert message =~ ~r/Moderator status .* false/
280
281 user = User.get_cached_by_nickname(user.nickname)
282 refute user.is_moderator
283 refute user.locked
284 refute user.is_admin
285 assert user.confirmation_pending
286 end
287
288 test "no user to set status" do
289 Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
290
291 assert_received {:mix_shell, :error, [message]}
292 assert message =~ "No local user"
293 end
294 end
295
296 describe "running reset_password" do
297 test "password reset token is generated" do
298 user = insert(:user)
299
300 assert capture_io(fn ->
301 Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname])
302 end) =~ "URL:"
303
304 assert_received {:mix_shell, :info, [message]}
305 assert message =~ "Generated"
306 end
307
308 test "no user to reset password" do
309 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
310
311 assert_received {:mix_shell, :error, [message]}
312 assert message =~ "No local user"
313 end
314 end
315
316 describe "running reset_mfa" do
317 test "disables MFA" do
318 user =
319 insert(:user,
320 multi_factor_authentication_settings: %MFA.Settings{
321 enabled: true,
322 totp: %MFA.Settings.TOTP{secret: "xx", confirmed: true}
323 }
324 )
325
326 Mix.Tasks.Pleroma.User.run(["reset_mfa", user.nickname])
327
328 assert_received {:mix_shell, :info, [message]}
329 assert message == "Multi-Factor Authentication disabled for #{user.nickname}"
330
331 assert %{enabled: false, totp: false} ==
332 user.nickname
333 |> User.get_cached_by_nickname()
334 |> MFA.mfa_settings()
335 end
336
337 test "no user to reset MFA" do
338 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
339
340 assert_received {:mix_shell, :error, [message]}
341 assert message =~ "No local user"
342 end
343 end
344
345 describe "running invite" do
346 test "invite token is generated" do
347 assert capture_io(fn ->
348 Mix.Tasks.Pleroma.User.run(["invite"])
349 end) =~ "http"
350
351 assert_received {:mix_shell, :info, [message]}
352 assert message =~ "Generated user invite token one time"
353 end
354
355 test "token is generated with expires_at" do
356 assert capture_io(fn ->
357 Mix.Tasks.Pleroma.User.run([
358 "invite",
359 "--expires-at",
360 Date.to_string(Date.utc_today())
361 ])
362 end)
363
364 assert_received {:mix_shell, :info, [message]}
365 assert message =~ "Generated user invite token date limited"
366 end
367
368 test "token is generated with max use" do
369 assert capture_io(fn ->
370 Mix.Tasks.Pleroma.User.run([
371 "invite",
372 "--max-use",
373 "5"
374 ])
375 end)
376
377 assert_received {:mix_shell, :info, [message]}
378 assert message =~ "Generated user invite token reusable"
379 end
380
381 test "token is generated with max use and expires date" do
382 assert capture_io(fn ->
383 Mix.Tasks.Pleroma.User.run([
384 "invite",
385 "--max-use",
386 "5",
387 "--expires-at",
388 Date.to_string(Date.utc_today())
389 ])
390 end)
391
392 assert_received {:mix_shell, :info, [message]}
393 assert message =~ "Generated user invite token reusable date limited"
394 end
395 end
396
397 describe "running invites" do
398 test "invites are listed" do
399 {:ok, invite} = Pleroma.UserInviteToken.create_invite()
400
401 {:ok, invite2} =
402 Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
403
404 # assert capture_io(fn ->
405 Mix.Tasks.Pleroma.User.run([
406 "invites"
407 ])
408
409 # end)
410
411 assert_received {:mix_shell, :info, [message]}
412 assert_received {:mix_shell, :info, [message2]}
413 assert_received {:mix_shell, :info, [message3]}
414 assert message =~ "Invites list:"
415 assert message2 =~ invite.invite_type
416 assert message3 =~ invite2.invite_type
417 end
418 end
419
420 describe "running revoke_invite" do
421 test "invite is revoked" do
422 {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
423
424 assert capture_io(fn ->
425 Mix.Tasks.Pleroma.User.run([
426 "revoke_invite",
427 invite.token
428 ])
429 end)
430
431 assert_received {:mix_shell, :info, [message]}
432 assert message =~ "Invite for token #{invite.token} was revoked."
433 end
434
435 test "it prints an error message when invite is not exist" do
436 Mix.Tasks.Pleroma.User.run(["revoke_invite", "foo"])
437
438 assert_received {:mix_shell, :error, [message]}
439 assert message =~ "No invite found"
440 end
441 end
442
443 describe "running delete_activities" do
444 test "activities are deleted" do
445 %{nickname: nickname} = insert(:user)
446
447 assert :ok == Mix.Tasks.Pleroma.User.run(["delete_activities", nickname])
448 assert_received {:mix_shell, :info, [message]}
449 assert message == "User #{nickname} statuses deleted."
450 end
451
452 test "it prints an error message when user is not exist" do
453 Mix.Tasks.Pleroma.User.run(["delete_activities", "foo"])
454
455 assert_received {:mix_shell, :error, [message]}
456 assert message =~ "No local user"
457 end
458 end
459
460 describe "running toggle_confirmed" do
461 test "user is confirmed" do
462 %{id: id, nickname: nickname} = insert(:user, confirmation_pending: false)
463
464 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
465 assert_received {:mix_shell, :info, [message]}
466 assert message == "#{nickname} needs confirmation."
467
468 user = Repo.get(User, id)
469 assert user.confirmation_pending
470 assert user.confirmation_token
471 end
472
473 test "user is not confirmed" do
474 %{id: id, nickname: nickname} =
475 insert(:user, confirmation_pending: true, confirmation_token: "some token")
476
477 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
478 assert_received {:mix_shell, :info, [message]}
479 assert message == "#{nickname} doesn't need confirmation."
480
481 user = Repo.get(User, id)
482 refute user.confirmation_pending
483 refute user.confirmation_token
484 end
485
486 test "it prints an error message when user is not exist" do
487 Mix.Tasks.Pleroma.User.run(["toggle_confirmed", "foo"])
488
489 assert_received {:mix_shell, :error, [message]}
490 assert message =~ "No local user"
491 end
492 end
493
494 describe "search" do
495 test "it returns users matching" do
496 user = insert(:user)
497 moon = insert(:user, nickname: "moon", name: "fediverse expert moon")
498 moot = insert(:user, nickname: "moot")
499 kawen = insert(:user, nickname: "kawen", name: "fediverse expert moon")
500
501 {:ok, user} = User.follow(user, moon)
502
503 assert [moon.id, kawen.id] == User.Search.search("moon") |> Enum.map(& &1.id)
504
505 res = User.search("moo") |> Enum.map(& &1.id)
506 assert Enum.sort([moon.id, moot.id, kawen.id]) == Enum.sort(res)
507
508 assert [kawen.id, moon.id] == User.Search.search("expert fediverse") |> Enum.map(& &1.id)
509
510 assert [moon.id, kawen.id] ==
511 User.Search.search("expert fediverse", for_user: user) |> Enum.map(& &1.id)
512 end
513 end
514
515 describe "signing out" do
516 test "it deletes all user's tokens and authorizations" do
517 user = insert(:user)
518 insert(:oauth_token, user: user)
519 insert(:oauth_authorization, user: user)
520
521 assert Repo.get_by(Token, user_id: user.id)
522 assert Repo.get_by(Authorization, user_id: user.id)
523
524 :ok = Mix.Tasks.Pleroma.User.run(["sign_out", user.nickname])
525
526 refute Repo.get_by(Token, user_id: user.id)
527 refute Repo.get_by(Authorization, user_id: user.id)
528 end
529
530 test "it prints an error message when user is not exist" do
531 Mix.Tasks.Pleroma.User.run(["sign_out", "foo"])
532
533 assert_received {:mix_shell, :error, [message]}
534 assert message =~ "No local user"
535 end
536 end
537
538 describe "tagging" do
539 test "it add tags to a user" do
540 user = insert(:user)
541
542 :ok = Mix.Tasks.Pleroma.User.run(["tag", user.nickname, "pleroma"])
543
544 user = User.get_cached_by_nickname(user.nickname)
545 assert "pleroma" in user.tags
546 end
547
548 test "it prints an error message when user is not exist" do
549 Mix.Tasks.Pleroma.User.run(["tag", "foo"])
550
551 assert_received {:mix_shell, :error, [message]}
552 assert message =~ "Could not change user tags"
553 end
554 end
555
556 describe "untagging" do
557 test "it deletes tags from a user" do
558 user = insert(:user, tags: ["pleroma"])
559 assert "pleroma" in user.tags
560
561 :ok = Mix.Tasks.Pleroma.User.run(["untag", user.nickname, "pleroma"])
562
563 user = User.get_cached_by_nickname(user.nickname)
564 assert Enum.empty?(user.tags)
565 end
566
567 test "it prints an error message when user is not exist" do
568 Mix.Tasks.Pleroma.User.run(["untag", "foo"])
569
570 assert_received {:mix_shell, :error, [message]}
571 assert message =~ "Could not change user tags"
572 end
573 end
574
575 describe "bulk confirm and unconfirm" do
576 test "confirm all" do
577 user1 = insert(:user, confirmation_pending: true)
578 user2 = insert(:user, confirmation_pending: true)
579
580 assert user1.confirmation_pending
581 assert user2.confirmation_pending
582
583 Mix.Tasks.Pleroma.User.run(["confirm_all"])
584
585 user1 = User.get_cached_by_nickname(user1.nickname)
586 user2 = User.get_cached_by_nickname(user2.nickname)
587
588 refute user1.confirmation_pending
589 refute user2.confirmation_pending
590 end
591
592 test "unconfirm all" do
593 user1 = insert(:user, confirmation_pending: false)
594 user2 = insert(:user, confirmation_pending: false)
595 admin = insert(:user, is_admin: true, confirmation_pending: false)
596 mod = insert(:user, is_moderator: true, confirmation_pending: false)
597
598 refute user1.confirmation_pending
599 refute user2.confirmation_pending
600
601 Mix.Tasks.Pleroma.User.run(["unconfirm_all"])
602
603 user1 = User.get_cached_by_nickname(user1.nickname)
604 user2 = User.get_cached_by_nickname(user2.nickname)
605 admin = User.get_cached_by_nickname(admin.nickname)
606 mod = User.get_cached_by_nickname(mod.nickname)
607
608 assert user1.confirmation_pending
609 assert user2.confirmation_pending
610 refute admin.confirmation_pending
611 refute mod.confirmation_pending
612 end
613 end
614 end