Skip to content
Snippets Groups Projects
Commit c55d8c8f authored by Lloyd Hilaiel's avatar Lloyd Hilaiel
Browse files

initial stubbing of db.js tests, also generalize db.js so that the path where...

initial stubbing of db.js tests, also generalize db.js so that the path where the database resides may be configured
parent 29085e6c
No related branches found
No related tags found
No related merge requests found
......@@ -15,6 +15,7 @@ All of the servers here are based on node.js, and some number of 3rd party node
* vows (>= 0.5.8)
* bcrypt (>= 0.2.3)
* ejs (>= 0.4.3)
* temp (>= 0.2.0)
## Getting started:
......
......@@ -5,29 +5,36 @@ const sqlite = require('sqlite'),
var VAR_DIR = path.join(path.dirname(__dirname), "var");
var db = new sqlite.Database();
var dbPath = path.join(VAR_DIR, "authdb.sqlite");
// a configurable parameter if set immediately after require() of db.js
exports.dbPath = path.join(VAR_DIR, "authdb.sqlite");
var ready = false;
var waiting = [];
db.open(dbPath, function (error) {
if (error) {
console.log("Couldn't open database: " + error);
throw error;
}
db.executeScript(
"CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, password TEXT );" +
"CREATE TABLE IF NOT EXISTS emails ( id INTEGER PRIMARY KEY, user INTEGER, address TEXT UNIQUE );" +
"CREATE TABLE IF NOT EXISTS keys ( id INTEGER PRIMARY KEY, email INTEGER, key TEXT, expires INTEGER )",
function (error) {
if (error) {
throw error;
}
ready = true;
waiting.forEach(function(f) { f() });
waiting = [];
});
});
// async break allow database path to be configured by calling code
// a touch tricky cause client must set dbPath before releasing
// control of the runloop
setTimeout(function() {
db.open(exports.dbPath, function (error) {
if (error) {
console.log("Couldn't open database: " + error);
throw error;
}
db.executeScript(
"CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, password TEXT );" +
"CREATE TABLE IF NOT EXISTS emails ( id INTEGER PRIMARY KEY, user INTEGER, address TEXT UNIQUE );" +
"CREATE TABLE IF NOT EXISTS keys ( id INTEGER PRIMARY KEY, email INTEGER, key TEXT, expires INTEGER )",
function (error) {
if (error) {
throw error;
}
ready = true;
waiting.forEach(function(f) { f() });
waiting = [];
});
});
}, 0);
exports.onReady = function(f) {
setTimeout(function() {
......@@ -366,4 +373,4 @@ exports.cancelAccount = function(authenticated_email, cb) {
else cb();
});
});
};
\ No newline at end of file
};
#!/usr/bin/env node
const assert = require('assert'),
vows = require('vows'),
db = require('../lib/db.js'),
temp = require('temp'),
fs = require('fs');
var suite = vows.describe('db');
db.dbPath = temp.path({suffix: '.sqlite'});
suite.addBatch({
"waiting for the database to become ready": {
topic: function() {
var cb = this.callback;
db.onReady(function() { cb(true) });
},
"the database is ready": function(err, r) {
assert.strictEqual(r, true);
}
}
});
// XXX: add exhaustive tests of the db API here
suite.addBatch({
"remove the database file": {
topic: function() {
fs.unlink(db.dbPath, this.callback);
},
"file is toast": function(err, exception) {
assert.strictEqual(exception, undefined);
}
}
});
// run or export the suite.
if (process.argv[1] === __filename) suite.run();
else suite.export(module);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment