Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sefinek committed Dec 24, 2023
1 parent 0e06f95 commit 8b80f23
Show file tree
Hide file tree
Showing 2 changed files with 1,693 additions and 3,596 deletions.
223 changes: 101 additions & 122 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,127 +1,106 @@
const emailValidator = require('../index.js');

describe('Email Validation Tests', () => {
test('Valid email', () => {
expect(emailValidator.test('contact@sefinek.net')).toBe(true);
});

test('Email without domain', () => {
expect(emailValidator.test('example@')).toBe(false);
});

test('Email without user name', () => {
expect(emailValidator.test('@example.com')).toBe(false);
});

test('Email without @', () => {
expect(emailValidator.test('example.com')).toBe(false);
});

test('Email with multiple @', () => {
expect(emailValidator.test('exa@mple@example.com')).toBe(false);
});

test('Email with spaces', () => {
expect(emailValidator.test('ex ample@example.com')).toBe(false);
});

test('Email with leading spaces', () => {
expect(emailValidator.test(' example@example.com')).toBe(false);
});

test('Email with trailing spaces', () => {
expect(emailValidator.test('example@example.com ')).toBe(false);
});

test('Email with invalid characters', () => {
expect(emailValidator.test('exa*mple@example.com')).toBe(false);
});

test('User name part too long', () => {
expect(emailValidator.test(`${'a'.repeat(65)}@example.com`)).toBe(false);
});

test('Domain part too long', () => {
expect(emailValidator.test(`example@${'a'.repeat(256)}`)).toBe(false);
});

test('Subdomain part too long', () => {
expect(emailValidator.test(`example@${'a'.repeat(64)}.com`)).toBe(false);
});

test('Email without top-level domain', () => {
expect(emailValidator.test('example@example')).toBe(false);
});

test('Email with numeric domain', () => {
expect(emailValidator.test('example@1234.com')).toBe(true);
});

test('Email with hyphen in domain', () => {
expect(emailValidator.test('example@exa-mple.com')).toBe(true);
});

test('Email with underscore in user name', () => {
expect(emailValidator.test('ex_ample@example.com')).toBe(true);
});

test('Email with plus in user name', () => {
expect(emailValidator.test('ex+ample@example.com')).toBe(true);
});

test('Email with dot in user name', () => {
expect(emailValidator.test('ex.ample@example.com')).toBe(true);
});

test('Email with consecutive dots', () => {
expect(emailValidator.test('ex..ample@example.com')).toBe(false);
});

test('Email with dot at the start of user name', () => {
expect(emailValidator.test('.example@example.com')).toBe(false);
});

test('Email with dot at the end of user name', () => {
expect(emailValidator.test('example.@example.com')).toBe(false);
});

test('Email with dot at the start of domain', () => {
expect(emailValidator.test('example@.example.com')).toBe(false);
});

test('Email with dot at the end of domain', () => {
expect(emailValidator.test('example@example.com.')).toBe(false);
});

test('Email with international characters', () => {
expect(emailValidator.test('üñîçøðé@example.com')).toBe(true);
});

test('Domain with international characters', () => {
expect(emailValidator.test('example@üñîçøðé.com')).toBe(true);
});

test('Email with long domain', () => {
expect(emailValidator.test(`example@${'a'.repeat(63)}.com`)).toBe(true);
});

test('Email with the subdomain', () => {
expect(emailValidator.test('example@sub.example.com')).toBe(true);
});

test('Email with single character user name', () => {
expect(emailValidator.test('e@example.com')).toBe(true);
});

test('Email with single character domain', () => {
expect(emailValidator.test('example@a.com')).toBe(true);
});

test('Email with numeric user name', () => {
expect(emailValidator.test('1234@example.com')).toBe(true);
});

test('Email with all allowed special characters', () => {
expect(emailValidator.test('user+mailbox/department=shipping@example.com')).toBe(true);
const validEmails = [
'contact@sefinek.net',
'example@example.com',
'user@example.co.uk',
'john.doe@example.co',
'user123@example.io',
'user@example.travel',
'user@example.museum',
'user@example.aero',
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@letters-in-local.org',
'01234567890@numbers-in-local.net',
// '&\'*+-./=?^_{}~@other-valid-characters-in-local.net',
'mixed-1234-in-{+^}-local@sld.net',
'a@single-character-in-local.org',
'one-character-third-level@a.example.com',
'single-character-in-sld@x.org',
'local@dash-in-sld.com',
'letters-in-sld@123.com',
'one-letter-sld@x.org',
'test@test--1.com',
'uncommon-tld@sld.museum',
'uncommon-tld@sld.travel',
'uncommon-tld@sld.mobi',
'country-code-tld@sld.uk',
'country-code-tld@sld.rw',
'local@sld.newTLD',
'the-total-length@of-an-entire-address.cannot-be-longer-than-two-hundred-and-fifty-four-characters.and-this-address-is-254-characters-exactly.so-it-should-be-valid.and-im-going-to-add-some-more-words-here.to-increase-the-length-blah-blah-blah-blah-bla.org',
'the-character-limit@for-each-part.of-the-domain.is-sixty-three-characters.this-is-exactly-sixty-three-characters-so-it-is-valid-blah-blah.com',
'local@sub.domains.com',
'backticks`are`legit@test.com',
'digit-only-domain@123.com',
'digit-only-domain-with-subdomain@sub.123.com',
'`a@a.fr',
'`aa@fr.com',
'com@sil.c1m',
// 't119037jskc_ihndkdoz@aakctgajathzffcsuqyjhgjuxnuulgnhxtnbquwtgxljfayeestsjdbalthtddy.lgtmsdhywswlameglunsaplsblljavswxrltovagexhtttodqedmicsekvpmpuu.pgjvdmvzyltpixvalfbktnnpjyjqswbfvtpbfsngqtmhgamhrbqqvyvlhqigggv.nxqglspfbwdhtfpibcrccvctmoxuxwlunghhwacjtrclgirrgppvshxvrzkoifl',
];

const invalidEmails = [
'example@',
'@example.com',
'example.com',
'exa@mple@example.com',
'ex ample@example.com',
' example@example.com',
'example@example.com ',
'exa*mple@example.com',
'user@example',
'user@.com',
'user@.example.com',
'user@com',
'user@.travel',
'user@.museum',
'user@.aero',
'user@.123',
// 'user@[123.123.123.123]',
// 'user@123.123.123.123]',
// 'user@123.123.123.123',
'@missing-local.org',
'! #$%`|@invalid-characters-in-local.org',
// '(),:;`|@more-invalid-characters-in-local.org',
'<>@[]\\`|@even-more-invalid-characters-in-local.org',
'.local-starts-with-dot@sld.com',
'local-ends-with-dot.@sld.com',
'two..consecutive-dots@sld.com',
// 'partially."quoted"@sld.com',
'the-local-part-is-invalid-if-it-is-longer-than-sixty-four-characters@sld.net',
'missing-sld@.com',
// 'sld-starts-with-dashsh@-sld.com',
// 'sld-ends-with-dash@sld-.com',
'invalid-characters-in-sld@! "#$%(),/;<>_[]`|.org',
'missing-dot-before-tld@com',
'missing-tld@sld.',
'invalid',
// 'the-total-length@of-an-entire-address.cannot-be-longer-than-two-hundred-and-fifty-six-characters.and-this-address-is-257-characters-exactly.so-it-should-be-invalid.and-im-going-to-add-some-more-words-here.to-increase-the-length-blah-blah-blah-blah-blah-.org',
'the-character-limit@for-each-part.of-the-domain.is-sixty-three-characters.this-is-exactly-sixty-four-characters-so-it-is-invalid-blah-blah.com',
'missing-at-sign.net',
// 'unbracketed-IP@127.0.0.1',
// 'invalid-ip@127.0.0.1.26',
// 'another-invalid-ip@127.0.0.256',
// 'IP-and-port@127.0.0.1:25',
'trailing-dots@test.de.',
'dot-on-dot-in-domainname@te..st.de',
'dot-first-in-domain@.test.de',
// 'mg@ns.i',
'.dot-start-and-end.@sil.com',
'double@a@com',
'',
// 'tr119037jskc_ihndkdoz@d.aakctgajathzffcsuqyjhgjuxnuulgnhxtnbquwtgxljfayeestsjdbalthtddy.lgtmsdhywswlameglunsaplsblljavswxrltovagexhtttodqedmicsekvpmpuu.pgjvdmvzyltpixvalfbktnnpjyjqswbfvtpbfsngqtmhgamhrbqqvyvlhqigggv.nxqglspfbwdhtfpibcrccvctmoxuxwlunghhwacjtrclgirrgppvshxvrzkoifl',
];

validEmails.forEach((email) => {
it(`Validation of a valid email address for '${email}'`, () => {
expect(emailValidator.test(email)).toBe(true);
});
});

invalidEmails.forEach((email) => {
it(`Validation of a invalid email address for '${email}'`, () => {
expect(emailValidator.test(email)).toBe(false);
});
});
});
Loading

0 comments on commit 8b80f23

Please sign in to comment.