1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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, :yes?, true})
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, :yes?, [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, :yes?, false})
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, :yes?, [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)
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)
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
253 refute user.confirmation_pending
256 test "All statuses unset" do
258 insert(:user, locked: true, is_moderator: true, is_admin: true, confirmation_pending: true)
260 Mix.Tasks.Pleroma.User.run([
269 assert_received {:mix_shell, :info, [message]}
270 assert message =~ ~r/Admin status .* false/
272 assert_received {:mix_shell, :info, [message]}
273 assert message =~ ~r/Confirmation pending .* true/
275 assert_received {:mix_shell, :info, [message]}
276 assert message =~ ~r/Locked status .* false/
278 assert_received {:mix_shell, :info, [message]}
279 assert message =~ ~r/Moderator status .* false/
281 user = User.get_cached_by_nickname(user.nickname)
282 refute user.is_moderator
285 assert user.confirmation_pending
288 test "no user to set status" do
289 Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
291 assert_received {:mix_shell, :error, [message]}
292 assert message =~ "No local user"
296 describe "running reset_password" do
297 test "password reset token is generated" do
300 assert capture_io(fn ->
301 Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname])
304 assert_received {:mix_shell, :info, [message]}
305 assert message =~ "Generated"
308 test "no user to reset password" do
309 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
311 assert_received {:mix_shell, :error, [message]}
312 assert message =~ "No local user"
316 describe "running reset_mfa" do
317 test "disables MFA" do
320 multi_factor_authentication_settings: %MFA.Settings{
322 totp: %MFA.Settings.TOTP{secret: "xx", confirmed: true}
326 Mix.Tasks.Pleroma.User.run(["reset_mfa", user.nickname])
328 assert_received {:mix_shell, :info, [message]}
329 assert message == "Multi-Factor Authentication disabled for #{user.nickname}"
331 assert %{enabled: false, totp: false} ==
333 |> User.get_cached_by_nickname()
334 |> MFA.mfa_settings()
337 test "no user to reset MFA" do
338 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
340 assert_received {:mix_shell, :error, [message]}
341 assert message =~ "No local user"
345 describe "running invite" do
346 test "invite token is generated" do
347 assert capture_io(fn ->
348 Mix.Tasks.Pleroma.User.run(["invite"])
351 assert_received {:mix_shell, :info, [message]}
352 assert message =~ "Generated user invite token one time"
355 test "token is generated with expires_at" do
356 assert capture_io(fn ->
357 Mix.Tasks.Pleroma.User.run([
360 Date.to_string(Date.utc_today())
364 assert_received {:mix_shell, :info, [message]}
365 assert message =~ "Generated user invite token date limited"
368 test "token is generated with max use" do
369 assert capture_io(fn ->
370 Mix.Tasks.Pleroma.User.run([
377 assert_received {:mix_shell, :info, [message]}
378 assert message =~ "Generated user invite token reusable"
381 test "token is generated with max use and expires date" do
382 assert capture_io(fn ->
383 Mix.Tasks.Pleroma.User.run([
388 Date.to_string(Date.utc_today())
392 assert_received {:mix_shell, :info, [message]}
393 assert message =~ "Generated user invite token reusable date limited"
397 describe "running invites" do
398 test "invites are listed" do
399 {:ok, invite} = Pleroma.UserInviteToken.create_invite()
402 Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
404 # assert capture_io(fn ->
405 Mix.Tasks.Pleroma.User.run([
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
420 describe "running revoke_invite" do
421 test "invite is revoked" do
422 {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
424 assert capture_io(fn ->
425 Mix.Tasks.Pleroma.User.run([
431 assert_received {:mix_shell, :info, [message]}
432 assert message =~ "Invite for token #{invite.token} was revoked."
435 test "it prints an error message when invite is not exist" do
436 Mix.Tasks.Pleroma.User.run(["revoke_invite", "foo"])
438 assert_received {:mix_shell, :error, [message]}
439 assert message =~ "No invite found"
443 describe "running delete_activities" do
444 test "activities are deleted" do
445 %{nickname: nickname} = insert(:user)
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."
452 test "it prints an error message when user is not exist" do
453 Mix.Tasks.Pleroma.User.run(["delete_activities", "foo"])
455 assert_received {:mix_shell, :error, [message]}
456 assert message =~ "No local user"
460 describe "running toggle_confirmed" do
461 test "user is confirmed" do
462 %{id: id, nickname: nickname} = insert(:user, confirmation_pending: false)
464 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
465 assert_received {:mix_shell, :info, [message]}
466 assert message == "#{nickname} needs confirmation."
468 user = Repo.get(User, id)
469 assert user.confirmation_pending
470 assert user.confirmation_token
473 test "user is not confirmed" do
474 %{id: id, nickname: nickname} =
475 insert(:user, confirmation_pending: true, confirmation_token: "some token")
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."
481 user = Repo.get(User, id)
482 refute user.confirmation_pending
483 refute user.confirmation_token
486 test "it prints an error message when user is not exist" do
487 Mix.Tasks.Pleroma.User.run(["toggle_confirmed", "foo"])
489 assert_received {:mix_shell, :error, [message]}
490 assert message =~ "No local user"
495 test "it returns users matching" do
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")
501 {:ok, user} = User.follow(user, moon)
503 assert [moon.id, kawen.id] == User.Search.search("moon") |> Enum.map(& &1.id)
505 res = User.search("moo") |> Enum.map(& &1.id)
506 assert Enum.sort([moon.id, moot.id, kawen.id]) == Enum.sort(res)
508 assert [kawen.id, moon.id] == User.Search.search("expert fediverse") |> Enum.map(& &1.id)
510 assert [moon.id, kawen.id] ==
511 User.Search.search("expert fediverse", for_user: user) |> Enum.map(& &1.id)
515 describe "signing out" do
516 test "it deletes all user's tokens and authorizations" do
518 insert(:oauth_token, user: user)
519 insert(:oauth_authorization, user: user)
521 assert Repo.get_by(Token, user_id: user.id)
522 assert Repo.get_by(Authorization, user_id: user.id)
524 :ok = Mix.Tasks.Pleroma.User.run(["sign_out", user.nickname])
526 refute Repo.get_by(Token, user_id: user.id)
527 refute Repo.get_by(Authorization, user_id: user.id)
530 test "it prints an error message when user is not exist" do
531 Mix.Tasks.Pleroma.User.run(["sign_out", "foo"])
533 assert_received {:mix_shell, :error, [message]}
534 assert message =~ "No local user"
538 describe "tagging" do
539 test "it add tags to a user" do
542 :ok = Mix.Tasks.Pleroma.User.run(["tag", user.nickname, "pleroma"])
544 user = User.get_cached_by_nickname(user.nickname)
545 assert "pleroma" in user.tags
548 test "it prints an error message when user is not exist" do
549 Mix.Tasks.Pleroma.User.run(["tag", "foo"])
551 assert_received {:mix_shell, :error, [message]}
552 assert message =~ "Could not change user tags"
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
561 :ok = Mix.Tasks.Pleroma.User.run(["untag", user.nickname, "pleroma"])
563 user = User.get_cached_by_nickname(user.nickname)
564 assert Enum.empty?(user.tags)
567 test "it prints an error message when user is not exist" do
568 Mix.Tasks.Pleroma.User.run(["untag", "foo"])
570 assert_received {:mix_shell, :error, [message]}
571 assert message =~ "Could not change user tags"
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)
580 assert user1.confirmation_pending
581 assert user2.confirmation_pending
583 Mix.Tasks.Pleroma.User.run(["confirm_all"])
585 user1 = User.get_cached_by_nickname(user1.nickname)
586 user2 = User.get_cached_by_nickname(user2.nickname)
588 refute user1.confirmation_pending
589 refute user2.confirmation_pending
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)
598 refute user1.confirmation_pending
599 refute user2.confirmation_pending
601 Mix.Tasks.Pleroma.User.run(["unconfirm_all"])
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)
608 assert user1.confirmation_pending
609 assert user2.confirmation_pending
610 refute admin.confirmation_pending
611 refute mod.confirmation_pending