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
41 changes: 29 additions & 12 deletions promo_code/business/tests/promocodes/base.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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()
Expand Down
17 changes: 8 additions & 9 deletions promo_code/business/tests/promocodes/operations/test_create.py
Original file line number Diff line number Diff line change
@@ -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!',
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
14 changes: 4 additions & 10 deletions promo_code/business/tests/promocodes/operations/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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'],
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion promo_code/business/tests/promocodes/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
[
Expand Down