From be238b680252cc997eb829bdda52b8e5aecfcafa Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Wed, 20 Jul 2011 13:30:40 -0600
Subject: [PATCH] test adding emails to accounts

---
 browserid/lib/db.js          |  6 +++-
 browserid/tests/db-test.js   | 69 +++++++++++++++++++++++++++++-------
 browserid/tests/lib/wsapi.js |  5 +--
 3 files changed, 64 insertions(+), 16 deletions(-)

diff --git a/browserid/lib/db.js b/browserid/lib/db.js
index 3ec55a06e..dc890736b 100644
--- a/browserid/lib/db.js
+++ b/browserid/lib/db.js
@@ -46,6 +46,8 @@ exports.onReady = function(f) {
   }, 0);
 };
 
+// XXX: g_staged and g_stagedEmails should be moved into persistent/fast storage.
+
 // half created user accounts (pending email verification)
 // OR
 // half added emails (pending verification)
@@ -101,6 +103,7 @@ exports.emailKnown = function(email, cb) {
     });
 };
 
+// XXX: should be moved to async.
 exports.isStaged = function(email) {
   return g_stagedEmails.hasOwnProperty(email);
 };
@@ -187,6 +190,7 @@ exports.stageUser = function(obj) {
 };
 
 /* takes an argument object including email, pass, and pubkey. */
+// XXX: change to async
 exports.stageEmail = function(existing_email, new_email, pubkey) {
   var secret = generateSecret();
   // overwrite previously staged users
@@ -264,7 +268,7 @@ exports.checkAuth = function(email, cb) {
   db.execute("SELECT users.password FROM emails, users WHERE users.id = emails.user AND emails.address = ?",
              [ email ],
              function (error, rows) {
-                 cb(rows.length !== 1 ? undefined : rows[0].password);
+               cb(rows.length !== 1 ? undefined : rows[0].password);
              });
 };
 
diff --git a/browserid/tests/db-test.js b/browserid/tests/db-test.js
index c749770c8..45e19f3eb 100755
--- a/browserid/tests/db-test.js
+++ b/browserid/tests/db-test.js
@@ -1,13 +1,16 @@
 #!/usr/bin/env node
 
-const assert = require('assert'),
-        vows = require('vows'),
-          db = require('../lib/db.js'),
-        temp = require('temp'),
-          fs = require('fs'),
-        path = require('path');
+const
+assert = require('assert'),
+vows = require('vows'),
+db = require('../lib/db.js'),
+temp = require('temp'),
+fs = require('fs'),
+path = require('path');
 
 var suite = vows.describe('db');
+// disable vows (often flakey?) async error behavior
+suite.options.error = false;
 
 db.dbPath = temp.path({suffix: '.sqlite'});
 
@@ -51,7 +54,7 @@ suite.addBatch({
       return secret = db.stageUser({
         email: 'lloyd@nowhe.re',
         pubkey: 'fakepublickey',
-        pass: 'fakepasswordhash'
+        hash: 'fakepasswordhash'
       });
     },
     "staging returns a valid secret": function(r) {
@@ -137,18 +140,58 @@ suite.addBatch({
     topic: function() {
       db.pubkeysForEmail('lloyd@nowhe.re', this.callback);
     },
-    "returns all public keys properly": function(r, e) {
+    "returns all public keys properly": function(r) {
       assert.isArray(r);
       assert.strictEqual(r.length, 3);
     }
   }
 });
 
+suite.addBatch({
+  "checkAuth returns": {
+    topic: function() {
+      db.checkAuth('lloyd@nowhe.re', this.callback);
+    },
+    "the correct password": function(r) {
+      assert.strictEqual(r, "fakepasswordhash");
+    }
+  }
+});
+
+suite.addBatch({
+  "staging an email": {
+    topic: function() {
+      return db.stageEmail('lloyd@nowhe.re', 'lloyd@somewhe.re', 'fakepubkey4');
+    },
+    "yields a valid secret": function(secret) {
+      assert.isString(secret);
+      assert.strictEqual(secret.length, 48);
+    },
+    "makes email addr via isStaged": {
+      topic: function() { return db.isStaged('lloyd@somewhe.re'); },
+      "visible": function(r) { assert.isTrue(r); }
+    },
+    "and verifying it": {
+      topic: function(secret) {
+        db.gotVerificationSecret(secret, this.callback);
+      },
+      "returns no error": function(r) {
+        assert.isUndefined(r);
+      },
+      "makes email addr via knownEmail": {
+        topic: function() { db.emailKnown('lloyd@somewhe.re', this.callback); },
+        "visible": function(r) { assert.isTrue(r); }
+      },
+      "makes email addr via isStaged": {
+        topic: function() { return db.isStaged('lloyd@somewhe.re'); },
+        "not visible": function(r) { assert.isFalse(r); }
+      }
+    }
+  }
+});
+
 // XXX: remaining APIs to test
-// exports.addEmailToAccount
 // exports.cancelAccount
-// exports.checkAuth
-// exports.checkAuthHash
 // exports.emailsBelongToSameAccount
 // exports.getSyncResponse
 // exports.removeEmail
@@ -160,8 +203,8 @@ suite.addBatch({
       fs.unlink(db.dbPath, this.callback);
     },
     "and unlink should not error": function(err) {
-      assert.strictEqual(err, undefined);
-     },
+      assert.isNull(err);
+    },
     "and the file": {
       topic: function() {
         path.exists(db.dbPath, this.callback);
diff --git a/browserid/tests/lib/wsapi.js b/browserid/tests/lib/wsapi.js
index f6ba8b174..dc3a9c93c 100644
--- a/browserid/tests/lib/wsapi.js
+++ b/browserid/tests/lib/wsapi.js
@@ -1,5 +1,6 @@
-const http = require('http'),
-      querystring = require('querystring');
+const
+http = require('http'),
+querystring = require('querystring');
 
 // wsapi abstractions trivial cookie jar
 var cookieJar = {};
-- 
GitLab