65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
import pytest
|
|||
|
|
from httpx import ASGITransport, AsyncClient
|
||
|
|
|
||
|
|
from app.config import settings
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
async def client(monkeypatch, tmp_path):
|
||
|
|
monkeypatch.setattr(settings, "database_url", f"sqlite+aiosqlite:///{tmp_path}/test.db")
|
||
|
|
monkeypatch.setattr(settings, "admin_token", "test-admin-token")
|
||
|
|
monkeypatch.setattr(settings, "jwt_secret", "test-jwt-secret")
|
||
|
|
|
||
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
||
|
|
|
||
|
|
from app.db import base as db_base
|
||
|
|
|
||
|
|
db_base.engine = create_async_engine(settings.database_url)
|
||
|
|
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||
|
|
|
||
|
|
db_base.AsyncSessionLocal = async_sessionmaker(db_base.engine, expire_on_commit=False)
|
||
|
|
|
||
|
|
async with db_base.engine.begin() as conn:
|
||
|
|
await conn.run_sync(db_base.Base.metadata.create_all)
|
||
|
|
|
||
|
|
from app.api.routes.admin import router as admin_router
|
||
|
|
from fastapi import FastAPI
|
||
|
|
|
||
|
|
app = FastAPI()
|
||
|
|
app.include_router(admin_router)
|
||
|
|
|
||
|
|
transport = ASGITransport(app=app)
|
||
|
|
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||
|
|
yield ac
|
||
|
|
|
||
|
|
await db_base.engine.dispose()
|
||
|
|
|
||
|
|
|
||
|
|
async def test_admin_requires_token(client):
|
||
|
|
resp = await client.get("/admin/config")
|
||
|
|
assert resp.status_code == 403
|
||
|
|
|
||
|
|
|
||
|
|
async def test_admin_rejects_wrong_token(client):
|
||
|
|
resp = await client.get("/admin/config", headers={"X-Admin-Token": "wrong"})
|
||
|
|
assert resp.status_code == 403
|
||
|
|
|
||
|
|
|
||
|
|
async def test_admin_reads_and_updates_config(client):
|
||
|
|
headers = {"X-Admin-Token": "test-admin-token"}
|
||
|
|
|
||
|
|
resp = await client.get("/admin/config", headers=headers)
|
||
|
|
assert resp.status_code == 200
|
||
|
|
assert resp.json()["fee_address"] == ""
|
||
|
|
|
||
|
|
resp = await client.put(
|
||
|
|
"/admin/config", headers=headers, json={"fee_address": "plm1qfeeaddress", "bet_amount_sats": 500_000_000}
|
||
|
|
)
|
||
|
|
assert resp.status_code == 200
|
||
|
|
body = resp.json()
|
||
|
|
assert body["fee_address"] == "plm1qfeeaddress"
|
||
|
|
assert body["bet_amount_sats"] == 500_000_000
|
||
|
|
|
||
|
|
resp = await client.get("/admin/config", headers=headers)
|
||
|
|
assert resp.json()["fee_address"] == "plm1qfeeaddress"
|