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 {:ok, post} = CommonAPI.post(user, %{status: "uguu"})
115 clear_config([:instance, :federating], true)
117 object = Object.normalize(post)
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()
125 assert_received {:mix_shell, :info, [message]}
126 assert message =~ " deleted"
127 assert %{deactivated: true} = User.get_by_nickname(user.nickname)
129 assert called(Pleroma.Web.Federator.publish(:_))
132 refute Activity.get_by_id(post.id)
135 test "no user to delete" do
136 Mix.Tasks.Pleroma.User.run(["rm", "nonexistent"])
138 assert_received {:mix_shell, :error, [message]}
139 assert message =~ "No local user"
143 describe "running toggle_activated" do
144 test "user is deactivated" do
147 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
149 assert_received {:mix_shell, :info, [message]}
150 assert message =~ " deactivated"
152 user = User.get_cached_by_nickname(user.nickname)
153 assert user.deactivated
156 test "user is activated" do
157 user = insert(:user, deactivated: true)
159 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
161 assert_received {:mix_shell, :info, [message]}
162 assert message =~ " activated"
164 user = User.get_cached_by_nickname(user.nickname)
165 refute user.deactivated
168 test "no user to toggle" do
169 Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistent"])
171 assert_received {:mix_shell, :error, [message]}
172 assert message =~ "No user"
176 describe "running deactivate" do
177 test "user is unsubscribed" do
178 followed = insert(:user)
179 remote_followed = insert(:user, local: false)
182 User.follow(user, followed, :follow_accept)
183 User.follow(user, remote_followed, :follow_accept)
185 Mix.Tasks.Pleroma.User.run(["deactivate", user.nickname])
187 assert_received {:mix_shell, :info, [message]}
188 assert message =~ "Deactivating"
190 # Note that the task has delay :timer.sleep(500)
191 assert_received {:mix_shell, :info, [message]}
192 assert message =~ "Successfully unsubscribed"
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
199 test "no user to deactivate" do
200 Mix.Tasks.Pleroma.User.run(["deactivate", "nonexistent"])
202 assert_received {:mix_shell, :error, [message]}
203 assert message =~ "No user"
207 describe "running set" do
208 test "All statuses set" do
211 Mix.Tasks.Pleroma.User.run(["set", user.nickname, "--moderator", "--admin", "--locked"])
213 assert_received {:mix_shell, :info, [message]}
214 assert message =~ ~r/Moderator status .* true/
216 assert_received {:mix_shell, :info, [message]}
217 assert message =~ ~r/Locked status .* true/
219 assert_received {:mix_shell, :info, [message]}
220 assert message =~ ~r/Admin status .* true/
222 user = User.get_cached_by_nickname(user.nickname)
223 assert user.is_moderator
228 test "All statuses unset" do
229 user = insert(:user, locked: true, is_moderator: true, is_admin: true)
231 Mix.Tasks.Pleroma.User.run([
239 assert_received {:mix_shell, :info, [message]}
240 assert message =~ ~r/Moderator status .* false/
242 assert_received {:mix_shell, :info, [message]}
243 assert message =~ ~r/Locked status .* false/
245 assert_received {:mix_shell, :info, [message]}
246 assert message =~ ~r/Admin status .* false/
248 user = User.get_cached_by_nickname(user.nickname)
249 refute user.is_moderator
254 test "no user to set status" do
255 Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
257 assert_received {:mix_shell, :error, [message]}
258 assert message =~ "No local user"
262 describe "running reset_password" do
263 test "password reset token is generated" do
266 assert capture_io(fn ->
267 Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname])
270 assert_received {:mix_shell, :info, [message]}
271 assert message =~ "Generated"
274 test "no user to reset password" do
275 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
277 assert_received {:mix_shell, :error, [message]}
278 assert message =~ "No local user"
282 describe "running reset_mfa" do
283 test "disables MFA" do
286 multi_factor_authentication_settings: %MFA.Settings{
288 totp: %MFA.Settings.TOTP{secret: "xx", confirmed: true}
292 Mix.Tasks.Pleroma.User.run(["reset_mfa", user.nickname])
294 assert_received {:mix_shell, :info, [message]}
295 assert message == "Multi-Factor Authentication disabled for #{user.nickname}"
297 assert %{enabled: false, totp: false} ==
299 |> User.get_cached_by_nickname()
300 |> MFA.mfa_settings()
303 test "no user to reset MFA" do
304 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
306 assert_received {:mix_shell, :error, [message]}
307 assert message =~ "No local user"
311 describe "running invite" do
312 test "invite token is generated" do
313 assert capture_io(fn ->
314 Mix.Tasks.Pleroma.User.run(["invite"])
317 assert_received {:mix_shell, :info, [message]}
318 assert message =~ "Generated user invite token one time"
321 test "token is generated with expires_at" do
322 assert capture_io(fn ->
323 Mix.Tasks.Pleroma.User.run([
326 Date.to_string(Date.utc_today())
330 assert_received {:mix_shell, :info, [message]}
331 assert message =~ "Generated user invite token date limited"
334 test "token is generated with max use" do
335 assert capture_io(fn ->
336 Mix.Tasks.Pleroma.User.run([
343 assert_received {:mix_shell, :info, [message]}
344 assert message =~ "Generated user invite token reusable"
347 test "token is generated with max use and expires date" do
348 assert capture_io(fn ->
349 Mix.Tasks.Pleroma.User.run([
354 Date.to_string(Date.utc_today())
358 assert_received {:mix_shell, :info, [message]}
359 assert message =~ "Generated user invite token reusable date limited"
363 describe "running invites" do
364 test "invites are listed" do
365 {:ok, invite} = Pleroma.UserInviteToken.create_invite()
368 Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
370 # assert capture_io(fn ->
371 Mix.Tasks.Pleroma.User.run([
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
386 describe "running revoke_invite" do
387 test "invite is revoked" do
388 {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
390 assert capture_io(fn ->
391 Mix.Tasks.Pleroma.User.run([
397 assert_received {:mix_shell, :info, [message]}
398 assert message =~ "Invite for token #{invite.token} was revoked."
401 test "it prints an error message when invite is not exist" do
402 Mix.Tasks.Pleroma.User.run(["revoke_invite", "foo"])
404 assert_received {:mix_shell, :error, [message]}
405 assert message =~ "No invite found"
409 describe "running delete_activities" do
410 test "activities are deleted" do
411 %{nickname: nickname} = insert(:user)
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."
418 test "it prints an error message when user is not exist" do
419 Mix.Tasks.Pleroma.User.run(["delete_activities", "foo"])
421 assert_received {:mix_shell, :error, [message]}
422 assert message =~ "No local user"
426 describe "running toggle_confirmed" do
427 test "user is confirmed" do
428 %{id: id, nickname: nickname} = insert(:user, confirmation_pending: false)
430 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
431 assert_received {:mix_shell, :info, [message]}
432 assert message == "#{nickname} needs confirmation."
434 user = Repo.get(User, id)
435 assert user.confirmation_pending
436 assert user.confirmation_token
439 test "user is not confirmed" do
440 %{id: id, nickname: nickname} =
441 insert(:user, confirmation_pending: true, confirmation_token: "some token")
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."
447 user = Repo.get(User, id)
448 refute user.confirmation_pending
449 refute user.confirmation_token
452 test "it prints an error message when user is not exist" do
453 Mix.Tasks.Pleroma.User.run(["toggle_confirmed", "foo"])
455 assert_received {:mix_shell, :error, [message]}
456 assert message =~ "No local user"
461 test "it returns users matching" do
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")
467 {:ok, user} = User.follow(user, kawen)
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)
476 assert [kawen.id, moon.id] ==
477 User.Search.search("moon fediverse", for_user: user) |> Enum.map(& &1.id)
481 describe "signing out" do
482 test "it deletes all user's tokens and authorizations" do
484 insert(:oauth_token, user: user)
485 insert(:oauth_authorization, user: user)
487 assert Repo.get_by(Token, user_id: user.id)
488 assert Repo.get_by(Authorization, user_id: user.id)
490 :ok = Mix.Tasks.Pleroma.User.run(["sign_out", user.nickname])
492 refute Repo.get_by(Token, user_id: user.id)
493 refute Repo.get_by(Authorization, user_id: user.id)
496 test "it prints an error message when user is not exist" do
497 Mix.Tasks.Pleroma.User.run(["sign_out", "foo"])
499 assert_received {:mix_shell, :error, [message]}
500 assert message =~ "No local user"
504 describe "tagging" do
505 test "it add tags to a user" do
508 :ok = Mix.Tasks.Pleroma.User.run(["tag", user.nickname, "pleroma"])
510 user = User.get_cached_by_nickname(user.nickname)
511 assert "pleroma" in user.tags
514 test "it prints an error message when user is not exist" do
515 Mix.Tasks.Pleroma.User.run(["tag", "foo"])
517 assert_received {:mix_shell, :error, [message]}
518 assert message =~ "Could not change user tags"
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
527 :ok = Mix.Tasks.Pleroma.User.run(["untag", user.nickname, "pleroma"])
529 user = User.get_cached_by_nickname(user.nickname)
530 assert Enum.empty?(user.tags)
533 test "it prints an error message when user is not exist" do
534 Mix.Tasks.Pleroma.User.run(["untag", "foo"])
536 assert_received {:mix_shell, :error, [message]}
537 assert message =~ "Could not change user tags"