Skip to content

Commit

Permalink
add tests, general check function
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Jan 22, 2025
1 parent cc4002b commit 06e44a3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
35 changes: 19 additions & 16 deletions src/edutap/wallet_google/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,21 @@ def listing(
break


def _convert_str_or_datetime_to_str(value: str | datetime.datetime) -> str:
"""convert and check the value to be a valid string for the JWT claim timestamps"""
if isinstance(value, datetime.datetime):
return str(int(value.timestamp()))
if value == "":
return value
if not value.isdecimal():
raise ValueError("string must be a decimal")
if int(value) < 0:
raise ValueError("string must be an int >= 0 number")
if int(value) > 2**32:
raise ValueError("string must be an int < 2**32 number")
return value


def save_link(
models: list[ClassModel | ObjectModel | Reference],
*,
Expand All @@ -363,6 +378,8 @@ def save_link(
The Google Wallet API button will not render when the origins field is not defined.
You could potentially get an "Load denied by X-Frame-Options" or "Refused to display"
messages in the browser console when the origins field is not defined.
:param: iat: Issued At Time. The time when the JWT was issued.
:param: exp: Expiration Time. The time when the JWT expires.
:return: Link with JWT to save the resources to the wallet.
"""
payload = JWTPayload()
Expand All @@ -378,26 +395,12 @@ def save_link(
setattr(payload, name, [])
getattr(payload, name).append(model)

if isinstance(iat, datetime.datetime):
iat = str(int(iat.timestamp()))
if iat != "":
assert iat.isdecimal()
assert int(iat) >= 0
assert int(iat) < 2**32

if isinstance(exp, datetime.datetime):
exp = str(int(exp.timestamp()))
if exp != "":
assert exp.isdecimal()
assert int(exp) >= 0
assert int(exp) < 2**32

claims = JWTClaims(
iss=session_manager.settings.credentials_info["client_email"],
origins=origins,
payload=payload,
iat=iat,
exp=exp,
iat=_convert_str_or_datetime_to_str(iat),
exp=_convert_str_or_datetime_to_str(exp),
)

logger.debug(claims.model_dump_json(indent=2))
Expand Down
52 changes: 48 additions & 4 deletions tests/test_api_save_link.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest


def test_api_save_link(mock_settings):
from edutap.wallet_google.settings import ROOT_DIR

Expand All @@ -19,7 +22,48 @@ def test_api_save_link(mock_settings):
),
]
)
assert (
link
== "https://pay.google.com/gp/v/save/eyJ0eXAiOiAiSldUIiwgImFsZyI6ICJSUzI1NiIsICJraWQiOiAiMTIzNDU2Nzg5MGFiY2RlZjEyMzQ1Njc4OTBhYmNkZWYxMjM0NTY3OCJ9.eyJpc3MiOiAiZWR1dGFwLXRlc3QtZXhhbXBsZUBzb2RpdW0tcmF5LTEyMzQ1Ni5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsICJhdWQiOiAiZ29vZ2xlIiwgInR5cCI6ICJzYXZldHRvd2FsbGV0IiwgImlhdCI6ICIiLCAicGF5bG9hZCI6IHsib2ZmZXJPYmplY3RzIjogW3siaWQiOiAidGVzdC0yLmVkdXRhcC5ldSIsICJjbGFzc0lkIjogInRlc3QtY2xhc3MtMS5lZHV0YXAuZXUiLCAic3RhdGUiOiAiU1RBVEVfVU5TUEVDSUZJRUQiLCAiaGFzTGlua2VkRGV2aWNlIjogZmFsc2UsICJkaXNhYmxlRXhwaXJhdGlvbk5vdGlmaWNhdGlvbiI6IGZhbHNlLCAibm90aWZ5UHJlZmVyZW5jZSI6ICJOT1RJRklDQVRJT05fU0VUVElOR1NfRk9SX1VQREFURVNfVU5TUEVDSUZJRUQifV0sICJnZW5lcmljT2JqZWN0cyI6IFt7ImlkIjogInRlc3QtMS5lZHV0YXAuZXUifV19LCAib3JpZ2lucyI6IFtdfQ.LEPJBlt7ic9cPWKUvpoxUWe5yvdK0_kqPlBFkHmqFBfO5eeYN-owTHCElCGhnHeE730D4U3XjQWeZXfcaEAQcdBKB8udoT2Tja7Rw_M8M18kpBrSdGDRKT_uXG_-RkG3uVB30Lu5otlJiX2VOJWg9H6NR7wD_pfUt67cLjiBeMILuIVi-h0CDUV0dObEjnOHrRhj6KeKdfqq6izwwmw4iSQxsaQrDxWZtwCZ__pV5UK54Od6-lNrsBQwz241SDY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QjXYQRE0yxCY_bQEDydqK2tzOVd3YGiqLK_zZjBA3H3_rtqY6pdPdyUlADIWTU0dJ0sc1sNl0rjGFCgeMdziNhgppjxQ0iWnVyFb2hZO8QEi83aD5A46X9A"

assert link == expected


def test__convert_str_or_datetime_to_str__timestamp():
from edutap.wallet_google.api import _convert_str_or_datetime_to_str

import datetime

dt = datetime.datetime(2021, 1, 1, 12, 0, 0)
expected = '1609498800'

assert _convert_str_or_datetime_to_str(dt) == expected


def test__convert_str_or_datetime_to_str__str_int_lt_zero():
from edutap.wallet_google.api import _convert_str_or_datetime_to_str

import datetime


with pytest.raises(ValueError) as exc:
_convert_str_or_datetime_to_str("-1")


def test__convert_str_or_datetime_to_str__str_int_tr_4bytes():
from edutap.wallet_google.api import _convert_str_or_datetime_to_str

import datetime


with pytest.raises(ValueError) as exc:
_convert_str_or_datetime_to_str(f"{2**32+1}")

def test__convert_str_or_datetime_to_str__not_decimal():
from edutap.wallet_google.api import _convert_str_or_datetime_to_str

import datetime


with pytest.raises(ValueError) as exc:
_convert_str_or_datetime_to_str("x 100")


0 comments on commit 06e44a3

Please sign in to comment.