From c55d8c8f5182ce0b4c4e75185af99ec4f5795e0b Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Tue, 19 Jul 2011 17:25:27 -0600
Subject: [PATCH] initial stubbing of db.js tests, also generalize db.js so
 that the path where the database resides may be configured

---
 README.md                  |  1 +
 browserid/lib/db.js        | 47 ++++++++++++++++++++++----------------
 browserid/tests/db-test.js | 40 ++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 20 deletions(-)
 create mode 100755 browserid/tests/db-test.js

diff --git a/README.md b/README.md
index 1ac3fe761..89f41ef2b 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 b15e71958..798e78b1d 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 000000000..61a70d4fa
--- /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);
-- 
GitLab