Skip to content
This repository has been archived by the owner on Jul 6, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sbalmer committed Oct 16, 2017
2 parents 1a630a1 + 2f3f78d commit 6cd30d0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
7 changes: 4 additions & 3 deletions imports/ui/account/AccountTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ export const SetupWarnings = (instance, warnings) => {
};

/** Check a string if it is a valid email adress
* Consider string as valid email if it matches this pattern:
* (1+ characters)@(1+ characters).(1+ characters)
*
* @param {String} the string to be checked
*/
export const IsEmail = str => str.search(/^[^@\s]+@[^@.\s]+\.\w+$/g) >= 0;
export const IsEmail = str => {
check(str, String);
return str.search(/^[^@\s]+@([^@.\s]+\.)+\w+$/g) === 0;
};

export const PleaseLogin = () => {
if (Meteor.userId()) return false;
Expand Down
63 changes: 63 additions & 0 deletions imports/ui/account/AccountTools.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* jshint -W024 */
/* jshint expr:true */

import { expect } from 'meteor/practicalmeteor:chai';
import { IsEmail } from './AccountTools.js';

// This should not be here
msgfmt.init('en');

describe('Email validation', function() {
const isEmail = str => expect(IsEmail(str)).to.be.true;
const isNotEmail = str => expect(IsEmail(str)).to.be.false;
const fails = notaString => expect(() => IsEmail(notaString)).to.throw();

it("accepts normal emails", function() {
isEmail("Jah7reix.poo0Ooz0@geemail.com");
isEmail("poo0Ooz0@geemail.com");
isEmail("diFoh7ma@wooow.example");
});

it("accepts more email formats", function() {
isEmail("Jah7reix.poo0Ooz0@subdomain.geemail.com");
isEmail("J@s.g.c");
isEmail("0@sub.sub.sub.sub.sub");
});

it("accepts weird email formats", function() {
isEmail("Jah7reix+poo0Ooz0@geemail.com");
isEmail("Ó@£.cooooooom");
isEmail("-@ä.c");
isEmail("{_}@n-o.c");
});

it("rejects incomplete addresses", function() {
// Yes it is technically possible to have an address @toplevel
// But we still assume it's an error.
isNotEmail("poo0Ooz0@geemail");

isNotEmail("@£.cooooooom");
isNotEmail("-@");
isNotEmail("diFoh7ma@.");
isNotEmail("diFoh7ma@.example");
isNotEmail("diFoh7ma@example.");
});

it("rejects other strings", function() {
isNotEmail("Jah7reix.poo0Ooz0geemail.com");
isNotEmail("Jah7reix.poo0Ooz0");
isNotEmail("poo0Ooz0");
isNotEmail("é");
isNotEmail("0");
isNotEmail("");
});

it("fails on non-strings", function() {
fails();
fails(undefined);
fails(true);
fails(false);
fails({});
fails(1.1);
});
});

0 comments on commit 6cd30d0

Please sign in to comment.