Skip to content

Commit

Permalink
Fix errors in Pindora client
Browse files Browse the repository at this point in the history
- Only account for CONFIRMED reservations
- Add PindoraClientError to differentiate non-API errors
- Use `local_iso_format` to format datetimes
- Fix a couple of typos
  • Loading branch information
matti-lamppu committed Jan 24, 2025
1 parent 1740e5d commit 934c1df
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
HTTP_418_IM_A_TEAPOT,
)

from tilavarauspalvelu.enums import ReservationStateChoice
from tilavarauspalvelu.integrations.keyless_entry import PindoraClient
from tilavarauspalvelu.integrations.keyless_entry.exceptions import PindoraAPIError
from tilavarauspalvelu.integrations.keyless_entry.exceptions import PindoraAPIError, PindoraClientError
from utils.date_utils import DEFAULT_TIMEZONE, local_datetime
from utils.external_service.base_external_service_client import BaseExternalServiceClient

Expand Down Expand Up @@ -141,6 +142,7 @@ def test_pindora_client__create_reservation_series(is_active: bool):
reservation = ReservationFactory.create(
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
state=ReservationStateChoice.CONFIRMED,
)

data = default_reservation_series_response(reservation, access_code_is_active=is_active)
Expand Down Expand Up @@ -177,7 +179,7 @@ def test_pindora_client__create_reservation_series(is_active: bool):
@pytest.mark.django_db
def test_pindora_client__create_reservation_series__403():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation)
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.CONFIRMED)

msg = "Pindora API key is invalid."
with pytest.raises(PindoraAPIError, match=exact(msg)):
Expand All @@ -191,7 +193,7 @@ def test_pindora_client__create_reservation_series__403():
@pytest.mark.django_db
def test_pindora_client__create_reservation_series__400():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation)
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.CONFIRMED)

msg = "Invalid Pindora API request: bad request."
with pytest.raises(PindoraAPIError, match=exact(msg)):
Expand All @@ -205,7 +207,7 @@ def test_pindora_client__create_reservation_series__400():
@pytest.mark.django_db
def test_pindora_client__create_reservation_series__409():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation)
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.CONFIRMED)

msg = f"Reservation series '{recurring_reservation.ext_uuid}' already exists in Pindora."
with pytest.raises(PindoraAPIError, match=exact(msg)):
Expand All @@ -219,7 +221,7 @@ def test_pindora_client__create_reservation_series__409():
@pytest.mark.django_db
def test_pindora_client__create_reservation_series__not_200():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation)
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.CONFIRMED)

msg = (
f"Unexpected response from Pindora when creating reservation series '{recurring_reservation.ext_uuid}': "
Expand All @@ -237,8 +239,22 @@ def test_pindora_client__create_reservation_series__not_200():
def test_pindora_client__create_reservation_series__no_reservations():
recurring_reservation = RecurringReservationFactory.create()

msg = f"No reservations in for reservation series '{recurring_reservation.ext_uuid}'."
with pytest.raises(PindoraAPIError, match=exact(msg)):
msg = f"No confirmed reservations in reservation series '{recurring_reservation.ext_uuid}'."
with pytest.raises(PindoraClientError, match=exact(msg)):
PindoraClient.create_reservation_series(recurring_reservation)


@patch_method(
BaseExternalServiceClient.generic,
return_value=ResponseMock(status_code=HTTP_200_OK),
)
@pytest.mark.django_db
def test_pindora_client__create_reservation_series__no_confirmed_reservations():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.DENIED)

msg = f"No confirmed reservations in reservation series '{recurring_reservation.ext_uuid}'."
with pytest.raises(PindoraClientError, match=exact(msg)):
PindoraClient.create_reservation_series(recurring_reservation)


Expand All @@ -250,7 +266,7 @@ def test_pindora_client__create_reservation_series__no_reservations():
@pytest.mark.parametrize("is_active", [True, False])
def test_pindora_client__update_reservation_series(is_active: bool):
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation)
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.CONFIRMED)

PindoraClient.update_reservation_series(recurring_reservation, is_active=is_active)

Expand All @@ -264,7 +280,7 @@ def test_pindora_client__update_reservation_series(is_active: bool):
@pytest.mark.django_db
def test_pindora_client__update_reservation_series__403():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation)
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.CONFIRMED)

msg = "Pindora API key is invalid."
with pytest.raises(PindoraAPIError, match=exact(msg)):
Expand All @@ -278,7 +294,7 @@ def test_pindora_client__update_reservation_series__403():
@pytest.mark.django_db
def test_pindora_client__update_reservation_series__400():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation)
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.CONFIRMED)

msg = "Invalid Pindora API request: bad request."
with pytest.raises(PindoraAPIError, match=exact(msg)):
Expand All @@ -292,7 +308,7 @@ def test_pindora_client__update_reservation_series__400():
@pytest.mark.django_db
def test_pindora_client__update_reservation_series__404():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation)
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.CONFIRMED)

msg = f"Reservation series '{recurring_reservation.ext_uuid}' not found from Pindora."
with pytest.raises(PindoraAPIError, match=exact(msg)):
Expand All @@ -306,7 +322,7 @@ def test_pindora_client__update_reservation_series__404():
@pytest.mark.django_db
def test_pindora_client__update_reservation_series__409():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation)
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.CONFIRMED)

msg = f"Reservation series '{recurring_reservation.ext_uuid}' already exists in Pindora."
with pytest.raises(PindoraAPIError, match=exact(msg)):
Expand All @@ -320,7 +336,7 @@ def test_pindora_client__update_reservation_series__409():
@pytest.mark.django_db
def test_pindora_client__update_reservation_series__not_204():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation)
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.CONFIRMED)

msg = (
f"Unexpected response from Pindora when updating reservation series '{recurring_reservation.ext_uuid}': "
Expand All @@ -338,8 +354,22 @@ def test_pindora_client__update_reservation_series__not_204():
def test_pindora_client__update_reservation_series__no_reservations():
recurring_reservation = RecurringReservationFactory.create()

msg = f"No reservations in for reservation series '{recurring_reservation.ext_uuid}'."
with pytest.raises(PindoraAPIError, match=exact(msg)):
msg = f"No confirmed reservations in reservation series '{recurring_reservation.ext_uuid}'."
with pytest.raises(PindoraClientError, match=exact(msg)):
PindoraClient.update_reservation_series(recurring_reservation)


@patch_method(
BaseExternalServiceClient.generic,
return_value=ResponseMock(status_code=HTTP_200_OK),
)
@pytest.mark.django_db
def test_pindora_client__update_reservation_series__no_confirmed_reservations():
recurring_reservation = RecurringReservationFactory.create()
ReservationFactory.create(recurring_reservation=recurring_reservation, state=ReservationStateChoice.DENIED)

msg = f"No confirmed reservations in reservation series '{recurring_reservation.ext_uuid}'."
with pytest.raises(PindoraClientError, match=exact(msg)):
PindoraClient.update_reservation_series(recurring_reservation)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_403_FORBIDDEN, HTTP_404_NOT_FOUND, HTTP_418_IM_A_TEAPOT

from tilavarauspalvelu.integrations.keyless_entry import PindoraClient
from tilavarauspalvelu.integrations.keyless_entry.exceptions import PindoraAPIConfigurationError, PindoraAPIError
from tilavarauspalvelu.integrations.keyless_entry.exceptions import PindoraAPIError, PindoraClientConfigurationError
from utils.external_service.base_external_service_client import BaseExternalServiceClient

from tests.factories import ReservationUnitFactory
Expand Down Expand Up @@ -34,7 +34,7 @@ def test_pindora_client__get_reservation_unit__missing_api_key(settings):
reservation_unit = ReservationUnitFactory.build()

msg = "'PINDORA_API_KEY' setting must to be configured for Pindora client to work."
with pytest.raises(PindoraAPIConfigurationError, match=exact(msg)):
with pytest.raises(PindoraClientConfigurationError, match=exact(msg)):
PindoraClient.get_reservation_unit(reservation_unit)


Expand All @@ -44,7 +44,7 @@ def test_pindora_client__get_reservation_unit__missing_api_url(settings):
reservation_unit = ReservationUnitFactory.build()

msg = "'PINDORA_API_URL' setting must to be configured for Pindora client to work."
with pytest.raises(PindoraAPIConfigurationError, match=exact(msg)):
with pytest.raises(PindoraClientConfigurationError, match=exact(msg)):
PindoraClient.get_reservation_unit(reservation_unit)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
HTTP_418_IM_A_TEAPOT,
)

from tilavarauspalvelu.enums import ReservationStateChoice
from tilavarauspalvelu.integrations.keyless_entry import PindoraClient
from tilavarauspalvelu.integrations.keyless_entry.exceptions import PindoraAPIError
from tilavarauspalvelu.integrations.keyless_entry.exceptions import PindoraAPIError, PindoraClientError
from utils.date_utils import DEFAULT_TIMEZONE, local_datetime
from utils.external_service.base_external_service_client import BaseExternalServiceClient

Expand Down Expand Up @@ -121,6 +122,7 @@ def test_pindora_client__create_seasonal_booking(is_active: bool):
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

data = default_seasonal_booking_response(reservation, access_code_is_active=is_active)
Expand Down Expand Up @@ -165,6 +167,7 @@ def test_pindora_client__create_seasonal_booking__403():
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

msg = "Pindora API key is invalid."
Expand All @@ -187,6 +190,7 @@ def test_pindora_client__create_seasonal_booking__400():
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

msg = "Invalid Pindora API request: bad request."
Expand All @@ -209,6 +213,7 @@ def test_pindora_client__create_seasonal_booking__404():
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

msg = f"Seasonal booking '{application_section.ext_uuid}' not found from Pindora."
Expand All @@ -231,6 +236,7 @@ def test_pindora_client__create_seasonal_booking__409():
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

msg = f"Seasonal booking '{application_section.ext_uuid}' already exists in Pindora."
Expand All @@ -253,6 +259,7 @@ def test_pindora_client__create_seasonal_booking__not_200():
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

msg = (
Expand All @@ -271,8 +278,31 @@ def test_pindora_client__create_seasonal_booking__not_200():
def test_pindora_client__create_seasonal_booking__no_reservations():
application_section = ApplicationSectionFactory.create()

msg = f"No reservations in for seasonal booking '{application_section.ext_uuid}'."
with pytest.raises(PindoraAPIError, match=exact(msg)):
msg = f"No confirmed reservations in seasonal booking '{application_section.ext_uuid}'."
with pytest.raises(PindoraClientError, match=exact(msg)):
PindoraClient.create_seasonal_booking(application_section)


@patch_method(
BaseExternalServiceClient.generic,
return_value=ResponseMock(status_code=HTTP_200_OK),
)
@pytest.mark.django_db
def test_pindora_client__create_seasonal_booking__no_confirmed_reservations():
application_section = ApplicationSectionFactory.create()
reservation_unit_option = ReservationUnitOptionFactory.create(application_section=application_section)
allocation = AllocatedTimeSlotFactory.create(reservation_unit_option=reservation_unit_option)
recurring_reservation = RecurringReservationFactory.create(allocated_time_slot=allocation)

ReservationFactory.create(
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.DENIED,
)

msg = f"No confirmed reservations in seasonal booking '{application_section.ext_uuid}'."
with pytest.raises(PindoraClientError, match=exact(msg)):
PindoraClient.create_seasonal_booking(application_section)


Expand All @@ -292,6 +322,7 @@ def test_pindora_client__update_seasonal_booking(is_active: bool):
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

PindoraClient.update_seasonal_reservation(application_section, is_active=is_active)
Expand All @@ -314,6 +345,7 @@ def test_pindora_client__update_seasonal_booking__403():
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

msg = "Pindora API key is invalid."
Expand All @@ -336,6 +368,7 @@ def test_pindora_client__update_seasonal_booking__400():
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

msg = "Invalid Pindora API request: bad request."
Expand All @@ -358,6 +391,7 @@ def test_pindora_client__update_seasonal_booking__404():
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

msg = f"Seasonal booking '{application_section.ext_uuid}' not found from Pindora."
Expand All @@ -380,6 +414,7 @@ def test_pindora_client__update_seasonal_booking__409():
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

msg = f"Seasonal booking '{application_section.ext_uuid}' already exists in Pindora."
Expand All @@ -402,6 +437,7 @@ def test_pindora_client__update_seasonal_booking__not_204():
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.CONFIRMED,
)

msg = (
Expand All @@ -420,8 +456,31 @@ def test_pindora_client__update_seasonal_booking__not_204():
def test_pindora_client__update_seasonal_booking__no_reservations():
application_section = ApplicationSectionFactory.create()

msg = f"No reservations in for seasonal booking '{application_section.ext_uuid}'."
with pytest.raises(PindoraAPIError, match=exact(msg)):
msg = f"No confirmed reservations in seasonal booking '{application_section.ext_uuid}'."
with pytest.raises(PindoraClientError, match=exact(msg)):
PindoraClient.update_seasonal_reservation(application_section)


@patch_method(
BaseExternalServiceClient.generic,
return_value=ResponseMock(status_code=HTTP_200_OK),
)
@pytest.mark.django_db
def test_pindora_client__update_seasonal_booking__no_confirmed_reservations():
application_section = ApplicationSectionFactory.create()
reservation_unit_option = ReservationUnitOptionFactory.create(application_section=application_section)
allocation = AllocatedTimeSlotFactory.create(reservation_unit_option=reservation_unit_option)
recurring_reservation = RecurringReservationFactory.create(allocated_time_slot=allocation)

ReservationFactory.create(
recurring_reservation=recurring_reservation,
created_at=local_datetime(),
user=application_section.application.user,
state=ReservationStateChoice.DENIED,
)

msg = f"No confirmed reservations in seasonal booking '{application_section.ext_uuid}'."
with pytest.raises(PindoraClientError, match=exact(msg)):
PindoraClient.update_seasonal_reservation(application_section)


Expand Down
Loading

0 comments on commit 934c1df

Please sign in to comment.