Skip to content

Commit

Permalink
Add email_validator_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
nospame committed Feb 18, 2025
1 parent c39df56 commit 453f37a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import hqMocha from "mocha/js/main";

import "hqwebapp/spec/assert_properties_spec";
import "hqwebapp/spec/email_validator_spec";
import "hqwebapp/spec/bootstrap3/inactivity_spec";
import "hqwebapp/spec/urllib_spec";
import "hqwebapp/spec/bootstrap3/widgets_spec";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import hqMocha from "mocha/js/main";

import "hqwebapp/spec/assert_properties_spec";
import "hqwebapp/spec/email_validator_spec";
import "hqwebapp/spec/bootstrap5/inactivity_spec";
import "hqwebapp/spec/urllib_spec";
import "hqwebapp/spec/bootstrap5/widgets_spec";
Expand Down
83 changes: 83 additions & 0 deletions corehq/apps/hqwebapp/static/hqwebapp/spec/email_validator_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* eslint-env mocha */
hqDefine("hqwebapp/spec/email_validator_spec", [
'hqwebapp/js/constants',
], function (
constants,
) {
describe('email_validator', function () {
const re = constants.EMAIL_VALIDATION_REGEX;
const testEmail = email => re.test(email);

it('should allow simple email addresses', function () {
assert.ok(testEmail('[email protected]'));
});

it('should allow capital letters in the local part', function () {
assert.ok(testEmail('[email protected]'));
assert.ok(testEmail('[email protected]'));
});

it('should allow capital letters in the domain part', function () {
assert.ok(testEmail('[email protected]'));
assert.ok(testEmail('[email protected]'));
});

it('should allow digits in the local part', function () {
assert.ok(testEmail('[email protected]'));
});

it('should allow specified special characters in the local part', function () {
assert.ok(testEmail('.\x01!#$%&"\'*+/=?^_`{|}[email protected]'));
});

it('should allow subdomains', function () {
assert.ok(testEmail('[email protected]'));
});

it('should allow hyphens in domain names', function () {
assert.ok(testEmail('[email protected]'));
});

it('should allow IP addresses in brackets as the domain', function () {
assert.ok(testEmail('whosaidthiswasok@[127.0.0.1]'));
});

it('should reject missing @ symbol', function () {
assert.notOk(testEmail('notanemail'));
});

it('should reject missing local part', function () {
assert.notOk(testEmail('@example.com'));
});

it('should reject missing top-level domain', function () {
assert.notOk(testEmail('nothing@toseehere'));
assert.notOk(testEmail('noteven@toseehere.'));
});

it('should reject invalid characters in the domain part', function () {
const emails = [
// allowed in local part but not domain
'me@ex\x01ample.com',
'me@ex!ample.com',
'me@ex#ample.com',
'me@ex$ample.com',
'me@ex%ample.com',
'me@ex&ample.com',
'me@ex"ample.com',
'me@ex\'ample.com',
'me@ex*ample.com',
'me@ex+ample.com',
'me@ex/ample.com',
'me@ex=ample.com',
'me@ex?ample.com',
'me@ex^ample.com',
'me@ex_ample.com',
'me@ex`ample.com',
'me@ex{ample}.com',
'me@ex|ample.com',
];
emails.forEach(email => assert.notOk(testEmail(email)));
});
});
});

0 comments on commit 453f37a

Please sign in to comment.