summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2019-04-11 04:18:02 +0300
committerDevaev Maxim <[email protected]>2019-04-11 04:18:02 +0300
commit060140d6540d045530ceefda6e639f39588e7dab (patch)
tree88c3bd55f6d7dcdda2e07f2b6126e1c15268da66
parentc59f8bdaf1ade25f26085a70940771503345d1e8 (diff)
better auth tests, refactoring
-rw-r--r--tests/apps/__init__.py20
-rw-r--r--tests/apps/test_cleanup.py (renamed from tests/test_app_cleanup.py)0
-rw-r--r--tests/apps/test_htpasswd.py (renamed from tests/test_app_htpasswd.py)0
-rw-r--r--tests/auth/__init__.py43
-rw-r--r--tests/auth/test_service_htpasswd.py (renamed from tests/test_auth_service_htpasswd.py)25
-rw-r--r--tests/auth/test_service_http.py (renamed from tests/test_auth_service_http.py)21
-rw-r--r--tests/validators/__init__.py20
-rw-r--r--tests/validators/test_auth.py (renamed from tests/test_validators_auth.py)0
-rw-r--r--tests/validators/test_basic.py (renamed from tests/test_validators_basic.py)0
-rw-r--r--tests/validators/test_hw.py (renamed from tests/test_validators_hw.py)0
-rw-r--r--tests/validators/test_kvm.py (renamed from tests/test_validators_kvm.py)0
-rw-r--r--tests/validators/test_net.py (renamed from tests/test_validators_net.py)0
-rw-r--r--tests/validators/test_os.py (renamed from tests/test_validators_os.py)0
13 files changed, 100 insertions, 29 deletions
diff --git a/tests/apps/__init__.py b/tests/apps/__init__.py
new file mode 100644
index 00000000..1e91f7fa
--- /dev/null
+++ b/tests/apps/__init__.py
@@ -0,0 +1,20 @@
+# ========================================================================== #
+# #
+# KVMD - The main Pi-KVM daemon. #
+# #
+# Copyright (C) 2018 Maxim Devaev <[email protected]> #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <https://www.gnu.org/licenses/>. #
+# #
+# ========================================================================== #
diff --git a/tests/test_app_cleanup.py b/tests/apps/test_cleanup.py
index 3e2e4c72..3e2e4c72 100644
--- a/tests/test_app_cleanup.py
+++ b/tests/apps/test_cleanup.py
diff --git a/tests/test_app_htpasswd.py b/tests/apps/test_htpasswd.py
index 11895c85..11895c85 100644
--- a/tests/test_app_htpasswd.py
+++ b/tests/apps/test_htpasswd.py
diff --git a/tests/auth/__init__.py b/tests/auth/__init__.py
new file mode 100644
index 00000000..cdaa6cfb
--- /dev/null
+++ b/tests/auth/__init__.py
@@ -0,0 +1,43 @@
+# ========================================================================== #
+# #
+# KVMD - The main Pi-KVM daemon. #
+# #
+# Copyright (C) 2018 Maxim Devaev <[email protected]> #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <https://www.gnu.org/licenses/>. #
+# #
+# ========================================================================== #
+
+
+import contextlib
+
+from typing import AsyncGenerator
+from typing import Any
+
+from kvmd.yamlconf import make_config
+
+from kvmd.plugins.auth import BaseAuthService
+from kvmd.plugins.auth import get_auth_service_class
+
+
+# =====
+async def get_configured_auth_service(name: str, **kwargs: Any) -> AsyncGenerator[BaseAuthService, None]:
+ service_class = get_auth_service_class(name)
+ config = make_config(kwargs, service_class.get_options())
+ service = service_class(**config._unpack()) # pylint: disable=protected-access
+ try:
+ yield service
+ finally:
+ await service.cleanup()
diff --git a/tests/test_auth_service_htpasswd.py b/tests/auth/test_service_htpasswd.py
index 2bca4988..9a24378e 100644
--- a/tests/test_auth_service_htpasswd.py
+++ b/tests/auth/test_service_htpasswd.py
@@ -26,7 +26,7 @@ import passlib.apache
import pytest
-from kvmd.plugins.auth import get_auth_service_class
+from . import get_configured_auth_service
# =====
@@ -38,18 +38,15 @@ async def test_ok__htpasswd_service(tmpdir) -> None: # type: ignore
htpasswd.set_password("admin", "foo")
htpasswd.save()
- service = get_auth_service_class("htpasswd")(path=path)
+ async with get_configured_auth_service("htpasswd", file=path) as service:
+ assert (await service.login("admin", "foo"))
+ assert not (await service.login("user", "foo"))
- assert (await service.login("admin", "foo"))
- assert not (await service.login("user", "foo"))
+ htpasswd.set_password("admin", "bar")
+ htpasswd.set_password("user", "bar")
+ htpasswd.save()
- htpasswd.set_password("admin", "bar")
- htpasswd.set_password("user", "bar")
- htpasswd.save()
-
- assert (await service.login("admin", "bar"))
- assert (await service.login("user", "bar"))
- assert not (await service.login("admin", "foo"))
- assert not (await service.login("user", "foo"))
-
- await service.cleanup()
+ assert (await service.login("admin", "bar"))
+ assert (await service.login("user", "bar"))
+ assert not (await service.login("admin", "foo"))
+ assert not (await service.login("user", "foo"))
diff --git a/tests/test_auth_service_http.py b/tests/auth/test_service_http.py
index 6ec9527b..6390f368 100644
--- a/tests/test_auth_service_http.py
+++ b/tests/auth/test_service_http.py
@@ -26,7 +26,7 @@ import aiohttp.web
import pytest
-from kvmd.plugins.auth import get_auth_service_class
+from . import get_configured_auth_service
# =====
@@ -53,17 +53,8 @@ async def _auth_server_port_fixture(aiohttp_server) -> AsyncGenerator[int, None]
# =====
@pytest.mark.asyncio
async def test_ok__http_service(auth_server_port: int) -> None:
- service = get_auth_service_class("http")(
- url="http://localhost:%d/auth_post" % (auth_server_port),
- verify=False,
- post=True,
- user="",
- passwd="",
- timeout=5.0,
- )
-
- assert not (await service.login("admin", "foo"))
- assert not (await service.login("user", "foo"))
- assert (await service.login("admin", "foobar"))
-
- await service.cleanup()
+ url = "http://localhost:%d/auth_post" % (auth_server_port)
+ async with get_configured_auth_service("http", url=url) as service:
+ assert not (await service.login("admin", "foo"))
+ assert not (await service.login("user", "foo"))
+ assert (await service.login("admin", "foobar"))
diff --git a/tests/validators/__init__.py b/tests/validators/__init__.py
new file mode 100644
index 00000000..1e91f7fa
--- /dev/null
+++ b/tests/validators/__init__.py
@@ -0,0 +1,20 @@
+# ========================================================================== #
+# #
+# KVMD - The main Pi-KVM daemon. #
+# #
+# Copyright (C) 2018 Maxim Devaev <[email protected]> #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <https://www.gnu.org/licenses/>. #
+# #
+# ========================================================================== #
diff --git a/tests/test_validators_auth.py b/tests/validators/test_auth.py
index f3cdf939..f3cdf939 100644
--- a/tests/test_validators_auth.py
+++ b/tests/validators/test_auth.py
diff --git a/tests/test_validators_basic.py b/tests/validators/test_basic.py
index 96feb666..96feb666 100644
--- a/tests/test_validators_basic.py
+++ b/tests/validators/test_basic.py
diff --git a/tests/test_validators_hw.py b/tests/validators/test_hw.py
index 2c93d3eb..2c93d3eb 100644
--- a/tests/test_validators_hw.py
+++ b/tests/validators/test_hw.py
diff --git a/tests/test_validators_kvm.py b/tests/validators/test_kvm.py
index f1050b36..f1050b36 100644
--- a/tests/test_validators_kvm.py
+++ b/tests/validators/test_kvm.py
diff --git a/tests/test_validators_net.py b/tests/validators/test_net.py
index eab67c9a..eab67c9a 100644
--- a/tests/test_validators_net.py
+++ b/tests/validators/test_net.py
diff --git a/tests/test_validators_os.py b/tests/validators/test_os.py
index b280b4b3..b280b4b3 100644
--- a/tests/test_validators_os.py
+++ b/tests/validators/test_os.py