From: Jeremy Tan Date: Wed, 28 Aug 2013 09:35:02 +0000 (+0800) Subject: Begin unit tests X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=acfa0e6c1ccbbaa00025b209272c46b386588d48;p=matches%2FMCTX3420.git Begin unit tests --- diff --git a/nginx-configs/README b/nginx-configs/README index 1c6ade5..99499a5 100644 --- a/nginx-configs/README +++ b/nginx-configs/README @@ -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 index 0000000..4f4dcfe --- /dev/null +++ b/testing/qunit/index.html @@ -0,0 +1,15 @@ + + + + + MCTX3420 2013 Server API unit tests + + + +
+
+ + + + + \ No newline at end of file diff --git a/testing/qunit/unit-tests.js b/testing/qunit/unit-tests.js new file mode 100644 index 0000000..c16b8c9 --- /dev/null +++ b/testing/qunit/unit-tests.js @@ -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