diff --git a/promo_code/business/tests/promocodes/base.py b/promo_code/business/tests/promocodes/base.py index 37c74b9..e03a996 100644 --- a/promo_code/business/tests/promocodes/base.py +++ b/promo_code/business/tests/promocodes/base.py @@ -1,12 +1,11 @@ import django.urls import rest_framework -import rest_framework.status import rest_framework.test import business.models -class BasePromoCreateTestCase(rest_framework.test.APITestCase): +class BasePromoTestCase(rest_framework.test.APITestCase): @classmethod def setUpTestData(cls): super().setUpTestData() @@ -17,28 +16,46 @@ def setUpTestData(cls): ) cls.signup_url = django.urls.reverse('api-business:company-sign-up') cls.signin_url = django.urls.reverse('api-business:company-sign-in') - cls.valid_data = { + + cls.company1_data = { 'name': 'Digital Marketing Solutions Inc.', - 'email': 'testcompany@example.com', + 'email': 'company1@example.com', 'password': 'SecurePass123!', } - business.models.Company.objects.create_company( - **cls.valid_data, + business.models.Company.objects.create_company(**cls.company1_data) + cls.company1 = business.models.Company.objects.get( + email=cls.company1_data['email'], + ) + + cls.company2_data = { + 'name': 'Global Retail Hub LLC', + 'email': 'company2@example.com', + 'password': 'SecurePass456!', + } + business.models.Company.objects.create_company(**cls.company2_data) + cls.company2 = business.models.Company.objects.get( + email=cls.company2_data['email'], ) - cls.company = business.models.Company.objects.get( - email=cls.valid_data['email'], + response1 = cls.client.post( + cls.signin_url, + { + 'email': cls.company1_data['email'], + 'password': cls.company1_data['password'], + }, + format='json', ) + cls.company1_token = response1.data['access'] - response = cls.client.post( + response2 = cls.client.post( cls.signin_url, { - 'email': cls.valid_data['email'], - 'password': cls.valid_data['password'], + 'email': cls.company2_data['email'], + 'password': cls.company2_data['password'], }, format='json', ) - cls.token = response.data['access'] + cls.company2_token = response2.data['access'] def tearDown(self): business.models.Company.objects.all().delete() diff --git a/promo_code/business/tests/promocodes/operations/test_create.py b/promo_code/business/tests/promocodes/operations/test_create.py index a41dced..84461cc 100644 --- a/promo_code/business/tests/promocodes/operations/test_create.py +++ b/promo_code/business/tests/promocodes/operations/test_create.py @@ -1,11 +1,18 @@ import rest_framework.status +import rest_framework.test import business.tests.promocodes.base class TestSuccessfulPromoCreation( - business.tests.promocodes.base.BasePromoCreateTestCase, + business.tests.promocodes.base.BasePromoTestCase, ): + def setUp(self): + self.client = rest_framework.test.APIClient() + self.client.credentials( + HTTP_AUTHORIZATION='Bearer ' + self.company1_token, + ) + def test_successful_promo_creation_1(self): payload = { 'description': 'Increased cashback 10% for new bank clients!', @@ -20,7 +27,6 @@ def test_successful_promo_creation_1(self): self.promo_create_url, payload, format='json', - HTTP_AUTHORIZATION='Bearer ' + self.token, ) self.assertEqual( response.status_code, @@ -41,7 +47,6 @@ def test_successful_promo_creation_2(self): self.promo_create_url, payload, format='json', - HTTP_AUTHORIZATION='Bearer ' + self.token, ) self.assertEqual( response.status_code, @@ -62,7 +67,6 @@ def test_successful_promo_creation_3(self): self.promo_create_url, payload, format='json', - HTTP_AUTHORIZATION='Bearer ' + self.token, ) self.assertEqual( response.status_code, @@ -82,7 +86,6 @@ def test_successful_promo_creation_4(self): self.promo_create_url, payload, format='json', - HTTP_AUTHORIZATION='Bearer ' + self.token, ) self.assertEqual( response.status_code, @@ -103,7 +106,6 @@ def test_successful_promo_creation_5(self): self.promo_create_url, payload, format='json', - HTTP_AUTHORIZATION='Bearer ' + self.token, ) self.assertEqual( response.status_code, @@ -123,7 +125,6 @@ def test_successful_promo_creation_6_country_lower(self): self.promo_create_url, payload, format='json', - HTTP_AUTHORIZATION='Bearer ' + self.token, ) self.assertEqual( response.status_code, @@ -143,7 +144,6 @@ def test_successful_promo_creation_6_country_upper(self): self.promo_create_url, payload, format='json', - HTTP_AUTHORIZATION='Bearer ' + self.token, ) self.assertEqual( response.status_code, @@ -163,7 +163,6 @@ def test_successful_promo_creation_7(self): self.promo_create_url, payload, format='json', - HTTP_AUTHORIZATION='Bearer ' + self.token, ) self.assertEqual( response.status_code, diff --git a/promo_code/business/tests/promocodes/operations/test_list.py b/promo_code/business/tests/promocodes/operations/test_list.py index 594ed8c..cf919c2 100644 --- a/promo_code/business/tests/promocodes/operations/test_list.py +++ b/promo_code/business/tests/promocodes/operations/test_list.py @@ -7,7 +7,7 @@ class TestPromoEndpoint( - business.tests.promocodes.base.BasePromoCreateTestCase, + business.tests.promocodes.base.BasePromoTestCase, ): def _create_additional_promo(self): self.__class__.promo5_data = { @@ -71,7 +71,7 @@ def setUpTestData(cls): for promo_data in [cls.promo1_data, cls.promo2_data, cls.promo3_data]: promo = business.models.Promo.objects.create( - company=cls.company, + company=cls.company1, description=promo_data['description'], image_url=promo_data.get('image_url'), target=promo_data['target'], @@ -94,14 +94,8 @@ def setUpTestData(cls): def setUp(self): self.client = rest_framework.test.APIClient() - self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.token) - - def test_get_promos_without_token(self): - client = rest_framework.test.APIClient() - response = client.get(self.promo_list_url) - self.assertEqual( - response.status_code, - rest_framework.status.HTTP_401_UNAUTHORIZED, + self.client.credentials( + HTTP_AUTHORIZATION='Bearer ' + self.company1_token, ) def test_get_all_promos(self): diff --git a/promo_code/business/tests/promocodes/test_permissions.py b/promo_code/business/tests/promocodes/test_permissions.py index e525d01..ed44440 100644 --- a/promo_code/business/tests/promocodes/test_permissions.py +++ b/promo_code/business/tests/promocodes/test_permissions.py @@ -9,7 +9,7 @@ class TestIsCompanyUserPermission( - business.tests.promocodes.base.BasePromoCreateTestCase, + business.tests.promocodes.base.BasePromoTestCase, ): def setUp(self): self.factory = rest_framework.test.APIRequestFactory() diff --git a/promo_code/business/tests/promocodes/validations/test_create_validation.py b/promo_code/business/tests/promocodes/validations/test_create_validation.py index 1156704..dcdd0ce 100644 --- a/promo_code/business/tests/promocodes/validations/test_create_validation.py +++ b/promo_code/business/tests/promocodes/validations/test_create_validation.py @@ -5,12 +5,14 @@ class TestPromoCodeCreation( - business.tests.promocodes.base.BasePromoCreateTestCase, + business.tests.promocodes.base.BasePromoTestCase, ): def setUp(self): super().setUp() - self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.token) + self.client.credentials( + HTTP_AUTHORIZATION='Bearer ' + self.company1_token, + ) def test_create_promo_with_old_token(self): self.client.credentials() diff --git a/promo_code/business/tests/promocodes/validations/test_list_validation.py b/promo_code/business/tests/promocodes/validations/test_list_validation.py index b2a5a97..b0dc43b 100644 --- a/promo_code/business/tests/promocodes/validations/test_list_validation.py +++ b/promo_code/business/tests/promocodes/validations/test_list_validation.py @@ -1,16 +1,27 @@ import parameterized import rest_framework.status +import rest_framework.test import business.tests.promocodes.base class TestPromoCodeList( - business.tests.promocodes.base.BasePromoCreateTestCase, + business.tests.promocodes.base.BasePromoTestCase, ): - def setUp(self): super().setUp() - self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.token) + self.client.credentials( + HTTP_AUTHORIZATION='Bearer ' + self.company1_token, + ) + + def test_get_promos_without_token(self): + self.client.credentials() + client = rest_framework.test.APIClient() + response = client.get(self.promo_list_url) + self.assertEqual( + response.status_code, + rest_framework.status.HTTP_401_UNAUTHORIZED, + ) @parameterized.parameterized.expand( [