Skip to content

Commit 52eb67a

Browse files
author
Juliya Smith
authored
Bugfix: make profile name required for delete (#175)
1 parent e7b2d21 commit 52eb67a

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
1010

1111
## Unreleased
1212

13+
### Fixed
14+
15+
- Issue where `code42 profile delete` was allowed without giving a `profile_name` even
16+
though deleting the default profile is not allowed.
17+
1318
### Added
1419

1520
- `code42 audit-logs` commands:
@@ -18,6 +23,8 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
1823

1924
### Changed
2025

26+
- `profile_name` argument is now required for `code42 profile delete`, as it was meant to be.
27+
2128
- The `--advanced-query` option on `alerts search` and `security-data (search|send-to)` commands has been updated:
2229
- It can now accept the query as a JSON string or as the path to a file containing the JSON query.
2330
- It can be used with the `--use-checkpoint/-c` option.

src/code42cli/cmds/profile.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ def profile():
1818
pass
1919

2020

21-
profile_name_arg = click.argument("profile_name", required=False)
21+
def profile_name_arg(required=False):
22+
return click.argument("profile_name", required=required)
23+
24+
2225
name_option = click.option(
2326
"-n",
2427
"--name",
@@ -48,7 +51,7 @@ def profile():
4851

4952

5053
@profile.command()
51-
@profile_name_arg
54+
@profile_name_arg()
5255
def show(profile_name):
5356
"""Print the details of a profile."""
5457
c42profile = cliprofile.get_profile(profile_name)
@@ -96,7 +99,7 @@ def update(name, server, username, password, disable_ssl_errors):
9699

97100

98101
@profile.command()
99-
@profile_name_arg
102+
@profile_name_arg()
100103
def reset_pw(profile_name):
101104
"""\b
102105
Change the stored password for a profile. Only affects what's stored in the local profile,
@@ -117,7 +120,7 @@ def _list():
117120

118121

119122
@profile.command()
120-
@profile_name_arg
123+
@profile_name_arg()
121124
def use(profile_name):
122125
"""Set a profile as the default."""
123126
cliprofile.switch_default_profile(profile_name)
@@ -126,7 +129,7 @@ def use(profile_name):
126129

127130
@profile.command()
128131
@yes_option
129-
@profile_name_arg
132+
@profile_name_arg(required=True)
130133
def delete(profile_name):
131134
"""Deletes a profile and its stored password (if any)."""
132135
message = "\nDeleting this profile will also delete any stored passwords and checkpoints. Are you sure? (y/n): "

src/code42cli/profile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def create_profile(name, server, username, ignore_ssl_errors):
108108

109109
def delete_profile(profile_name):
110110
profile = _get_profile(profile_name)
111+
profile_name = profile.name
111112
if password.get_stored_password(profile) is not None:
112113
password.delete_password(profile)
113114
cursor_stores = get_all_cursor_stores_for_profile(profile_name)

tests/cmds/test_profile.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,12 @@ def test_delete_profile_warns_if_deleting_default(runner, mock_cliprofile_namesp
333333
assert "'mockdefault' is currently the default profile!" in result.output
334334

335335

336+
def test_delete_profile_requires_profile_name_arg(runner, mock_cliprofile_namespace):
337+
result = runner.invoke(cli, ["profile", "delete"])
338+
assert "Error: Missing argument 'PROFILE_NAME'." in result.output
339+
assert mock_cliprofile_namespace.delete_profile.call_count == 0
340+
341+
336342
def test_delete_profile_does_nothing_if_user_doesnt_agree(
337343
runner, user_disagreement, mock_cliprofile_namespace
338344
):

tests/test_profile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ def test_set_password_uses_expected_password(config_accessor, password_setter):
201201
assert password_setter.call_args[0][1] == "newpassword"
202202

203203

204+
def test_delete_profile_deletes_profile(config_accessor, mocker):
205+
name = "deleteme"
206+
profile = create_mock_profile(name)
207+
mock_get_profile = mocker.patch("code42cli.profile._get_profile")
208+
mock_get_profile.return_value = profile
209+
cliprofile.delete_profile(name)
210+
config_accessor.delete_profile.assert_called_once_with(name)
211+
212+
213+
def test_delete_profile_deletes_profile_from_object_name(config_accessor, mocker):
214+
expected = "deleteme - different name than the arg"
215+
profile = create_mock_profile(expected)
216+
mock_get_profile = mocker.patch("code42cli.profile._get_profile")
217+
mock_get_profile.return_value = profile
218+
cliprofile.delete_profile("deleteme")
219+
config_accessor.delete_profile.assert_called_once_with(expected)
220+
221+
204222
def test_delete_profile_deletes_password_if_exists(
205223
config_accessor, mocker, password_getter, password_deleter
206224
):

0 commit comments

Comments
 (0)