1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.UserTest do
8 alias Pleroma.Web.OAuth.Authorization
9 alias Pleroma.Web.OAuth.Token
13 import Pleroma.Factory
14 import ExUnit.CaptureIO
17 Mix.shell(Mix.Shell.Process)
20 Mix.shell(Mix.Shell.IO)
26 describe "running new" do
27 test "user is created" do
28 # just get random data
29 unsaved = build(:user)
31 # prepare to answer yes
32 send(self(), {:mix_shell_input, :yes?, true})
34 Mix.Tasks.Pleroma.User.run([
48 assert_received {:mix_shell, :info, [message]}
49 assert message =~ "user will be created"
51 assert_received {:mix_shell, :yes?, [message]}
52 assert message =~ "Continue"
54 assert_received {:mix_shell, :info, [message]}
55 assert message =~ "created"
57 user = User.get_cached_by_nickname(unsaved.nickname)
58 assert user.name == unsaved.name
59 assert user.email == unsaved.email
60 assert user.bio == unsaved.bio
61 assert user.info.is_moderator
62 assert user.info.is_admin
65 test "user is not created" do
66 unsaved = build(:user)
68 # prepare to answer no
69 send(self(), {:mix_shell_input, :yes?, false})
71 Mix.Tasks.Pleroma.User.run(["new", unsaved.nickname, unsaved.email])
73 assert_received {:mix_shell, :info, [message]}
74 assert message =~ "user will be created"
76 assert_received {:mix_shell, :yes?, [message]}
77 assert message =~ "Continue"
79 assert_received {:mix_shell, :info, [message]}
80 assert message =~ "will not be created"
82 refute User.get_cached_by_nickname(unsaved.nickname)
86 describe "running rm" do
87 test "user is deleted" do
90 Mix.Tasks.Pleroma.User.run(["rm", user.nickname])
92 assert_received {:mix_shell, :info, [message]}
93 assert message =~ " deleted"
95 refute User.get_by_nickname(user.nickname)
98 test "no user to delete" do
99 Mix.Tasks.Pleroma.User.run(["rm", "nonexistent"])
101 assert_received {:mix_shell, :error, [message]}
102 assert message =~ "No local user"
106 describe "running toggle_activated" do
107 test "user is deactivated" do
110 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
112 assert_received {:mix_shell, :info, [message]}
113 assert message =~ " deactivated"
115 user = User.get_cached_by_nickname(user.nickname)
116 assert user.info.deactivated
119 test "user is activated" do
120 user = insert(:user, info: %{deactivated: true})
122 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
124 assert_received {:mix_shell, :info, [message]}
125 assert message =~ " activated"
127 user = User.get_cached_by_nickname(user.nickname)
128 refute user.info.deactivated
131 test "no user to toggle" do
132 Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistent"])
134 assert_received {:mix_shell, :error, [message]}
135 assert message =~ "No user"
139 describe "running unsubscribe" do
140 test "user is unsubscribed" do
141 followed = insert(:user)
142 user = insert(:user, %{following: [User.ap_followers(followed)]})
144 Mix.Tasks.Pleroma.User.run(["unsubscribe", user.nickname])
146 assert_received {:mix_shell, :info, [message]}
147 assert message =~ "Deactivating"
149 assert_received {:mix_shell, :info, [message]}
150 assert message =~ "Unsubscribing"
152 # Note that the task has delay :timer.sleep(500)
153 assert_received {:mix_shell, :info, [message]}
154 assert message =~ "Successfully unsubscribed"
156 user = User.get_cached_by_nickname(user.nickname)
157 assert Enum.empty?(user.following)
158 assert user.info.deactivated
161 test "no user to unsubscribe" do
162 Mix.Tasks.Pleroma.User.run(["unsubscribe", "nonexistent"])
164 assert_received {:mix_shell, :error, [message]}
165 assert message =~ "No user"
169 describe "running set" do
170 test "All statuses set" do
173 Mix.Tasks.Pleroma.User.run(["set", user.nickname, "--moderator", "--admin", "--locked"])
175 assert_received {:mix_shell, :info, [message]}
176 assert message =~ ~r/Moderator status .* true/
178 assert_received {:mix_shell, :info, [message]}
179 assert message =~ ~r/Locked status .* true/
181 assert_received {:mix_shell, :info, [message]}
182 assert message =~ ~r/Admin status .* true/
184 user = User.get_cached_by_nickname(user.nickname)
185 assert user.info.is_moderator
186 assert user.info.locked
187 assert user.info.is_admin
190 test "All statuses unset" do
191 user = insert(:user, info: %{is_moderator: true, locked: true, is_admin: true})
193 Mix.Tasks.Pleroma.User.run([
201 assert_received {:mix_shell, :info, [message]}
202 assert message =~ ~r/Moderator status .* false/
204 assert_received {:mix_shell, :info, [message]}
205 assert message =~ ~r/Locked status .* false/
207 assert_received {:mix_shell, :info, [message]}
208 assert message =~ ~r/Admin status .* false/
210 user = User.get_cached_by_nickname(user.nickname)
211 refute user.info.is_moderator
212 refute user.info.locked
213 refute user.info.is_admin
216 test "no user to set status" do
217 Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
219 assert_received {:mix_shell, :error, [message]}
220 assert message =~ "No local user"
224 describe "running reset_password" do
225 test "password reset token is generated" do
228 assert capture_io(fn ->
229 Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname])
232 assert_received {:mix_shell, :info, [message]}
233 assert message =~ "Generated"
236 test "no user to reset password" do
237 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
239 assert_received {:mix_shell, :error, [message]}
240 assert message =~ "No local user"
244 describe "running invite" do
245 test "invite token is generated" do
246 assert capture_io(fn ->
247 Mix.Tasks.Pleroma.User.run(["invite"])
250 assert_received {:mix_shell, :info, [message]}
251 assert message =~ "Generated user invite token one time"
254 test "token is generated with expires_at" do
255 assert capture_io(fn ->
256 Mix.Tasks.Pleroma.User.run([
259 Date.to_string(Date.utc_today())
263 assert_received {:mix_shell, :info, [message]}
264 assert message =~ "Generated user invite token date limited"
267 test "token is generated with max use" do
268 assert capture_io(fn ->
269 Mix.Tasks.Pleroma.User.run([
276 assert_received {:mix_shell, :info, [message]}
277 assert message =~ "Generated user invite token reusable"
280 test "token is generated with max use and expires date" do
281 assert capture_io(fn ->
282 Mix.Tasks.Pleroma.User.run([
287 Date.to_string(Date.utc_today())
291 assert_received {:mix_shell, :info, [message]}
292 assert message =~ "Generated user invite token reusable date limited"
296 describe "running invites" do
297 test "invites are listed" do
298 {:ok, invite} = Pleroma.UserInviteToken.create_invite()
301 Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
303 # assert capture_io(fn ->
304 Mix.Tasks.Pleroma.User.run([
310 assert_received {:mix_shell, :info, [message]}
311 assert_received {:mix_shell, :info, [message2]}
312 assert_received {:mix_shell, :info, [message3]}
313 assert message =~ "Invites list:"
314 assert message2 =~ invite.invite_type
315 assert message3 =~ invite2.invite_type
319 describe "running revoke_invite" do
320 test "invite is revoked" do
321 {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
323 assert capture_io(fn ->
324 Mix.Tasks.Pleroma.User.run([
330 assert_received {:mix_shell, :info, [message]}
331 assert message =~ "Invite for token #{invite.token} was revoked."
334 test "it prints an error message when invite is not exist" do
335 Mix.Tasks.Pleroma.User.run(["revoke_invite", "foo"])
337 assert_received {:mix_shell, :error, [message]}
338 assert message =~ "No invite found"
342 describe "running delete_activities" do
343 test "activities are deleted" do
344 %{nickname: nickname} = insert(:user)
346 assert :ok == Mix.Tasks.Pleroma.User.run(["delete_activities", nickname])
347 assert_received {:mix_shell, :info, [message]}
348 assert message == "User #{nickname} statuses deleted."
351 test "it prints an error message when user is not exist" do
352 Mix.Tasks.Pleroma.User.run(["delete_activities", "foo"])
354 assert_received {:mix_shell, :error, [message]}
355 assert message =~ "No local user"
359 describe "running toggle_confirmed" do
360 test "user is confirmed" do
361 %{id: id, nickname: nickname} = insert(:user, info: %{confirmation_pending: false})
363 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
364 assert_received {:mix_shell, :info, [message]}
365 assert message == "#{nickname} needs confirmation."
367 user = Repo.get(User, id)
368 assert user.info.confirmation_pending
369 assert user.info.confirmation_token
372 test "user is not confirmed" do
373 %{id: id, nickname: nickname} =
374 insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
376 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
377 assert_received {:mix_shell, :info, [message]}
378 assert message == "#{nickname} doesn't need confirmation."
380 user = Repo.get(User, id)
381 refute user.info.confirmation_pending
382 refute user.info.confirmation_token
385 test "it prints an error message when user is not exist" do
386 Mix.Tasks.Pleroma.User.run(["toggle_confirmed", "foo"])
388 assert_received {:mix_shell, :error, [message]}
389 assert message =~ "No local user"
394 test "it returns users matching" do
396 moon = insert(:user, nickname: "moon", name: "fediverse expert moon")
397 moot = insert(:user, nickname: "moot")
398 kawen = insert(:user, nickname: "kawen", name: "fediverse expert moon")
400 {:ok, user} = User.follow(user, kawen)
402 assert [moon.id, kawen.id] == User.Search.search("moon") |> Enum.map(& &1.id)
403 res = User.search("moo") |> Enum.map(& &1.id)
404 assert moon.id in res
405 assert moot.id in res
406 assert kawen.id in res
407 assert [moon.id, kawen.id] == User.Search.search("moon fediverse") |> Enum.map(& &1.id)
409 assert [kawen.id, moon.id] ==
410 User.Search.search("moon fediverse", for_user: user) |> Enum.map(& &1.id)
414 describe "signing out" do
415 test "it deletes all user's tokens and authorizations" do
417 insert(:oauth_token, user: user)
418 insert(:oauth_authorization, user: user)
420 assert Repo.get_by(Token, user_id: user.id)
421 assert Repo.get_by(Authorization, user_id: user.id)
423 :ok = Mix.Tasks.Pleroma.User.run(["sign_out", user.nickname])
425 refute Repo.get_by(Token, user_id: user.id)
426 refute Repo.get_by(Authorization, user_id: user.id)
429 test "it prints an error message when user is not exist" do
430 Mix.Tasks.Pleroma.User.run(["sign_out", "foo"])
432 assert_received {:mix_shell, :error, [message]}
433 assert message =~ "No local user"
437 describe "tagging" do
438 test "it add tags to a user" do
441 :ok = Mix.Tasks.Pleroma.User.run(["tag", user.nickname, "pleroma"])
443 user = User.get_cached_by_nickname(user.nickname)
444 assert "pleroma" in user.tags
447 test "it prints an error message when user is not exist" do
448 Mix.Tasks.Pleroma.User.run(["tag", "foo"])
450 assert_received {:mix_shell, :error, [message]}
451 assert message =~ "Could not change user tags"
455 describe "untagging" do
456 test "it deletes tags from a user" do
457 user = insert(:user, tags: ["pleroma"])
458 assert "pleroma" in user.tags
460 :ok = Mix.Tasks.Pleroma.User.run(["untag", user.nickname, "pleroma"])
462 user = User.get_cached_by_nickname(user.nickname)
463 assert Enum.empty?(user.tags)
466 test "it prints an error message when user is not exist" do
467 Mix.Tasks.Pleroma.User.run(["untag", "foo"])
469 assert_received {:mix_shell, :error, [message]}
470 assert message =~ "Could not change user tags"