Merge pull request #2671 from edx/waheed/lms1479-front-end-validation-for-username-email

Added validation for username and email max length.
This commit is contained in:
Waheed Ahmed
2014-02-26 19:42:04 +05:00
4 changed files with 73 additions and 10 deletions

View File

@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
import json
from django.test import TestCase
from django.core.urlresolvers import reverse
class TestLongUsernameEmail(TestCase):
def setUp(self):
self.url = reverse('create_account')
self.url_params = {
'username': 'username',
'email': 'foo_bar' + '@bar.com',
'name': 'foo bar',
'password': '123',
'terms_of_service': 'true',
'honor_code': 'true',
}
def test_long_username(self):
"""
Test username cannot be more than 30 characters long.
"""
self.url_params['username'] = 'username' * 4
response = self.client.post(self.url, self.url_params)
# Status code should be 400.
self.assertEqual(response.status_code, 400)
obj = json.loads(response.content)
self.assertEqual(
obj['value'],
"Username cannot be more than 30 characters long",
)
def test_long_email(self):
"""
Test email cannot be more than 75 characters long.
"""
self.url_params['email'] = '{0}@bar.com'.format('foo_bar' * 15)
response = self.client.post(self.url, self.url_params)
# Status code should be 400.
self.assertEqual(response.status_code, 400)
obj = json.loads(response.content)
self.assertEqual(
obj['value'],
"Email cannot be more than 75 characters long",
)

View File

@@ -3,8 +3,6 @@
This test file will verify proper password policy enforcement, which is an option feature
"""
import json
import uuid
from django.test import TestCase
from django.core.urlresolvers import reverse
from mock import patch
@@ -19,9 +17,10 @@ class TestPasswordPolicy(TestCase):
def setUp(self):
super(TestPasswordPolicy, self).setUp()
self.url = reverse('create_account')
self.url_params = {
'username': 'foo_bar' + uuid.uuid4().hex,
'email': 'foo' + uuid.uuid4().hex + '@bar.com',
'username': 'username',
'email': 'foo_bar@bar.com',
'name': 'username',
'terms_of_service': 'true',
'honor_code': 'true',

View File

@@ -1080,6 +1080,19 @@ def create_account(request, post_override=None):
js['field'] = field_name
return JsonResponse(js, status=400)
max_length = 75
if field_name == 'username':
max_length = 30
if field_name in ('email', 'username') and len(post_vars[field_name]) > max_length:
error_str = {
'username': _('Username cannot be more than {0} characters long').format(max_length),
'email': _('Email cannot be more than {0} characters long').format(max_length)
}
js['value'] = error_str[field_name]
js['field'] = field_name
return JsonResponse(js, status=400)
try:
validate_email(post_vars['email'])
except ValidationError:

View File

@@ -3,8 +3,6 @@
Tests for extra registration variables
"""
import json
import uuid
from django.conf import settings
from django.test import TestCase
from django.core.urlresolvers import reverse
@@ -18,11 +16,11 @@ class TestExtraRegistrationVariables(TestCase):
def setUp(self):
super(TestExtraRegistrationVariables, self).setUp()
self.url = reverse('create_account')
username = 'foo_bar' + uuid.uuid4().hex
self.url_params = {
'username': username,
'name': username,
'email': 'foo' + uuid.uuid4().hex + '@bar.com',
'username': 'username',
'name': 'name',
'email': 'foo_bar@bar.com',
'password': 'password',
'terms_of_service': 'true',
'honor_code': 'true',