Skip to content

Commit bb44a92

Browse files
committed
feat(commands/delete): confirm unless forced
1 parent 56af4e6 commit bb44a92

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

compiler_admin/commands/delete.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def delete(args: Namespace) -> int:
2121
print(f"User does not exist: {account}")
2222
return RESULT_FAILURE
2323

24+
if getattr(args, "force", False) is False:
25+
cont = input(f"Delete account {account}? (Y/n)")
26+
if not cont.lower().startswith("y"):
27+
print("Aborting delete.")
28+
return RESULT_SUCCESS
29+
2430
print(f"User exists, deleting: {account}")
2531

2632
res = CallGAMCommand(("delete", "user", account, "noactionifalias"))

compiler_admin/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ def _subcmd(name, help, add_username_arg=True) -> argparse.ArgumentParser:
4949
"account_type", choices=ACCOUNT_TYPE_OU.keys(), help="Target account type for this conversion."
5050
)
5151

52-
_subcmd("delete", help="Delete a user account.")
52+
delete_parser = _subcmd("delete", help="Delete a user account.")
53+
delete_parser.add_argument(
54+
"--force", action="store_true", default=False, help="Don't ask for confirmation before deletion."
55+
)
5356

5457
offboard_parser = _subcmd("offboard", help="Offboard a user account.")
5558
offboard_parser.add_argument("--alias", help="Account to assign username as an alias.")

tests/commands/test_delete.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55
from compiler_admin.commands.delete import delete, __name__ as MODULE
66

77

8+
@pytest.fixture
9+
def mock_input_yes(mock_input):
10+
fix = mock_input(MODULE)
11+
fix.return_value = "y"
12+
return fix
13+
14+
15+
@pytest.fixture
16+
def mock_input_no(mock_input):
17+
fix = mock_input(MODULE)
18+
fix.return_value = "n"
19+
return fix
20+
21+
822
@pytest.fixture
923
def mock_google_user_exists(mock_google_user_exists):
1024
return mock_google_user_exists(MODULE)
@@ -22,7 +36,8 @@ def test_delete_user_username_required():
2236
delete(args)
2337

2438

25-
def test_delete_user_exists(mock_google_user_exists, mock_google_CallGAMCommand):
39+
@pytest.mark.usefixtures("mock_input_yes")
40+
def test_delete_confirm_yes(mock_google_user_exists, mock_google_CallGAMCommand):
2641
mock_google_user_exists.return_value = True
2742

2843
args = Namespace(username="username")
@@ -36,6 +51,17 @@ def test_delete_user_exists(mock_google_user_exists, mock_google_CallGAMCommand)
3651
assert "noactionifalias" in call_args
3752

3853

54+
@pytest.mark.usefixtures("mock_input_no")
55+
def test_delete_confirm_no(mock_google_user_exists, mock_google_CallGAMCommand):
56+
mock_google_user_exists.return_value = True
57+
58+
args = Namespace(username="username")
59+
res = delete(args)
60+
61+
assert res == RESULT_SUCCESS
62+
mock_google_CallGAMCommand.assert_not_called()
63+
64+
3965
def test_delete_user_does_not_exist(mock_google_user_exists, mock_google_CallGAMCommand):
4066
mock_google_user_exists.return_value = False
4167

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def __mock_module_name(module):
2222
return _mock_module_name
2323

2424

25+
@pytest.fixture
26+
def mock_input(mock_module_name):
27+
"""Fixture returns a function that patches the built-in input in a given module."""
28+
return mock_module_name("input")
29+
30+
2531
@pytest.fixture
2632
def mock_commands_create(mock_module_name):
2733
"""Fixture returns a function that patches commands.create in a given module."""

tests/test_main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,15 @@ def test_main_delete(mock_commands_delete):
8888

8989
mock_commands_delete.assert_called_once()
9090
call_args = mock_commands_delete.call_args.args
91-
assert Namespace(command="delete", username="username") in call_args
91+
assert Namespace(command="delete", username="username", force=False) in call_args
92+
93+
94+
def test_main_delete_force(mock_commands_delete):
95+
main(argv=["delete", "username", "--force"])
96+
97+
mock_commands_delete.assert_called_once()
98+
call_args = mock_commands_delete.call_args.args
99+
assert Namespace(command="delete", username="username", force=True) in call_args
92100

93101

94102
def test_main_delete_no_username(mock_commands_delete):

0 commit comments

Comments
 (0)