Commit e4230fbc by attiyaishaque Committed by Attiya Ishaque

Don't allow Publisher to add negative prices for course run.

parent b03a1a86
......@@ -440,6 +440,9 @@ class SeatForm(BaseForm):
credit_price = self.cleaned_data.get('credit_price')
seat_type = self.cleaned_data.get('type')
if seat_type != Seat.AUDIT and price is not None and price < 0.01:
self.add_error('price', _('Price must be greater than or equal to 0.01'))
if seat_type in [Seat.PROFESSIONAL, Seat.VERIFIED, Seat.CREDIT] and not price:
self.add_error('price', _('Only audit seat can be without price.'))
......@@ -502,10 +505,10 @@ class CourseEntitlementForm(BaseForm):
mode = cleaned_data.get('mode')
price = cleaned_data.get('price')
has_mode = bool(mode)
has_price = price is not None and price > 0
if has_mode != has_price:
raise ValidationError({'price': ''}) # mimics required field behavior (red border only, no error string)
if mode in [CourseEntitlement.VERIFIED, CourseEntitlement.PROFESSIONAL] and price is not None and price < 0.01:
self.add_error('price', _('Price must be greater than or equal to 0.01'))
if mode in [CourseEntitlement.VERIFIED, CourseEntitlement.PROFESSIONAL] and not price:
self.add_error('price', _('Only audit seat can be without price.'))
return cleaned_data
......
......@@ -146,6 +146,13 @@
{{ entitlement_form.price }}
</div>
</div>
{% if entitlement_form.price.errors %}
<div class="field-message has-error js-seat-form-errors">
<span class="field-message-content">
{{ entitlement_form.price.errors|escape }}
</span>
</div>
{% endif %}
</div>
</div>
{% endif %}
......
......@@ -349,27 +349,24 @@ class PublisherCustomCourseFormTests(TestCase):
@ddt.ddt
class PublisherCourseEntitlementFormTests(TestCase):
without_price_error = 'Only audit seat can be without price.'
negative_price_error = 'Price must be greater than or equal to 0.01'
@ddt.data(
(None, 50),
(CourseEntitlement.VERIFIED, None),
(CourseEntitlement.PROFESSIONAL, None),
(CourseEntitlement.PROFESSIONAL, 0),
(CourseEntitlement.PROFESSIONAL, -1),
(CourseEntitlement.VERIFIED, None, without_price_error),
(CourseEntitlement.PROFESSIONAL, None, without_price_error),
(CourseEntitlement.VERIFIED, -0.05, negative_price_error),
(CourseEntitlement.PROFESSIONAL, -0.05, negative_price_error),
)
@ddt.unpack
def test_invalid_price(self, mode, price):
def test_invalid_price(self, mode, price, error_message):
"""
Verify that clean raises an error if the price is invalid for the course type
Verify that form raises an error if the price is None or in -ive format
"""
entitlement_form = CourseEntitlementForm()
entitlement_form.cleaned_data = {}
if mode is not None:
entitlement_form.cleaned_data['mode'] = mode
if price is not None:
entitlement_form.cleaned_data['price'] = price
with self.assertRaises(ValidationError):
entitlement_form.clean()
form_data = {'mode': mode, 'price': price}
entitlement_form = CourseEntitlementForm(data=form_data)
self.assertFalse(entitlement_form.is_valid())
self.assertEqual(entitlement_form.errors, {'price': [error_message]})
@ddt.data(
(None, None),
......@@ -473,3 +470,17 @@ class CourseSearchFormTests(TestCase):
# But it *will* work if we add a role for this user
CourseUserRoleFactory(course=self.course, user=self.user, role=PublisherUserRole.MarketingReviewer)
self.assertTrue(self._check_form())
class SeatFormTests(TestCase):
"""
Tests for Seat Form
"""
def test_negative_price(self):
"""
Verify that form raises an error when price is in -ive format
"""
form_data = {'type': Seat.VERIFIED, 'price': -0.05}
seat_form = SeatForm(data=form_data)
self.assertFalse(seat_form.is_valid())
self.assertEqual(seat_form.errors, {'price': ['Price must be greater than or equal to 0.01']})
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-02-12 17:50+0000\n"
"POT-Creation-Date: 2018-02-21 06:07+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -828,6 +828,10 @@ msgid "Enrollment Track"
msgstr ""
#: apps/publisher/forms.py
msgid "Price must be greater than or equal to 0.01"
msgstr ""
#: apps/publisher/forms.py
msgid "Only audit seat can be without price."
msgstr ""
......@@ -3326,7 +3330,7 @@ msgstr ""
#: apps/publisher/views.py
msgid ""
"The certificate configuration for this Course is incorrect. Please fix it, "
"The certificate configuration for this course is incorrect. Please fix it, "
"then try again."
msgstr ""
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-02-12 17:50+0000\n"
"POT-Creation-Date: 2018-02-21 06:08+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-02-12 17:50+0000\n"
"POT-Creation-Date: 2018-02-21 06:07+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -995,6 +995,12 @@ msgid "Enrollment Track"
msgstr "Énröllmént Träçk Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αм#"
#: apps/publisher/forms.py
msgid "Price must be greater than or equal to 0.01"
msgstr ""
"Prïçé müst ßé gréätér thän ör éqüäl tö 0.01 Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, "
"¢σηѕє¢тєтυя #"
#: apps/publisher/forms.py
msgid "Only audit seat can be without price."
msgstr ""
"Önlý äüdït séät çän ßé wïthöüt prïçé. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, "
......@@ -4037,10 +4043,10 @@ msgstr ""
#: apps/publisher/views.py
msgid ""
"The certificate configuration for this Course is incorrect. Please fix it, "
"The certificate configuration for this course is incorrect. Please fix it, "
"then try again."
msgstr ""
"Thé çértïfïçäté çönfïgürätïön för thïs Çöürsé ïs ïnçörréçt. Pléäsé fïx ït, "
"Thé çértïfïçäté çönfïgürätïön för thïs çöürsé ïs ïnçörréçt. Pléäsé fïx ït, "
"thén trý ägäïn. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕ#"
#: apps/publisher/views.py
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-02-12 17:50+0000\n"
"POT-Creation-Date: 2018-02-21 06:08+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......
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