Skip to content

Commit

Permalink
Update access code active state when reservation state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Jan 28, 2025
1 parent 709e903 commit 3e21536
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 21 deletions.
72 changes: 71 additions & 1 deletion tests/test_graphql_api/test_reservation/test_approve.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import pytest

from tilavarauspalvelu.enums import ReservationStateChoice
from tilavarauspalvelu.enums import AccessType, ReservationStateChoice
from tilavarauspalvelu.integrations.keyless_entry import PindoraClient
from tilavarauspalvelu.integrations.keyless_entry.exceptions import PindoraAPIError

from tests.factories import ReservationFactory, ReservationUnitFactory
from tests.helpers import patch_method

from .helpers import APPROVE_MUTATION, get_approve_data

Expand All @@ -13,6 +16,26 @@
]


@patch_method(PindoraClient.activate_reservation_access_code)
def test_reservation__approve__succeeds(graphql):
reservation_unit = ReservationUnitFactory.create()
reservation = ReservationFactory.create(
state=ReservationStateChoice.REQUIRES_HANDLING,
reservation_units=[reservation_unit],
)

graphql.login_with_superuser()
data = get_approve_data(reservation)
response = graphql(APPROVE_MUTATION, input_data=data)

assert response.has_errors is False, response.errors

reservation.refresh_from_db()
assert reservation.state == ReservationStateChoice.CONFIRMED

assert PindoraClient.activate_reservation_access_code.call_count == 0


def test_reservation__approve__cant_approve_if_status_not_requires_handling(graphql):
reservation_unit = ReservationUnitFactory.create()
reservation = ReservationFactory.create(
Expand Down Expand Up @@ -83,3 +106,50 @@ def test_reservation__approve__succeeds_with_empty_handling_details(graphql):

reservation.refresh_from_db()
assert reservation.state == ReservationStateChoice.CONFIRMED


@patch_method(PindoraClient.activate_reservation_access_code)
def test_reservation__approve__succeeds__pindora_api__call_succeeds(graphql):
reservation_unit = ReservationUnitFactory.create(
access_type=AccessType.ACCESS_CODE,
)
reservation = ReservationFactory.create(
state=ReservationStateChoice.REQUIRES_HANDLING,
reservation_units=[reservation_unit],
access_type=AccessType.ACCESS_CODE,
)

graphql.login_with_superuser()
data = get_approve_data(reservation)
response = graphql(APPROVE_MUTATION, input_data=data)

assert response.has_errors is False, response.errors

reservation.refresh_from_db()
assert reservation.state == ReservationStateChoice.CONFIRMED

assert PindoraClient.activate_reservation_access_code.call_count == 1


@patch_method(PindoraClient.activate_reservation_access_code, side_effect=PindoraAPIError("Error"))
def test_reservation__approve__succeeds__pindora_api__call_fails(graphql):
reservation_unit = ReservationUnitFactory.create(
access_type=AccessType.ACCESS_CODE,
)
reservation = ReservationFactory.create(
state=ReservationStateChoice.REQUIRES_HANDLING,
reservation_units=[reservation_unit],
access_type=AccessType.ACCESS_CODE,
)

graphql.login_with_superuser()
data = get_approve_data(reservation)
response = graphql(APPROVE_MUTATION, input_data=data)

# Request is still successful, even if Pindora API call fails
assert response.has_errors is False, response.errors

reservation.refresh_from_db()
assert reservation.state == ReservationStateChoice.CONFIRMED

assert PindoraClient.activate_reservation_access_code.call_count == 1
50 changes: 49 additions & 1 deletion tests/test_graphql_api/test_reservation/test_confirm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
from django.test import override_settings
from graphene_django_extensions.testing import build_mutation

from tilavarauspalvelu.enums import OrderStatus, PaymentType, ReservationNotification, ReservationStateChoice
from tilavarauspalvelu.enums import (
AccessType,
OrderStatus,
PaymentType,
ReservationNotification,
ReservationStateChoice,
)
from tilavarauspalvelu.integrations.keyless_entry import PindoraClient
from tilavarauspalvelu.integrations.keyless_entry.exceptions import PindoraAPIError
from tilavarauspalvelu.integrations.sentry import SentryLogger
from tilavarauspalvelu.integrations.verkkokauppa.order.exceptions import CreateOrderError
from tilavarauspalvelu.integrations.verkkokauppa.verkkokauppa_api_client import VerkkokauppaAPIClient
Expand All @@ -30,6 +38,7 @@


@override_settings(SEND_EMAILS=True)
@patch_method(PindoraClient.activate_reservation_access_code)
def test_reservation__confirm__changes_state__confirmed(graphql, outbox):
reservation = ReservationFactory.create_for_confirmation()

Expand All @@ -54,6 +63,8 @@ def test_reservation__confirm__changes_state__confirmed(graphql, outbox):
unit_name = reservation.reservation_units.first().unit.name
assert outbox[1].subject == f"New booking {reservation.id} has been made for {unit_name}"

assert PindoraClient.activate_reservation_access_code.call_count == 0


@override_settings(SEND_EMAILS=True)
def test_reservation__confirm__changes_state__requires_handling(graphql, outbox):
Expand Down Expand Up @@ -453,3 +464,40 @@ def test_reservation__confirm__without_price_and_with_free_pricing_does_not_requ
assert reservation.state == ReservationStateChoice.CONFIRMED

assert PaymentOrder.objects.count() == 0


@patch_method(PindoraClient.activate_reservation_access_code)
def test_reservation__confirm__pindora_api__call_succeeds(graphql):
reservation = ReservationFactory.create_for_confirmation(
access_type=AccessType.ACCESS_CODE,
)

graphql.login_with_superuser()
data = get_confirm_data(reservation)
response = graphql(CONFIRM_MUTATION, input_data=data)

assert response.has_errors is False, response.errors

reservation.refresh_from_db()
assert reservation.state == ReservationStateChoice.CONFIRMED

assert PindoraClient.activate_reservation_access_code.call_count == 1


@patch_method(PindoraClient.activate_reservation_access_code, side_effect=PindoraAPIError("Error"))
def test_reservation__confirm__pindora_api__call_fails(graphql):
reservation = ReservationFactory.create_for_confirmation(
access_type=AccessType.ACCESS_CODE,
)

graphql.login_with_superuser()
data = get_confirm_data(reservation)
response = graphql(CONFIRM_MUTATION, input_data=data)

# Request is still successful, even if Pindora API call fails
assert response.has_errors is False, response.errors

reservation.refresh_from_db()
assert reservation.state == ReservationStateChoice.CONFIRMED

assert PindoraClient.activate_reservation_access_code.call_count == 1
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import pytest
from django.test import override_settings

from tilavarauspalvelu.enums import ReservationNotification, ReservationStateChoice
from tilavarauspalvelu.enums import AccessType, ReservationNotification, ReservationStateChoice
from tilavarauspalvelu.integrations.keyless_entry import PindoraClient
from tilavarauspalvelu.integrations.keyless_entry.exceptions import PindoraAPIError, PindoraNotFoundError

from tests.factories import ReservationFactory, UserFactory
from tests.helpers import patch_method

from .helpers import REQUIRE_HANDLING_MUTATION, get_require_handling_data

Expand All @@ -22,6 +25,7 @@
ReservationStateChoice.DENIED,
],
)
@patch_method(PindoraClient.deactivate_reservation_access_code)
def test_reservation__requires_handling__allowed_states(graphql, outbox, state):
reservation = ReservationFactory.create_for_requires_handling(state=state)

