Skip to content

Commit

Permalink
Merge pull request #67 from City-of-Helsinki/MAAS-109-ticket-system-a…
Browse files Browse the repository at this point in the history
…pi-call-log

MAAS-109 | Log ticketing system API calls for debugging
  • Loading branch information
Pekka Lampila authored Jun 24, 2021
2 parents e073c4e + ea892cd commit 1723b4b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
8 changes: 8 additions & 0 deletions bookings/tests/snapshots/snap_test_api_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"transaction_id": "transactionID",
}

snapshots[
"test_api_call_for_confirmation 2"
] = 'Ticketing system API call - URL: https://api.example.com/test_confirmation_id/confirm/ data: {"request_id": "requestID", "transaction_id": "transactionID", "locale": "fi", "maas_operator_id": "identifier of maas operator 1"}' # noqa: E501

snapshots["test_api_call_for_reservation 1"] = {
"departures": [{"date": "2021-04-28", "trip_id": "source_id of trip 1"}],
"locale": "fi",
Expand All @@ -27,3 +31,7 @@
],
"transaction_id": "transactionID",
}

snapshots[
"test_api_call_for_reservation 2"
] = 'Ticketing system API call - URL: https://api.example.com data: {"request_id": "requestID", "transaction_id": "transactionID", "locale": "fi", "maas_operator_id": "identifier of maas operator 1", "route_id": "source_id of route 1", "departures": [{"trip_id": "source_id of trip 1", "date": "2021-04-28"}], "tickets": [{"ticket_type_id": "source_id of test fare 1", "customer_type_id": "source_id of test rider category 1"}]}' # noqa: E501
27 changes: 25 additions & 2 deletions bookings/tests/test_api_call.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from urllib.parse import quote, urljoin

import pytest
Expand All @@ -10,11 +11,25 @@
from mock_ticket_api.utils import get_confirmations_data, get_reservation_data


def get_log_records(caplog):
"""Return api call log messages.
Format of record tuples is (logger_name, log_level, message).
"""
return [
log[2]
for log in caplog.record_tuples
if log[0] == "bookings.ticketing_system" and log[1] == logging.INFO
]


@pytest.mark.django_db
def test_api_call_for_reservation(
maas_operator, fare_test_data, requests_mock, snapshot
maas_operator, fare_test_data, requests_mock, snapshot, caplog
):
ticketing_system = fare_test_data.feed.ticketing_system
ticketing_system.api_url = "https://api.example.com"
ticketing_system.save()
requests_mock.post(
ticketing_system.api_url,
json=get_reservation_data(),
Expand All @@ -35,14 +50,17 @@ def test_api_call_for_reservation(
}

Booking.objects.create_reservation(maas_operator, ticketing_system, ticket_data)
log_messages = get_log_records(caplog)

assert requests_mock.call_count == 1
snapshot.assert_match(requests_mock.request_history[0].json())
assert requests_mock.request_history[0].headers["Authorization"] == "Bearer APIKEY"
assert len(log_messages) == 1
snapshot.assert_match(log_messages[0])


@pytest.mark.django_db
def test_api_call_for_confirmation(maas_operator, requests_mock, snapshot):
def test_api_call_for_confirmation(maas_operator, requests_mock, snapshot, caplog):
feed = get_feed_for_maas_operator(maas_operator, True)
extra_params = {
"locale": "fi",
Expand All @@ -56,17 +74,22 @@ def test_api_call_for_confirmation(maas_operator, requests_mock, snapshot):
ticketing_system=feed.ticketing_system,
)
ticketing_system = feed.ticketing_system
ticketing_system.api_url = "https://api.example.com"
ticketing_system.save()
requests_mock.post(
urljoin(ticketing_system.api_url, f"{reserved_booking.source_id}/confirm/"),
json=get_confirmations_data(reserved_booking.source_id, include_qr=False),
status_code=status.HTTP_200_OK,
)

reserved_booking.confirm(passed_parameters=extra_params)
log_messages = get_log_records(caplog)

assert requests_mock.call_count == 1
snapshot.assert_match(requests_mock.request_history[0].json())
assert requests_mock.request_history[0].headers["Authorization"] == "Bearer APIKEY"
assert len(log_messages) == 1
snapshot.assert_match(log_messages[0])


@pytest.mark.django_db
Expand Down
8 changes: 8 additions & 0 deletions bookings/ticketing_system.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
import logging
from json import JSONDecodeError
from typing import Optional
from urllib.parse import quote, urljoin
Expand All @@ -11,6 +13,8 @@
from .choices import BookingStatus
from .utils import TokenAuth

logger = logging.getLogger(__name__)

reservation_error_codes = (
"MAX_CAPACITY_EXCEEDED",
"MAX_NUMBER_OF_TICKETS_REQUESTED_EXCEEDED",
Expand Down Expand Up @@ -107,6 +111,10 @@ def _post(self, url: str, data, success_serializer, error_serializer):
if not self.ticketing_system.api_key:
raise Exception("Ticketing system doesn't define an API key.")

logger.info(
f"Ticketing system API call - URL: {url} data: {json.dumps(payload)}"
)

response = requests.post(
url,
json=payload,
Expand Down

0 comments on commit 1723b4b

Please sign in to comment.