From 3f26f7696cc04c49aa0b6d32ef3624bea24842e3 Mon Sep 17 00:00:00 2001 From: Shubham Padkonde Date: Sun, 4 Oct 2026 14:03:45 +0530 Subject: [PATCH] fix: require integer offer period counts --- .../models/HelperValidationUtils.py | 9 +++++---- tests/test_advanced_commerce_models.py | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/appstoreserverlibrary/models/HelperValidationUtils.py b/appstoreserverlibrary/models/HelperValidationUtils.py index 3469fbad..ae009ecb 100644 --- a/appstoreserverlibrary/models/HelperValidationUtils.py +++ b/appstoreserverlibrary/models/HelperValidationUtils.py @@ -57,15 +57,16 @@ def sku_validator(instance, attribute, value): @staticmethod def period_count_validator(instance, attribute, value): """ - Validates periodCount is not None and between `MIN_PERIOD` and `MAX_PERIOD` inclusive. + Validates periodCount is an integer between `MIN_PERIOD` and `MAX_PERIOD` inclusive. Raises: - ValueError: If period_count is out of range + ValueError: If period_count is not an integer or is out of range """ - if (value < HelperValidationUtils.MIN_PERIOD or + if (not isinstance(value, int) or isinstance(value, bool) or + value < HelperValidationUtils.MIN_PERIOD or value > HelperValidationUtils.MAX_PERIOD): raise ValueError( - f"Period count must be between " + f"Period count must be an integer between " f"{HelperValidationUtils.MIN_PERIOD} and " f"{HelperValidationUtils.MAX_PERIOD}" ) diff --git a/tests/test_advanced_commerce_models.py b/tests/test_advanced_commerce_models.py index 93e2315d..205ef408 100644 --- a/tests/test_advanced_commerce_models.py +++ b/tests/test_advanced_commerce_models.py @@ -197,6 +197,26 @@ def test_validation_utils_period_count(self): with self.assertRaises(ValueError): HelperValidationUtils.period_count_validator(None, None, 13) + def test_offer_rejects_non_integer_period_counts(self): + for count in [1.5, 1.0, float('nan'), True, False, None, '1']: + with self.subTest(count=count): + with self.assertRaises(ValueError): + AdvancedCommerceOffer( + periodCount=count, price=1000, + period=AdvancedCommerceOfferPeriod.P1M, + reason=AdvancedCommerceOfferReason.ACQUISITION + ) + + def test_offer_accepts_integer_period_counts(self): + for count in range(1, 13): + with self.subTest(count=count): + offer = AdvancedCommerceOffer( + periodCount=count, price=1000, + period=AdvancedCommerceOfferPeriod.P1M, + reason=AdvancedCommerceOfferReason.ACQUISITION + ) + self.assertEqual(offer.periodCount, count) + def test_validation_utils_items(self): valid_list = [ AdvancedCommerceOneTimeChargeItem(