Skip to content

Instantly share code, notes, and snippets.

@simong
Last active August 29, 2015 14:17
Show Gist options
  • Save simong/88f6e14b776669098a88 to your computer and use it in GitHub Desktop.
Save simong/88f6e14b776669098a88 to your computer and use it in GitHub Desktop.
/**
* Assert that a user can be created
*
* @param {RestContext} restCtx The REST context of a user who can create a user account
* @param {Object} params The parameters to create the user with
* @param {Function} callback Invoked when the user has been created an the verification email has been sent
* @param {String} callback.user The created user
* @param {String} [callback.token] The email verification token. Is omitted when a tenant administrator is create a user
* @throws {AssertionError} Thrown if the create is unsuccessful
*/
var assertCreateUserSucceeds = module.exports.assertCreateUserSucceeds = function(restCtx, params, callback) {
// Determine if the current user is an administrator. User accounts created by an administrator
// won't have to verify their email address, so we shouldn't wait for such an email when the
// passed in `restCtx` is an administrator
RestAPI.User.getMe(restCtx, function(err, me) {
assert.ok(!err);
var isAdmin = (me.isTenantAdmin || me.isGlobalAdmin);
var user = null;
var token = null;
// Wait until both the request is done and the email has been delivered
var done = _.after(2, function() {
return callback(user, token);
});
// Create the user account
RestAPI.User.createUser(restCtx, params.username, params.password, params.displayName, params.email, params, function(err, _user) {
assert.ok(!err);
user = _user;
done();
});
// Wait until the verification email has been delivered, if any
OaeUtil.invokeIfNecessary(!isAdmin, onceVerificationEmailSent, params.email, function(_token) {
token = _token;
done();
});
});
};
/**
* Execute a callback function when a verification email is sent
*
* @param {String} [email] The email address too which the token should have been sent
* @param {Function} callback Invoked when the verification email was sent to the user
* @param {String} callback.token The email verification token that was sent to the user
* @throws {AssertionError} Thrown if an email was sent to the wrong email address or does not contain a proper token
*/
var whenVerificationEmailSent = module.exports.whenVerificationEmailSent = function(email, callback) {
EmailAPI.once('debugSent', function(message) {
// Verify the email address
if (email) {
assert.strictEqual(message.to[0].address, email);
}
// Verify a token is passed in both the html and text email
assert.ok(message.html.match(/\?token=(email-[a-zA-Z0-9]{32})/));
assert.ok(message.text.match(/\?token=(email-[a-zA-Z0-9]{32})/));
// Verify a token is passed in the email
var token = message.text.match(/\?token=(email-[a-zA-Z0-9]{32})/)[1];
assert.ok(token);
token = decodeURIComponent(token);
return callback(token);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment