From bbf2734b2f2239ad754f2b8f9284a1d1bcb3bb70 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Thu, 4 Aug 2011 17:21:41 -0600
Subject: [PATCH] tests should test both sqlite and json database drivers

---
 browserid/tests/db-test.js | 633 +++++++++++++++++++------------------
 1 file changed, 325 insertions(+), 308 deletions(-)

diff --git a/browserid/tests/db-test.js b/browserid/tests/db-test.js
index e6aece9f9..4ab985fab 100755
--- a/browserid/tests/db-test.js
+++ b/browserid/tests/db-test.js
@@ -12,375 +12,392 @@ var suite = vows.describe('db');
 // disable vows (often flakey?) async error behavior
 suite.options.error = false;
 
-var dbPath = temp.path({suffix: '.db'});
+function addTestsForDriver(driver) {
+  var dbPath = temp.path({suffix: '.db'});
 
-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({ driver: 'json', path: dbPath }, this.callback);
-    },
-    "and its ready": function(r) {
-      assert.isUndefined(r);
-    },
-    "doesn't prevent onReady": {
+  suite.addBatch({
+    "onReady": {
       topic: function() { db.onReady(this.callback); },
-      "from working": function(r) { }
-    }
-  }
-});
-
-// 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);
+      "works": function(r) { }
     },
-    "isStaged returns false": function (r) {
-      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 (r) {
-      assert.isFalse(r);
-    }
-  }
-});
-
-suite.addBatch({
-  "stage a user for creation pending verification": {
-    topic: function() {
-      db.stageUser({
-        email: 'lloyd@nowhe.re',
-        pubkey: 'fakepubkey',
-        hash: 'fakepasswordhash'
-      }, this.callback);
-    },
-    "staging returns a valid secret": function(r) {
-      secret = r;
-      assert.isString(secret);
-      assert.strictEqual(secret.length, 48);
-    }
-  }
-});
-
-suite.addBatch({
-  "an email address is reported": {
-    topic: function() {
-      db.isStaged('lloyd@nowhe.re', this.callback);
-    },
-    " 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);
+    "onReady still": {
+      topic: function() { db.onReady(this.callback); },
+      "works for more than one caller": function(r) { }
     },
-    " as known when it is only staged": function (r) {
-      assert.strictEqual(r, false);
+    "opening the database": {
+      topic: function() {
+        db.open({ driver: driver, path: dbPath }, this.callback);
+      },
+      "and its ready": function(r) {
+        assert.isUndefined(r);
+      },
+      "doesn't prevent onReady": {
+        topic: function() { db.onReady(this.callback); },
+        "from working": function(r) { }
+      }
     }
-  }
-});
+  });
 
-suite.addBatch({
-  "upon receipt of a secret": {
-    topic: function() {
-      db.gotVerificationSecret(secret, this.callback);
-    },
-    "gotVerificationSecret completes without error": function (r) {
-      assert.strictEqual(r, undefined);
-    }
-  }
-});
+  // caching of secrets between test batches.
+  var secret = undefined;
 
-suite.addBatch({
-  "an email address is not reported": {
-    topic: function() {
-      db.isStaged('lloyd@nowhe.re', this.callback);
-    },
-    "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);
+  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 (r) {
+        assert.isFalse(r);
+      }
     },
-    "when it is": function (r) {
-      assert.strictEqual(r, true);
+    "an email address is not reported as known before it is": {
+      topic: function() {
+        db.emailKnown('lloyd@nowhe.re', this.callback);
+      },
+      "emailKnown returns false": function (r) {
+        assert.isFalse(r);
+      }
     }
-  }
-});
+  });
 
