Begin unit tests
authorJeremy Tan <[email protected]>
Wed, 28 Aug 2013 09:35:02 +0000 (17:35 +0800)
committerJeremy Tan <[email protected]>
Wed, 28 Aug 2013 09:35:02 +0000 (17:35 +0800)
nginx-configs/README
testing/qunit/index.html [new file with mode: 0644]
testing/qunit/unit-tests.js [new file with mode: 0644]

index 1c6ade5..99499a5 100644 (file)
@@ -16,3 +16,8 @@ To get the login functionality working, you need to place a .htpasswd file
 under /usr/share/nginx/access (create folder if it doesn't exist). To generate
 the htpasswd file, install the apache2-utils package and use the 'htpasswd'
 executable.
+
+
+P.S: (I always forget these)
+Set file permissions to: 644
+Set folder permissions to: 755
\ No newline at end of file
diff --git a/testing/qunit/index.html b/testing/qunit/index.html
new file mode 100644 (file)
index 0000000..4f4dcfe
--- /dev/null
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html>
+       <head>
+       <meta charset="utf-8">
+               <title>MCTX3420 2013 Server API unit tests</title>
+               <link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
+       </head>
+<body>
+       <div id="qunit"></div>
+       <div id="qunit-fixture"></div>
+    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
+       <script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
+       <script src="unit-tests.js"></script>
+</body>
+</html>
\ No newline at end of file
diff --git a/testing/qunit/unit-tests.js b/testing/qunit/unit-tests.js
new file mode 100644 (file)
index 0000000..c16b8c9
--- /dev/null
@@ -0,0 +1,71 @@
+/**
+ * MCTX3420 2013 - Remote pressurised can experiment.
+ * Unit testing for the server API.
+ * These unit tests use the QUnit unit testing framework.
+ * @requires QUnit and jQuery
+ * @date 28/8/13
+ * @author Jeremy Tan
+ */
+
+var api = location.protocol + "//" +  location.host + "/api/";
+
+/**
+ * Sends an AJAX query to the API
+ * @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
+ * @returns JSON data
+ */
+function query(module, opts, callback, username, password) {
+  function buildQuery(opts) {
+    var result = "?";
+    var first = true;
+    
+    for (key in opts) {
+      if (!first) 
+        result += "&";
+      else 
+        first = false;
+      result += encodeURIComponent(key) + 
+                (opts.key ? "=" + encodeURIComponent(opts.key) : "");
+    }
+    return result;
+  }
+  
+  var queryurl = api + module;
+  if (opts)
+    queryurl += buildQuery(opts);
+  
+  $.ajax({
+    url: queryurl,
+    type: 'GET',
+    dataType: 'json',
+    beforeSend: !username ? undefined : function (xhr) { 
+        xhr.setRequestHeader("Authorization", 
+            "Basic " + btoa(username + ":" + password)); 
+    }
+  }).done(callback)
+    .fail(function(jqXHR) {
+      if (jqXHR.status === 400) {
+        callback($.parseJSON(jqXHR.responseText));
+      } else {
+        callback({status:-999, 
+          description: jqXHR.status.toString() + " " + jqXHR.responseText});
+      }
+    });
+}
+
+QUnit.test("API Existence", function () {
+  stop(); //?????
+  query("test", undefined, function(data) {
+   equal(parseInt(data.status, 10), -1, "Nonexistent module"); //Magic numbers!
+   start();
+  });   
+
+  /*query("version", undefined, function (data) {
+    assert.equal(data.status, 0);
+  });*/
+  
+});
\ No newline at end of file

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