Skip to content

Commit

Permalink
Changed backend email validation to match discussed HTML5 standards. …
Browse files Browse the repository at this point in the history
…Added new tests for email validation feature. close mozilla#3546
  • Loading branch information
cacois committed Aug 17, 2013
1 parent c580f5e commit 808dc4f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ httputils = require('./httputils.js'),
check = require('validator').check;

var hostnameRegex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;
// HTML5 email regex from: http://blog.gerv.net/2011/05/html5_email_address_regexp/
var emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?\^_`{|}~\-]+@[a-zA-Z0-9](?:[a-zA-Z0-9]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9]{0,253}[a-zA-Z0-9])?)*$/;

var types = {
email: function(x) {
check(x).isEmail();
check(x).is(emailRegex);
},
email_type: function(x) {
check(x).isIn([ 'primary', 'secondary' ]);
Expand Down
96 changes: 96 additions & 0 deletions tests/email-validation-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env node

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

require('./lib/test_env.js');

const
assert = require('assert'),
vows = require('vows'),
validate = require('../lib/validate.js'),
wsapi = require('./lib/wsapi.js'),
start_stop = require('./lib/start-stop.js');

var suite = vows.describe('email-validation-test');

start_stop.addStartupBatches(suite);

// this test verifies that a user who has only authenticated with
// an assertion from their primary, may not call restricted apis

var validEmail1 = 'test@test.com';
var validEmail2 = 'test-email@test.com';
var validEmail3 = '.test@test.com';
var validEmail4 = 'my.test@test.com';

var invalidEmail1 = 'test@-test.com';
var invalidEmail2 = 'test@';
var invalidEmail3 = 'test@.org';

suite.addBatch({
"Testing first valid email address": {
topic: wsapi.get('/wsapi/address_info', {
email: validEmail1
}),
"returns 200": function(err, r) {
assert.strictEqual(r.code, 200);
}
},
"Testing second valid email address": {
topic: wsapi.get('/wsapi/address_info', {
email: validEmail2
}),
"returns 200": function(err, r) {
assert.strictEqual(r.code, 200);
}
},
"Testing third valid email address": {
topic: wsapi.get('/wsapi/address_info', {
email: validEmail3
}),
"returns 200": function(err, r) {
assert.strictEqual(r.code, 200);
}
},
"Testing fourth valid email address": {
topic: wsapi.get('/wsapi/address_info', {
email: validEmail4
}),
"returns 200": function(err, r) {
assert.strictEqual(r.code, 200);
}
},
"Testing first invalid email address": {
topic: wsapi.get('/wsapi/address_info', {
email: invalidEmail1
}),
"returns 400": function(err, r) {
assert.strictEqual(r.code, 400);
}
},
"Testing second invalid email address": {
topic: wsapi.get('/wsapi/address_info', {
email: invalidEmail2
}),
"returns 400": function(err, r) {
assert.strictEqual(r.code, 400);
}
},
"Testing third invalid email address": {
topic: wsapi.get('/wsapi/address_info', {
email: invalidEmail3
}),
"returns 400": function(err, r) {
assert.strictEqual(r.code, 400);
}
}
});

// shut the server down and cleanup
start_stop.addShutdownBatches(suite);

// run or export the suite.
if (process.argv[1] === __filename) suite.run();
else suite.export(module);

0 comments on commit 808dc4f

Please sign in to comment.