mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 01:00:29 +08:00
htpasswd: raise error on del if user is not exist
This commit is contained in:
parent
30a82efea4
commit
dd3f4c16e3
@ -136,8 +136,15 @@ def _cmd_set(config: Section, options: argparse.Namespace) -> None:
|
|||||||
|
|
||||||
def _cmd_delete(config: Section, options: argparse.Namespace) -> None:
|
def _cmd_delete(config: Section, options: argparse.Namespace) -> None:
|
||||||
with _get_htpasswd_for_write(config) as htpasswd:
|
with _get_htpasswd_for_write(config) as htpasswd:
|
||||||
|
assert options.user == options.user.strip()
|
||||||
|
assert options.user
|
||||||
|
|
||||||
has_user = (options.user in htpasswd.users())
|
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)
|
htpasswd.delete(options.user)
|
||||||
|
|
||||||
if has_user and not options.quiet:
|
if has_user and not options.quiet:
|
||||||
_print_invalidate_tip(False)
|
_print_invalidate_tip(False)
|
||||||
|
|
||||||
|
|||||||
@ -49,7 +49,9 @@ def _htpasswd_fixture(request) -> Generator[KvmdHtpasswdFile, None, None]: # ty
|
|||||||
for user in request.param:
|
for user in request.param:
|
||||||
htpasswd.set_password(user, _make_passwd(user))
|
htpasswd.set_password(user, _make_passwd(user))
|
||||||
htpasswd.save()
|
htpasswd.save()
|
||||||
|
try:
|
||||||
yield htpasswd
|
yield htpasswd
|
||||||
|
finally:
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
|
|
||||||
|
|
||||||
@ -154,7 +156,9 @@ def test_ok__del(htpasswd: KvmdHtpasswdFile) -> None:
|
|||||||
|
|
||||||
if old_users:
|
if old_users:
|
||||||
assert htpasswd.check_password("admin", _make_passwd("admin"))
|
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)
|
_run_htpasswd(["del", "admin"], htpasswd.path)
|
||||||
|
|
||||||
htpasswd.load(force=True)
|
htpasswd.load(force=True)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user