Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions promo_code/business/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
25 changes: 25 additions & 0 deletions promo_code/business/tests/promocodes/operations/test_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down