48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
|
|
import unittest
|
||
|
|
from fastapi.testclient import TestClient
|
||
|
|
|
||
|
|
import main
|
||
|
|
from db import connect, create
|
||
|
|
|
||
|
|
|
||
|
|
class TestAuthAndHouseholdsV2(unittest.IsolatedAsyncioTestCase):
|
||
|
|
async def asyncSetUp(self):
|
||
|
|
self.conn = await connect(":memory:")
|
||
|
|
await create(self.conn)
|
||
|
|
|
||
|
|
async def override_get_db():
|
||
|
|
try:
|
||
|
|
yield self.conn
|
||
|
|
finally:
|
||
|
|
pass
|
||
|
|
|
||
|
|
main.app.dependency_overrides[main.get_db] = override_get_db
|
||
|
|
self.client = TestClient(main.app)
|
||
|
|
|
||
|
|
async def asyncTearDown(self):
|
||
|
|
await self.conn.close()
|
||
|
|
main.app.dependency_overrides.clear()
|
||
|
|
|
||
|
|
def test_register_and_login_and_households(self):
|
||
|
|
# Register a user
|
||
|
|
r = self.client.post(
|
||
|
|
"/api/v1/auth/register",
|
||
|
|
json={"email": "test@example.com", "password": "pw", "displayName": "Test"},
|
||
|
|
)
|
||
|
|
assert r.status_code == 200, r.text
|
||
|
|
body = r.json()
|
||
|
|
token = body["accessToken"]
|
||
|
|
assert token.startswith("user-")
|
||
|
|
|
||
|
|
headers = {"Authorization": f"Bearer {token}"}
|
||
|
|
# List households (migration created default household 'default' and membership set to admin)
|
||
|
|
r = self.client.get("/api/v1/users/me/households", headers=headers)
|
||
|
|
assert r.status_code == 200, r.text
|
||
|
|
households = r.json()
|
||
|
|
|
||
|
|
# Create a new household
|
||
|
|
r = self.client.post("/api/v1/households", headers=headers, json={"name": "Family"})
|
||
|
|
assert r.status_code == 200, r.text
|
||
|
|
created = r.json()
|
||
|
|
assert created["slug"].startswith("family")
|