pleroma/test/tasks/user_test.exs

541 lines
16 KiB
Elixir
Raw Normal View History

2018-12-23 20:11:29 +00:00
# Pleroma: A lightweight social networking server
2020-03-03 22:44:49 +00:00
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
2018-12-23 20:11:29 +00:00
# SPDX-License-Identifier: AGPL-3.0-only
2018-12-16 00:14:23 +00:00
defmodule Mix.Tasks.Pleroma.UserTest do
alias Pleroma.Activity
alias Pleroma.MFA
alias Pleroma.Object
alias Pleroma.Repo
alias Pleroma.Tests.ObanHelpers
2018-12-16 00:14:23 +00:00
alias Pleroma.User
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.OAuth.Authorization
alias Pleroma.Web.OAuth.Token
2018-12-16 00:14:23 +00:00
use Pleroma.DataCase
use Oban.Testing, repo: Pleroma.Repo
2018-12-16 00:14:23 +00:00
import ExUnit.CaptureIO
import Mock
import Pleroma.Factory
2018-12-16 00:14:23 +00:00
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 00:14:23 +00:00
assert_received {:mix_shell, :yes?, [message]}
assert message =~ "Continue"
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ "created"
2019-04-22 07:20:43 +00:00
user = User.get_cached_by_nickname(unsaved.nickname)
2018-12-16 00:14:23 +00:00
assert user.name == unsaved.name
assert user.email == unsaved.email
assert user.bio == unsaved.bio
assert user.is_moderator
assert user.is_admin
2018-12-16 00:14:23 +00:00
end
test "user is not created" do
unsaved = build(:user)
# prepare to answer no
send(self(), {:mix_shell_input, :yes?, false})
2018-12-16 00:14:23 +00:00
Mix.Tasks.Pleroma.User.run(["new", unsaved.nickname, unsaved.email])
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ "user will be created"
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :yes?, [message]}
assert message =~ "Continue"
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ "will not be created"
2019-04-22 07:20:43 +00:00
refute User.get_cached_by_nickname(unsaved.nickname)
2018-12-16 00:14:23 +00:00
end
end
describe "running rm" do
test "user is deleted" do
2020-05-21 11:16:21 +00:00
clear_config([:instance, :federating], true)
2018-12-16 00:14:23 +00:00
user = insert(:user)
with_mock Pleroma.Web.Federator,
publish: fn _ -> nil end do
Mix.Tasks.Pleroma.User.run(["rm", user.nickname])
ObanHelpers.perform_all()
assert_received {:mix_shell, :info, [message]}
assert message =~ " deleted"
assert %{deactivated: true} = User.get_by_nickname(user.nickname)
2018-12-16 00:14:23 +00:00
assert called(Pleroma.Web.Federator.publish(:_))
end
2018-12-16 00:14:23 +00:00
end
test "a remote user's create activity is deleted when the object has been pruned" do
user = insert(:user)
2020-05-12 19:59:26 +00:00
{:ok, post} = CommonAPI.post(user, %{status: "uguu"})
2020-05-21 11:16:21 +00:00
clear_config([:instance, :federating], true)
object = Object.normalize(post)
Object.prune(object)
with_mock Pleroma.Web.Federator,
publish: fn _ -> nil end do
Mix.Tasks.Pleroma.User.run(["rm", user.nickname])
ObanHelpers.perform_all()
assert_received {:mix_shell, :info, [message]}
assert message =~ " deleted"
assert %{deactivated: true} = User.get_by_nickname(user.nickname)
assert called(Pleroma.Web.Federator.publish(:_))
end
refute Activity.get_by_id(post.id)
end
2018-12-16 00:14:23 +00:00
test "no user to delete" do
Mix.Tasks.Pleroma.User.run(["rm", "nonexistent"])
2018-12-16 00:14:23 +00: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 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ " deactivated"
2019-04-22 07:20:43 +00:00
user = User.get_cached_by_nickname(user.nickname)
assert user.deactivated
2018-12-16 00:14:23 +00:00
end
test "user is activated" do
user = insert(:user, deactivated: true)
2018-12-16 00:14:23 +00:00
Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ " activated"
2019-04-22 07:20:43 +00:00
user = User.get_cached_by_nickname(user.nickname)
refute user.deactivated
2018-12-16 00:14:23 +00:00
end
test "no user to toggle" do
Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistent"])
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :error, [message]}
assert message =~ "No user"
end
end
describe "running deactivate" do
2018-12-16 00:14:23 +00:00
test "user is unsubscribed" do
followed = insert(:user)
remote_followed = insert(:user, local: false)
user = insert(:user)
User.follow(user, followed, :follow_accept)
User.follow(user, remote_followed, :follow_accept)
2018-12-16 00:14:23 +00:00
Mix.Tasks.Pleroma.User.run(["deactivate", user.nickname])
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ "Deactivating"
2018-12-16 00:14:23 +00:00
# Note that the task has delay :timer.sleep(500)
assert_received {:mix_shell, :info, [message]}
assert message =~ "Successfully unsubscribed"
2019-04-22 07:20:43 +00:00
user = User.get_cached_by_nickname(user.nickname)
assert Enum.empty?(Enum.filter(User.get_friends(user), & &1.local))
assert user.deactivated
2018-12-16 00:14:23 +00:00
end
test "no user to deactivate" do
Mix.Tasks.Pleroma.User.run(["deactivate", "nonexistent"])
2018-12-16 00:14:23 +00: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 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ ~r/Moderator status .* true/
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ ~r/Locked status .* true/
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ ~r/Admin status .* true/
2018-12-16 00:14:23 +00:00
2019-04-22 07:20:43 +00:00
user = User.get_cached_by_nickname(user.nickname)
assert user.is_moderator
assert user.locked
assert user.is_admin
2018-12-16 00:14:23 +00:00
end
test "All statuses unset" do
user = insert(:user, locked: true, is_moderator: true, is_admin: true)
2018-12-16 00:14:23 +00:00
Mix.Tasks.Pleroma.User.run([
"set",
user.nickname,
"--no-moderator",
"--no-admin",
"--no-locked"
])
assert_received {:mix_shell, :info, [message]}
assert message =~ ~r/Moderator status .* false/
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ ~r/Locked status .* false/
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :info, [message]}
assert message =~ ~r/Admin status .* false/
2018-12-16 00:14:23 +00:00
2019-04-22 07:20:43 +00:00
user = User.get_cached_by_nickname(user.nickname)
refute user.is_moderator
refute user.locked
refute user.is_admin
2018-12-16 00:14:23 +00:00
end
test "no user to set status" do
Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
2018-12-16 00:14:23 +00: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
Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
2018-12-16 00:14:23 +00:00
assert_received {:mix_shell, :error, [message]}
assert message =~ "No local user"
end
end
describe "running reset_mfa" do
test "disables MFA" do
user =
insert(:user,
multi_factor_authentication_settings: %MFA.Settings{
enabled: true,
totp: %MFA.Settings.TOTP{secret: "xx", confirmed: true}
}
)
Mix.Tasks.Pleroma.User.run(["reset_mfa", user.nickname])
assert_received {:mix_shell, :info, [message]}
assert message == "Multi-Factor Authentication disabled for #{user.nickname}"
assert %{enabled: false, totp: false} ==
user.nickname
|> User.get_cached_by_nickname()
|> MFA.mfa_settings()
end
test "no user to reset MFA" do
Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
assert_received {:mix_shell, :error, [message]}
assert message =~ "No local user"
end
end
2018-12-16 00:14:23 +00:00
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 09:58:22 +00:00
assert message =~ "Generated user invite token one time"
end
test "token is generated with expires_at" do
2019-04-06 09:58:22 +00:00
assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
"invite",
"--expires-at",
2019-04-06 09:58:22 +00: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
test "token is generated with max use and expires date" do
2019-04-06 09:58:22 +00:00
assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
"invite",
"--max-use",
"5",
"--expires-at",
2019-04-06 09:58:22 +00: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 15:38:35 +00:00
describe "running invites" do
2019-04-06 09:58:22 +00:00
test "invites are listed" do
2019-04-06 13:24:22 +00:00
{:ok, invite} = Pleroma.UserInviteToken.create_invite()
2019-04-06 09:58:22 +00:00
{:ok, invite2} =
Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
2019-04-06 09:58:22 +00:00
2019-04-06 15:38:35 +00:00
# assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
"invites"
])
# end)
2019-04-06 09:58:22 +00: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 13:49:43 +00:00
assert message2 =~ invite.invite_type
assert message3 =~ invite2.invite_type
2019-04-06 09:58:22 +00:00
end
end
2019-04-06 15:38:35 +00:00
describe "running revoke_invite" do
2019-04-06 09:58:22 +00:00
test "invite is revoked" do
{:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
2019-04-06 09:58:22 +00:00
assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
2019-04-06 15:38:35 +00:00
"revoke_invite",
2019-04-06 09:58:22 +00:00
invite.token
])
end)
assert_received {:mix_shell, :info, [message]}
assert message =~ "Invite for token #{invite.token} was revoked."
2018-12-16 00:14:23 +00:00
end
test "it prints an error message when invite is not exist" do
Mix.Tasks.Pleroma.User.run(["revoke_invite", "foo"])
assert_received {:mix_shell, :error, [message]}
assert message =~ "No invite found"
end
2018-12-16 00:14:23 +00:00
end
2019-04-02 09:30:11 +00: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
test "it prints an error message when user is not exist" do
Mix.Tasks.Pleroma.User.run(["delete_activities", "foo"])
assert_received {:mix_shell, :error, [message]}
assert message =~ "No local user"
end
2019-04-02 09:30:11 +00:00
end
2019-05-16 13:23:41 +00:00
describe "running toggle_confirmed" do
test "user is confirmed" do
%{id: id, nickname: nickname} = insert(:user, confirmation_pending: false)
2019-05-16 13:23:41 +00:00
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.confirmation_pending
assert user.confirmation_token
2019-05-16 13:23:41 +00:00
end
test "user is not confirmed" do
%{id: id, nickname: nickname} =
insert(:user, confirmation_pending: true, confirmation_token: "some token")
2019-05-16 13:23:41 +00:00
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.confirmation_pending
refute user.confirmation_token
2019-05-16 13:23:41 +00:00
end
test "it prints an error message when user is not exist" do
Mix.Tasks.Pleroma.User.run(["toggle_confirmed", "foo"])
assert_received {:mix_shell, :error, [message]}
assert message =~ "No local user"
end
2019-05-16 13:23:41 +00:00
end
describe "search" do
test "it returns users matching" do
user = insert(:user)
moon = insert(:user, nickname: "moon", name: "fediverse expert moon")
moot = insert(:user, nickname: "moot")
kawen = insert(:user, nickname: "kawen", name: "fediverse expert moon")
{:ok, user} = User.follow(user, kawen)
assert [moon.id, kawen.id] == User.Search.search("moon") |> Enum.map(& &1.id)
res = User.search("moo") |> Enum.map(& &1.id)
assert moon.id in res
assert moot.id in res
assert kawen.id in res
assert [moon.id, kawen.id] == User.Search.search("moon fediverse") |> Enum.map(& &1.id)
assert [kawen.id, moon.id] ==
User.Search.search("moon fediverse", for_user: user) |> Enum.map(& &1.id)
end
end
describe "signing out" do
test "it deletes all user's tokens and authorizations" do
user = insert(:user)
insert(:oauth_token, user: user)
insert(:oauth_authorization, user: user)
assert Repo.get_by(Token, user_id: user.id)
assert Repo.get_by(Authorization, user_id: user.id)
:ok = Mix.Tasks.Pleroma.User.run(["sign_out", user.nickname])
refute Repo.get_by(Token, user_id: user.id)
refute Repo.get_by(Authorization, user_id: user.id)
end
test "it prints an error message when user is not exist" do
Mix.Tasks.Pleroma.User.run(["sign_out", "foo"])
assert_received {:mix_shell, :error, [message]}
assert message =~ "No local user"
end
end
describe "tagging" do
test "it add tags to a user" do
user = insert(:user)
:ok = Mix.Tasks.Pleroma.User.run(["tag", user.nickname, "pleroma"])
user = User.get_cached_by_nickname(user.nickname)
assert "pleroma" in user.tags
end
test "it prints an error message when user is not exist" do
Mix.Tasks.Pleroma.User.run(["tag", "foo"])
assert_received {:mix_shell, :error, [message]}
assert message =~ "Could not change user tags"
end
end
describe "untagging" do
test "it deletes tags from a user" do
user = insert(:user, tags: ["pleroma"])
assert "pleroma" in user.tags
:ok = Mix.Tasks.Pleroma.User.run(["untag", user.nickname, "pleroma"])
user = User.get_cached_by_nickname(user.nickname)
assert Enum.empty?(user.tags)
end
test "it prints an error message when user is not exist" do
Mix.Tasks.Pleroma.User.run(["untag", "foo"])
assert_received {:mix_shell, :error, [message]}
assert message =~ "Could not change user tags"
end
end
2018-12-16 00:14:23 +00:00
end