From f868dcf3acc0fd687a4a74e74f6e150ef565f787 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 25 Jan 2021 10:48:28 -0600 Subject: [PATCH] Ensure consistent behavior between pleroma.user activate/deactivate mix tasks --- lib/mix/tasks/pleroma/user.ex | 9 ++++++--- test/mix/tasks/pleroma/user_test.exs | 22 ++++++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index bb9a080a4..53d5fc6d9 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -162,17 +162,20 @@ defmodule Mix.Tasks.Pleroma.User do def run(["deactivate", nickname]) do start_pleroma() - with %User{} = user <- User.get_cached_by_nickname(nickname) do - shell_info("Deactivating #{user.nickname}") + with %User{} = user <- User.get_cached_by_nickname(nickname), + true <- user.is_active do User.set_activation(user, false) :timer.sleep(500) user = User.get_cached_by_id(user.id) if Enum.empty?(Enum.filter(User.get_friends(user), & &1.local)) do - shell_info("Successfully unsubscribed all local followers from #{user.nickname}") + shell_info("Successfully deactivated #{nickname} and unsubscribed all local followers") end else + false -> + shell_info("User #{nickname} already deactivated") + _ -> shell_error("No user #{nickname}") end diff --git a/test/mix/tasks/pleroma/user_test.exs b/test/mix/tasks/pleroma/user_test.exs index 768beb0a6..a2178bbd1 100644 --- a/test/mix/tasks/pleroma/user_test.exs +++ b/test/mix/tasks/pleroma/user_test.exs @@ -158,7 +158,7 @@ defmodule Mix.Tasks.Pleroma.UserTest do end describe "running deactivate" do - test "user is unsubscribed" do + test "active user is deactivated and unsubscribed" do followed = insert(:user) remote_followed = insert(:user, local: false) user = insert(:user) @@ -168,18 +168,28 @@ defmodule Mix.Tasks.Pleroma.UserTest do Mix.Tasks.Pleroma.User.run(["deactivate", user.nickname]) - assert_received {:mix_shell, :info, [message]} - assert message =~ "Deactivating" - # Note that the task has delay :timer.sleep(500) assert_received {:mix_shell, :info, [message]} - assert message =~ "Successfully unsubscribed" + + assert message == + "Successfully deactivated #{user.nickname} and unsubscribed all local followers" user = User.get_cached_by_nickname(user.nickname) assert Enum.empty?(Enum.filter(User.get_friends(user), & &1.local)) refute user.is_active end + test "user is deactivated" do + %{id: id, nickname: nickname} = insert(:user, is_active: false) + + assert :ok = Mix.Tasks.Pleroma.User.run(["deactivate", nickname]) + assert_received {:mix_shell, :info, [message]} + assert message == "User #{nickname} already deactivated" + + user = Repo.get(User, id) + refute user.is_active + end + test "no user to deactivate" do Mix.Tasks.Pleroma.User.run(["deactivate", "nonexistent"]) @@ -479,7 +489,7 @@ defmodule Mix.Tasks.Pleroma.UserTest do assert user.is_active end - test "it prints an error message when user is not exist" do + test "no user to activate" do Mix.Tasks.Pleroma.User.run(["activate", "foo"]) assert_received {:mix_shell, :error, [message]}