diff --git a/README.md b/README.md index 1ac3fe7610e83bb2433129778dbcd8e4c252299c..89f41ef2b3c8381159f38da2e7d57fb12a1902fd 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/browserid/lib/db.js b/browserid/lib/db.js index b15e7195862a0d9000163533148891481755ae6b..798e78b1d896c3a7f8189116c736bbf7f5206b2b 100644 --- a/browserid/lib/db.js +++ b/browserid/lib/db.js @@ -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 +}; diff --git a/browserid/tests/db-test.js b/browserid/tests/db-test.js new file mode 100755 index 0000000000000000000000000000000000000000..61a70d4faa5a48ffdb5ea695fadd4a96d4ec9db8 --- /dev/null +++ b/browserid/tests/db-test.js @@ -0,0 +1,40 @@ +#!/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);