From 8f3c54730d9a9d0aef3c34b9866809489b87b846 Mon Sep 17 00:00:00 2001 From: dr-frmr Date: Tue, 8 Apr 2025 10:38:36 -0400 Subject: [PATCH 1/2] chore: nits & add one exhaustive test of a query route --- fly.toml | 2 +- src/crud.py | 3 +- src/routers/apps.py | 2 -- tests/routes/test_scoped_api.py | 61 ++++++++++++++++++++++++++++++--- 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/fly.toml b/fly.toml index 09c90275..e64509ea 100644 --- a/fly.toml +++ b/fly.toml @@ -1,6 +1,6 @@ # See https://fly.io/docs/reference/configuration/ for information about how to use this file. -app = 'honcho' +app = 'honcho-image' primary_region = 'ewr' kill_signal = 'SIGINT' kill_timeout = '5s' diff --git a/src/crud.py b/src/crud.py index 918d1ee3..9cf3110f 100644 --- a/src/crud.py +++ b/src/crud.py @@ -931,8 +931,7 @@ async def get_metamessage( stmt = stmt.where(models.Metamessage.message_id == message_id) result = await db.execute(stmt) - metamessage = result.scalar_one_or_none() - return metamessage + return result.scalar_one_or_none() async def update_metamessage( diff --git a/src/routers/apps.py b/src/routers/apps.py index 929e0eae..3c37cd28 100644 --- a/src/routers/apps.py +++ b/src/routers/apps.py @@ -17,8 +17,6 @@ router = APIRouter( tags=["apps"], ) -# jwt_params = Depends(require_auth(app_id="app_id")) - @router.get("", response_model=schemas.App) async def get_app( diff --git a/tests/routes/test_scoped_api.py b/tests/routes/test_scoped_api.py index 36539c2a..8dbee588 100644 --- a/tests/routes/test_scoped_api.py +++ b/tests/routes/test_scoped_api.py @@ -347,8 +347,8 @@ def test_get_session_by_id_with_auth(auth_client, sample_data): ) assert response.status_code == 200 - # Test with session-scoped JWT if auth_client.auth_type == "empty": + # Test with session-scoped JWT auth_client.headers["Authorization"] = ( f"Bearer {create_jwt(JWTParams(se=session_id))}" ) @@ -358,13 +358,66 @@ def test_get_session_by_id_with_auth(auth_client, sample_data): ) assert response.status_code == 200 - response2 = auth_client.get( + response = auth_client.get( f"/v1/apps/{test_app.public_id}/users/{test_user.public_id}/sessions" ) - assert response2.status_code == 200 + assert response.status_code == 200 - assert response2.json()["id"] == session_id + assert response.json()["id"] == session_id + + # Test with wrong session_id (should be 401 since we have a session-scoped JWT) + assert ( + auth_client.get( + f"/v1/apps/{test_app.public_id}/users/{test_user.public_id}/sessions?session_id={generate_nanoid()}" + ).status_code + == 401 + ) + + # Test with user-scoped JWT + auth_client.headers["Authorization"] = ( + f"Bearer {create_jwt(JWTParams(us=test_user.public_id))}" + ) + + assert ( + auth_client.get( + f"/v1/apps/{test_app.public_id}/users/{test_user.public_id}/sessions?session_id={session_id}" + ).status_code + == 200 + ) + assert ( + auth_client.get( + f"/v1/apps/{test_app.public_id}/users/{test_user.public_id}/sessions" + ).status_code + == 401 + ) + + # Test with app-scoped JWT + auth_client.headers["Authorization"] = ( + f"Bearer {create_jwt(JWTParams(ap=test_app.public_id))}" + ) + + assert ( + auth_client.get( + f"/v1/apps/{test_app.public_id}/users/{test_user.public_id}/sessions?session_id={session_id}" + ).status_code + == 200 + ) + + assert ( + auth_client.get( + f"/v1/apps/{test_app.public_id}/users/{test_user.public_id}/sessions" + ).status_code + == 401 + ) + + # Test with wrong session_id (should be 404 since we have an app-scoped JWT) + assert ( + auth_client.get( + f"/v1/apps/{test_app.public_id}/users/{test_user.public_id}/sessions?session_id={generate_nanoid()}" + ).status_code + == 404 + ) def test_create_collection(auth_client, sample_data) -> None: From 7618e5c4b52346dc4ab835ee5a0b72b8bffebab9 Mon Sep 17 00:00:00 2001 From: dr-frmr Date: Tue, 8 Apr 2025 15:46:57 -0400 Subject: [PATCH 2/2] fix: undo change to fly.toml --- fly.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fly.toml b/fly.toml index e64509ea..09c90275 100644 --- a/fly.toml +++ b/fly.toml @@ -1,6 +1,6 @@ # See https://fly.io/docs/reference/configuration/ for information about how to use this file. -app = 'honcho-image' +app = 'honcho' primary_region = 'ewr' kill_signal = 'SIGINT' kill_timeout = '5s'