Skip to content

Commit d63f258

Browse files
test(business): Rename and extend base class for promo tests.
- Rename base test class to reflect its broader purpose - Add second company creation and token setup to the base class - Update dependent test cases to use the new base class structure
1 parent b9592cd commit d63f258

File tree

6 files changed

+60
-37
lines changed

6 files changed

+60
-37
lines changed

promo_code/business/tests/promocodes/base.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import django.urls
22
import rest_framework
3-
import rest_framework.status
43
import rest_framework.test
54

65
import business.models
76

87

9-
class BasePromoCreateTestCase(rest_framework.test.APITestCase):
8+
class BasePromoTestCase(rest_framework.test.APITestCase):
109
@classmethod
1110
def setUpTestData(cls):
1211
super().setUpTestData()
@@ -17,28 +16,46 @@ def setUpTestData(cls):
1716
)
1817
cls.signup_url = django.urls.reverse('api-business:company-sign-up')
1918
cls.signin_url = django.urls.reverse('api-business:company-sign-in')
20-
cls.valid_data = {
19+
20+
cls.company1_data = {
2121
'name': 'Digital Marketing Solutions Inc.',
22-
'email': 'testcompany@example.com',
22+
'email': 'company1@example.com',
2323
'password': 'SecurePass123!',
2424
}
25-
business.models.Company.objects.create_company(
26-
**cls.valid_data,
25+
business.models.Company.objects.create_company(**cls.company1_data)
26+
cls.company1 = business.models.Company.objects.get(
27+
email=cls.company1_data['email'],
28+
)
29+
30+
cls.company2_data = {
31+
'name': 'Global Retail Hub LLC',
32+
'email': '[email protected]',
33+
'password': 'SecurePass456!',
34+
}
35+
business.models.Company.objects.create_company(**cls.company2_data)
36+
cls.company2 = business.models.Company.objects.get(
37+
email=cls.company2_data['email'],
2738
)
2839

29-
cls.company = business.models.Company.objects.get(
30-
email=cls.valid_data['email'],
40+
response1 = cls.client.post(
41+
cls.signin_url,
42+
{
43+
'email': cls.company1_data['email'],
44+
'password': cls.company1_data['password'],
45+
},
46+
format='json',
3147
)
48+
cls.company1_token = response1.data['access']
3249

33-
response = cls.client.post(
50+
response2 = cls.client.post(
3451
cls.signin_url,
3552
{
36-
'email': cls.valid_data['email'],
37-
'password': cls.valid_data['password'],
53+
'email': cls.company2_data['email'],
54+
'password': cls.company2_data['password'],
3855
},
3956
format='json',
4057
)
41-
cls.token = response.data['access']
58+
cls.company2_token = response2.data['access']
4259

4360
def tearDown(self):
4461
business.models.Company.objects.all().delete()

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import rest_framework.status
2+
import rest_framework.test
23

34
import business.tests.promocodes.base
45

56

67
class TestSuccessfulPromoCreation(
7-
business.tests.promocodes.base.BasePromoCreateTestCase,
8+
business.tests.promocodes.base.BasePromoTestCase,
89
):
10+
def setUp(self):
11+
self.client = rest_framework.test.APIClient()
12+
self.client.credentials(
13+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
14+
)
15+
916
def test_successful_promo_creation_1(self):
1017
payload = {
1118
'description': 'Increased cashback 10% for new bank clients!',
@@ -20,7 +27,6 @@ def test_successful_promo_creation_1(self):
2027
self.promo_create_url,
2128
payload,
2229
format='json',
23-
HTTP_AUTHORIZATION='Bearer ' + self.token,
2430
)
2531
self.assertEqual(
2632
response.status_code,
@@ -41,7 +47,6 @@ def test_successful_promo_creation_2(self):
4147
self.promo_create_url,
4248
payload,
4349
format='json',
44-
HTTP_AUTHORIZATION='Bearer ' + self.token,
4550
)
4651
self.assertEqual(
4752
response.status_code,
@@ -62,7 +67,6 @@ def test_successful_promo_creation_3(self):
6267
self.promo_create_url,
6368
payload,
6469
format='json',
65-
HTTP_AUTHORIZATION='Bearer ' + self.token,
6670
)
6771
self.assertEqual(
6872
response.status_code,
@@ -82,7 +86,6 @@ def test_successful_promo_creation_4(self):
8286
self.promo_create_url,
8387
payload,
8488
format='json',
85-
HTTP_AUTHORIZATION='Bearer ' + self.token,
8689
)
8790
self.assertEqual(
8891
response.status_code,
@@ -103,7 +106,6 @@ def test_successful_promo_creation_5(self):
103106
self.promo_create_url,
104107
payload,
105108
format='json',
106-
HTTP_AUTHORIZATION='Bearer ' + self.token,
107109
)
108110
self.assertEqual(
109111
response.status_code,
@@ -123,7 +125,6 @@ def test_successful_promo_creation_6_country_lower(self):
123125
self.promo_create_url,
124126
payload,
125127
format='json',
126-
HTTP_AUTHORIZATION='Bearer ' + self.token,
127128
)
128129
self.assertEqual(
129130
response.status_code,
@@ -143,7 +144,6 @@ def test_successful_promo_creation_6_country_upper(self):
143144
self.promo_create_url,
144145
payload,
145146
format='json',
146-
HTTP_AUTHORIZATION='Bearer ' + self.token,
147147
)
148148
self.assertEqual(
149149
response.status_code,
@@ -163,7 +163,6 @@ def test_successful_promo_creation_7(self):
163163
self.promo_create_url,
164164
payload,
165165
format='json',
166-
HTTP_AUTHORIZATION='Bearer ' + self.token,
167166
)
168167
self.assertEqual(
169168
response.status_code,

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
class TestPromoEndpoint(
10-
business.tests.promocodes.base.BasePromoCreateTestCase,
10+
business.tests.promocodes.base.BasePromoTestCase,
1111
):
1212
def _create_additional_promo(self):
1313
self.__class__.promo5_data = {
@@ -71,7 +71,7 @@ def setUpTestData(cls):
7171

7272
for promo_data in [cls.promo1_data, cls.promo2_data, cls.promo3_data]:
7373
promo = business.models.Promo.objects.create(
74-
company=cls.company,
74+
company=cls.company1,
7575
description=promo_data['description'],
7676
image_url=promo_data.get('image_url'),
7777
target=promo_data['target'],
@@ -94,14 +94,8 @@ def setUpTestData(cls):
9494

9595
def setUp(self):
9696
self.client = rest_framework.test.APIClient()
97-
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.token)
98-
99-
def test_get_promos_without_token(self):
100-
client = rest_framework.test.APIClient()
101-
response = client.get(self.promo_list_url)
102-
self.assertEqual(
103-
response.status_code,
104-
rest_framework.status.HTTP_401_UNAUTHORIZED,
97+
self.client.credentials(
98+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
10599
)
106100

107101
def test_get_all_promos(self):

promo_code/business/tests/promocodes/test_permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
class TestIsCompanyUserPermission(
12-
business.tests.promocodes.base.BasePromoCreateTestCase,
12+
business.tests.promocodes.base.BasePromoTestCase,
1313
):
1414
def setUp(self):
1515
self.factory = rest_framework.test.APIRequestFactory()

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66

77
class TestPromoCodeCreation(
8-
business.tests.promocodes.base.BasePromoCreateTestCase,
8+
business.tests.promocodes.base.BasePromoTestCase,
99
):
1010

1111
def setUp(self):
1212
super().setUp()
13-
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.token)
13+
self.client.credentials(
14+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
15+
)
1416

1517
def test_create_promo_with_old_token(self):
1618
self.client.credentials()

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
import parameterized
22
import rest_framework.status
3+
import rest_framework.test
34

45
import business.tests.promocodes.base
56

67

78
class TestPromoCodeList(
8-
business.tests.promocodes.base.BasePromoCreateTestCase,
9+
business.tests.promocodes.base.BasePromoTestCase,
910
):
10-
1111
def setUp(self):
1212
super().setUp()
13-
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.token)
13+
self.client.credentials(
14+
HTTP_AUTHORIZATION='Bearer ' + self.company1_token,
15+
)
16+
17+
def test_get_promos_without_token(self):
18+
self.client.credentials()
19+
client = rest_framework.test.APIClient()
20+
response = client.get(self.promo_list_url)
21+
self.assertEqual(
22+
response.status_code,
23+
rest_framework.status.HTTP_401_UNAUTHORIZED,
24+
)
1425

1526
@parameterized.parameterized.expand(
1627
[

0 commit comments

Comments
 (0)