1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.UserTest do
10 alias Pleroma.Tests.ObanHelpers
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.OAuth.Authorization
14 alias Pleroma.Web.OAuth.Token
17 use Oban.Testing, repo: Pleroma.Repo
19 import ExUnit.CaptureIO
21 import Pleroma.Factory
24 Mix.shell(Mix.Shell.Process)
27 Mix.shell(Mix.Shell.IO)
33 describe "running new" do
34 test "user is created" do
35 # just get random data
36 unsaved = build(:user)
38 # prepare to answer yes
39 send(self(), {:mix_shell_input, :prompt, "Y"})
41 Mix.Tasks.Pleroma.User.run([
55 assert_received {:mix_shell, :info, [message]}
56 assert message =~ "user will be created"
58 assert_received {:mix_shell, :prompt, [message]}
59 assert message =~ "Continue"
61 assert_received {:mix_shell, :info, [message]}
62 assert message =~ "created"
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
72 test "user is not created" do
73 unsaved = build(:user)
75 # prepare to answer no
76 send(self(), {:mix_shell_input, :prompt, "N"})
78 Mix.Tasks.Pleroma.User.run(["new", unsaved.nickname, unsaved.email])
80 assert_received {:mix_shell, :info, [message]}
81 assert message =~ "user will be created"
83 assert_received {:mix_shell, :prompt, [message]}
84 assert message =~ "Continue"
86 assert_received {:mix_shell, :info, [message]}
87 assert message =~ "will not be created"
89 refute User.get_cached_by_nickname(unsaved.nickname)
93 describe "running rm" do
94 test "user is deleted" do
95 clear_config([:instance, :federating], true)
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()
103 assert_received {:mix_shell, :info, [message]}
104 assert message =~ " deleted"
105 assert %{deactivated: true} = User.get_by_nickname(user.nickname)
107 assert called(Pleroma.Web.Federator.publish(:_))
111 test "a remote user's create activity is deleted when the object has been pruned" do
113 user2 = insert(:user)
115 {:ok, post} = CommonAPI.post(user, %{status: "uguu"})
116 {:ok, post2} = CommonAPI.post(user2, %{status: "test"})
117 obj = Object.normalize(post2, fetch: false)
119 {:ok, like_object, meta} = Pleroma.Web.ActivityPub.Builder.like(user, obj)
121 {:ok, like_activity, _meta} =
122 Pleroma.Web.ActivityPub.Pipeline.common_pipeline(
124 Keyword.put(meta, :local, true)
127 like_activity.data["object"]
128 |> Pleroma.Object.get_by_ap_id()
131 clear_config([:instance, :federating], true)
133 object = Object.normalize(post, fetch: false)
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()
141 assert_received {:mix_shell, :info, [message]}
142 assert message =~ " deleted"
143 assert %{deactivated: true} = User.get_by_nickname(user.nickname)
145 assert called(Pleroma.Web.Federator.publish(:_))
146 refute Pleroma.Repo.get(Pleroma.Activity, like_activity.id)
149 refute Activity.get_by_id(post.id)
152 test "no user to delete" do
153 Mix.Tasks.Pleroma.User.run(["rm", "nonexistent"])
155 assert_received {:mix_shell, :error, [message]}
156 assert message =~ "No local user"
160 describe "running toggle_activated" do
161 test "user is deactivated" do
164 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
166 assert_received {:mix_shell, :info, [message]}
167 assert message =~ " deactivated"
169 user = User.get_cached_by_nickname(user.nickname)
170 assert user.deactivated
173 test "user is activated" do
174 user = insert(:user, deactivated: true)
176 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
178 assert_received {:mix_shell, :info, [message]}
179 assert message =~ " activated"
181 user = User.get_cached_by_nickname(user.nickname)
182 refute user.deactivated
185 test "no user to toggle" do
186 Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistent"])
188 assert_received {:mix_shell, :error, [message]}
189 assert message =~ "No user"
193 describe "running deactivate" do
194 test "user is unsubscribed" do
195 followed = insert(:user)
196 remote_followed = insert(:user, local: false)
199 User.follow(user, followed, :follow_accept)
200 User.follow(user, remote_followed, :follow_accept)
202 Mix.Tasks.Pleroma.User.run(["deactivate", user.nickname])
204 assert_received {:mix_shell, :info, [message]}
205 assert message =~ "Deactivating"
207 # Note that the task has delay :timer.sleep(500)
208 assert_received {:mix_shell, :info, [message]}
209 assert message =~ "Successfully unsubscribed"
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
216 test "no user to deactivate" do
217 Mix.Tasks.Pleroma.User.run(["deactivate", "nonexistent"])
219 assert_received {:mix_shell, :error, [message]}
220 assert message =~ "No user"
224 describe "running set" do
225 test "All statuses set" do
228 Mix.Tasks.Pleroma.User.run([
237 assert_received {:mix_shell, :info, [message]}
238 assert message =~ ~r/Admin status .* true/
240 assert_received {:mix_shell, :info, [message]}
241 assert message =~ ~r/Confirmation pending .* false/
243 assert_received {:mix_shell, :info, [message]}
244 assert message =~ ~r/Locked status .* true/
246 assert_received {:mix_shell, :info, [message]}
247 assert message =~ ~r/Moderator status .* true/
249 user = User.get_cached_by_nickname(user.nickname)
250 assert user.is_moderator
251 assert user.is_locked
253 refute user.confirmation_pending
256 test "All statuses unset" do
262 confirmation_pending: true
265 Mix.Tasks.Pleroma.User.run([
274 assert_received {:mix_shell, :info, [message]}
275 assert message =~ ~r/Admin status .* false/
277 assert_received {:mix_shell, :info, [message]}
278 assert message =~ ~r/Confirmation pending .* true/
280 assert_received {:mix_shell, :info, [message]}
281 assert message =~ ~r/Locked status .* false/
283 assert_received {:mix_shell, :info, [message]}
284 assert message =~ ~r/Moderator status .* false/
286 user = User.get_cached_by_nickname(user.nickname)
287 refute user.is_moderator
288 refute user.is_locked
290 assert user.confirmation_pending
293 test "no user to set status" do
294 Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
296 assert_received {:mix_shell, :error, [message]}
297 assert message =~ "No local user"
301 describe "running reset_password" do
302 test "password reset token is generated" do
305 assert capture_io(fn ->
306 Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname])
309 assert_received {:mix_shell, :info, [message]}
310 assert message =~ "Generated"
313 test "no user to reset password" do
314 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
316 assert_received {:mix_shell, :error, [message]}
317 assert message =~ "No local user"
321 describe "running reset_mfa" do
322 test "disables MFA" do
325 multi_factor_authentication_settings: %MFA.Settings{
327 totp: %MFA.Settings.TOTP{secret: "xx", confirmed: true}
331 Mix.Tasks.Pleroma.User.run(["reset_mfa", user.nickname])
333 assert_received {:mix_shell, :info, [message]}
334 assert message == "Multi-Factor Authentication disabled for #{user.nickname}"
336 assert %{enabled: false, totp: false} ==
338 |> User.get_cached_by_nickname()
339 |> MFA.mfa_settings()
342 test "no user to reset MFA" do
343 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
345 assert_received {:mix_shell, :error, [message]}
346 assert message =~ "No local user"
350 describe "running invite" do
351 test "invite token is generated" do
352 assert capture_io(fn ->
353 Mix.Tasks.Pleroma.User.run(["invite"])
356 assert_received {:mix_shell, :info, [message]}
357 assert message =~ "Generated user invite token one time"
360 test "token is generated with expires_at" do
361 assert capture_io(fn ->
362 Mix.Tasks.Pleroma.User.run([
365 Date.to_string(Date.utc_today())
369 assert_received {:mix_shell, :info, [message]}
370 assert message =~ "Generated user invite token date limited"
373 test "token is generated with max use" do
374 assert capture_io(fn ->
375 Mix.Tasks.Pleroma.User.run([
382 assert_received {:mix_shell, :info, [message]}
383 assert message =~ "Generated user invite token reusable"
386 test "token is generated with max use and expires date" do
387 assert capture_io(fn ->
388 Mix.Tasks.Pleroma.User.run([
393 Date.to_string(Date.utc_today())
397 assert_received {:mix_shell, :info, [message]}
398 assert message =~ "Generated user invite token reusable date limited"
402 describe "running invites" do
403 test "invites are listed" do
404 {:ok, invite} = Pleroma.UserInviteToken.create_invite()
407 Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
409 # assert capture_io(fn ->
410 Mix.Tasks.Pleroma.User.run([
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
425 describe "running revoke_invite" do
426 test "invite is revoked" do
427 {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
429 assert capture_io(fn ->
430 Mix.Tasks.Pleroma.User.run([
436 assert_received {:mix_shell, :info, [message]}
437 assert message =~ "Invite for token #{invite.token} was revoked."
440 test "it prints an error message when invite is not exist" do
441 Mix.Tasks.Pleroma.User.run(["revoke_invite", "foo"])
443 assert_received {:mix_shell, :error, [message]}
444 assert message =~ "No invite found"
448 describe "running delete_activities" do
449 test "activities are deleted" do
450 %{nickname: nickname} = insert(:user)
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."
457 test "it prints an error message when user is not exist" do
458 Mix.Tasks.Pleroma.User.run(["delete_activities", "foo"])
460 assert_received {:mix_shell, :error, [message]}
461 assert message =~ "No local user"
465 describe "running confirm" do
466 test "user is confirmed" do
467 %{id: id, nickname: nickname} = insert(:user, confirmation_pending: false)
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."
473 user = Repo.get(User, id)
474 refute user.confirmation_pending
475 refute user.confirmation_token
478 test "user is not confirmed" do
479 %{id: id, nickname: nickname} =
480 insert(:user, confirmation_pending: true, confirmation_token: "some token")
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."
486 user = Repo.get(User, id)
487 refute user.confirmation_pending
488 refute user.confirmation_token
491 test "it prints an error message when user is not exist" do
492 Mix.Tasks.Pleroma.User.run(["confirm", "foo"])
494 assert_received {:mix_shell, :error, [message]}
495 assert message =~ "No local user"
500 test "it returns users matching" do
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")
506 {:ok, user, moon} = User.follow(user, moon)
508 assert [moon.id, kawen.id] == User.Search.search("moon") |> Enum.map(& &1.id)
510 res = User.search("moo") |> Enum.map(& &1.id)
511 assert Enum.sort([moon.id, moot.id, kawen.id]) == Enum.sort(res)
513 assert [kawen.id, moon.id] == User.Search.search("expert fediverse") |> Enum.map(& &1.id)
515 assert [moon.id, kawen.id] ==
516 User.Search.search("expert fediverse", for_user: user) |> Enum.map(& &1.id)
520 describe "signing out" do
521 test "it deletes all user's tokens and authorizations" do
523 insert(:oauth_token, user: user)
524 insert(:oauth_authorization, user: user)
526 assert Repo.get_by(Token, user_id: user.id)
527 assert Repo.get_by(Authorization, user_id: user.id)
529 :ok = Mix.Tasks.Pleroma.User.run(["sign_out", user.nickname])
531 refute Repo.get_by(Token, user_id: user.id)
532 refute Repo.get_by(Authorization, user_id: user.id)
535 test "it prints an error message when user is not exist" do
536 Mix.Tasks.Pleroma.User.run(["sign_out", "foo"])
538 assert_received {:mix_shell, :error, [message]}
539 assert message =~ "No local user"
543 describe "tagging" do
544 test "it add tags to a user" do
547 :ok = Mix.Tasks.Pleroma.User.run(["tag", user.nickname, "pleroma"])
549 user = User.get_cached_by_nickname(user.nickname)
550 assert "pleroma" in user.tags
553 test "it prints an error message when user is not exist" do
554 Mix.Tasks.Pleroma.User.run(["tag", "foo"])
556 assert_received {:mix_shell, :error, [message]}
557 assert message =~ "Could not change user tags"
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
566 :ok = Mix.Tasks.Pleroma.User.run(["untag", user.nickname, "pleroma"])
568 user = User.get_cached_by_nickname(user.nickname)
569 assert Enum.empty?(user.tags)
572 test "it prints an error message when user is not exist" do
573 Mix.Tasks.Pleroma.User.run(["untag", "foo"])
575 assert_received {:mix_shell, :error, [message]}
576 assert message =~ "Could not change user tags"
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)
585 assert user1.confirmation_pending
586 assert user2.confirmation_pending
588 Mix.Tasks.Pleroma.User.run(["confirm_all"])
590 user1 = User.get_cached_by_nickname(user1.nickname)
591 user2 = User.get_cached_by_nickname(user2.nickname)
593 refute user1.confirmation_pending
594 refute user2.confirmation_pending
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)
603 refute user1.confirmation_pending
604 refute user2.confirmation_pending
606 Mix.Tasks.Pleroma.User.run(["unconfirm_all"])
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)
613 assert user1.confirmation_pending
614 assert user2.confirmation_pending
615 refute admin.confirmation_pending
616 refute mod.confirmation_pending