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
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
max-line-length = 79
application_import_names = promo_code, user, core
application_import_names = promo_code, user, core, business
import-order-style = google
exclude = */migrations/, venv/, verdict.py, .venv/, env/, venv, .git, __pycache__
max-complexity = 10
Expand Down
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[settings]
profile = black
skip = migrations, venv/, venv
known_first_party = promo_code, user, core
known_first_party = promo_code, user, core, business
default_section = THIRDPARTY
force_sort_within_sections = true
line_length = 79
3 changes: 2 additions & 1 deletion promo_code/business/permissions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import business.models
import rest_framework.permissions

import business.models


class IsCompanyUser(rest_framework.permissions.BasePermission):
def has_permission(self, request, view):
Expand Down
24 changes: 17 additions & 7 deletions promo_code/business/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import business.models as business_models
import business.validators
import django.contrib.auth.password_validation
import django.core.exceptions
import django.core.validators
Expand All @@ -12,6 +10,9 @@
import rest_framework_simplejwt.tokens
import rest_framework_simplejwt.views

import business.models as business_models
import business.validators


class CompanySignUpSerializer(rest_framework.serializers.ModelSerializer):
password = rest_framework.serializers.CharField(
Expand Down Expand Up @@ -147,6 +148,8 @@ class TargetSerializer(rest_framework.serializers.Serializer):
allow_null=True,
)
country = rest_framework.serializers.CharField(
max_length=2,
min_length=2,
required=False,
allow_null=True,
allow_blank=True,
Expand All @@ -173,7 +176,6 @@ def validate(self, data):
{'age_until': 'Must be greater than or equal to age_from.'},
)

# change validation
country = data.get('country')
if country:
country = country.strip().upper()
Expand All @@ -189,6 +191,18 @@ def validate(self, data):


class PromoCreateSerializer(rest_framework.serializers.ModelSerializer):
description = rest_framework.serializers.CharField(
min_length=10,
max_length=300,
required=True,
)
image_url = rest_framework.serializers.CharField(
required=False,
max_length=350,
validators=[
django.core.validators.URLValidator(schemes=['http', 'https']),
],
)
target = TargetSerializer(required=True)
promo_common = rest_framework.serializers.CharField(
min_length=5,
Expand Down Expand Up @@ -220,10 +234,6 @@ class Meta:
'promo_common',
'promo_unique',
)
extra_kwargs = {
'description': {'min_length': 10, 'max_length': 300},
'image_url': {'max_length': 350},
}

def validate(self, data):
mode = data.get('mode')
Expand Down
3 changes: 2 additions & 1 deletion promo_code/business/tests/auth/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import business.models
import django.urls
import rest_framework
import rest_framework.status
import rest_framework.test

import business.models


class BaseBusinessAuthTestCase(rest_framework.test.APITestCase):
@classmethod
Expand Down
5 changes: 3 additions & 2 deletions promo_code/business/tests/auth/test_authentication.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import business.models
import business.tests.auth.base
import rest_framework.status
import rest_framework.test

import business.models
import business.tests.auth.base


class AuthenticationTests(business.tests.auth.base.BaseBusinessAuthTestCase):
def test_signin_success(self):
Expand Down
5 changes: 3 additions & 2 deletions promo_code/business/tests/auth/test_registration.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import business.models
import business.tests.auth.base
import rest_framework.status
import rest_framework.test

import business.models
import business.tests.auth.base


