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 * @param {string} module The name of the module to be queried
15 * @param {Object} opts Object holding the parameters, username, password and
16 * callback. The parameters should be an object of key/value
20 function query(module, opts) {
21 function buildQuery(opts) {
30 result += encodeURIComponent(key) +
31 ((opts[key] !== undefined) ? "=" + encodeURIComponent(opts[key]) : "");
36 var queryurl = api + module;
38 queryurl += buildQuery(opts.params);
42 authfunc = function(xhr) {
43 xhr.setRequestHeader("Authorization",
44 "Basic " + btoa(opts.username + ":" + opts.password));
53 }).done(opts.callback)
54 .fail(function(jqXHR) {
55 alert("Request Failed!");
56 ok(false, "Request failed: " + jqXHR.status.toString() + " " + jqXHR.statusText);
61 QUnit.module("API basics");
62 QUnit.asyncTest("Existence (identify)", function () {
63 query("identify", {callback : function(data) {
65 ok(data.status >= 0, "Return status");
66 ok(data.description, data.description);
67 ok(data.build_date, data.build_date);
71 QUnit.asyncTest("Invalid module", function () {
72 query("dontexist", {callback : function(data) {
78 QUnit.module("Sensors");
79 QUnit.asyncTest("Existence", function() {
80 query("sensors", {params : {id : 0}, callback : function(data) {
82 ok(data.status >= 0, "Return status");
83 ok(data.data !== undefined, "Data field existence");
84 var result = "Data: ";
85 for (var i = 0; i < data.data.length; i++) {
86 result += data.data[i][0] + ":" + data.data[i][1] + ", ";
92 QUnit.asyncTest("Invalid sensor id 1", function() {
93 query("sensors", {params : {id : 999}, callback : function(data) {
95 ok(data.status < 0, "Return status");
99 QUnit.asyncTest("Invalid sensor id 2", function() {
100 query("sensors", {params : {id : ""}, callback : function(data) {
102 ok(data.status < 0, "Return status");
106 QUnit.module("Controls and access");
107 QUnit.asyncTest("Gaining access", function() {
108 query("control", {params : {action : "start", force : true},
109 username : "mctxadmin", password : "admin",
110 callback : function(data) {
112 ok(data.status >= 0, "Return status");