diff --git a/backend/schema.graphql b/backend/schema.graphql index d058613..c66c0da 100644 --- a/backend/schema.graphql +++ b/backend/schema.graphql @@ -49,7 +49,7 @@ type RoomsResult { } type Query { - getReservation(roomId: String!): ReservationResult! + getReservation(id: ID!): ReservationResult! getAllReservations: ReservationsResult! getAllRooms: RoomsResult! getAvailableRooms(input: AvailableRoomInput!): RoomsResult! diff --git a/backend/specs/resolvers/when_cancelling_reservations.py b/backend/specs/resolvers/when_cancelling_reservations.py index 262b6b7..45dd1e3 100644 --- a/backend/specs/resolvers/when_cancelling_reservations.py +++ b/backend/specs/resolvers/when_cancelling_reservations.py @@ -10,13 +10,6 @@ class DescribeDeleteReservationResolver: - def find_by_id(self, reservation_id): - return [ - reservation - for reservation in self.reservations - if reservation.id != reservation_id - ] - @pytest.mark.asyncio async def should_delete_existing_reservation(self, mocker): original_reservations = [ @@ -60,7 +53,7 @@ async def should_delete_existing_reservation(self, mocker): assert result["reservations"] == expected_reservations @pytest.mark.asyncio - async def test_should_give_reservation_not_found(self, mocker): + async def should_give_reservation_not_found(self, mocker): result = await delete_reservation_resolver( None, MOCK_EXECUTION_CONTEXT, reservationId=999 ) diff --git a/backend/specs/when_authorizing.py b/backend/specs/when_authorizing.py index 0d04bbd..4783d4a 100644 --- a/backend/specs/when_authorizing.py +++ b/backend/specs/when_authorizing.py @@ -3,8 +3,8 @@ import pytest from jose import jwt -from auth import create_access_token, create_refresh_token -from settings import ALGORITHM, REFRESH_SECRET_KEY, SECRET_KEY +from auth import create_access_token +from settings import ALGORITHM, SECRET_KEY class DescribeAuthorization: @@ -22,12 +22,3 @@ def should_create_access_token(self, subject, expires_delta): decoded_token = jwt.decode(generated_token, SECRET_KEY, algorithms=[ALGORITHM]) assert decoded_token["sub"] == subject - - def should_create_refresh_token(self, subject, expires_delta): - generated_token = create_refresh_token(subject, expires_delta) - assert generated_token is not None - - decoded_token = jwt.decode( - generated_token, REFRESH_SECRET_KEY, algorithms=[ALGORITHM] - ) - assert decoded_token["sub"] == subject diff --git a/backend/src/api/resolvers/data.py b/backend/src/api/resolvers/data.py index ccf031b..221bf6c 100644 --- a/backend/src/api/resolvers/data.py +++ b/backend/src/api/resolvers/data.py @@ -37,13 +37,9 @@ async def create_reservation( async def delete_reservation(db, reservation_id: int) -> Dict[str, Any]: - reservation = await fetch_reservation(db, reservation_id) - if reservation: - await db.execute("DELETE FROM reservations WHERE id = $1", reservation_id) - return await fetch_all_rows(db, Reservation) - else: - errors = ["Reservation not found"] - return {"success": False, "errors": errors, "reservations": None} + await fetch_reservation(db, reservation_id) + await db.execute("DELETE FROM reservations WHERE id = $1", reservation_id) + return await fetch_all_rows(db, Reservation) async def is_room_available( @@ -116,7 +112,7 @@ async def fetch_room(db, room_id: str) -> Room: raise ValueError(f"Room with id {room_id} not found") -async def fetch_reservation(db, reservation_id: id): +async def fetch_reservation(db, reservation_id: int): result = await fetch_by_id(db, Reservation, id=reservation_id) if result["success"]: return result diff --git a/backend/src/api/resolvers/queries.py b/backend/src/api/resolvers/queries.py index e9c0435..c073aef 100644 --- a/backend/src/api/resolvers/queries.py +++ b/backend/src/api/resolvers/queries.py @@ -7,9 +7,8 @@ async def get_reservation_resolver(obj, info, id) -> Dict[str, Any]: + db = await DbSession() try: - db = await DbSession() - result = await fetch_reservation(db, id) return result except ValueError as error: diff --git a/backend/src/api/utils.py b/backend/src/api/utils.py index 81c650f..f857121 100644 --- a/backend/src/api/utils.py +++ b/backend/src/api/utils.py @@ -32,8 +32,7 @@ def convert_to_local_date(dt_utc: datetime) -> datetime: def convert_to_local_date_from_str(date_str: str) -> datetime: - dt_utc = datetime.strptime(date_str, "%Y-%m-%d") - return dt_utc.astimezone(timezone.utc).replace(tzinfo=None) + return datetime.strptime(date_str, "%Y-%m-%d") def get_calling_function_name() -> str: diff --git a/backend/src/auth.py b/backend/src/auth.py index 37354ad..35c7d7c 100644 --- a/backend/src/auth.py +++ b/backend/src/auth.py @@ -2,12 +2,7 @@ from jose import jwt -from settings import ( - ACCESS_TOKEN_EXPIRE_MINUTES, - ALGORITHM, - REFRESH_SECRET_KEY, - SECRET_KEY, -) +from settings import ACCESS_TOKEN_EXPIRE_MINUTES, ALGORITHM, SECRET_KEY def create_token(subject: str, secret_key: str, expires_delta: timedelta) -> str: @@ -27,10 +22,6 @@ def create_access_token(subject: str, expires_delta: timedelta) -> str: return create_token(subject, str(SECRET_KEY), expires_delta) -def create_refresh_token(subject: str, expires_delta: timedelta) -> str: - return create_token(subject, str(REFRESH_SECRET_KEY), expires_delta) - - def verify_user(username: str, password: str): # for this example application, we are just going to hard-code this return username == "example-user" and password == "example-user" diff --git a/backend/src/settings.py b/backend/src/settings.py index dc6050b..0bcce5a 100644 --- a/backend/src/settings.py +++ b/backend/src/settings.py @@ -2,7 +2,7 @@ API_NAME = "Acme Hotel Reservation - Graphql API" API_PORT = getenv("RESERVATION_PORT") or 80 -ENV = getenv("ENV") +ENV = getenv("ENV", "development") DB_URL = getenv("PG_URL") ALLOWED_ORIGINS = getenv("ALLOWED_ORIGINS", "http://localhost:3000").split(",") @@ -14,3 +14,14 @@ SECRET_KEY = getenv("SECRET_KEY") REFRESH_SECRET_KEY = getenv("REFRESH_SECRET_KEY") + +_missing = [ + name + for name, val in [ + ("SECRET_KEY", SECRET_KEY), + ("REFRESH_SECRET_KEY", REFRESH_SECRET_KEY), + ] + if not val +] +if _missing and ENV != "test": + raise RuntimeError(f"Missing required environment variables: {', '.join(_missing)}")