class TestCompanyRegistration(
business.tests.auth.base.BaseBusinessAuthTestCase,
Expand Down
4 changes: 2 additions & 2 deletions promo_code/business/tests/auth/test_tokens.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import business.models
import business.tests.auth.base
import rest_framework.status
import rest_framework.test
import rest_framework_simplejwt.tokens

import business.models
import business.tests.auth.base
import user.models


Expand Down
5 changes: 3 additions & 2 deletions promo_code/business/tests/auth/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import business.models
import business.tests.auth.base
import parameterized
import rest_framework.status
import rest_framework.test

import business.models
import business.tests.auth.base


class InvalidCompanyRegistrationTestCase(
business.tests.auth.base.BaseBusinessAuthTestCase,
Expand Down
Empty file.
41 changes: 41 additions & 0 deletions promo_code/business/tests/promocodes/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import django.urls
import rest_framework
import rest_framework.status
import rest_framework.test

import business.models


class BasePromoCreateTestCase(rest_framework.test.APITestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()
cls.client = rest_framework.test.APIClient()
cls.promo_create_url = django.urls.reverse('api-business:promo-create')
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 = {
'name': 'Digital Marketing Solutions Inc.',
'email': '[email protected]',
'password': 'SecurePass123!',
}
business.models.Company.objects.create_company(
name=cls.valid_data['name'],
email=cls.valid_data['email'],
password=cls.valid_data['password'],
)

response = cls.client.post(
cls.signin_url,
{
'email': cls.valid_data['email'],
'password': cls.valid_data['password'],
},
format='json',
)
cls.token = response.data['access']

def tearDown(self):
business.models.Company.objects.all().delete()
business.models.Promo.objects.all().delete()
business.models.PromoCode.objects.all().delete()
Empty file.
171 changes: 171 additions & 0 deletions promo_code/business/tests/promocodes/operations/test_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import rest_framework.status

import business.tests.promocodes.base


class TestSuccessfulPromoCreation(
business.tests.promocodes.base.BasePromoCreateTestCase,
):
def test_successful_promo_creation_1(self):
payload = {
'description': 'Increased cashback 10% for new bank clients!',
'image_url': 'https://cdn2.thecatapi.com/images/3lo.jpg',
'target': {},
'max_count': 10,
'active_from': '2025-01-10',
'mode': 'COMMON',
'promo_common': 'sale-10',
}
response = self.client.post(
self.promo_create_url,
payload,
format='json',
HTTP_AUTHORIZATION='Bearer ' + self.token,
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_201_CREATED,
)

def test_successful_promo_creation_2(self):
payload = {
'description': 'Increased cashback 40% for new bank clients!',
'image_url': 'https://cdn2.thecatapi.com/images/3lo.jpg',
'target': {'age_from': 15, 'country': 'fr'},
'max_count': 100,
'active_from': '2028-12-20',
'mode': 'COMMON',
'promo_common': 'sale-40',
}
response = self.client.post(
self.promo_create_url,
payload,
format='json',
HTTP_AUTHORIZATION='Bearer ' + self.token,
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_201_CREATED,
)

def test_successful_promo_creation_3(self):
payload = {
'description': 'Gift sleeping mask with car loan application',
'target': {'age_from': 28, 'age_until': 50, 'country': 'us'},
'max_count': 1,
'active_from': '2025-01-01',
'active_until': '2028-12-30',
'mode': 'UNIQUE',
'promo_unique': ['uniq1', 'uniq2', 'uniq3'],
}
response = self.client.post(
self.promo_create_url,
payload,
format='json',
HTTP_AUTHORIZATION='Bearer ' + self.token,
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_201_CREATED,
)

def test_successful_promo_creation_4(self):
payload = {
'description': 'We gift a globe when ordering for 30000!',
'target': {'age_from': 28, 'age_until': 50, 'country': 'us'},
'max_count': 1,
'active_until': '2025-01-10',
'mode': 'UNIQUE',
'promo_unique': ['only_youuuu', 'not_only_you'],
}
response = self.client.post(
self.promo_create_url,
payload,
format='json',
HTTP_AUTHORIZATION='Bearer ' + self.token,
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_201_CREATED,
)

def test_successful_promo_creation_5(self):
payload = {
'description': 'Increased cashback 10% for new bank clients!',
'image_url': 'http://cdn2.thecatapi.com/',
'target': {},
'max_count': 10,
'active_from': '1950-01-01',
'mode': 'COMMON',
'promo_common': 'sale-10',
}
response = self.client.post(
self.promo_create_url,
payload,
format='json',
HTTP_AUTHORIZATION='Bearer ' + self.token,
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_201_CREATED,
)

def test_successful_promo_creation_6_country_lower(self):
payload = {
'description': 'Increased cashback 10% for new bank clients!',
'image_url': 'http://cdn2.thecatapi.com/',
'target': {'country': 'Us'},
'max_count': 10,
'mode': 'COMMON',
'promo_common': 'sale-10',
}
response = self.client.post(
self.promo_create_url,
payload,
format='json',
HTTP_AUTHORIZATION='Bearer ' + self.token,
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_201_CREATED,
)

def test_successful_promo_creation_6_country_upper(self):
payload = {
'description': 'Increased cashback 10% for new bank clients!',
'image_url': 'http://cdn2.thecatapi.com/',
'target': {'country': 'US'},
'max_count': 10,
'mode': 'COMMON',
'promo_common': 'sale-10',
}
response = self.client.post(
self.promo_create_url,
payload,
format='json',
HTTP_AUTHORIZATION='Bearer ' + self.token,
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_201_CREATED,
)

def test_successful_promo_creation_7(self):
payload = {
'description': 'Increased cashback 10% for new bank clients!',
'image_url': 'http://cdn2.thecatapi.com/',
'target': {'age_from': 100, 'age_until': 100},
'max_count': 10,
'mode': 'COMMON',
'promo_common': 'sale-10',
}
response = self.client.post(
self.promo_create_url,
payload,
format='json',
HTTP_AUTHORIZATION='Bearer ' + self.token,
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_201_CREATED,
)
Loading