htpasswd: raise error on del if user is not exist

This commit is contained in:
Maxim Devaev 2025-02-13 14:20:33 +02:00
parent 30a82efea4
commit dd3f4c16e3
2 changed files with 14 additions and 3 deletions

View File

@ -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)

View File

@ -49,7 +49,9 @@ def _htpasswd_fixture(request) -> Generator[KvmdHtpasswdFile, None, None]: # ty
for user in request.param:
htpasswd.set_password(user, _make_passwd(user))
htpasswd.save()
try:
yield htpasswd
finally:
os.remove(path)
@ -154,7 +156,9 @@ def test_ok__del(htpasswd: KvmdHtpasswdFile) -> None:
if old_users:
assert htpasswd.check_password("admin", _make_passwd("admin"))
_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)