X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=testing%2Fqunit%2Funit-tests.js;h=2bab3f835d0c37a6a6d742c8052cebd22bfc925f;hb=459042e32b6ffb31338e3f5a88ddb401530d1beb;hp=c16b8c916a8676ed17b29ad01ec5766ba3fa5d53;hpb=acfa0e6c1ccbbaa00025b209272c46b386588d48;p=matches%2FMCTX3420.git diff --git a/testing/qunit/unit-tests.js b/testing/qunit/unit-tests.js index c16b8c9..2bab3f8 100644 --- a/testing/qunit/unit-tests.js +++ b/testing/qunit/unit-tests.js @@ -12,13 +12,12 @@ 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 + * @param {Object} opts Object holding the parameters, username, password and + * callback. The parameters should be an object of key/value + * pairs. * @returns JSON data */ -function query(module, opts, callback, username, password) { +function query(module, opts) { function buildQuery(opts) { var result = "?"; var first = true; @@ -29,43 +28,90 @@ function query(module, opts, callback, username, password) { else first = false; result += encodeURIComponent(key) + - (opts.key ? "=" + encodeURIComponent(opts.key) : ""); + ((opts[key] !== undefined) ? "=" + encodeURIComponent(opts[key]) : ""); } return result; } var queryurl = api + module; - if (opts) - queryurl += buildQuery(opts); + if (opts.params) + queryurl += buildQuery(opts.params); + + var authfunc; + if (opts.username) { + authfunc = function(xhr) { + xhr.setRequestHeader("Authorization", + "Basic " + btoa(opts.username + ":" + opts.password)); + }; + } $.ajax({ url: queryurl, type: 'GET', dataType: 'json', - beforeSend: !username ? undefined : function (xhr) { - xhr.setRequestHeader("Authorization", - "Basic " + btoa(username + ":" + password)); - } - }).done(callback) + beforeSend: authfunc + }).done(opts.callback) .fail(function(jqXHR) { - if (jqXHR.status === 400) { - callback($.parseJSON(jqXHR.responseText)); - } else { - callback({status:-999, - description: jqXHR.status.toString() + " " + jqXHR.responseText}); - } + alert("Request Failed!"); + ok(false, "Request failed: " + jqXHR.status.toString() + " " + jqXHR.statusText); + opts.callback(null); }); } -QUnit.test("API Existence", function () { - stop(); //????? - query("test", undefined, function(data) { - equal(parseInt(data.status, 10), -1, "Nonexistent module"); //Magic numbers! +QUnit.module("API basics"); +QUnit.asyncTest("Existence (identify)", function () { + query("identify", {callback : function(data) { start(); - }); + ok(data.status >= 0, "Return status"); + ok(data.description, data.description); + ok(data.build_date, data.build_date); + }}); +}); - /*query("version", undefined, function (data) { - assert.equal(data.status, 0); - });*/ - -}); \ No newline at end of file +QUnit.asyncTest("Invalid module", function () { + query("dontexist", {callback : function(data) { + start(); + ok(data.status < 0); + }}); +}); + +QUnit.module("Sensors"); +QUnit.asyncTest("Existence", function() { + query("sensors", {params : {id : 0}, callback : function(data) { + start(); + ok(data.status >= 0, "Return status"); + ok(data.data !== undefined, "Data field existence"); + var result = "Data: "; + for (var i = 0; i < data.data.length; i++) { + result += data.data[i][0] + ":" + data.data[i][1] + ", "; + } + ok(true, result); + }}); +}); + +QUnit.asyncTest("Invalid sensor id 1", function() { + query("sensors", {params : {id : 999}, callback : function(data) { + start(); + ok(data.status < 0, "Return status"); + }}); +}); + +QUnit.asyncTest("Invalid sensor id 2", function() { + query("sensors", {params : {id : ""}, callback : function(data) { + start(); + ok(data.status < 0, "Return status"); + }}); +}); + +QUnit.module("Controls and access"); +QUnit.asyncTest("Gaining access", function() { + query("control", {params : {action : "start", force : true}, + username : "mctxadmin", password : "admin", + callback : function(data) { + start(); + ok(data.status >= 0, "Return status"); + + var key = data.key; + + }}); +});