Merge branch 'develop' into issue/1855
[akkoma] / test / tasks / 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 {:ok, post} = CommonAPI.post(user, %{status: "uguu"})
114
115 clear_config([:instance, :federating], true)
116
117 object = Object.normalize(post)
118 Object.prune(object)
119
120 with_mock Pleroma.Web.Federator,
121 publish: fn _ -> nil end do
122 Mix.Tasks.Pleroma.User.run(["rm", user.nickname])
123 ObanHelpers.perform_all()
124
125 assert_received {:mix_shell, :info, [message]}
126 assert message =~ " deleted"
127 assert %{deactivated: true} = User.get_by_nickname(user.nickname)
128
129 assert called(Pleroma.Web.Federator.publish(:_))
130 end
131
132 refute Activity.get_by_id(post.id)
133 end
134
135 test "no user to delete" do
136 Mix.Tasks.Pleroma.User.run(["rm", "nonexistent"])
137
138 assert_received {:mix_shell, :error, [message]}
139 assert message =~ "No local user"
140 end
141 end
142
143 describe "running toggle_activated" do
144 test "user is deactivated" do
145 user = insert(:user)
146
147 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
148
149 assert_received {:mix_shell, :info, [message]}
150 assert message =~ " deactivated"
151
152 user = User.get_cached_by_nickname(user.nickname)
153 assert user.deactivated
154 end
155
156 test "user is activated" do
157 user = insert(:user, deactivated: true)
158
159 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
160
161 assert_received {:mix_shell, :info, [message]}
162 assert message =~ " activated"
163
164 user = User.get_cached_by_nickname(user.nickname)
165 refute user.deactivated
166 end
167
168 test "no user to toggle" do
169 Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistent"])
170
171 assert_received {:mix_shell, :error, [message]}
172 assert message =~ "No user"
173 end
174 end
175
176 describe "running deactivate" do
177 test "user is unsubscribed" do
178 followed = insert(:user)
179 remote_followed = insert(:user, local: false)
180 user = insert(:user)
181
182 User.follow(user, followed, :follow_accept)
183 User.follow(user, remote_followed, :follow_accept)
184
185 Mix.Tasks.Pleroma.User.run(["deactivate", user.nickname])
186
187 assert_received {:mix_shell, :info, [message]}
188 assert message =~ "Deactivating"
189
190 # Note that the task has delay :timer.sleep(500)
191 assert_received {:mix_shell, :info, [message]}
192 assert message =~ "Successfully unsubscribed"
193
194 user = User.get_cached_by_nickname(user.nickname)
195 assert Enum.empty?(Enum.filter(User.get_friends(user), & &1.local))
196 assert user.deactivated
197 end
198
199 test "no user to deactivate" do
200 Mix.Tasks.Pleroma.User.run(["deactivate", "nonexistent"])
201
202 assert_received {:mix_shell, :error, [message]}
203 assert message =~ "No user"
204 end
205 end
206
207 describe "running set" do
208 test "All statuses set" do
209 user = insert(:user)
210
211 Mix.Tasks.Pleroma.User.run(["set", user.nickname, "--moderator", "--admin", "--locked"])
212
213 assert_received {:mix_shell, :info, [message]}
214 assert message =~ ~r/Moderator status .* true/
215
216 assert_received {:mix_shell, :info, [message]}
217 assert message =~ ~r/Locked status .* true/
218
219 assert_received {:mix_shell, :info, [message]}
220 assert message =~ ~r/Admin status .* true/
221
222 user = User.get_cached_by_nickname(user.nickname)
223 assert user.is_moderator
224 assert user.locked
225 assert user.is_admin
226 end
227
228 test "All statuses unset" do
229 user = insert(:user, locked: true, is_moderator: true, is_admin: true)
230
231 Mix.Tasks.Pleroma.User.run([
232 "set",
233 user.nickname,
234 "--no-moderator",
235 "--no-admin",
236 "--no-locked"
237 ])
238
239 assert_received {:mix_shell, :info, [message]}
240 assert message =~ ~r/Moderator status .* false/
241
242 assert_received {:mix_shell, :info, [message]}
243 assert message =~ ~r/Locked status .* false/
244
245 assert_received {:mix_shell, :info, [message]}
246 assert message =~ ~r/Admin status .* false/
247
248 user = User.get_cached_by_nickname(user.nickname)
249 refute user.is_moderator
250 refute user.locked
251 refute user.is_admin
252 end
253
254 test "no user to set status" do
255 Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
256
257 assert_received {:mix_shell, :error, [message]}
258 assert message =~ "No local user"
259 end
260 end
261
262 describe "running reset_password" do
263 test "password reset token is generated" do
264 user = insert(:user)
265
266 assert capture_io(fn ->
267 Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname])
268 end) =~ "URL:"
269
270 assert_received {:mix_shell, :info, [message]}
271 assert message =~ "Generated"
272 end
273
274 test "no user to reset password" do
275 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
276
277 assert_received {:mix_shell, :error, [message]}
278 assert message =~ "No local user"
279 end
280 end
281
282 describe "running reset_mfa" do
283 test "disables MFA" do
284 user =
285 insert(:user,
286 multi_factor_authentication_settings: %MFA.Settings{
287 enabled: true,
288 totp: %MFA.Settings.TOTP{secret: "xx", confirmed: true}
289 }
290 )
291
292 Mix.Tasks.Pleroma.User.run(["reset_mfa", user.nickname])
293
294 assert_received {:mix_shell, :info, [message]}
295 assert message == "Multi-Factor Authentication disabled for #{user.nickname}"
296
297 assert %{enabled: false, totp: false} ==
298 user.nickname
299 |> User.get_cached_by_nickname()
300 |> MFA.mfa_settings()
301 end
302
303 test "no user to reset MFA" do
304 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
305
306 assert_received {:mix_shell, :error, [message]}
307 assert message =~ "No local user"
308 end
309 end
310
311 describe "running invite" do
312 test "invite token is generated" do
313 assert capture_io(fn ->
314 Mix.Tasks.Pleroma.User.run(["invite"])
315 end) =~ "http"
316
317 assert_received {:mix_shell, :info, [message]}
318 assert message =~ "Generated user invite token one time"
319 end
320
321 test "token is generated with expires_at" do
322 assert capture_io(fn ->
323 Mix.Tasks.Pleroma.User.run([
324 "invite",
325 "--expires-at",
326 Date.to_string(Date.utc_today())
327 ])
328 end)
329
330 assert_received {:mix_shell, :info, [message]}
331 assert message =~ "Generated user invite token date limited"
332 end
333
334 test "token is generated with max use" do
335 assert capture_io(fn ->
336 Mix.Tasks.Pleroma.User.run([
337 "invite",
338 "--max-use",
339 "5"
340 ])
341 end)
342
343 assert_received {:mix_shell, :info, [message]}
344 assert message =~ "Generated user invite token reusable"
345 end
346
347 test "token is generated with max use and expires date" do
348 assert capture_io(fn ->
349 Mix.Tasks.Pleroma.User.run([
350 "invite",
351 "--max-use",
352 "5",
353 "--expires-at",
354 Date.to_string(Date.utc_today())
355 ])
356 end)
357
358 assert_received {:mix_shell, :info, [message]}
359 assert message =~ "Generated user invite token reusable date limited"
360 end
361 end
362
363 describe "running invites" do
364 test "invites are listed" do
365 {:ok, invite} = Pleroma.UserInviteToken.create_invite()
366
367 {:ok, invite2} =
368 Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
369
370 # assert capture_io(fn ->
371 Mix.Tasks.Pleroma.User.run([
372 "invites"
373 ])
374
375 # end)
376
377 assert_received {:mix_shell, :info, [message]}
378 assert_received {:mix_shell, :info, [message2]}
379 assert_received {:mix_shell, :info, [message3]}
380 assert message =~ "Invites list:"
381 assert message2 =~ invite.invite_type
382 assert message3 =~ invite2.invite_type
383 end
384 end
385
386 describe "running revoke_invite" do
387 test "invite is revoked" do
388 {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
389
390 assert capture_io(fn ->
391 Mix.Tasks.Pleroma.User.run([
392 "revoke_invite",
393 invite.token
394 ])
395 end)
396
397 assert_received {:mix_shell, :info, [message]}
398 assert message =~ "Invite for token #{invite.token} was revoked."
399 end
400
401 test "it prints an error message when invite is not exist" do
402 Mix.Tasks.Pleroma.User.run(["revoke_invite", "foo"])
403
404 assert_received {:mix_shell, :error, [message]}
405 assert message =~ "No invite found"
406 end
407 end
408
409 describe "running delete_activities" do
410 test "activities are deleted" do
411 %{nickname: nickname} = insert(:user)
412
413 assert :ok == Mix.Tasks.Pleroma.User.run(["delete_activities", nickname])
414 assert_received {:mix_shell, :info, [message]}
415 assert message == "User #{nickname} statuses deleted."
416 end
417
418 test "it prints an error message when user is not exist" do
419 Mix.Tasks.Pleroma.User.run(["delete_activities", "foo"])
420
421 assert_received {:mix_shell, :error, [message]}
422 assert message =~ "No local user"
423 end
424 end
425
426 describe "running toggle_confirmed" do
427 test "user is confirmed" do
428 %{id: id, nickname: nickname} = insert(:user, confirmation_pending: false)
429
430 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
431 assert_received {:mix_shell, :info, [message]}
432 assert message == "#{nickname} needs confirmation."
433
434 user = Repo.get(User, id)
435 assert user.confirmation_pending
436 assert user.confirmation_token
437 end
438
439 test "user is not confirmed" do
440 %{id: id, nickname: nickname} =
441 insert(:user, confirmation_pending: true, confirmation_token: "some token")
442
443 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
444 assert_received {:mix_shell, :info, [message]}
445 assert message == "#{nickname} doesn't need confirmation."
446
447 user = Repo.get(User, id)
448 refute user.confirmation_pending
449 refute user.confirmation_token
450 end
451
452 test "it prints an error message when user is not exist" do
453 Mix.Tasks.Pleroma.User.run(["toggle_confirmed", "foo"])
454
455 assert_received {:mix_shell, :error, [message]}
456 assert message =~ "No local user"
457 end
458 end
459
460 describe "search" do
461 test "it returns users matching" do
462 user = insert(:user)
463 moon = insert(:user, nickname: "moon", name: "fediverse expert moon")
464 moot = insert(:user, nickname: "moot")
465 kawen = insert(:user, nickname: "kawen", name: "fediverse expert moon")
466
467 {:ok, user} = User.follow(user, kawen)
468
469 assert [moon.id, kawen.id] == User.Search.search("moon") |> Enum.map(& &1.id)
470 res = User.search("moo") |> Enum.map(& &1.id)
471 assert moon.id in res
472 assert moot.id in res
473 assert kawen.id in res
474 assert [moon.id, kawen.id] == User.Search.search("moon fediverse") |> Enum.map(& &1.id)
475
476 assert [kawen.id, moon.id] ==
477 User.Search.search("moon fediverse", for_user: user) |> Enum.map(& &1.id)
478 end
479 end
480
481 describe "signing out" do
482 test "it deletes all user's tokens and authorizations" do
483 user = insert(:user)
484 insert(:oauth_token, user: user)
485 insert(:oauth_authorization, user: user)
486
487 assert Repo.get_by(Token, user_id: user.id)
488 assert Repo.get_by(Authorization, user_id: user.id)
489
490 :ok = Mix.Tasks.Pleroma.User.run(["sign_out", user.nickname])
491
492 refute Repo.get_by(Token, user_id: user.id)
493 refute Repo.get_by(Authorization, user_id: user.id)
494 end
495
496 test "it prints an error message when user is not exist" do
497 Mix.Tasks.Pleroma.User.run(["sign_out", "foo"])
498
499 assert_received {:mix_shell, :error, [message]}
500 assert message =~ "No local user"
501 end
502 end
503
504 describe "tagging" do
505 test "it add tags to a user" do
506 user = insert(:user)
507
508 :ok = Mix.Tasks.Pleroma.User.run(["tag", user.nickname, "pleroma"])
509
510 user = User.get_cached_by_nickname(user.nickname)
511 assert "pleroma" in user.tags
512 end
513
514 test "it prints an error message when user is not exist" do
515 Mix.Tasks.Pleroma.User.run(["tag", "foo"])
516
517 assert_received {:mix_shell, :error, [message]}
518 assert message =~ "Could not change user tags"
519 end
520 end
521
522 describe "untagging" do
523 test "it deletes tags from a user" do
524 user = insert(:user, tags: ["pleroma"])
525 assert "pleroma" in user.tags
526
527 :ok = Mix.Tasks.Pleroma.User.run(["untag", user.nickname, "pleroma"])
528
529 user = User.get_cached_by_nickname(user.nickname)
530 assert Enum.empty?(user.tags)
531 end
532
533 test "it prints an error message when user is not exist" do
534 Mix.Tasks.Pleroma.User.run(["untag", "foo"])
535
536 assert_received {:mix_shell, :error, [message]}
537 assert message =~ "Could not change user tags"
538 end
539 end
540 end