45 lines
1.6 KiB
Python
Executable File
45 lines
1.6 KiB
Python
Executable File
import base64
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from kraken_trader.web import check_basic_auth, create_app
|
|
|
|
|
|
def _header(user: str, pwd: str) -> str:
|
|
return "Basic " + base64.b64encode(f"{user}:{pwd}".encode()).decode()
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
engine = SimpleNamespace(healthy=lambda: True)
|
|
return TestClient(create_app(engine, store=None, username="admin", password="s3cret"))
|
|
|
|
|
|
def test_check_basic_auth():
|
|
assert check_basic_auth(_header("admin", "s3cret"), "admin", "s3cret")
|
|
assert check_basic_auth(_header("admin", "pa:ss"), "admin", "pa:ss")
|
|
assert not check_basic_auth(_header("admin", "wrong"), "admin", "s3cret")
|
|
assert not check_basic_auth(_header("root", "s3cret"), "admin", "s3cret")
|
|
assert not check_basic_auth("Basic not-base64!!", "admin", "s3cret")
|
|
assert not check_basic_auth("Bearer x", "admin", "s3cret")
|
|
assert not check_basic_auth(None, "admin", "s3cret")
|
|
|
|
|
|
def test_pages_require_auth(client):
|
|
response = client.get("/api/docs")
|
|
assert response.status_code == 401
|
|
assert response.headers["www-authenticate"].startswith("Basic")
|
|
assert client.get("/api/docs", headers={"Authorization": _header("admin", "nope")}).status_code == 401
|
|
assert client.get("/api/docs", headers={"Authorization": _header("admin", "s3cret")}).status_code == 200
|
|
|
|
|
|
def test_healthz_is_open(client):
|
|
assert client.get("/healthz").status_code == 200
|
|
|
|
|
|
def test_password_is_required():
|
|
with pytest.raises(ValueError):
|
|
create_app(SimpleNamespace(), store=None, username="admin", password="")
|