46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
import unittest
|
|
from fastapi.testclient import TestClient
|
|
|
|
import main
|
|
from db import connect, create
|
|
|
|
|
|
class TestHouseholdScoping(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)
|
|
|
|
# Register a user and capture token
|
|
r = self.client.post(
|
|
"/api/v1/auth/register",
|
|
json={"email": "h@test.com", "password": "pw", "displayName": "H"},
|
|
)
|
|
assert r.status_code == 200, r.text
|
|
self.token = r.json()["accessToken"]
|
|
self.headers = {"Authorization": f"Bearer {self.token}"}
|
|
|
|
async def asyncTearDown(self):
|
|
await self.conn.close()
|
|
main.app.dependency_overrides.clear()
|
|
|
|
def test_scoped_whoami_forbidden_on_default_when_not_member(self):
|
|
r = self.client.get("/api/v1/households/default/whoami", headers=self.headers)
|
|
assert r.status_code in (403, 404) # may be 404 if default household missing
|
|
|
|
def test_scoped_whoami_ok_after_creating_household(self):
|
|
r = self.client.post("/api/v1/households", json={"name": "Family"}, headers=self.headers)
|
|
assert r.status_code == 200, r.text
|
|
slug = r.json()["slug"]
|
|
r = self.client.get(f"/api/v1/households/{slug}/whoami", headers=self.headers)
|
|
assert r.status_code == 200, r.text
|
|
body = r.json()
|
|
assert body["householdSlug"] == slug
|