From dd3f4c16e3a21793b38c588892a0e7467ab7482b Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Thu, 13 Feb 2025 14:20:33 +0200 Subject: [PATCH] htpasswd: raise error on del if user is not exist --- kvmd/apps/htpasswd/__init__.py | 7 +++++++ testenv/tests/apps/htpasswd/test_main.py | 10 +++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/kvmd/apps/htpasswd/__init__.py b/kvmd/apps/htpasswd/__init__.py index f6f9177b..244c30f4 100644 --- a/kvmd/apps/htpasswd/__init__.py +++ b/kvmd/apps/htpasswd/__init__.py @@ -136,8 +136,15 @@ def _cmd_set(config: Section, options: argparse.Namespace) -> None: def _cmd_delete(config: Section, options: argparse.Namespace) -> None: with _get_htpasswd_for_write(config) as htpasswd: + assert options.user == options.user.strip() + assert options.user + has_user = (options.user in htpasswd.users()) + if not has_user: + raise SystemExit(f"The user {options.user!r} is not exist") + htpasswd.delete(options.user) + if has_user and not options.quiet: _print_invalidate_tip(False) diff --git a/testenv/tests/apps/htpasswd/test_main.py b/testenv/tests/apps/htpasswd/test_main.py index 8ce70cf6..be48a3ed 100644 --- a/testenv/tests/apps/htpasswd/test_main.py +++ b/testenv/tests/apps/htpasswd/test_main.py @@ -49,8 +49,10 @@ def _htpasswd_fixture(request) -> Generator[KvmdHtpasswdFile, None, None]: # ty for user in request.param: htpasswd.set_password(user, _make_passwd(user)) htpasswd.save() - yield htpasswd - os.remove(path) + try: + yield htpasswd + finally: + os.remove(path) def _run_htpasswd(cmd: list[str], htpasswd_path: str, int_type: str="htpasswd") -> None: @@ -154,8 +156,10 @@ def test_ok__del(htpasswd: KvmdHtpasswdFile) -> None: if old_users: assert htpasswd.check_password("admin", _make_passwd("admin")) + _run_htpasswd(["del", "admin"], htpasswd.path) - _run_htpasswd(["del", "admin"], htpasswd.path) + with pytest.raises(SystemExit, match="The user 'admin' is not exist"): + _run_htpasswd(["del", "admin"], htpasswd.path) htpasswd.load(force=True) assert not htpasswd.check_password("admin", _make_passwd("admin"))