From c4e8544178721314382754cef3f3e5da7242a0f8 Mon Sep 17 00:00:00 2001 From: alipov_d Date: Wed, 12 Jul 2023 21:33:18 +0200 Subject: [PATCH] fix: error 500 dashboard after applying ecommerse coupon --- ecommerce/extensions/api/serializers.py | 1 + .../api/v2/tests/views/test_vouchers.py | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/ecommerce/extensions/api/serializers.py b/ecommerce/extensions/api/serializers.py index 5e8b29f94d5..b3c17b59e3e 100644 --- a/ecommerce/extensions/api/serializers.py +++ b/ecommerce/extensions/api/serializers.py @@ -943,6 +943,7 @@ class VoucherSerializer(serializers.ModelSerializer): is_available_to_user = serializers.SerializerMethodField() benefit = serializers.SerializerMethodField() redeem_url = serializers.SerializerMethodField() + end_datetime = serializers.DateTimeField(format=ISO_8601_FORMAT) def get_is_available_to_user(self, obj): request = self.context.get('request') diff --git a/ecommerce/extensions/api/v2/tests/views/test_vouchers.py b/ecommerce/extensions/api/v2/tests/views/test_vouchers.py index da9c146b27c..626dc1cf0e4 100644 --- a/ecommerce/extensions/api/v2/tests/views/test_vouchers.py +++ b/ecommerce/extensions/api/v2/tests/views/test_vouchers.py @@ -68,6 +68,30 @@ def create_vouchers(self, partner=None, count=1): coupon_vouchers.vouchers.add(voucher) return vouchers + def test_generate_correct_end_datetime(self): + """ + Test that the 'end_datetime' parameter is generated in the correct format. + + The 'end_datetime' parameter should be generated as a string in the + format '%Y-%m-%dT%H:%M:%SZ', representing the end date and time. + The created voucher's 'end_datetime' has the milliseconds part if to + retrieve it directly from the DB. + """ + self.create_vouchers() + response = self.client.get(self.path) + response_data = response.json() + result = response_data['results'][0] + end_datetime = result['end_datetime'] + is_valid_format = False + + try: + datetime.datetime.strptime(end_datetime, '%Y-%m-%dT%H:%M:%SZ') + is_valid_format = True + except ValueError: + pass + + self.assertTrue(is_valid_format) + def test_list(self): """ Verify the endpoint lists all vouchers. """ vouchers = self.create_vouchers(count=3)