From ae8d94f17a53c39607b7f9778c7eb99f0c21084d Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 16 Apr 2025 21:27:20 +0300 Subject: [PATCH] test(business): Add multiple tests - Add tests for data validation during promo code update - Add test for successful update of promo code's unique field --- promo_code/business/serializers.py | 12 +++ .../promocodes/operations/test_detail.py | 25 ++++++ .../validations/test_detail_validation.py | 80 ++++++++++++++++++- 3 files changed, 113 insertions(+), 4 deletions(-) diff --git a/promo_code/business/serializers.py b/promo_code/business/serializers.py index 14c0ce5..89e0818 100644 --- a/promo_code/business/serializers.py +++ b/promo_code/business/serializers.py @@ -448,6 +448,18 @@ class PromoDetailSerializer(rest_framework.serializers.ModelSerializer): source='id', read_only=True, ) + description = rest_framework.serializers.CharField( + min_length=10, + max_length=300, + required=True, + ) + image_url = rest_framework.serializers.CharField( + required=False, + max_length=350, + validators=[ + django.core.validators.URLValidator(schemes=['http', 'https']), + ], + ) target = TargetSerializer(allow_null=True, required=False) promo_unique = rest_framework.serializers.SerializerMethodField() company_name = rest_framework.serializers.CharField( diff --git a/promo_code/business/tests/promocodes/operations/test_detail.py b/promo_code/business/tests/promocodes/operations/test_detail.py index d319704..615aebd 100644 --- a/promo_code/business/tests/promocodes/operations/test_detail.py +++ b/promo_code/business/tests/promocodes/operations/test_detail.py @@ -226,6 +226,31 @@ def test_patch_decrease_max_count_company1(self): ) self.assertEqual(response.data.get('max_count'), 4) + def test_patch_edit_image_url_company2(self): + promo_detail_url = self.promo_detail_url(self.__class__.promo2_id) + self.client.credentials( + HTTP_AUTHORIZATION='Bearer ' + self.company2_token, + ) + response = self.client.get(promo_detail_url) + self.assertEqual( + response.status_code, + rest_framework.status.HTTP_200_OK, + ) + self.assertIsNone(response.data.get('image_url')) + data = { + 'image_url': 'https://cdn2.thecatapi.com/images/3lo.jpg', + } + + response = self.client.patch(promo_detail_url, data, format='json') + self.assertEqual( + response.status_code, + rest_framework.status.HTTP_200_OK, + ) + self.assertEqual( + response.data.get('image_url'), + 'https://cdn2.thecatapi.com/images/3lo.jpg', + ) + def test_final_get_promo_company1(self): promo_detail_url = self.promo_detail_url(self.__class__.promo1_id) data = { diff --git a/promo_code/business/tests/promocodes/validations/test_detail_validation.py b/promo_code/business/tests/promocodes/validations/test_detail_validation.py index 451dd24..ba3bfba 100644 --- a/promo_code/business/tests/promocodes/validations/test_detail_validation.py +++ b/promo_code/business/tests/promocodes/validations/test_detail_validation.py @@ -139,6 +139,14 @@ def test_edit_invalid_short_description(self): 'malformed_url', {'image_url': 'notalink'}, ), + ( + 'empty_url', + {'image_url': ''}, + ), + ( + 'invalid_url_with_spaces', + {'image_url': 'https://example.com/image with spaces.jpg'}, + ), ], ) def test_edit_invalid_image_urls(self, _, patch_payload): @@ -158,21 +166,43 @@ def test_edit_invalid_image_urls(self, _, patch_payload): ( 'age_range_invalid', { - 'description': 'Bonus 10000%!', 'target': {'age_from': 19, 'age_until': 17}, }, ), + ('age_from_too_high', {'target': {'age_from': 200}}), + ('age_until_too_high', {'target': {'age_until': 200}}), + ('age_from_too_low', {'target': {'age_from': -1}}), + ('age_until_too_low', {'target': {'age_until': -1}}), + ( + 'invalid_target_country_format', + {'target': {'country': 'USA'}}, + ), ( - 'incomplete_target', - {'description': 'Bonus 10000%!', 'target': {'country': 'USA'}}, + 'invalid_target_country_too_short', + {'target': {'country': 'U'}}, + ), + ( + 'invalid_target_country_does_not_exist', + {'target': {'country': 'XX'}}, ), ( 'empty_category', { - 'description': 'Bonus 10000%!', 'target': {'categories': ['']}, }, ), + ( + 'non_string_category', + { + 'target': {'categories': [1, 2, 3]}, + }, + ), + ( + 'non_dict_target', + { + 'target': ['not', 'a', 'dict'], + }, + ), ], ) def test_edit_invalid_target(self, _, patch_payload): @@ -225,6 +255,36 @@ def test_edit_invalid_target(self, _, patch_payload): 'common_payload', {'max_count': 100_000_001}, ), + ( + 'non_integer_max_count_float', + 'company1_token', + 'common_payload', + {'max_count': 10.5}, + ), + ( + 'max_count_is_empty_string', + 'company1_token', + 'common_payload', + {'max_count': ''}, + ), + ( + 'max_count_is_list', + 'company1_token', + 'common_payload', + {'max_count': [1, 2, 3]}, + ), + ( + 'max_count_is_dict', + 'company1_token', + 'common_payload', + {'max_count': {'key': 'value'}}, + ), + ( + 'max_count_is_boolean', + 'company1_token', + 'common_payload', + {'max_count': True}, + ), ], ) def test_edit_invalid_max_count( @@ -268,6 +328,18 @@ def test_get_promo_verify_fields_unchanged(self): HTTP_AUTHORIZATION='Bearer ' + self.company2_token, ) url = self.promo_detail_url(promo_id) + patch_payload = { + 'active_from': '2024-12-28 12:00:00', + 'max_count': 100, + 'description': 'short', + 'target': {'age_from': 26, 'country': 'fr'}, + } + response = self.client.patch(url, patch_payload, format='json') + self.assertEqual( + response.status_code, + rest_framework.status.HTTP_400_BAD_REQUEST, + ) + response = self.client.get(url, format='json') self.assertEqual( response.status_code,