Skip to content

Commit 8016593

Browse files
test(business): Add tests for promo code detail endpoint.
- Validate successful retrieval and update of promo codes. - Verify error responses for 400, 401, 403, and 404 scenarios. - Standardize naming conventions for all business promo code tests.
1 parent 4512dbf commit 8016593

File tree

7 files changed

+537
-5
lines changed

7 files changed

+537
-5
lines changed

promo_code/business/tests/promocodes/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import django.urls
22
import rest_framework
3+
import rest_framework.status
34
import rest_framework.test
45

56
import business.models
@@ -57,6 +58,13 @@ def setUpTestData(cls):
5758
)
5859
cls.company2_token = response2.data['access']
5960

61+
@classmethod
62+
def promo_detail_url(cls, promo_id):
63+
return django.urls.reverse(
64+
'api-business:promo-detail',
65+
kwargs={'id': promo_id},
66+
)
67+
6068
def tearDown(self):
6169
business.models.Company.objects.all().delete()
6270
business.models.Promo.objects.all().delete()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import business.tests.promocodes.base
55

66

7-
class TestSuccessfulPromoCreation(
7+
class TestPromoCreate(
88
business.tests.promocodes.base.BasePromoTestCase,
99
):
1010
def setUp(self):
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
import django.urls
2+
import rest_framework.status
3+
import rest_framework.test
4+
5+
import business.tests.promocodes.base
6+
7+
8+
class TestPromoDetail(business.tests.promocodes.base.BasePromoTestCase):
9+
@classmethod
10+
def setUpTestData(cls):
11+
super().setUpTestData()
12+
client = rest_framework.test.APIClient()
13+
14+
client.credentials(HTTP_AUTHORIZATION='Bearer ' + cls.company1_token)
15+
promo1_data = {
16+
'description': 'Increased 10% cashback for new bank clients!',
17+
'image_url': 'https://cdn2.thecatapi.com/images/3lo.jpg',
18+
'target': {},
19+
'max_count': 10,
20+
'active_from': '2025-01-10',
21+
'mode': 'COMMON',
22+
'promo_common': 'sale-10',
23+
}
24+
response1 = client.post(
25+
cls.promo_create_url,
26+
promo1_data,
27+
format='json',
28+
)
29+
cls.promo1_id = response1.data['id']
30+
31+
client.credentials(HTTP_AUTHORIZATION='Bearer ' + cls.company2_token)
32+
promo2_data = {
33+
'description': 'We gift a globe when order exceeds 30000!',
34+
'target': {'age_from': 28, 'age_until': 50, 'country': 'us'},
35+
'max_count': 1,
36+
'active_until': '2025-01-10',
37+
'mode': 'UNIQUE',
38+
'promo_unique': ['only_youuuu', 'not_only_you'],
39+
}
40+
response2 = client.post(
41+
cls.promo_create_url,
42+
promo2_data,
43+
format='json',
44+
)
45+
cls.promo2_id = response2.data['id']
46+
47+
def test_get_promo_company1(self):
48+
49+
promo_detail_url = django.urls.reverse(
50+
'api-business:promo-detail',
51+
kwargs={'id': self.__class__.promo1_id},
52+
)
53+
self.client.credentials(
54+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
55+
)
56+
response = self.client.get(promo_detail_url)
57+
self.assertEqual(
58+
response.status_code,
59+
rest_framework.status.HTTP_200_OK,
60+
)
61+
expected = {
62+
'description': 'Increased 10% cashback for new bank clients!',
63+
'image_url': 'https://cdn2.thecatapi.com/images/3lo.jpg',
64+
'target': {},
65+
'max_count': 10,
66+
'active_from': '2025-01-10',
67+
'mode': 'COMMON',
68+
'promo_common': 'sale-10',
69+
'promo_id': str(self.promo1_id),
70+
'company_name': self.company1_data['name'],
71+
'like_count': 0,
72+
'used_count': 0,
73+
}
74+
for key, value in expected.items():
75+
self.assertEqual(response.data.get(key), value)
76+
77+
def test_get_promo_company2(self):
78+
promo_detail_url = self.promo_detail_url(self.__class__.promo2_id)
79+
self.client.credentials(
80+
HTTP_AUTHORIZATION='Bearer ' + self.company2_token,
81+
)
82+
response = self.client.get(promo_detail_url)
83+
self.assertEqual(
84+
response.status_code,
85+
rest_framework.status.HTTP_200_OK,
86+
)
87+
expected = {
88+
'description': 'We gift a globe when order exceeds 30000!',
89+
'target': {'age_from': 28, 'age_until': 50, 'country': 'us'},
90+
'max_count': 1,
91+
'active_until': '2025-01-10',
92+
'mode': 'UNIQUE',
93+
'promo_unique': ['only_youuuu', 'not_only_you'],
94+
'promo_id': str(self.promo2_id),
95+
'company_name': self.company2_data['name'],
96+
'like_count': 0,
97+
'used_count': 0,
98+
}
99+
for key, value in expected.items():
100+
self.assertEqual(response.data.get(key), value)
101+
102+
def test_patch_description_image_company1(self):
103+
promo_detail_url = self.promo_detail_url(self.__class__.promo1_id)
104+
data = {
105+
'description': '100% Cashback',
106+
'image_url': 'https://doesitexist.com/',
107+
}
108+
self.client.credentials(
109+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
110+
)
111+
response = self.client.patch(promo_detail_url, data, format='json')
112+
self.assertEqual(
113+
response.status_code,
114+
rest_framework.status.HTTP_200_OK,
115+
)
116+
self.assertEqual(response.data.get('description'), '100% Cashback')
117+
self.assertEqual(
118+
response.data.get('image_url'),
119+
'https://doesitexist.com/',
120+
)
121+
self.assertEqual(response.data.get('target'), {})
122+
123+
def test_patch_active_from_company1(self):
124+
promo_detail_url = self.promo_detail_url(self.__class__.promo1_id)
125+
data = {'active_from': '2023-12-20'}
126+
self.client.credentials(
127+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
128+
)
129+
response = self.client.patch(promo_detail_url, data, format='json')
130+
self.assertEqual(
131+
response.status_code,
132+
rest_framework.status.HTTP_200_OK,
133+
)
134+
self.assertEqual(response.data.get('active_from'), '2023-12-20')
135+
136+
def test_patch_partial_target_company1(self):
137+
promo_detail_url = self.promo_detail_url(self.__class__.promo1_id)
138+
data = {'target': {'country': 'fr', 'age_from': 28}}
139+
self.client.credentials(
140+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
141+
)
142+
response = self.client.patch(promo_detail_url, data, format='json')
143+
self.assertEqual(
144+
response.status_code,
145+
rest_framework.status.HTTP_200_OK,
146+
)
147+
self.assertEqual(
148+
response.data.get('target'),
149+
{'country': 'fr', 'age_from': 28},
150+
)
151+
152+
def test_patch_replace_target_company1(self):
153+
promo_detail_url = self.promo_detail_url(self.__class__.promo1_id)
154+
data = {
155+
'target': {'country': 'Us', 'categories': ['ios', 'footballer']},
156+
}
157+
self.client.credentials(
158+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
159+
)
160+
response = self.client.patch(promo_detail_url, data, format='json')
161+
self.assertEqual(
162+
response.status_code,
163+
rest_framework.status.HTTP_200_OK,
164+
)
165+
self.assertEqual(
166+
response.data.get('target'),
167+
{'country': 'Us', 'categories': ['ios', 'footballer']},
168+
)
169+
170+
def test_patch_active_until_company1(self):
171+
promo_detail_url = self.promo_detail_url(self.__class__.promo1_id)
172+
data = {
173+
'active_until': '2050-10-08',
174+
'target': {'country': 'Us', 'categories': ['ios', 'footballer']},
175+
}
176+
self.client.credentials(
177+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
178+
)
179+
response = self.client.patch(promo_detail_url, data, format='json')
180+
self.assertEqual(
181+
response.status_code,
182+
rest_framework.status.HTTP_200_OK,
183+
)
184+
self.assertEqual(
185+
response.data.get('target'),
186+
{'country': 'Us', 'categories': ['ios', 'footballer']},
187+
)
188+
self.assertEqual(response.data.get('active_until'), '2050-10-08')
189+
190+
def test_patch_clear_target_company1(self):
191+
promo_detail_url = self.promo_detail_url(self.__class__.promo1_id)
192+
data = {'target': {}}
193+
self.client.credentials(
194+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
195+
)
196+
response = self.client.patch(promo_detail_url, data, format='json')
197+
self.assertEqual(
198+
response.status_code,
199+
rest_framework.status.HTTP_200_OK,
200+
)
201+
self.assertEqual(response.data.get('target'), {})
202+
203+
def test_patch_increase_max_count_company1(self):
204+
promo_detail_url = self.promo_detail_url(self.__class__.promo1_id)
205+
data = {'max_count': 20}
206+
self.client.credentials(
207+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
208+
)
209+
response = self.client.patch(promo_detail_url, data, format='json')
210+
self.assertEqual(
211+
response.status_code,
212+
rest_framework.status.HTTP_200_OK,
213+
)
214+
self.assertEqual(response.data.get('max_count'), 20)
215+
216+
def test_patch_decrease_max_count_company1(self):
217+
promo_detail_url = self.promo_detail_url(self.__class__.promo1_id)
218+
data = {'max_count': 4}
219+
self.client.credentials(
220+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
221+
)
222+
response = self.client.patch(promo_detail_url, data, format='json')
223+
self.assertEqual(
224+
response.status_code,
225+
rest_framework.status.HTTP_200_OK,
226+
)
227+
self.assertEqual(response.data.get('max_count'), 4)
228+
229+
def test_final_get_promo_company1(self):
230+
promo_detail_url = self.promo_detail_url(self.__class__.promo1_id)
231+
data = {
232+
'description': '100% Cashback',
233+
'image_url': 'https://doesitexist.com/',
234+
'active_from': '2023-12-20',
235+
'active_until': '2050-10-08',
236+
'target': {},
237+
'max_count': 4,
238+
}
239+
self.client.credentials(
240+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
241+
)
242+
response = self.client.patch(promo_detail_url, data, format='json')
243+
self.assertEqual(
244+
response.status_code,
245+
rest_framework.status.HTTP_200_OK,
246+
)
247+
248+
response = self.client.get(self.promo_list_url)
249+
self.assertEqual(
250+
response.status_code,
251+
rest_framework.status.HTTP_200_OK,
252+
)
253+
data = response.data
254+
255+
promo_detail_url = django.urls.reverse(
256+
'api-business:promo-detail',
257+
kwargs={'id': self.__class__.promo1_id},
258+
)
259+
260+
response = self.client.get(promo_detail_url)
261+
self.assertEqual(
262+
response.status_code,
263+
rest_framework.status.HTTP_200_OK,
264+
)
265+
expected = {
266+
'description': '100% Cashback',
267+
'image_url': 'https://doesitexist.com/',
268+
'target': {},
269+
'max_count': 4,
270+
'active_from': '2023-12-20',
271+
'mode': 'COMMON',
272+
'promo_common': 'sale-10',
273+
'promo_id': str(self.__class__.promo1_id),
274+
'company_name': self.company1_data['name'],
275+
'like_count': 0,
276+
'used_count': 0,
277+
}
278+
for key, value in expected.items():
279+
self.assertEqual(response.data.get(key), value)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import business.tests.promocodes.base
77

88

9-
class TestPromoEndpoint(
9+
class TestPromoList(
1010
business.tests.promocodes.base.BasePromoTestCase,
1111
):
1212
def _create_additional_promo(self):
@@ -30,7 +30,7 @@ def _create_additional_promo(self):
3030

3131
@classmethod
3232
def setUpTestData(cls):
33-
business.tests.promocodes.base.BasePromoCreateTestCase.setUpTestData()
33+
business.tests.promocodes.base.BasePromoTestCase.setUpTestData()
3434

3535
cls.promo1_data = {
3636
'description': 'Increased cashback 10% for new bank customers!',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import business.tests.promocodes.base
55

66

7-
class TestPromoCodeCreation(
7+
class TestPromoCreate(
88
business.tests.promocodes.base.BasePromoTestCase,
99
):
1010

0 commit comments

Comments
 (0)