Newer
Older
Lloyd Hilaiel
committed
#!/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/. */
Lloyd Hilaiel
committed
require('./lib/test_env.js');
// add lib/ to the require path
const
assert = require('assert'),
vows = require('vows'),
fs = require('fs'),
Lloyd Hilaiel
committed
path = require('path'),
db = require('../lib/db.js'),
configuration = require('../lib/configuration.js');
Lloyd Hilaiel
committed
var suite = vows.describe('db');
// disable vows (often flakey?) async error behavior
suite.options.error = false;
Lloyd Hilaiel
committed
Lloyd Hilaiel
committed
var dbCfg = configuration.get('database');
dbCfg.drop_on_close = true;
Lloyd Hilaiel
committed
Lloyd Hilaiel
committed
suite.addBatch({
"onReady": {
topic: function() { db.onReady(this.callback); },
"works": function(r) { }
},
"onReady still": {
topic: function() { db.onReady(this.callback); },
"works for more than one caller": function(r) { }
},
"opening the database": {
topic: function() {
db.open(dbCfg, this.callback);
},
"and its ready": function(err) {
assert.isNull(err);
Lloyd Hilaiel
committed
},
"doesn't prevent onReady": {
topic: function() { db.onReady(this.callback); },
"from working": function(r) { }
}
}
});
Lloyd Hilaiel
committed
Lloyd Hilaiel
committed
// caching of secrets between test batches.
var secret = undefined;
suite.addBatch({
"an email address is not reported as staged before it is": {
topic: function() {
db.isStaged('lloyd@nowhe.re', this.callback);
},
"isStaged returns false": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.isFalse(r);
}
},
"an email address is not reported as known before it is": {
topic: function() {
db.emailKnown('lloyd@nowhe.re', this.callback);
},
"emailKnown returns false": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.isFalse(r);
}
Lloyd Hilaiel
committed
}
Lloyd Hilaiel
committed
});
Lloyd Hilaiel
committed
Lloyd Hilaiel
committed
suite.addBatch({
"stage a user for creation pending verification": {
topic: function() {
Shane Tomlinson
committed
db.stageUser('lloyd@nowhe.re', 'biglonghashofapassword', this.callback);
"staging returns a valid secret": function(err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
secret = r;
assert.isString(secret);
assert.strictEqual(secret.length, 48);
},
"fetch email for given secret": {
topic: function(err, secret) {
db.emailForVerificationSecret(secret, this.callback);
},
Lloyd Hilaiel
committed
"matches expected email": function(err, email, uid) {
assert.strictEqual(email, 'lloyd@nowhe.re');
Lloyd Hilaiel
committed
},
"fetch secret for email": {
topic: function(err, secret) {
Lloyd Hilaiel
committed
db.verificationSecretForEmail('lloyd@nowhe.re', this.callback);
},
"matches expected secret": function(err, storedSecret) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.strictEqual(storedSecret, secret);
}
Lloyd Hilaiel
committed
}
}
});
suite.addBatch({
"an email address is reported": {
topic: function() {
db.isStaged('lloyd@nowhe.re', this.callback);
},
" as staged after it is": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.strictEqual(r, true);
}
},
"an email address is not reported": {
topic: function() {
db.emailKnown('lloyd@nowhe.re', this.callback);
},
" as known when it is only staged": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.strictEqual(r, false);
Lloyd Hilaiel
committed
}
});
Lloyd Hilaiel
committed
suite.addBatch({
"upon receipt of a secret": {
topic: function() {
Lloyd Hilaiel
committed
db.completeCreateUser(secret, this.callback);
Lloyd Hilaiel
committed
},
"gotVerificationSecret completes without error": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
}
}
});
Lloyd Hilaiel
committed
suite.addBatch({
"an email address is not reported": {
topic: function() {
db.isStaged('lloyd@nowhe.re', this.callback);
},
"as staged immediately after its verified": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.strictEqual(r, false);
Lloyd Hilaiel
committed
},
"an email address is known": {
topic: function() {
db.emailKnown('lloyd@nowhe.re', this.callback);
},
"when it is": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.strictEqual(r, true);
}
}
});
Lloyd Hilaiel
committed
suite.addBatch({
"checkAuth returns": {
topic: function() {
Lloyd Hilaiel
committed
var cb = this.callback;
db.emailToUID('lloyd@nowhe.re', function(err, uid) {
Lloyd Hilaiel
committed
db.checkAuth(uid, cb);
});
"the correct password": function(err, r) {
assert.isNull(err);
Shane Tomlinson
committed
assert.strictEqual(r, "biglonghashofapassword");
Lloyd Hilaiel
committed
}
});
Lloyd Hilaiel
committed
suite.addBatch({
Lloyd Hilaiel
committed
"emailToUID": {
Lloyd Hilaiel
committed
topic: function() {
Lloyd Hilaiel
committed
db.emailToUID('lloyd@nowhe.re', this.callback);
Lloyd Hilaiel
committed
},
"returns a valid userid": function(err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.isNumber(r);
Lloyd Hilaiel
committed
},
Lloyd Hilaiel
committed
"returns a UID": {
topic: function(err, uid) {
Lloyd Hilaiel
committed
db.userOwnsEmail(uid, 'lloyd@nowhe.re', this.callback);
"that owns the original email": function(err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.ok(r);
}
}
}
});
suite.addBatch({
"getting a UID": {
Lloyd Hilaiel
committed
topic: function() {
db.emailToUID('lloyd@nowhe.re', this.callback);
},
"does not error": function(err, uid) {
assert.isNull(err);
},
"then staging an email": {
topic: function(err, uid) {
Lloyd Hilaiel
committed
// do not supply a password here. Email addition only supplies a password
// in the case it's the addition of a secondary address to an account with
// only primaries.
db.stageEmail(uid, 'lloyd@somewhe.re', undefined, this.callback);
Lloyd Hilaiel
committed
},
"yields a valid secret": function(err, secret) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.isString(secret);
assert.strictEqual(secret.length, 48);
},
"then": {
topic: function(err, secret) {
Lloyd Hilaiel
committed
var cb = this.callback;
db.isStaged('lloyd@somewhe.re', function(err, r) { cb(secret, r); });
Lloyd Hilaiel
committed
},
Lloyd Hilaiel
committed
"makes it visible via isStaged": function(sekret, r) { assert.isTrue(r); },
"lets you verify it": {
topic: function(secret, r) {
Lloyd Hilaiel
committed
db.completeConfirmEmail(secret, this.callback);
Lloyd Hilaiel
committed
},
"successfully": function(err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
},
"and knownEmail": {
topic: function() { db.emailKnown('lloyd@somewhe.re', this.callback); },
"returns true": function(err, r) {
assert.isNull(err);
assert.isTrue(r);
}
Lloyd Hilaiel
committed
},
"and isStaged": {
topic: function() { db.isStaged('lloyd@somewhe.re', this.callback); },
"returns false": function(err, r) {
assert.isNull(err);
assert.isFalse(r);
}
Lloyd Hilaiel
committed
},
"and user's password": {
topic: function() {
var self = this;
db.emailToUID('lloyd@nowhe.re', function(err, uid) {
db.checkAuth(uid, self.callback);
});
},
"is still populated": function(err, hash) {
assert.strictEqual(hash, "biglonghashofapassword");
}
Lloyd Hilaiel
committed
}
Lloyd Hilaiel
committed
}
Lloyd Hilaiel
committed
}
});
Lloyd Hilaiel
committed
// exports.emailsBelongToSameAccount
suite.addBatch({
"emails do belong to the same account": {
"is true": {
Lloyd Hilaiel
committed
topic: function() {
Lloyd Hilaiel
committed
db.emailsBelongToSameAccount('lloyd@nowhe.re', 'lloyd@somewhe.re', this.callback);
Lloyd Hilaiel
committed
},
"when they do": function(err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.isTrue(r);
Lloyd Hilaiel
committed
}
Lloyd Hilaiel
committed
},
"is false": {
Lloyd Hilaiel
committed
topic: function() {
Lloyd Hilaiel
committed
db.emailsBelongToSameAccount('lloyd@anywhe.re', 'lloyd@somewhe.re', this.callback);
Lloyd Hilaiel
committed
},
"when they don't": function(err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.isFalse(r);
Lloyd Hilaiel
committed
}
}
Lloyd Hilaiel
committed
}
});
Lloyd Hilaiel
committed
Lloyd Hilaiel
committed
suite.addBatch({
"emailType of lloyd@anywhe.re": {
topic: function() {
db.emailType('lloyd@anywhe.re', this.callback);
},
"is null": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.isUndefined(r);
}
},
"emailType of lloyd@somewhe.re": {
topic: function() {
db.emailType('lloyd@somewhe.re', this.callback);
},
"is 'secondary'": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.strictEqual(r, 'secondary');
}
},
"emailType of lloyd@nowhe.re": {
topic: function() {
db.emailType('lloyd@nowhe.re', this.callback);
},
"is 'secondary'": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.strictEqual(r, 'secondary');
}
}
});
Lloyd Hilaiel
committed
suite.addBatch({
"removing an existing email": {
topic: function() {
Lloyd Hilaiel
committed
var cb = this.callback;
db.emailToUID("lloyd@somewhe.re", function(err, uid) {
Lloyd Hilaiel
committed
db.removeEmail(uid, "lloyd@nowhe.re", cb);
});
Lloyd Hilaiel
committed
},
"returns no error": function(err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.isUndefined(r);
},
"causes emailKnown": {
Lloyd Hilaiel
committed
topic: function() {
Lloyd Hilaiel
committed
db.emailKnown('lloyd@nowhe.re', this.callback);
"to return false": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.strictEqual(r, false);
Lloyd Hilaiel
committed
}
}
Lloyd Hilaiel
committed
}
});
Lloyd Hilaiel
committed
Lloyd Hilaiel
committed
suite.addBatch({
"creating a primary account": {
Lloyd Hilaiel
committed
topic: function() {
db.createUserWithPrimaryEmail("lloyd@primary.domain", this.callback);
Lloyd Hilaiel
committed
},
"returns no error": function(err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
},
"causes emailKnown": {
Lloyd Hilaiel
committed
topic: function() {
db.emailKnown('lloyd@primary.domain', this.callback);
Lloyd Hilaiel
committed
},
"to return true": function (err, r) {
assert.isNull(err);
assert.strictEqual(r, true);
}
},
"causes emailType": {
topic: function() {
db.emailType('lloyd@primary.domain', this.callback);
},
"to return 'primary'": function (err, r) {
assert.isNull(err);
assert.strictEqual(r, 'primary');
Lloyd Hilaiel
committed
}
}
Lloyd Hilaiel
committed
});
Lloyd Hilaiel
committed
suite.addBatch({
"adding a primary email to that account": {
Lloyd Hilaiel
committed
topic: function() {
Lloyd Hilaiel
committed
var cb = this.callback;
db.emailToUID('lloyd@primary.domain', function(err, uid) {
Lloyd Hilaiel
committed
db.addPrimaryEmailToAccount(uid, "lloyd2@primary.domain", cb);
});
Lloyd Hilaiel
committed
},
"returns no error": function(err) {
assert.isNull(err);
Lloyd Hilaiel
committed
},
"causes emailKnown": {
topic: function() {
db.emailKnown('lloyd2@primary.domain', this.callback);
Lloyd Hilaiel
committed
},
"to return true": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.strictEqual(r, true);
}
},
"causes emailType": {
topic: function() {
db.emailType('lloyd@primary.domain', this.callback);
},
"to return 'primary'": function (err, r) {
assert.isNull(err);
Lloyd Hilaiel
committed
assert.strictEqual(r, 'primary');
}
}
},
"adding a primary email to an account with only secondaries": {
topic: function() {
Lloyd Hilaiel
committed
var cb = this.callback;
db.emailToUID('lloyd@somewhe.re', function(err, uid) {
Lloyd Hilaiel
committed
db.addPrimaryEmailToAccount(uid, "lloyd3@primary.domain", cb);
});
"returns no error": function(err) {
assert.isNull(err);
},
"causes emailKnown": {
topic: function() {
db.emailKnown('lloyd3@primary.domain', this.callback);
},
"to return true": function (err, r) {
assert.isNull(err);
assert.strictEqual(r, true);
}
},
"causes emailType": {
topic: function() {
db.emailType('lloyd3@primary.domain', this.callback);
},
"to return 'primary'": function (err, r) {
assert.isNull(err);
assert.strictEqual(r, 'primary');
}
}
}
});
suite.addBatch({
"adding a registered primary email to an account": {
topic: function() {
Lloyd Hilaiel
committed
var cb = this.callback;
db.emailToUID('lloyd@primary.domain', function(err, uid) {
Lloyd Hilaiel
committed
db.addPrimaryEmailToAccount(uid, "lloyd3@primary.domain", cb);
});
"returns no error": function(err) {
assert.isNull(err);
},
"and emailKnown": {
topic: function() {
db.emailKnown('lloyd3@primary.domain', this.callback);
},
"still returns true": function (err, r) {
assert.isNull(err);
assert.strictEqual(r, true);
}
},
"and emailType": {
topic: function() {
db.emailType('lloyd@primary.domain', this.callback);
},
"still returns 'primary'": function (err, r) {
assert.isNull(err);
assert.strictEqual(r, 'primary');
}
},
"and email is removed": {
topic: function() {
db.emailsBelongToSameAccount('lloyd3@primary.domain', 'lloyd@somewhe.re', this.callback);
},
"from original account": function(err, r) {
assert.isNull(err);
assert.isFalse(r);
}
},
"and email is added": {
topic: function() {
db.emailsBelongToSameAccount('lloyd3@primary.domain', 'lloyd@primary.domain', this.callback);
},
"to new account": function(err, r) {
assert.isNull(err);
assert.isTrue(r);
}
}
}
});
suite.addBatch({
"canceling an account": {
topic: function() {
Lloyd Hilaiel
committed
var cb = this.callback;
db.emailToUID("lloyd@somewhe.re", function(err, uid) {
Lloyd Hilaiel
committed
db.cancelAccount(uid, cb);
});
"returns no error": function(err) {
assert.isNull(err);
},
"causes emailKnown": {
topic: function() {
db.emailKnown('lloyd@somewhe.re', this.callback);
},
"to return false": function (err, r) {
assert.isNull(err);
assert.strictEqual(r, false);
}
}
Lloyd Hilaiel
committed
}
});
Lloyd Hilaiel
committed
suite.addBatch({
"closing the database": {
topic: function() {
db.close(this.callback);
},
"should work": function(err) {
assert.isNull(err);
Lloyd Hilaiel
committed
},
"re-opening the database": {
topic: function() {
db.open(dbCfg, this.callback);
},
"works": function(err) {
assert.isNull(err);
Lloyd Hilaiel
committed
},
"and then purging": {
topic: function() {
db.closeAndRemove(this.callback);
},
"works": function(r) {
assert.isNull(r);
Lloyd Hilaiel
committed
}
}
Lloyd Hilaiel
committed
}
Lloyd Hilaiel
committed
}
Lloyd Hilaiel
committed
Lloyd Hilaiel
committed
// run or export the suite.
if (process.argv[1] === __filename) suite.run();
else suite.export(module);