mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-30 09:31:53 +08:00
better auth testing
This commit is contained in:
@@ -35,12 +35,14 @@ async def test_ok__htpasswd_service(tmpdir) -> None: # type: ignore
|
||||
path = os.path.abspath(str(tmpdir.join("htpasswd")))
|
||||
|
||||
htpasswd = passlib.apache.HtpasswdFile(path, new=True)
|
||||
htpasswd.set_password("admin", "foo")
|
||||
htpasswd.set_password("admin", "pass")
|
||||
htpasswd.save()
|
||||
|
||||
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 not (await service.login("admin", "foo"))
|
||||
assert not (await service.login("user", "pass"))
|
||||
assert (await service.login("admin", "pass"))
|
||||
|
||||
htpasswd.set_password("admin", "bar")
|
||||
htpasswd.set_password("user", "bar")
|
||||
|
||||
@@ -20,9 +20,11 @@
|
||||
# ========================================================================== #
|
||||
|
||||
|
||||
from typing import Dict
|
||||
from typing import AsyncGenerator
|
||||
|
||||
import aiohttp.web
|
||||
import aiohttp_basicauth
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -30,19 +32,27 @@ from . import get_configured_auth_service
|
||||
|
||||
|
||||
# =====
|
||||
async def _handle_auth_post(request: aiohttp.web.BaseRequest) -> aiohttp.web.Response:
|
||||
async def _handle_auth(request: aiohttp.web.BaseRequest) -> aiohttp.web.Response:
|
||||
status = 400
|
||||
if request.method == "POST":
|
||||
credentials = (await request.json())
|
||||
if credentials["user"] == "admin" and credentials["passwd"] == "foobar":
|
||||
if credentials["user"] == "admin" and credentials["passwd"] == "pass":
|
||||
status = 200
|
||||
return aiohttp.web.Response(text=str(status), status=status)
|
||||
|
||||
|
||||
@pytest.fixture(name="auth_server_port")
|
||||
async def _auth_server_port_fixture(aiohttp_server) -> AsyncGenerator[int, None]: # type: ignore
|
||||
app = aiohttp.web.Application()
|
||||
app.router.add_post("/auth_post", _handle_auth_post)
|
||||
auth = aiohttp_basicauth.BasicAuthMiddleware(
|
||||
username="server-admin",
|
||||
password="server-pass",
|
||||
force=False,
|
||||
)
|
||||
|
||||
app = aiohttp.web.Application(middlewares=[auth])
|
||||
app.router.add_post("/auth", _handle_auth)
|
||||
app.router.add_post("/auth_plus_basic", auth.required(_handle_auth))
|
||||
|
||||
server = await aiohttp_server(app)
|
||||
try:
|
||||
yield server.port
|
||||
@@ -52,9 +62,18 @@ 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:
|
||||
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"))
|
||||
@pytest.mark.parametrize("kwargs", [
|
||||
{},
|
||||
{"verify": False},
|
||||
{"user": "server-admin", "passwd": "server-pass"},
|
||||
])
|
||||
async def test_ok(auth_server_port: int, kwargs: Dict) -> None:
|
||||
url = "http://localhost:%d/%s" % (
|
||||
auth_server_port,
|
||||
("auth_plus_basic" if kwargs.get("user") else "auth"),
|
||||
)
|
||||
async with get_configured_auth_service("http", url=url, **kwargs) as service:
|
||||
assert not (await service.login("user", "foobar"))
|
||||
assert not (await service.login("admin", "foobar"))
|
||||
assert not (await service.login("user", "pass"))
|
||||
assert (await service.login("admin", "pass"))
|
||||
|
||||
Reference in New Issue
Block a user