mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 09:01:54 +08:00
moar validators
This commit is contained in:
@@ -35,7 +35,8 @@ from kvmd.apps.cleanup import main
|
||||
def test_ok(tmpdir) -> None: # type: ignore
|
||||
queue: multiprocessing.queues.Queue = multiprocessing.Queue()
|
||||
|
||||
ustreamer_fake_name = "ustr-" + secrets.token_hex(3)
|
||||
ustreamer_tmp_path = os.path.abspath(str(tmpdir.join("ustr-" + secrets.token_hex(3))))
|
||||
os.symlink("/usr/bin/ustreamer", ustreamer_tmp_path)
|
||||
|
||||
ustreamer_sock_path = os.path.abspath(str(tmpdir.join("ustreamer-fake.sock")))
|
||||
open(ustreamer_sock_path, "w").close()
|
||||
@@ -43,7 +44,7 @@ def test_ok(tmpdir) -> None: # type: ignore
|
||||
open(kvmd_sock_path, "w").close()
|
||||
|
||||
def ustreamer_fake() -> None:
|
||||
setproctitle.setproctitle(ustreamer_fake_name)
|
||||
setproctitle.setproctitle(os.path.basename(ustreamer_tmp_path))
|
||||
queue.put(True)
|
||||
while True:
|
||||
time.sleep(1)
|
||||
@@ -60,7 +61,7 @@ def test_ok(tmpdir) -> None: # type: ignore
|
||||
"kvmd/server/unix=" + kvmd_sock_path,
|
||||
"kvmd/streamer/port=0",
|
||||
"kvmd/streamer/unix=" + ustreamer_sock_path,
|
||||
"kvmd/streamer/cmd=[\"%s\"]" % (ustreamer_fake_name),
|
||||
"kvmd/streamer/cmd=" + ustreamer_tmp_path,
|
||||
])
|
||||
|
||||
assert not os.path.exists(ustreamer_sock_path)
|
||||
|
||||
@@ -20,12 +20,14 @@
|
||||
# ========================================================================== #
|
||||
|
||||
|
||||
from typing import List
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from kvmd.validators import ValidatorError
|
||||
from kvmd.validators.auth import valid_user
|
||||
from kvmd.validators.auth import valid_users_list
|
||||
from kvmd.validators.auth import valid_passwd
|
||||
from kvmd.validators.auth import valid_auth_token
|
||||
|
||||
@@ -58,6 +60,27 @@ def test_fail__valid_user(arg: Any) -> None:
|
||||
print(valid_user(arg))
|
||||
|
||||
|
||||
# =====
|
||||
@pytest.mark.parametrize("arg, retval", [
|
||||
("foo, bar, ", ["foo", "bar"]),
|
||||
("foo bar", ["foo", "bar"]),
|
||||
(["foo", "bar"], ["foo", "bar"]),
|
||||
("", []),
|
||||
(" ", []),
|
||||
(", ", []),
|
||||
(", foo, ", ["foo"]),
|
||||
([], []),
|
||||
])
|
||||
def test_ok__valid_users_list(arg: Any, retval: List) -> None:
|
||||
assert valid_users_list(arg) == retval
|
||||
|
||||
|
||||
@pytest.mark.parametrize("arg", [None, [None], [""], [" "], ["user,"]])
|
||||
def test_fail__valid_users_list(arg: Any) -> None: # pylint: disable=invalid-name
|
||||
with pytest.raises(ValidatorError):
|
||||
print(valid_users_list(arg))
|
||||
|
||||
|
||||
# =====
|
||||
@pytest.mark.parametrize("arg", [
|
||||
"glados",
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
# ========================================================================== #
|
||||
|
||||
|
||||
from typing import List
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
@@ -29,6 +30,7 @@ from kvmd.validators.basic import valid_bool
|
||||
from kvmd.validators.basic import valid_number
|
||||
from kvmd.validators.basic import valid_int_f1
|
||||
from kvmd.validators.basic import valid_float_f01
|
||||
from kvmd.validators.basic import valid_string_list
|
||||
|
||||
|
||||
# =====
|
||||
@@ -105,3 +107,38 @@ def test_ok__valid_float_f01(arg: Any) -> None:
|
||||
def test_fail__valid_float_f01(arg: Any) -> None:
|
||||
with pytest.raises(ValidatorError):
|
||||
print(valid_float_f01(arg))
|
||||
|
||||
|
||||
# =====
|
||||
@pytest.mark.parametrize("arg, retval", [
|
||||
("a, b, c", ["a", "b", "c"]),
|
||||
("a b c", ["a", "b", "c"]),
|
||||
(["a", "b", "c"], ["a", "b", "c"]),
|
||||
("", []),
|
||||
(" ", []),
|
||||
(", ", []),
|
||||
(", a, ", ["a"]),
|
||||
([], []),
|
||||
])
|
||||
def test_ok__valid_string_list(arg: Any, retval: List) -> None:
|
||||
assert valid_string_list(arg) == retval
|
||||
|
||||
|
||||
@pytest.mark.parametrize("arg, retval", [
|
||||
("1, 2, 3", [1, 2, 3]),
|
||||
("1 2 3", [1, 2, 3]),
|
||||
([1, 2, 3], [1, 2, 3]),
|
||||
("", []),
|
||||
(" ", []),
|
||||
(", ", []),
|
||||
(", 1, ", [1]),
|
||||
([], []),
|
||||
])
|
||||
def test_ok__valid_string_list__subval(arg: Any, retval: List) -> None: # pylint: disable=invalid-name
|
||||
assert valid_string_list(arg, subval=int) == retval
|
||||
|
||||
|
||||
@pytest.mark.parametrize("arg", [None, [None]])
|
||||
def test_fail__valid_string_list(arg: Any) -> None: # pylint: disable=invalid-name
|
||||
with pytest.raises(ValidatorError):
|
||||
print(valid_string_list(arg))
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
import os
|
||||
|
||||
from typing import List
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
@@ -30,6 +31,7 @@ from kvmd.validators import ValidatorError
|
||||
from kvmd.validators.fs import valid_abs_path
|
||||
from kvmd.validators.fs import valid_abs_path_exists
|
||||
from kvmd.validators.fs import valid_unix_mode
|
||||
from kvmd.validators.fs import valid_command
|
||||
|
||||
|
||||
# =====
|
||||
@@ -89,3 +91,28 @@ def test_ok__valid_unix_mode(arg: Any) -> None:
|
||||
def test_fail__valid_unix_mode(arg: Any) -> None:
|
||||
with pytest.raises(ValidatorError):
|
||||
print(valid_unix_mode(arg))
|
||||
|
||||
|
||||
# =====
|
||||
@pytest.mark.parametrize("arg, retval", [
|
||||
(["/bin/true"], ["/bin/true"]),
|
||||
(["/bin/true", 1, 2, 3], ["/bin/true", "1", "2", "3"]),
|
||||
("/bin/true, 1, 2, 3,", ["/bin/true", "1", "2", "3"]),
|
||||
("/bin/true", ["/bin/true"]),
|
||||
])
|
||||
def test_ok__valid_command(arg: Any, retval: List[str]) -> None:
|
||||
assert valid_command(arg) == retval
|
||||
|
||||
|
||||
@pytest.mark.parametrize("arg", [
|
||||
["/bin/blahblahblah"],
|
||||
["/bin/blahblahblah", 1, 2, 3],
|
||||
[" "],
|
||||
[],
|
||||
"/bin/blahblahblah, 1, 2, 3,",
|
||||
"/bin/blahblahblah",
|
||||
" ",
|
||||
])
|
||||
def test_fail__valid_command(arg: Any) -> None:
|
||||
with pytest.raises(ValidatorError):
|
||||
print(valid_command(arg))
|
||||
|
||||
Reference in New Issue
Block a user