-suite.addBatch({
-  "adding keys to email": {
-    topic: function() {
-      db.addKeyToEmail('lloyd@nowhe.re', 'lloyd@nowhe.re', 'fakepubkey2', this.callback);
-    },
-    "works": function(r) {
-      assert.isUndefined(r);
+  suite.addBatch({
+    "stage a user for creation pending verification": {
+      topic: function() {
+        db.stageUser({
+          email: 'lloyd@nowhe.re',
+          pubkey: 'fakepubkey',
+          hash: 'fakepasswordhash'
+        }, this.callback);
+      },
+      "staging returns a valid secret": function(r) {
+        secret = r;
+        assert.isString(secret);
+        assert.strictEqual(secret.length, 48);
+      }
     }
-  }
-});
+  });
 
-suite.addBatch({
-  "adding multiple keys to email": {
-    topic: function() {
-      db.addKeyToEmail('lloyd@nowhe.re', 'lloyd@nowhe.re', 'fakepubkey3', this.callback);
+  suite.addBatch({
+    "an email address is reported": {
+      topic: function() {
+        db.isStaged('lloyd@nowhe.re', this.callback);
+      },
+      " as staged after it is": function (r) {
+        assert.strictEqual(r, true);
+      }
     },
-    "works too": function(r) {
-      assert.isUndefined(r);
+    "an email address is not reported": {
+      topic: function() {
+        db.emailKnown('lloyd@nowhe.re', this.callback);
+      },
+      " as known when it is only staged": function (r) {
+        assert.strictEqual(r, false);
+      }
     }
-  }
-});
+  });
 
-suite.addBatch({
-  "pubkeysForEmail": {
-    topic: function() {
-      db.pubkeysForEmail('lloyd@nowhe.re', this.callback);
-    },
-    "returns all public keys properly": function(r) {
-      assert.isArray(r);
-      assert.strictEqual(r.length, 3);
+  suite.addBatch({
+    "upon receipt of a secret": {
+      topic: function() {
+        db.gotVerificationSecret(secret, this.callback);
+      },
+      "gotVerificationSecret completes without error": function (r) {
+        assert.strictEqual(r, undefined);
+      }
     }
-  }
-});
+  });
 
-suite.addBatch({
-  "checkAuth returns": {
-    topic: function() {
-      db.checkAuth('lloyd@nowhe.re', this.callback);
+  suite.addBatch({
+    "an email address is not reported": {
+      topic: function() {
+        db.isStaged('lloyd@nowhe.re', this.callback);
+      },
+      "as staged immediately after its verified": function (r) {
+        assert.strictEqual(r, false);
+      }
     },
-    "the correct password": function(r) {
-      assert.strictEqual(r, "fakepasswordhash");
+    "an email address is known": {
+      topic: function() {
+        db.emailKnown('lloyd@nowhe.re', this.callback);
+      },
+      "when it is": function (r) {
+        assert.strictEqual(r, true);
+      }
     }
-  }
-});
+  });
 
-suite.addBatch({
-  "staging an email": {
-    topic: function() {
-      db.stageEmail('lloyd@nowhe.re', 'lloyd@somewhe.re', 'fakepubkey4', this.callback);
-    },
-    "yields a valid secret": function(secret) {
-      assert.isString(secret);
-      assert.strictEqual(secret.length, 48);
-    },
-    "then": {
-      topic: function(secret) {
-        var cb = this.callback;
-        db.isStaged('lloyd@somewhe.re', function(r) { cb(secret, r); });
+  suite.addBatch({
+    "adding keys to email": {
+      topic: function() {
+        db.addKeyToEmail('lloyd@nowhe.re', 'lloyd@nowhe.re', 'fakepubkey2', this.callback);
       },
-      "makes it visible via isStaged": function(sekret, r) { assert.isTrue(r); },
-      "and lets you verify it": {
-        topic: function(secret, r) {
-          db.gotVerificationSecret(secret, this.callback);
-        },
-        "successfully": function(r) {
-          assert.isUndefined(r);
-        },
-        "and knownEmail": {
-          topic: function() { db.emailKnown('lloyd@somewhe.re', this.callback); },
-          "returns true": function(r) { assert.isTrue(r); }
-        },
-        "and isStaged": {
-          topic: function() { db.isStaged('lloyd@somewhe.re', this.callback); },
-          "returns false": function(r) { assert.isFalse(r); }
-        }
+      "works": function(r) {
+        assert.isUndefined(r);
       }
     }
-  }
-});
+  });
 
-// exports.emailsBelongToSameAccount
-suite.addBatch({
-  "emails do belong to the same account": {
-    "is true": { 
+  suite.addBatch({
+    "adding multiple keys to email": {
       topic: function() {
-        db.emailsBelongToSameAccount('lloyd@nowhe.re', 'lloyd@somewhe.re', this.callback);
+        db.addKeyToEmail('lloyd@nowhe.re', 'lloyd@nowhe.re', 'fakepubkey3', this.callback);
       },
-      "when they do": function(r) {
-        assert.isTrue(r);
+      "works too": function(r) {
+        assert.isUndefined(r);
       }
-    },
-    "is false": { 
+    }
+  });
+
+  suite.addBatch({
+    "pubkeysForEmail": {
       topic: function() {
-        db.emailsBelongToSameAccount('lloyd@anywhe.re', 'lloyd@somewhe.re', this.callback);
+        db.pubkeysForEmail('lloyd@nowhe.re', this.callback);
       },
-      "when they don't": function(r) {
-        assert.isFalse(r);
+      "returns all public keys properly": function(r) {
+        assert.isArray(r);
+        assert.strictEqual(r.length, 3);
       }
     }
-  }
-});
+  });
 
-// exports.getSyncResponse
-suite.addBatch({
-  "sync responses": {  
-    "are empty": {
+  suite.addBatch({
+    "checkAuth returns": {
       topic: function() {
-        db.getSyncResponse('lloyd@nowhe.re',
-                           {
-                             'lloyd@nowhe.re': 'fakepubkey',
-                             'lloyd@somewhe.re': 'fakepubkey4'
-                           },
-                           this.callback);
+        db.checkAuth('lloyd@nowhe.re', this.callback);
       },
-      "when everything is in sync": function (err, resp) {
-        assert.isUndefined(err);
-        assert.isArray(resp.unknown_emails);
-        assert.isArray(resp.key_refresh);
-        assert.strictEqual(resp.unknown_emails.length, 0);
-        assert.strictEqual(resp.key_refresh.length, 0);
+      "the correct password": function(r) {
+        assert.strictEqual(r, "fakepasswordhash");
       }
-    },
-    "handles client unknown emails": {
+    }
+  });
+
+  suite.addBatch({
+    "staging an email": {
       topic: function() {
-        db.getSyncResponse('lloyd@nowhe.re',
-                           {
-                             'lloyd@nowhe.re': 'fakepubkey'
-                           },
-                           this.callback);
+        db.stageEmail('lloyd@nowhe.re', 'lloyd@somewhe.re', 'fakepubkey4', this.callback);
       },
-      "by returning them in the key_refresh list": function (err, resp) {
-        assert.isUndefined(err);
-        assert.isArray(resp.unknown_emails);
-        assert.isArray(resp.key_refresh);
-        assert.strictEqual(resp.unknown_emails.length, 0);
-        assert.strictEqual(resp.key_refresh.length, 1);
-        assert.strictEqual(resp.key_refresh[0], 'lloyd@somewhe.re');
+      "yields a valid secret": function(secret) {
+        assert.isString(secret);
+        assert.strictEqual(secret.length, 48);
+      },
+      "then": {
+        topic: function(secret) {
+          var cb = this.callback;
+          db.isStaged('lloyd@somewhe.re', function(r) { cb(secret, r); });
+        },
+        "makes it visible via isStaged": function(sekret, r) { assert.isTrue(r); },
+        "and lets you verify it": {
+          topic: function(secret, r) {
+            db.gotVerificationSecret(secret, this.callback);
+          },
+          "successfully": function(r) {
+            assert.isUndefined(r);
+          },
+          "and knownEmail": {
+            topic: function() { db.emailKnown('lloyd@somewhe.re', this.callback); },
+            "returns true": function(r) { assert.isTrue(r); }
+          },
+          "and isStaged": {
+            topic: function() { db.isStaged('lloyd@somewhe.re', this.callback); },
+            "returns false": function(r) { assert.isFalse(r); }
+          }
+        }
       }
-    },
-    "handles server unknown emails": {
-      topic: function() {
-        db.getSyncResponse('lloyd@nowhe.re',
-                           {
-                             'lloyd@nowhe.re': 'fakepubkey',
-                             'lloyd@somewhe.re': 'fakepubkey4',
-                             'lloyd@anywhe.re': 'nofakepubkey',
-                           },
-                           this.callback);
+    }
+  });
+
+  // exports.emailsBelongToSameAccount
+  suite.addBatch({
+    "emails do belong to the same account": {
+      "is true": { 
+        topic: function() {
+          db.emailsBelongToSameAccount('lloyd@nowhe.re', 'lloyd@somewhe.re', this.callback);
+        },
+        "when they do": function(r) {
+          assert.isTrue(r);
+        }
       },
-      "by returning them in the unknown_emails list": function (err, resp) {
-        assert.isUndefined(err);
-        assert.isArray(resp.unknown_emails);
-        assert.strictEqual(resp.unknown_emails.length, 1);
-        assert.strictEqual(resp.unknown_emails[0], 'lloyd@anywhe.re');
-        assert.isArray(resp.key_refresh);
-        assert.strictEqual(resp.key_refresh.length, 0);
+      "is false": { 
+        topic: function() {
+          db.emailsBelongToSameAccount('lloyd@anywhe.re', 'lloyd@somewhe.re', this.callback);
+        },
+        "when they don't": function(r) {
+          assert.isFalse(r);
+        }
       }
-    },
-    "handles server unknown keys": {
-      topic: function() {
-        db.getSyncResponse('lloyd@nowhe.re',
-                           {
-                             'lloyd@nowhe.re': 'fakepubkeyINVALID',
-                             'lloyd@somewhe.re': 'fakepubkey4'
-                           },
-                           this.callback);
+    }
+  });
+
+  // exports.getSyncResponse
+  suite.addBatch({
+    "sync responses": {  
+      "are empty": {
+        topic: function() {
+          db.getSyncResponse('lloyd@nowhe.re',
+                             {
+                               'lloyd@nowhe.re': 'fakepubkey',
+                               'lloyd@somewhe.re': 'fakepubkey4'
+                             },
+                             this.callback);
+        },
+        "when everything is in sync": function (err, resp) {
+          assert.isUndefined(err);
+          assert.isArray(resp.unknown_emails);
+          assert.isArray(resp.key_refresh);
+          assert.strictEqual(resp.unknown_emails.length, 0);
+          assert.strictEqual(resp.key_refresh.length, 0);
+        }
       },
-      "by returning them in the key_refresh list": function (err, resp) {
-        assert.isUndefined(err);
-        assert.isArray(resp.unknown_emails);
-        assert.strictEqual(resp.unknown_emails.length, 0);
-        assert.isArray(resp.key_refresh);
-        assert.strictEqual(resp.key_refresh.length, 1);
-        assert.strictEqual(resp.key_refresh[0], 'lloyd@nowhe.re');
+      "handles client unknown emails": {
+        topic: function() {
+          db.getSyncResponse('lloyd@nowhe.re',
+                             {
+                               'lloyd@nowhe.re': 'fakepubkey'
+                             },
+                             this.callback);
+        },
+        "by returning them in the key_refresh list": function (err, resp) {
+          assert.isUndefined(err);
+          assert.isArray(resp.unknown_emails);
+          assert.isArray(resp.key_refresh);
+          assert.strictEqual(resp.unknown_emails.length, 0);
+          assert.strictEqual(resp.key_refresh.length, 1);
+          assert.strictEqual(resp.key_refresh[0], 'lloyd@somewhe.re');
+        }
+      },
+      "handles server unknown emails": {
+        topic: function() {
+          db.getSyncResponse('lloyd@nowhe.re',
+                             {
+                               'lloyd@nowhe.re': 'fakepubkey',
+                               'lloyd@somewhe.re': 'fakepubkey4',
+                               'lloyd@anywhe.re': 'nofakepubkey',
+                             },
+                             this.callback);
+        },
+        "by returning them in the unknown_emails list": function (err, resp) {
+          assert.isUndefined(err);
+          assert.isArray(resp.unknown_emails);
+          assert.strictEqual(resp.unknown_emails.length, 1);
+          assert.strictEqual(resp.unknown_emails[0], 'lloyd@anywhe.re');
+          assert.isArray(resp.key_refresh);
+          assert.strictEqual(resp.key_refresh.length, 0);
+        }
+      },
+      "handles server unknown keys": {
+        topic: function() {
+          db.getSyncResponse('lloyd@nowhe.re',
+                             {
+                               'lloyd@nowhe.re': 'fakepubkeyINVALID',
+                               'lloyd@somewhe.re': 'fakepubkey4'
+                             },
+                             this.callback);
+        },
+        "by returning them in the key_refresh list": function (err, resp) {
+          assert.isUndefined(err);
+          assert.isArray(resp.unknown_emails);
+          assert.strictEqual(resp.unknown_emails.length, 0);
+          assert.isArray(resp.key_refresh);
+          assert.strictEqual(resp.key_refresh.length, 1);
+          assert.strictEqual(resp.key_refresh[0], 'lloyd@nowhe.re');
+        }
+      },
+      "handle more than one case at a time": {
+        topic: function() {
+          db.getSyncResponse('lloyd@nowhe.re',
+                             {
+                               'lloyd@somewhe.re': 'fakepubkeyINVALID',
+                               'lloyd@anywhe.re': 'notreally'
+                             },
+                             this.callback);
+        },
+        "when everything is outta sync": function (err, resp) {
+          assert.isUndefined(err);
+          assert.isArray(resp.unknown_emails);
+          assert.strictEqual(resp.unknown_emails.length, 1);
+          assert.strictEqual(resp.unknown_emails[0], 'lloyd@anywhe.re');
+
+          assert.isArray(resp.key_refresh);
+          assert.strictEqual(resp.key_refresh.length, 2);
+          assert.strictEqual(resp.key_refresh[0], 'lloyd@nowhe.re');
+          assert.strictEqual(resp.key_refresh[1], 'lloyd@somewhe.re');
+        }
       }
-    },
-    "handle more than one case at a time": {
+    }
+  });
+
+  suite.addBatch({
+    "removing an existing email": {
       topic: function() {
-        db.getSyncResponse('lloyd@nowhe.re',
-                           {
-                             'lloyd@somewhe.re': 'fakepubkeyINVALID',
-                             'lloyd@anywhe.re': 'notreally'
-                           },
-                           this.callback);
+        db.removeEmail("lloyd@somewhe.re", "lloyd@nowhe.re", this.callback);
       },
-      "when everything is outta sync": function (err, resp) {
-        assert.isUndefined(err);
-        assert.isArray(resp.unknown_emails);
-        assert.strictEqual(resp.unknown_emails.length, 1);
-        assert.strictEqual(resp.unknown_emails[0], 'lloyd@anywhe.re');
-
-        assert.isArray(resp.key_refresh);
-        assert.strictEqual(resp.key_refresh.length, 2);
-        assert.strictEqual(resp.key_refresh[0], 'lloyd@nowhe.re');
-        assert.strictEqual(resp.key_refresh[1], 'lloyd@somewhe.re');
+      "returns no error": function(r) {
+        assert.isUndefined(r);
+      },
+      "causes emailKnown": {
+        topic: function() {
+          db.emailKnown('lloyd@nowhe.re', this.callback);
+        },
+        "to return false": function (r) {
+          assert.strictEqual(r, false);
+        }
       }
     }
-  }
-});
+  });
 
-suite.addBatch({
-  "removing an existing email": {
-    topic: function() {
-      db.removeEmail("lloyd@somewhe.re", "lloyd@nowhe.re", this.callback);
-    },
-    "returns no error": function(r) {
-      assert.isUndefined(r);
-    },
-    "causes emailKnown": {
+  suite.addBatch({
+    "canceling an account": {
       topic: function() {
-        db.emailKnown('lloyd@nowhe.re', this.callback);
+        db.cancelAccount("lloyd@somewhe.re", this.callback);
       },
-      "to return false": function (r) {
-        assert.strictEqual(r, false);
+      "returns no error": function(r) {
+        assert.isUndefined(r);
+      },
+      "causes emailKnown": {
+        topic: function() {
+          db.emailKnown('lloyd@somewhe.re', this.callback);
+        },
+        "to return false": function (r) {
+          assert.strictEqual(r, false);
+        }
       }
     }
-  }
-});
+  });
 
-suite.addBatch({
-  "canceling an account": {
-    topic: function() {
-      db.cancelAccount("lloyd@somewhe.re", this.callback);
-    },
-    "returns no error": function(r) {
-      assert.isUndefined(r);
-    },
-    "causes emailKnown": {
+  suite.addBatch({
+    "closing the database": {
       topic: function() {
-        db.emailKnown('lloyd@somewhe.re', this.callback);
+        db.close(this.callback);
       },
-      "to return false": function (r) {
-        assert.strictEqual(r, false);
+      "should work": function(err) {
+        assert.isUndefined(err);
       }
     }
-  }
-});
+  });
 
-suite.addBatch({
-  "remove the database file": {
-    topic: function() {
-      fs.unlink(dbPath, this.callback);
-    },
-    "and unlink should not error": function(err) {
-      assert.isNull(err);
-    },
-    "and the file": {
+  suite.addBatch({
+    "remove the database file": {
       topic: function() {
-        path.exists(dbPath, this.callback);
+        fs.unlink(dbPath, this.callback);
       },
-      "should be missing": function(r) {
-        assert.isFalse(r);
+      "and unlink should not error": function(err) {
+        assert.isNull(err);
+      },
+      "and the file": {
+        topic: function() {
+          path.exists(dbPath, this.callback);
+        },
+        "should be missing": function(r) {
+          assert.isFalse(r);
+        }
       }
     }
-  }
-});
+  });
+}
+
+// test all available drivers
+addTestsForDriver("json");
+addTestsForDriver("sqlite");
 
 // run or export the suite.
 if (process.argv[1] === __filename) suite.run();
-- 
GitLab