Commit 3577b7e4 by Ivan Ivic Committed by GitHub

Merge pull request #1008 from edx/iivic/SOL-2111

[SOL-2111] Add Backend Validation for Coupon Creation - Benefit Type
parents 5866f2f3 9b8cf0ff
...@@ -273,6 +273,13 @@ class CouponViewSetFunctionalTest(CouponMixin, CourseCatalogTestMixin, CourseCat ...@@ -273,6 +273,13 @@ class CouponViewSetFunctionalTest(CouponMixin, CourseCatalogTestMixin, CourseCat
response_data = self.client.post(COUPONS_LINK, json.dumps(self.data), 'application/json') response_data = self.client.post(COUPONS_LINK, json.dumps(self.data), 'application/json')
self.assertEqual(response_data.status_code, expected_status_code) self.assertEqual(response_data.status_code, expected_status_code)
@ddt.data('', 'Incorrect benefit type')
def test_create_coupon_product_invalid_benefit_type(self, benefit_type):
"""Test creating coupon when provided benefit type is invalid."""
self.data.update({'benefit_type': benefit_type})
response_data = self.client.post(COUPONS_LINK, json.dumps(self.data), 'application/json')
self.assertEqual(response_data.status_code, status.HTTP_400_BAD_REQUEST)
def test_response(self): def test_response(self):
"""Test the response data given after the order was created.""" """Test the response data given after the order was created."""
self.assertEqual(self.response.status_code, status.HTTP_200_OK) self.assertEqual(self.response.status_code, status.HTTP_200_OK)
......
...@@ -6,10 +6,28 @@ from django.conf import settings ...@@ -6,10 +6,28 @@ from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from oscar.apps.offer.abstract_models import AbstractConditionalOffer, AbstractRange from django.utils.translation import ugettext_lazy as _
from oscar.apps.offer.abstract_models import AbstractBenefit, AbstractConditionalOffer, AbstractRange
from threadlocals.threadlocals import get_current_request from threadlocals.threadlocals import get_current_request
VALID_BENEFIT_TYPES = [AbstractBenefit.PERCENTAGE, AbstractBenefit.FIXED]
class Benefit(AbstractBenefit):
def save(self, *args, **kwargs):
self.clean()
super(Benefit, self).save(*args, **kwargs) # pylint: disable=bad-super-call
def clean(self):
self.clean_type()
super(Benefit, self).clean() # pylint: disable=bad-super-call
def clean_type(self):
if self.type not in VALID_BENEFIT_TYPES:
raise ValidationError(_('Unrecognised benefit type {type}'.format(type=self.type)))
class ConditionalOffer(AbstractConditionalOffer): class ConditionalOffer(AbstractConditionalOffer):
email_domains = models.CharField(max_length=255, blank=True, null=True) email_domains = models.CharField(max_length=255, blank=True, null=True)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment