61 lines
2.1 KiB
Python
61 lines
2.1 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"]
|
|
# Expect a JWT (three segments separated by '.')
|
|
assert token.count(".") == 2
|
|
|
|
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")
|
|
|
|
def test_refresh_flow(self):
|
|
# Register to set refresh cookie
|
|
r = self.client.post(
|
|
"/api/v1/auth/register",
|
|
json={"email": "refresh@test.com", "password": "pw", "displayName": "Ref"},
|
|
)
|
|
assert r.status_code == 200, r.text
|
|
# Call refresh endpoint; cookie should be sent automatically by TestClient
|
|
r2 = self.client.post("/api/v1/auth/refresh")
|
|
assert r2.status_code == 200, r2.text
|
|
new_access = r2.json()["accessToken"]
|
|
assert new_access.count(".") == 2
|