Skip to content

Commit 453b98c

Browse files
Merge pull request #25 from RandomProgramm3r/develop
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
2 parents 7edf873 + ae8d94f commit 453b98c

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

promo_code/business/serializers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,18 @@ class PromoDetailSerializer(rest_framework.serializers.ModelSerializer):
448448
source='id',
449449
read_only=True,
450450
)
451+
description = rest_framework.serializers.CharField(
452+
min_length=10,
453+
max_length=300,
454+
required=True,
455+
)
456+
image_url = rest_framework.serializers.CharField(
457+
required=False,
458+
max_length=350,
459+
validators=[
460+
django.core.validators.URLValidator(schemes=['http', 'https']),
461+
],
462+
)
451463
target = TargetSerializer(allow_null=True, required=False)
452464
promo_unique = rest_framework.serializers.SerializerMethodField()
453465
company_name = rest_framework.serializers.CharField(

promo_code/business/tests/promocodes/operations/test_detail.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,31 @@ def test_patch_decrease_max_count_company1(self):
226226
)
227227
self.assertEqual(response.data.get('max_count'), 4)
228228

229+
def test_patch_edit_image_url_company2(self):
230+
promo_detail_url = self.promo_detail_url(self.__class__.promo2_id)
231+
self.client.credentials(
232+
HTTP_AUTHORIZATION='Bearer ' + self.company2_token,
233+
)
234+
response = self.client.get(promo_detail_url)
235+
self.assertEqual(
236+
response.status_code,
237+
rest_framework.status.HTTP_200_OK,
238+
)
239+
self.assertIsNone(response.data.get('image_url'))
240+
data = {
241+
'image_url': 'https://cdn2.thecatapi.com/images/3lo.jpg',
242+
}
243+
244+
response = self.client.patch(promo_detail_url, data, format='json')
245+
self.assertEqual(
246+
response.status_code,
247+
rest_framework.status.HTTP_200_OK,
248+
)
249+
self.assertEqual(
250+
response.data.get('image_url'),
251+
'https://cdn2.thecatapi.com/images/3lo.jpg',
252+
)
253+
229254
def test_final_get_promo_company1(self):
230255
promo_detail_url = self.promo_detail_url(self.__class__.promo1_id)
231256
data = {

promo_code/business/tests/promocodes/validations/test_detail_validation.py

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ def test_edit_invalid_short_description(self):
139139
'malformed_url',
140140
{'image_url': 'notalink'},
141141
),
142+
(
143+
'empty_url',
144+
{'image_url': ''},
145+
),
146+
(
147+
'invalid_url_with_spaces',
148+
{'image_url': 'https://example.com/image with spaces.jpg'},
149+
),
142150
],
143151
)
144152
def test_edit_invalid_image_urls(self, _, patch_payload):
@@ -158,21 +166,43 @@ def test_edit_invalid_image_urls(self, _, patch_payload):
158166
(
159167
'age_range_invalid',
160168
{
161-
'description': 'Bonus 10000%!',
162169
'target': {'age_from': 19, 'age_until': 17},
163170
},
164171
),
172+
('age_from_too_high', {'target': {'age_from': 200}}),
173+
('age_until_too_high', {'target': {'age_until': 200}}),
174+
('age_from_too_low', {'target': {'age_from': -1}}),
175+
('age_until_too_low', {'target': {'age_until': -1}}),
176+
(
177+
'invalid_target_country_format',
178+
{'target': {'country': 'USA'}},
179+
),
165180
(
166-
'incomplete_target',
167-
{'description': 'Bonus 10000%!', 'target': {'country': 'USA'}},
181+
'invalid_target_country_too_short',
182+
{'target': {'country': 'U'}},
183+
),
184+
(
185+
'invalid_target_country_does_not_exist',
186+
{'target': {'country': 'XX'}},
168187
),
169188
(
170189
'empty_category',
171190
{
172-
'description': 'Bonus 10000%!',
173191
'target': {'categories': ['']},
174192
},
175193
),
194+
(
195+
'non_string_category',
196+
{
197+
'target': {'categories': [1, 2, 3]},
198+
},
199+
),
200+
(
201+
'non_dict_target',
202+
{
203+
'target': ['not', 'a', 'dict'],
204+
},
205+
),
176206
],
177207
)
178208
def test_edit_invalid_target(self, _, patch_payload):
@@ -225,6 +255,36 @@ def test_edit_invalid_target(self, _, patch_payload):
225255
'common_payload',
226256
{'max_count': 100_000_001},
227257
),
258+
(
259+
'non_integer_max_count_float',
260+
'company1_token',
261+
'common_payload',
262+
{'max_count': 10.5},
263+
),
264+
(
265+
'max_count_is_empty_string',
266+
'company1_token',
267+
'common_payload',
268+
{'max_count': ''},
269+
),
270+
(
271+
'max_count_is_list',
272+
'company1_token',
273+
'common_payload',
274+
{'max_count': [1, 2, 3]},
275+
),
276+
(
277+
'max_count_is_dict',
278+
'company1_token',
279+
'common_payload',
280+
{'max_count': {'key': 'value'}},
281+
),
282+
(
283+
'max_count_is_boolean',
284+
'company1_token',
285+
'common_payload',
286+
{'max_count': True},
287+
),
228288
],
229289
)
230290
def test_edit_invalid_max_count(
@@ -268,6 +328,18 @@ def test_get_promo_verify_fields_unchanged(self):
268328
HTTP_AUTHORIZATION='Bearer ' + self.company2_token,
269329
)
270330
url = self.promo_detail_url(promo_id)
331+
patch_payload = {
332+
'active_from': '2024-12-28 12:00:00',
333+
'max_count': 100,
334+
'description': 'short',
335+
'target': {'age_from': 26, 'country': 'fr'},
336+
}
337+
response = self.client.patch(url, patch_payload, format='json')
338+
self.assertEqual(
339+
response.status_code,
340+
rest_framework.status.HTTP_400_BAD_REQUEST,
341+
)
342+
271343
response = self.client.get(url, format='json')
272344
self.assertEqual(
273345
response.status_code,

0 commit comments

Comments
 (0)