2 * MCTX3420 2013 - Remote pressurised can experiment.
3 * Unit testing for the server API.
4 * These unit tests use the QUnit unit testing framework.
5 * @requires QUnit and jQuery
10 var api = location.protocol + "//" + location.host + "/api/";
13 * Sends an AJAX query to the API
14 * query(module, username, password, callback);
15 * query(module, callback);
16 * query(module, opts, callback);
17 * query(module, opts, username, password, callback);
18 * @param {string} module The name of the module to be queried
19 * @param {Object} opts Object containing parameters to pass to module
20 * @param {string} username Optional
21 * @param {string} password Required if username specified
22 * @param {function} callback Function that receives JSON data
25 function query(module, opts, username, password, callback) {
26 if (typeof opts === 'string') {
31 } else if (typeof opts === 'function') {
34 } else if (typeof username === 'function') {
39 function buildQuery(opts) {
48 result += encodeURIComponent(key) +
49 (opts.key ? "=" + encodeURIComponent(opts.key) : "");
54 var queryurl = api + module;
56 queryurl += buildQuery(opts);
60 authfunc = function(xhr) {
61 xhr.setRequestHeader("Authorization",
62 "Basic " + btoa(username + ":" + password));
72 .fail(function(jqXHR) {
73 //Note:Callback must be called so the QUnit test can run.
74 if (jqXHR.status !== 400) {
75 callback({"status" : jqXHR.status, "description" : jqXHR.statusText});
78 callback($.parseJSON(jqXHR.responseText));
80 callback({"status" : jqXHR.status, "description" : jqXHR.statusText});
87 QUnit.asyncTest("API Existence", function () {
88 query("test", function(data) {
90 //TODO:Change fastcgi error codes
91 equal(parseInt(data.status, 10), 400, "Nonexistent module"); //Magic numbers!
95 QUnit.asyncTest("Login test", function() {
96 query("login", {"force" : true}, "mctxadmin", "admin", function(data) {
98 equal(parseInt(data.status, 10), 200, "Login ok"); //Magic numbers!
102 QUnit.test("Sensors module", function() {
106 /*QUnit.test("Login module", function () {
110 QUnit.test("Access control", function () {