Skip to content

Commit

Permalink
test db.addKeyToEmail and db.pubkeysForEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
lloyd committed Jul 20, 2011
1 parent 6a64b84 commit 3d4ecd1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
23 changes: 12 additions & 11 deletions browserid/lib/db.js
Expand Up @@ -326,18 +326,19 @@ exports.getSyncResponse = function(email, identities, cb) {
});
};


// get all public keys associated with an email address
exports.pubkeysForEmail = function(identity, cb) {
db.execute('SELECT keys.key FROM keys, emails WHERE emails.address = ? AND keys.email = emails.id',
[ identity ],
function(err, rows) {
var keys = undefined;
if (!err && rows && rows.length) {
keys = [ ];
for (var i = 0; i < rows.length; i++) keys.push(rows[i].key);
}
cb(keys);
});
db.execute(
'SELECT keys.key FROM keys, emails WHERE emails.address = ? AND keys.email = emails.id',
[ identity ],
function(err, rows) {
var keys = undefined;
if (!err && rows && rows.length) {
keys = [ ];
for (var i = 0; i < rows.length; i++) keys.push(rows[i].key);
}
cb(keys);
});
};


Expand Down
71 changes: 56 additions & 15 deletions browserid/tests/db-test.js
Expand Up @@ -4,7 +4,8 @@ const assert = require('assert'),
vows = require('vows'),
db = require('../lib/db.js'),
temp = require('temp'),
fs = require('fs');
fs = require('fs'),
path = require('path');

var suite = vows.describe('db');

Expand All @@ -16,7 +17,7 @@ suite.addBatch({
var cb = this.callback;
db.onReady(function() { cb(true) });
},
"the database is ready": function(err, r) {
"the database is ready": function(r) {
assert.strictEqual(r, true);
}
}
Expand All @@ -30,15 +31,15 @@ suite.addBatch({
topic: function() {
return db.isStaged('lloyd@nowhe.re');
},
"isStaged returns false": function (e, r) {
"isStaged returns false": function (r) {
assert.strictEqual(r, false);
}
},
"an email address is not reported as known before it is": {
topic: function() {
db.emailKnown('lloyd@nowhe.re', this.callback);
},
"emailKnown returns false": function (e, r) {
"emailKnown returns false": function (r) {
assert.strictEqual(r, false);
}
}
Expand All @@ -53,7 +54,7 @@ suite.addBatch({
pass: 'fakepasswordhash'
});
},
"staging returns a valid secret": function(e, r) {
"staging returns a valid secret": function(r) {
assert.isString(secret);
assert.strictEqual(secret.length, 48);
}
Expand All @@ -65,15 +66,15 @@ suite.addBatch({
topic: function() {
return db.isStaged('lloyd@nowhe.re');
},
" as staged after it is": function (e, r) {
" as staged after it is": function (r) {
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 (e, r) {
" as known when it is only staged": function (r) {
assert.strictEqual(r, false);
}
}
Expand All @@ -84,7 +85,7 @@ suite.addBatch({
topic: function() {
db.gotVerificationSecret(secret, this.callback);
},
"gotVerificationSecret completes without error": function (e, r) {
"gotVerificationSecret completes without error": function (r) {
assert.strictEqual(r, undefined);
}
}
Expand All @@ -95,40 +96,80 @@ suite.addBatch({
topic: function() {
return db.isStaged('lloyd@nowhe.re');
},
"as staged immediately after its verified": function (e, r) {
"as staged immediately after its verified": function (r) {
assert.strictEqual(r, false);
}
},
"an email address is known": {
topic: function() {
db.emailKnown('lloyd@nowhe.re', this.callback);
},
"when it is": function (e, r) {
"when it is": function (r) {
assert.strictEqual(r, true);
}
}
});

suite.addBatch({
"adding keys to email": {
topic: function() {
db.addKeyToEmail('lloyd@nowhe.re', 'lloyd@nowhe.re', 'fakepublickey2', this.callback);
},
"works": function(r) {
assert.isUndefined(r);
}
}
});

suite.addBatch({
"adding multiple keys to email": {
topic: function() {
db.addKeyToEmail('lloyd@nowhe.re', 'lloyd@nowhe.re', 'fakepublickey3', this.callback);
},
"works too": function(r) {
assert.isUndefined(r);
}
}
});

suite.addBatch({
"pubkeysForEmail": {
topic: function() {
db.pubkeysForEmail('lloyd@nowhe.re', this.callback);
},
"returns all public keys properly": function(r, e) {
assert.isArray(r);
assert.strictEqual(r.length, 3);
}
}
});


// XXX: remaining APIs to test
// exports.addEmailToAccount
// exports.addKeyToEmail
// exports.cancelAccount
// exports.checkAuth
// exports.checkAuthHash
// exports.emailsBelongToSameAccount
// exports.getSyncResponse
// exports.pubkeysForEmail
// exports.removeEmail
// exports.stageEmail


suite.addBatch({
"remove the database file": {
topic: function() {
fs.unlink(db.dbPath, this.callback);
},
"file is toast": function(err, exception) {
assert.strictEqual(exception, undefined);
"and unlink should not error": function(err) {
assert.strictEqual(err, undefined);
},
"and the file": {
topic: function() {
path.exists(db.dbPath, this.callback);
},
"should be missing": function(r) {
assert.isFalse(r);
}
}
}
});
Expand Down

0 comments on commit 3d4ecd1

Please sign in to comment.