Expand All @@ -43,6 +47,8 @@ def test_reservation__requires_handling__allowed_states(graphql, outbox, state):
assert len(outbox) == 1
assert outbox[0].subject == "Your booking is waiting for processing"

assert PindoraClient.deactivate_reservation_access_code.call_count == 0


@pytest.mark.parametrize(
"state",
Expand Down Expand Up @@ -71,3 +77,58 @@ def test_reservation__requires_handling__disallowed_states(graphql, state):

reservation.refresh_from_db()
assert reservation.state == state


@patch_method(PindoraClient.deactivate_reservation_access_code)
def test_reservation__requires_handling__pindora_api__call_succeeds(graphql):
reservation = ReservationFactory.create_for_requires_handling(
state=ReservationStateChoice.CONFIRMED,
access_type=AccessType.ACCESS_CODE,
)

graphql.login_with_superuser()
input_data = get_require_handling_data(reservation)
response = graphql(REQUIRE_HANDLING_MUTATION, input_data=input_data)

assert response.has_errors is False, response.errors

reservation.refresh_from_db()
assert reservation.state == ReservationStateChoice.REQUIRES_HANDLING

assert PindoraClient.deactivate_reservation_access_code.call_count == 1


@patch_method(PindoraClient.deactivate_reservation_access_code, side_effect=PindoraAPIError("Pindora API error"))
def test_reservation__requires_handling__pindora_api__call_fails(graphql):
reservation = ReservationFactory.create_for_requires_handling(
state=ReservationStateChoice.CONFIRMED,
access_type=AccessType.ACCESS_CODE,
)

graphql.login_with_superuser()
input_data = get_require_handling_data(reservation)
response = graphql(REQUIRE_HANDLING_MUTATION, input_data=input_data)

assert response.error_message() == "Pindora API error"

assert PindoraClient.deactivate_reservation_access_code.call_count == 1


@patch_method(PindoraClient.deactivate_reservation_access_code, side_effect=PindoraNotFoundError("Error"))
def test_reservation__requires_handling__pindora_api__call_fails__404(graphql):
reservation = ReservationFactory.create_for_requires_handling(
state=ReservationStateChoice.CONFIRMED,
access_type=AccessType.ACCESS_CODE,
)

graphql.login_with_superuser()
input_data = get_require_handling_data(reservation)
response = graphql(REQUIRE_HANDLING_MUTATION, input_data=input_data)

# Request is still successful if Pindora fails with 404
assert response.has_errors is False, response.errors

reservation.refresh_from_db()
assert reservation.state == ReservationStateChoice.REQUIRES_HANDLING

assert PindoraClient.deactivate_reservation_access_code.call_count == 1
Loading

0 comments on commit 3e21536

Please sign in to comment.