Skip to content

Commit 19df780

Browse files
committed
fix: tests
1 parent 24bc499 commit 19df780

File tree

12 files changed

+96
-82
lines changed

12 files changed

+96
-82
lines changed

app/auth.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ async def websocket_authenticate(websocket: WebSocket) -> str | None:
7676
return None
7777
except Exception as e:
7878
logger.error(f"Unexpected error occurred during websocket authentication: {e}")
79-
await WSStatusMessage(
80-
type="error",
81-
message="Something went wrong during authentication. Please try again.",
82-
).model_dump()
79+
await websocket.send_json(
80+
WSStatusMessage(
81+
type="error",
82+
message="Something went wrong during authentication. Please try again.",
83+
).model_dump()
84+
)
8385
await websocket.close(code=1008, reason="INTERNAL_ERROR")
8486
return None
8587

@@ -160,7 +162,9 @@ async def exchange_token_for_provider(
160162
raise AuthException(
161163
http_status=client_status,
162164
message=(
163-
f"Please link your account with {provider} in your <a href='https://{settings.keycloak_host}/realms/{settings.keycloak_realm}/account'>Account Dashboard</a>"
165+
f"Please link your account with {provider} in your "
166+
"<a href='https://{settings.keycloak_host}/realms/{settings.keycloak_realm}/"
167+
"account'>Account Dashboard</a>"
164168
if body.get("error", "") == "not_linked"
165169
else f"Could not authenticate with {provider}: {err}"
166170
),

app/error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class AuthException(DispatcherException):
4545
def __init__(
4646
self,
4747
http_status: Optional[int] = status.HTTP_401_UNAUTHORIZED,
48-
message: Optional[Dict[str, Any]] = "Authentication failed.",
48+
message: Optional[str] = "Authentication failed.",
4949
):
5050
super().__init__(message, "AUTHENTICATION_FAILED", http_status)
5151

app/routers/jobs_status.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ async def ws_jobs_status(
119119
await websocket.send_json(
120120
WSStatusMessage(type="error", message=ae.message).model_dump()
121121
)
122-
await websocket.close(code=1008, reason=ae.error_code)
122+
await websocket.close(code=1011, reason=ae.error_code)
123123
except Exception as e:
124124
logger.error(f"Unexpected error occurred during websocket : {e}")
125-
await WSStatusMessage(
126-
type="error",
127-
message="An error occurred while monitoring the job status.",
128-
).model_dump()
129-
await websocket.close(code=1008, reason="INTERNAL_ERROR")
125+
await websocket.send_json(
126+
WSStatusMessage(
127+
type="error",
128+
message="An error occurred while monitoring the job status.",
129+
).model_dump()
130+
)
131+
await websocket.close(code=1011, reason="INTERNAL_ERROR")
130132
finally:
131133
db.close()

app/routers/sync_jobs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Annotated
2-
from fastapi import Body, APIRouter, Depends, HTTPException, Response, status
2+
from fastapi import Body, APIRouter, Depends, Response, status
33
from loguru import logger
44

55
from app.error import DispatcherException, ErrorResponse, InternalException

app/routers/tiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Annotated
2-
from fastapi import APIRouter, HTTPException, status, Body
2+
from fastapi import APIRouter, status, Body
33
from geojson_pydantic import GeometryCollection, Polygon
44
from loguru import logger
55

app/routers/unit_jobs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from typing import Annotated
2-
from fastapi import Body, APIRouter, Depends, HTTPException, status
2+
from fastapi import Body, APIRouter, Depends, status
33
from loguru import logger
44
from sqlalchemy.orm import Session
55

66
from app.auth import oauth2_scheme
77
from app.database.db import get_db
88
from app.error import (
9-
AuthException,
109
DispatcherException,
1110
ErrorResponse,
1211
InternalException,

app/routers/upscale_tasks.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
Body,
77
APIRouter,
88
Depends,
9-
HTTPException,
109
WebSocket,
1110
WebSocketDisconnect,
1211
status,
@@ -255,16 +254,18 @@ async def ws_task_status(
255254
type="error", task_id=task_id, message=ae.message
256255
).model_dump()
257256
)
258-
await websocket.close(code=1008, reason=ae.error_code)
257+
await websocket.close(code=1011, reason=ae.error_code)
259258
except Exception as e:
260259
logger.error(
261260
f"An error occurred while monitoring upscaling task {task_id}: {e}"
262261
)
263-
await WSTaskStatusMessage(
264-
type="error",
265-
task_id=task_id,
266-
message="An error occurred while monitoring upscaling task.",
267-
).model_dump()
268-
await websocket.close(code=1008, reason="INTERNAL_ERROR")
262+
await websocket.send_json(
263+
WSTaskStatusMessage(
264+
type="error",
265+
task_id=task_id,
266+
message="An error occurred while monitoring upscaling task.",
267+
).model_dump()
268+
)
269+
await websocket.close(code=1011, reason="INTERNAL_ERROR")
269270
finally:
270271
db.close()

tests/routers/test_job_status.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,6 @@ async def test_ws_jobs_status_closes_on_error(mock_get_jobs_status, client):
103103
websocket.receive_json()
104104
websocket.receive_json()
105105
websocket.receive_json()
106+
websocket.receive_json()
106107

107108
assert exc_info.value.code == 1011

tests/routers/test_sync_jobs.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import json
22
from unittest.mock import patch
33

4-
from fastapi import HTTPException
4+
from fastapi import status
5+
6+
from app.error import InternalException
57

68

79
@patch("app.routers.sync_jobs.create_synchronous_job")
@@ -27,21 +29,19 @@ def test_sync_jobs_create_500(
2729
mock_create_sync_job.side_effect = SystemError("Could not launch the job")
2830

2931
r = client.post("/sync_jobs", json=fake_processing_job_request.model_dump())
30-
assert r.status_code == 500
31-
assert "could not launch the job" in r.json().get("detail", "").lower()
32+
assert r.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
33+
assert "An error occurred while creating the synchronous job." in r.json().get("message", "")
3234

3335

3436
@patch("app.routers.sync_jobs.create_synchronous_job")
35-
def test_sync_jobs_create_http_error(
37+
def test_sync_jobs_create_internal_error(
3638
mock_create_sync_job,
3739
client,
3840
fake_processing_job_request,
3941
):
4042

41-
mock_create_sync_job.side_effect = HTTPException(
42-
status_code=503, detail="Oops, service unavailable"
43-
)
43+
mock_create_sync_job.side_effect = InternalException()
4444

4545
r = client.post("/sync_jobs", json=fake_processing_job_request.model_dump())
46-
assert r.status_code == 503
47-
assert "service unavailable" in r.json().get("detail", "").lower()
46+
assert r.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
47+
assert "An internal server error occurred." in r.json().get("message", "")

tests/routers/test_tiles.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest.mock import patch
22
import pytest
33
from app.schemas.tiles import GridTypeEnum
4+
from fastapi import status
45

56

67
@pytest.fixture
@@ -28,5 +29,5 @@ def test_split_in_tiles_success(client, dummy_payload):
2829
def test_split_in_tiles_unknown_grid(mock_split, client, dummy_payload):
2930
mock_split.side_effect = ValueError("Unknown grid: INVALID_GRID")
3031
response = client.post("/tiles", json=dummy_payload)
31-
assert response.status_code == 500
32-
assert "Unknown grid" in response.json()["detail"]
32+
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
33+
assert "An error occurred while calculating tiles for 20x20km" in response.json()["message"]

0 commit comments

Comments
 (0)