2018-12-23 15:11:29 -05:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
defmodule Mix.Tasks.Pleroma.UserTest do
|
2019-05-16 16:04:08 -04:00
|
|
|
alias Pleroma.Repo
|
2018-12-15 19:14:23 -05:00
|
|
|
alias Pleroma.User
|
|
|
|
use Pleroma.DataCase
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
import ExUnit.CaptureIO
|
|
|
|
|
|
|
|
setup_all do
|
|
|
|
Mix.shell(Mix.Shell.Process)
|
|
|
|
|
|
|
|
on_exit(fn ->
|
|
|
|
Mix.shell(Mix.Shell.IO)
|
|
|
|
end)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "running new" do
|
|
|
|
test "user is created" do
|
|
|
|
# just get random data
|
|
|
|
unsaved = build(:user)
|
|
|
|
|
|
|
|
# prepare to answer yes
|
|
|
|
send(self(), {:mix_shell_input, :yes?, true})
|
|
|
|
|
|
|
|
Mix.Tasks.Pleroma.User.run([
|
|
|
|
"new",
|
|
|
|
unsaved.nickname,
|
|
|
|
unsaved.email,
|
|
|
|
"--name",
|
|
|
|
unsaved.name,
|
|
|
|
"--bio",
|
|
|
|
unsaved.bio,
|
|
|
|
"--password",
|
|
|
|
"test",
|
|
|
|
"--moderator",
|
|
|
|
"--admin"
|
|
|
|
])
|
|
|
|
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "user will be created"
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :yes?, [message]}
|
|
|
|
assert message =~ "Continue"
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "created"
|
|
|
|
|
2019-04-22 03:20:43 -04:00
|
|
|
user = User.get_cached_by_nickname(unsaved.nickname)
|
2018-12-15 19:14:23 -05:00
|
|
|
assert user.name == unsaved.name
|
|
|
|
assert user.email == unsaved.email
|
|
|
|
assert user.bio == unsaved.bio
|
|
|
|
assert user.info.is_moderator
|
|
|
|
assert user.info.is_admin
|
|
|
|
end
|
|
|
|
|
|
|
|
test "user is not created" do
|
|
|
|
unsaved = build(:user)
|
|
|
|
|
|
|
|
# prepare to answer no
|
|
|
|
send(self(), {:mix_shell_input, :yes?, false})
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
Mix.Tasks.Pleroma.User.run(["new", unsaved.nickname, unsaved.email])
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "user will be created"
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :yes?, [message]}
|
|
|
|
assert message =~ "Continue"
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "will not be created"
|
|
|
|
|
2019-04-22 03:20:43 -04:00
|
|
|
refute User.get_cached_by_nickname(unsaved.nickname)
|
2018-12-15 19:14:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "running rm" do
|
|
|
|
test "user is deleted" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
Mix.Tasks.Pleroma.User.run(["rm", user.nickname])
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ " deleted"
|
|
|
|
|
2019-04-22 03:20:43 -04:00
|
|
|
user = User.get_cached_by_nickname(user.nickname)
|
2018-12-15 19:14:23 -05:00
|
|
|
assert user.info.deactivated
|
|
|
|
end
|
|
|
|
|
|
|
|
test "no user to delete" do
|
2018-12-16 11:04:31 -05:00
|
|
|
Mix.Tasks.Pleroma.User.run(["rm", "nonexistent"])
|
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :error, [message]}
|
|
|
|
assert message =~ "No local user"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "running toggle_activated" do
|
|
|
|
test "user is deactivated" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ " deactivated"
|
|
|
|
|
2019-04-22 03:20:43 -04:00
|
|
|
user = User.get_cached_by_nickname(user.nickname)
|
2018-12-15 19:14:23 -05:00
|
|
|
assert user.info.deactivated
|
|
|
|
end
|
|
|
|
|
|
|
|
test "user is activated" do
|
|
|
|
user = insert(:user, info: %{deactivated: true})
|
|
|
|
|
|
|
|
Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ " activated"
|
|
|
|
|
2019-04-22 03:20:43 -04:00
|
|
|
user = User.get_cached_by_nickname(user.nickname)
|
2018-12-15 19:14:23 -05:00
|
|
|
refute user.info.deactivated
|
|
|
|
end
|
|
|
|
|
|
|
|
test "no user to toggle" do
|
2018-12-16 11:04:31 -05:00
|
|
|
Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistent"])
|
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :error, [message]}
|
|
|
|
assert message =~ "No user"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "running unsubscribe" do
|
|
|
|
test "user is unsubscribed" do
|
|
|
|
followed = insert(:user)
|
|
|
|
user = insert(:user, %{following: [User.ap_followers(followed)]})
|
|
|
|
|
|
|
|
Mix.Tasks.Pleroma.User.run(["unsubscribe", user.nickname])
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "Deactivating"
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "Unsubscribing"
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
# Note that the task has delay :timer.sleep(500)
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "Successfully unsubscribed"
|
|
|
|
|
2019-04-22 03:20:43 -04:00
|
|
|
user = User.get_cached_by_nickname(user.nickname)
|
2019-02-10 18:08:48 -05:00
|
|
|
assert Enum.empty?(user.following)
|
2018-12-15 19:14:23 -05:00
|
|
|
assert user.info.deactivated
|
|
|
|
end
|
|
|
|
|
|
|
|
test "no user to unsubscribe" do
|
2018-12-16 11:04:31 -05:00
|
|
|
Mix.Tasks.Pleroma.User.run(["unsubscribe", "nonexistent"])
|
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :error, [message]}
|
|
|
|
assert message =~ "No user"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "running set" do
|
|
|
|
test "All statuses set" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
Mix.Tasks.Pleroma.User.run(["set", user.nickname, "--moderator", "--admin", "--locked"])
|
2018-12-16 11:04:31 -05:00
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
2018-12-16 11:04:31 -05:00
|
|
|
assert message =~ ~r/Moderator status .* true/
|
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
2018-12-16 11:04:31 -05:00
|
|
|
assert message =~ ~r/Locked status .* true/
|
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
2018-12-16 11:04:31 -05:00
|
|
|
assert message =~ ~r/Admin status .* true/
|
2018-12-15 19:14:23 -05:00
|
|
|
|
2019-04-22 03:20:43 -04:00
|
|
|
user = User.get_cached_by_nickname(user.nickname)
|
2018-12-15 19:14:23 -05:00
|
|
|
assert user.info.is_moderator
|
|
|
|
assert user.info.locked
|
|
|
|
assert user.info.is_admin
|
|
|
|
end
|
|
|
|
|
|
|
|
test "All statuses unset" do
|
|
|
|
user = insert(:user, info: %{is_moderator: true, locked: true, is_admin: true})
|
|
|
|
|
|
|
|
Mix.Tasks.Pleroma.User.run([
|
|
|
|
"set",
|
|
|
|
user.nickname,
|
|
|
|
"--no-moderator",
|
|
|
|
"--no-admin",
|
|
|
|
"--no-locked"
|
|
|
|
])
|
|
|
|
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
2018-12-16 11:04:31 -05:00
|
|
|
assert message =~ ~r/Moderator status .* false/
|
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
2018-12-16 11:04:31 -05:00
|
|
|
assert message =~ ~r/Locked status .* false/
|
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :info, [message]}
|
2018-12-16 11:04:31 -05:00
|
|
|
assert message =~ ~r/Admin status .* false/
|
2018-12-15 19:14:23 -05:00
|
|
|
|
2019-04-22 03:20:43 -04:00
|
|
|
user = User.get_cached_by_nickname(user.nickname)
|
2018-12-15 19:14:23 -05:00
|
|
|
refute user.info.is_moderator
|
|
|
|
refute user.info.locked
|
|
|
|
refute user.info.is_admin
|
|
|
|
end
|
|
|
|
|
|
|
|
test "no user to set status" do
|
2018-12-16 11:04:31 -05:00
|
|
|
Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
|
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :error, [message]}
|
|
|
|
assert message =~ "No local user"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "running reset_password" do
|
|
|
|
test "password reset token is generated" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
assert capture_io(fn ->
|
|
|
|
Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname])
|
|
|
|
end) =~ "URL:"
|
|
|
|
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "Generated"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "no user to reset password" do
|
2018-12-16 11:04:31 -05:00
|
|
|
Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
|
|
|
|
|
2018-12-15 19:14:23 -05:00
|
|
|
assert_received {:mix_shell, :error, [message]}
|
|
|
|
assert message =~ "No local user"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "running invite" do
|
|
|
|
test "invite token is generated" do
|
|
|
|
assert capture_io(fn ->
|
|
|
|
Mix.Tasks.Pleroma.User.run(["invite"])
|
|
|
|
end) =~ "http"
|
|
|
|
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
2019-04-06 05:58:22 -04:00
|
|
|
assert message =~ "Generated user invite token one time"
|
|
|
|
end
|
|
|
|
|
2019-04-08 05:01:28 -04:00
|
|
|
test "token is generated with expires_at" do
|
2019-04-06 05:58:22 -04:00
|
|
|
assert capture_io(fn ->
|
|
|
|
Mix.Tasks.Pleroma.User.run([
|
|
|
|
"invite",
|
2019-04-08 05:01:28 -04:00
|
|
|
"--expires-at",
|
2019-04-06 05:58:22 -04:00
|
|
|
Date.to_string(Date.utc_today())
|
|
|
|
])
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "Generated user invite token date limited"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "token is generated with max use" do
|
|
|
|
assert capture_io(fn ->
|
|
|
|
Mix.Tasks.Pleroma.User.run([
|
|
|
|
"invite",
|
|
|
|
"--max-use",
|
|
|
|
"5"
|
|
|
|
])
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "Generated user invite token reusable"
|
|
|
|
end
|
|
|
|
|
2019-04-08 05:01:28 -04:00
|
|
|
test "token is generated with max use and expires date" do
|
2019-04-06 05:58:22 -04:00
|
|
|
assert capture_io(fn ->
|
|
|
|
Mix.Tasks.Pleroma.User.run([
|
|
|
|
"invite",
|
|
|
|
"--max-use",
|
|
|
|
"5",
|
2019-04-08 05:01:28 -04:00
|
|
|
"--expires-at",
|
2019-04-06 05:58:22 -04:00
|
|
|
Date.to_string(Date.utc_today())
|
|
|
|
])
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "Generated user invite token reusable date limited"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-06 11:38:35 -04:00
|
|
|
describe "running invites" do
|
2019-04-06 05:58:22 -04:00
|
|
|
test "invites are listed" do
|
2019-04-06 09:24:22 -04:00
|
|
|
{:ok, invite} = Pleroma.UserInviteToken.create_invite()
|
2019-04-06 05:58:22 -04:00
|
|
|
|
|
|
|
{:ok, invite2} =
|
2019-04-08 05:01:28 -04:00
|
|
|
Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
|
2019-04-06 05:58:22 -04:00
|
|
|
|
2019-04-06 11:38:35 -04:00
|
|
|
# assert capture_io(fn ->
|
|
|
|
Mix.Tasks.Pleroma.User.run([
|
|
|
|
"invites"
|
|
|
|
])
|
|
|
|
|
|
|
|
# end)
|
2019-04-06 05:58:22 -04:00
|
|
|
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert_received {:mix_shell, :info, [message2]}
|
|
|
|
assert_received {:mix_shell, :info, [message3]}
|
|
|
|
assert message =~ "Invites list:"
|
2019-04-06 09:49:43 -04:00
|
|
|
assert message2 =~ invite.invite_type
|
|
|
|
assert message3 =~ invite2.invite_type
|
2019-04-06 05:58:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-06 11:38:35 -04:00
|
|
|
describe "running revoke_invite" do
|
2019-04-06 05:58:22 -04:00
|
|
|
test "invite is revoked" do
|
2019-04-08 05:01:28 -04:00
|
|
|
{:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
|
2019-04-06 05:58:22 -04:00
|
|
|
|
|
|
|
assert capture_io(fn ->
|
|
|
|
Mix.Tasks.Pleroma.User.run([
|
2019-04-06 11:38:35 -04:00
|
|
|
"revoke_invite",
|
2019-04-06 05:58:22 -04:00
|
|
|
invite.token
|
|
|
|
])
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message =~ "Invite for token #{invite.token} was revoked."
|
2018-12-15 19:14:23 -05:00
|
|
|
end
|
|
|
|
end
|
2019-04-02 05:30:11 -04:00
|
|
|
|
|
|
|
describe "running delete_activities" do
|
|
|
|
test "activities are deleted" do
|
|
|
|
%{nickname: nickname} = insert(:user)
|
|
|
|
|
|
|
|
assert :ok == Mix.Tasks.Pleroma.User.run(["delete_activities", nickname])
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message == "User #{nickname} statuses deleted."
|
|
|
|
end
|
|
|
|
end
|
2019-05-16 09:23:41 -04:00
|
|
|
|
|
|
|
describe "running toggle_confirmed" do
|
|
|
|
test "user is confirmed" do
|
|
|
|
%{id: id, nickname: nickname} = insert(:user, info: %{confirmation_pending: false})
|
|
|
|
|
|
|
|
assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message == "#{nickname} needs confirmation."
|
|
|
|
|
|
|
|
user = Repo.get(User, id)
|
|
|
|
assert user.info.confirmation_pending
|
|
|
|
assert user.info.confirmation_token
|
|
|
|
end
|
|
|
|
|
|
|
|
test "user is not confirmed" do
|
|
|
|
%{id: id, nickname: nickname} =
|
|
|
|
insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
|
|
|
|
|
|
|
|
assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
|
|
|
|
assert_received {:mix_shell, :info, [message]}
|
|
|
|
assert message == "#{nickname} doesn't need confirmation."
|
|
|
|
|
|
|
|
user = Repo.get(User, id)
|
|
|
|
refute user.info.confirmation_pending
|
|
|
|
refute user.info.confirmation_token
|
|
|
|
end
|
|
|
|
end
|
2018-12-15 19:14:23 -05:00
|
|
|
end
|