Skip to content

Commit fa715b7

Browse files
Merge pull request #916 from openedx/mkeating/ENT-10753
feat: Add more checkout field constraints
2 parents 1d1da08 + 5a62f65 commit fa715b7

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

enterprise_access/apps/api/serializers/customer_billing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ class CustomerBillingCreateCheckoutSessionValidationFailedResponseSerializer(ser
125125
required=False,
126126
help_text='Validation results for stripe_price_id if validation failed. Absent otherwise.',
127127
)
128+
company_name = FieldValidationSerializer(
129+
required=False,
130+
help_text='Validation results for company_name if validation failed. Absent otherwise.',
131+
)
128132
errors = UnprocessableEntityErrorSerializer(
129133
required=False,
130134
many=True,

enterprise_access/apps/bffs/checkout/handlers.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,32 @@ def _get_field_constraints(self) -> Dict:
207207
min_val, max_val = product_config['quantity_range']
208208
quantity_constraints = {'min': min_val, 'max': max_val}
209209
break
210-
211210
return {
212211
'quantity': quantity_constraints,
213212
'enterprise_slug': {
214-
'min_length': 3,
215-
'max_length': 30,
213+
'min_length': 1,
214+
'max_length': 255,
216215
'pattern': '^[a-z0-9-]+$'
217216
},
218-
'embargoed_countries': get_embargoed_countries()
217+
'embargoed_countries': get_embargoed_countries(),
218+
'full_name': {
219+
'min_length': 1,
220+
'max_length': 150,
221+
},
222+
'admin_email': {
223+
'min_length': 6,
224+
'max_length': 253,
225+
'pattern': '^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$', # enforce the format of [email protected] without spaces
226+
},
227+
'country': {
228+
'min_length': 2,
229+
'max_length': 2,
230+
'pattern': '^[A-Z]{2}$'
231+
},
232+
'company_name': {
233+
'min_length': 1,
234+
'max_length': 255,
235+
}
219236
}
220237

221238

enterprise_access/apps/bffs/tests/test_checkout_handlers.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Tests for Checkout BFF handlers.
33
"""
44
import random
5+
import re
56
from datetime import datetime
67
from decimal import Decimal
78
from unittest import mock
@@ -224,6 +225,43 @@ def test_get_field_constraints_includes_embargoed_countries(self):
224225
self.assertIsInstance(constraints['embargoed_countries'], list)
225226
self.assertEqual(constraints['embargoed_countries'], ['RU', 'IR', 'KP', 'SY', 'CU'])
226227

228+
@ddt.data(
229+
{
230+
'constraint_name': 'enterprise_slug',
231+
'should_match': ['valid-slug', '123', '0kay-slug'],
232+
'should_not_match': ['Capitalization-fail', 'b&d-symbol', '/-and-burn']
233+
},
234+
{
235+
'constraint_name': 'admin_email',
236+
'should_match': ['[email protected]', '[email protected]'],
237+
'should_not_match': ['notanemail', 'too@[email protected]', 'forbidden [email protected]']
238+
},
239+
{
240+
'constraint_name': 'country',
241+
'should_match': ['US', 'CN'],
242+
'should_not_match': ['us', 'CANADA']
243+
},
244+
)
245+
@ddt.unpack
246+
def test_get_field_constraints_regexes(self, constraint_name, should_match, should_not_match):
247+
"""
248+
Test that _get_field_constraints includes embargoed countries list.
249+
"""
250+
context = self._create_context()
251+
handler = CheckoutContextHandler(context)
252+
253+
constraints = handler._get_field_constraints()
254+
255+
self.assertIn(constraint_name, constraints)
256+
constraint_definition = constraints[constraint_name]
257+
self.assertIn('pattern', constraint_definition)
258+
pattern = constraint_definition['pattern']
259+
260+
for str in should_match:
261+
self.assertTrue(re.match(pattern, str))
262+
for str in should_not_match:
263+
self.assertFalse(re.match(pattern, str))
264+
227265
@mock.patch('enterprise_access.apps.bffs.checkout.handlers.get_ssp_product_pricing')
228266
def test_handler_adds_error_on_pricing_failure(self, mock_get_pricing):
229267
"""

requirements/common_constraints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ elasticsearch<7.14.0
2626
# The constraint can be removed once a release (pip-tools > 7.5.1) is available with support for pip 25.3
2727
# Issue to track this dependency and unpin later on: https://github.com/openedx/edx-lint/issues/503
2828
pip<25.3
29+
30+
pip-tools>=7.4.0

0 commit comments

Comments
 (0)