Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: devices routes wrong path parameter and 403 instead of 404 response #22

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/py_carlos_database/carlos/database/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
class NotFound(HTTPException):
def __init__(self, detail: str, **kwargs):
"""Returns HTTP 403"""
super().__init__(status.HTTP_403_FORBIDDEN, detail=detail)
super().__init__(status.HTTP_404_NOT_FOUND, detail=detail)
10 changes: 5 additions & 5 deletions services/api/carlos/api/routes/devices_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@devices_router.get("", summary="Get all devices.", response_model=list[CarlosDevice])
async def list_devices_route(
context: RequestContext = Depends(request_context),
): # pragma: no cover
):
"""List all devices."""
return await list_devices(context=context)

Expand All @@ -31,14 +31,14 @@ async def list_devices_route(
async def register_device_route(
device: CarlosDeviceCreate,
context: RequestContext = Depends(request_context),
): # pragma: no cover
):
"""Register a new device."""
return await create_device(context=context, device=device)


DEVICE_ID_PATH: UUID = Path(
...,
title="Device ID",
alias="deviceId",
description="The unique identifier of the device.",
)

Expand All @@ -49,7 +49,7 @@ async def register_device_route(
async def get_device_route(
device_id: UUID = DEVICE_ID_PATH,
context: RequestContext = Depends(request_context),
): # pragma: no cover
):
"""Get a device by its ID."""
return await get_device(context=context, device_id=device_id)

Expand All @@ -61,6 +61,6 @@ async def update_device_route(
device: CarlosDeviceUpdate,
device_id: UUID = DEVICE_ID_PATH,
context: RequestContext = Depends(request_context),
): # pragma: no cover
):
"""Update a device by its ID."""
return await update_device(context=context, device_id=device_id, device=device)
46 changes: 43 additions & 3 deletions services/api/carlos/api/routes/devices_routes_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
from carlos.database.device.device_management import CarlosDevice
from uuid import uuid4

from carlos.database.device.device_management import (
CarlosDevice,
CarlosDeviceCreate,
CarlosDeviceUpdate,
)
from pydantic import TypeAdapter
from starlette.status import HTTP_200_OK
from starlette import status
from starlette.testclient import TestClient


def test_list_devices_route(client: TestClient):
"""This test ensures that the list devices route works as expected."""
response = client.get("/devices")
assert response.status_code == HTTP_200_OK
assert response.status_code == status.HTTP_200_OK

devices = TypeAdapter(list[CarlosDevice]).validate_json(response.content)
assert isinstance(devices, list)


def test_devices_crud(client: TestClient):
"""This test ensures that the devices CRUD routes work as expected."""

# query non existent device
device_id = uuid4()
response = client.get(f"/devices/{device_id}")
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text

# create a new device
device = CarlosDeviceCreate(display_name="test_device")
response = client.post("/devices", content=device.model_dump_json())
assert response.status_code == status.HTTP_200_OK, response.text
created_device = CarlosDevice.model_validate_json(response.content)
assert created_device.display_name == device.display_name

# query the created device
response = client.get(f"/devices/{created_device.device_id}")
assert response.status_code == status.HTTP_200_OK, response.text
queried_device = CarlosDevice.model_validate_json(response.content)
assert queried_device == created_device

# update the created device
updated_device = CarlosDeviceUpdate(
display_name="updated_device", description="updated"
)
response = client.put(
f"/devices/{created_device.device_id}", content=updated_device.model_dump_json()
)
assert response.status_code == status.HTTP_200_OK, response.text
updated_device = CarlosDevice.model_validate_json(response.content)
assert updated_device.display_name == "updated_device"
assert updated_device.description == "updated"
Loading