From 866d3e110671afc45bd60bf6a8b21c763d93dcb2 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@lothar.com>
Date: Fri, 13 Jul 2012 01:15:04 -0700
Subject: [PATCH] update stalled-mysql tests to match new behavior

All wsapi operations now require the database (to update+check the
superSessionToken), so some tests that previously expected operations to
succeed without a database now expect them to fail (generally 503).

wsapi_client.js was changed to pass HTTP errors during
/wsapi/session_context back to the caller, so their response code can be
checked, rather than throwing an error (and preventing any other
assertions from being made).
---
 lib/wsapi_client.js         | 12 ++++++++++--
 tests/stalled-mysql-test.js | 19 +++++++------------
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/lib/wsapi_client.js b/lib/wsapi_client.js
index 81dbce7e0..a71202306 100644
--- a/lib/wsapi_client.js
+++ b/lib/wsapi_client.js
@@ -95,7 +95,8 @@ function withCSRF(cfg, context, cb) {
     exports.get(cfg, '/wsapi/session_context', context, undefined, function(err, r) {
       if (err) return cb(err);
       try {
-        if (r.code !== 200) throw 'http error';
+        if (r.code !== 200)
+            return cb({what: "http error", resp: r}); // report first error
         context.session = JSON.parse(r.body);
         context.sessionStartedAt = new Date().getTime();
         cb(null, context.session.csrf_token);
@@ -109,7 +110,14 @@ function withCSRF(cfg, context, cb) {
 
 exports.post = function(cfg, path, context, postArgs, cb) {
   withCSRF(cfg, context, function(err, csrf) {
-    if (err) return cb(err);
+    if (err) {
+        if (err.what == "http error") {
+            // let the session_context HTTP return code speak for the overall
+            // POST
+            return cb(null, err.resp);
+        }
+        return cb(err);
+    }
 
     // parse the server URL (cfg.browserid)
     var uObj;
diff --git a/tests/stalled-mysql-test.js b/tests/stalled-mysql-test.js
index 46b529baa..6ebb61eb8 100755
--- a/tests/stalled-mysql-test.js
+++ b/tests/stalled-mysql-test.js
@@ -79,9 +79,8 @@ suite.addBatch({
 suite.addBatch({
   "ping": {
     topic: wsapi.get('/wsapi/ping', {}),
-    "fails with 500 when db is stalled": function(err, r) {
-      // address info with a primary address doesn't need db access.
-      assert.strictEqual(r.code, 500);
+    "fails with 503 when db is stalled": function(err, r) {
+      assert.strictEqual(r.code, 503);
     }
   },
   "address_info": {
@@ -216,7 +215,7 @@ suite.addBatch({
   "ping": {
     topic: wsapi.get('/wsapi/ping', { }),
     "fails": function(err, r) {
-      assert.strictEqual(r.code, 500);
+      assert.strictEqual(r.code, 503);
     }
   },
 
@@ -391,15 +390,11 @@ suite.addBatch({
     "fails with 404": function(err, r) {
       assert.strictEqual(r.code, 404);
     }
-  }
-});
-
-// logout doesn't need database, it should still succeed
-suite.addBatch({
-  "logout": {
+  },
+  "logout": { // logout needs the database too
     topic: wsapi.post('/wsapi/logout', { }),
-    "succeeds": function(err, r) {
-      assert.strictEqual(r.code, 200);
+    "fails with 503": function(err, r) {
+      assert.strictEqual(r.code, 503);
     }
   }
 });
-- 
GitLab