Switch back to HTTP status codes for fastcgi + update unit tests
[matches/MCTX3420.git] / testing / qunit / unit-tests.js
index c16b8c9..b7ba7e1 100644 (file)
@@ -11,14 +11,31 @@ var api = location.protocol + "//" +  location.host + "/api/";
 
 /**
  * Sends an AJAX query to the API
+ * query(module, username, password, callback);
+ * query(module, callback);
+ * query(module, opts, callback);
+ * query(module, opts, username, password, callback);
  * @param {string} module The name of the module to be queried
  * @param {Object} opts Object containing parameters to pass to module 
- * @param {function} callback Function that receives JSON data
  * @param {string} username Optional
  * @param {string} password Required if username specified
+ * @param {function} callback Function that receives JSON data
  * @returns JSON data
  */
-function query(module, opts, callback, username, password) {
+function query(module, opts, username, password, callback) {
+  if (typeof opts === 'string') {
+    callback = password;
+    password = username;
+    username = opts;
+    opts = undefined;
+  } else if (typeof opts === 'function') {
+    callback = opts;
+    opts = undefined;
+  } else if (typeof username === 'function') {
+    callback = username;
+    username = undefined;
+  }
+  
   function buildQuery(opts) {
     var result = "?";
     var first = true;
@@ -38,34 +55,58 @@ function query(module, opts, callback, username, password) {
   if (opts)
     queryurl += buildQuery(opts);
   
+  var authfunc;
+  if (username) {
+    authfunc = function(xhr) {
+      xhr.setRequestHeader("Authorization",
+        "Basic " + btoa(username + ":" + password));
+    };
+  }
+  
   $.ajax({
     url: queryurl,
     type: 'GET',
     dataType: 'json',
-    beforeSend: !username ? undefined : function (xhr) { 
-        xhr.setRequestHeader("Authorization", 
-            "Basic " + btoa(username + ":" + password)); 
-    }
+    beforeSend: authfunc
   }).done(callback)
     .fail(function(jqXHR) {
-      if (jqXHR.status === 400) {
-        callback($.parseJSON(jqXHR.responseText));
+      //Note:Callback must be called so the QUnit test can run.
+      if (jqXHR.status !== 400) {
+        callback({"status" : jqXHR.status, "description" : jqXHR.statusText});
       } else {
-        callback({status:-999, 
-          description: jqXHR.status.toString() + " " + jqXHR.responseText});
+        try {
+          callback($.parseJSON(jqXHR.responseText));
+        } catch (err) {
+          callback({"status" : jqXHR.status, "description" : jqXHR.statusText});
+        }
       }
     });
 }
 
-QUnit.test("API Existence", function () {
-  stop(); //?????
-  query("test", undefined, function(data) {
+
+QUnit.asyncTest("API Existence", function () {
+  query("test", function(data) {
+   start();
+   //TODO:Change fastcgi error codes
    equal(parseInt(data.status, 10), -1, "Nonexistent module"); //Magic numbers!
+  });
+});
+
+QUnit.asyncTest("Login test", function() {
+  query("login", {"force" : true}, "mctxadmin", "admin", function(data) {
    start();
-  });   
+   equal(parseInt(data.status, 10), 0, "Login ok"); //Magic numbers!
+  });
+});
+
+QUnit.test("Sensors module", function() {
+  
+});
+
+/*QUnit.test("Login module", function () {
+  
+});*/
 
-  /*query("version", undefined, function (data) {
-    assert.equal(data.status, 0);
-  });*/
+QUnit.test("Access control", function () {
   
 });
\ No newline at end of file

UCC git Repository :: git.ucc.asn.au