Skip to content

Commit

Permalink
fix tests and data structure for callback 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Dec 16, 2024
1 parent c20ee47 commit c8662d2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/edutap/wallet_google/models/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
34 changes: 34 additions & 0 deletions tests/test_handler_fastapi.py
Original file line number Diff line number Diff line change
@@ -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
24 changes: 16 additions & 8 deletions tests/test_handler_validate.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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

0 comments on commit c8662d2

Please sign in to comment.