From c8662d20a1f36165ce20dd13fc96f006f5c2573f Mon Sep 17 00:00:00 2001 From: "Jens W. Klein" Date: Mon, 16 Dec 2024 18:30:36 +0100 Subject: [PATCH] fix tests and data structure for callback 2 --- src/edutap/wallet_google/models/callback.py | 4 +-- tests/test_handler_fastapi.py | 34 +++++++++++++++++++++ tests/test_handler_validate.py | 24 ++++++++++----- 3 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 tests/test_handler_fastapi.py diff --git a/src/edutap/wallet_google/models/callback.py b/src/edutap/wallet_google/models/callback.py index f5b202a..159a4c4 100644 --- a/src/edutap/wallet_google/models/callback.py +++ b/src/edutap/wallet_google/models/callback.py @@ -34,9 +34,7 @@ class CallbackData(Model): signature: str intermediateSigningKey: IntermediateSigningKey protocolVersion: str - signedMessage: ( - SignedMessage | str - ) # google sends this as a string, but we want to parse it as a SignedMessage + signedMessage: str class RootSigningPublicKey(Model): diff --git a/tests/test_handler_fastapi.py b/tests/test_handler_fastapi.py new file mode 100644 index 0000000..8a3386c --- /dev/null +++ b/tests/test_handler_fastapi.py @@ -0,0 +1,34 @@ +from fastapi import FastAPI +from fastapi.testclient import TestClient + + +# this callback data can be verified given the credentials.json from demo.edutap.eu is provided. +real_callback_data = { + "signature": "MEYCIQCyuBQo/Dao7yUBDUWK12ATFBDkUfJUnropjOaPbPiKEwIhAKXNiVrbNmydpEVIxXRz5z36f8HV2Meq/Td6tqt2+DYO", + "intermediateSigningKey": { + "signedKey": '{"keyValue":"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE3JpSX3AU53vH+IpWBdsbqrL7Ey67QSERsDUztFt8q7t7PzVkh14SeYeokI1zSZiVAWnx4tXD1tCPbrvfFGB8OA\\u003d\\u003d","keyExpiration":"1735023986000"}', + "signatures": [ + "MEUCIQD3IBTpRM45gpno9Remtx/FiDCOJUp45+C+Qzw6IrgphwIgJijXISc+Ft8Sj9eXNowzuYyXyWlgKAE+tVnN24Sek5M=" + ], + }, + "protocolVersion": "ECv2SigningOnly", + "signedMessage": '{"classId":"3388000000022141777.pass.gift.dev.edutap.eu","objectId":"3388000000022141777.9fd4e525-777c-4e0d-878a-b7993e211997","eventType":"save","expTimeMillis":1734366082269,"count":1,"nonce":"c1359b53-f2bb-4e8f-b392-9a560a21a9a0"}', +} + + +def test_callback(): + ... + from edutap.wallet_google.models.handlers import CallbackData + from edutap.wallet_google.session import session_manager + + settings = session_manager.settings + settings.callback_verify_signature = False + callback_data = CallbackData(**real_callback_data) + + from edutap.wallet_google.handlers.fastapi import router + + app = FastAPI() + app.include_router(router) + client = TestClient(app) + client.post("/googlewallet/callback", json=callback_data.model_dump(mode="json")) + del session_manager._settings diff --git a/tests/test_handler_validate.py b/tests/test_handler_validate.py index 69ffc72..09fe00e 100644 --- a/tests/test_handler_validate.py +++ b/tests/test_handler_validate.py @@ -1,6 +1,7 @@ from edutap.wallet_google._vendor.google_pay_token_decryption import GooglePayError from edutap.wallet_google.models.callback import CallbackData +import json import pytest @@ -11,20 +12,27 @@ "signatures": ["bar"], }, "protocolVersion": "ECv2SigningOnly", - "signedMessage": { - "classId": "1", - "objectId": "2", - "expTimeMillis": 0, - "eventType": "SAVE", - "count": 0, - "nonce": "3", - }, + "signedMessage": json.dumps( + { + "classId": "1", + "objectId": "2", + "expTimeMillis": 0, + "eventType": "save", + "count": 0, + "nonce": "fooo", + } + ), } def test_handler_validate_invalid(): from edutap.wallet_google.handlers.validate import verified_signed_message + from edutap.wallet_google.session import session_manager + + settings = session_manager.settings + settings.callback_verify_signature = True data = CallbackData.model_validate(callbackdata_for_test_failure) with pytest.raises(GooglePayError): verified_signed_message(data) + del session_manager._settings