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, jQuery, and base64.js
13 ut.api = location.protocol + "//" + location.host + "/api/";
14 ut.controlcb = $.Deferred();
17 * Sends a synchronous AJAX query to the API
18 * A synchronous request makes it easier for unit testing.
19 * @param {string} module The name of the module to be queried
20 * @param {Object} opts Object holding the parameters, username, password and
21 * callback. The parameters should be an object of key/value
23 * @returns jqXHR object (but calls callback with JSON data, or null on AJAX error)
25 function query(module, opts) {
26 var queryurl = ut.api + module;
30 authfunc = function(xhr) {
31 xhr.setRequestHeader("Authorization",
32 "Basic " + base64.encode(opts.username + ":" + opts.password));
43 }).done(opts.callback)
44 .fail(function(jqXHR) {
45 ok(false, "Request failed: " + jqXHR.status.toString() + " " + jqXHR.statusText);
50 QUnit.module("API basics");
51 QUnit.test("API Existence (identify)", function () {
52 query("identify", {params : {actuators : true, sensors : true},
53 callback : function(data) {
54 ok(data.status > 0, "Return status");
55 ok(data.description !== undefined, data.description);
56 ok(data.build_date !== undefined, data.build_date);
57 ok(data.api_version !== undefined, "API version: " + data.api_version);
58 ok(data.sensors !== undefined, "Sensors list");
59 ok(data.actuators !== undefined, "Actuators list");
61 var sl = "Sensors: ", al = "Actuators: ";
62 for (var id in data.sensors) {
63 sl += id + ":" + data.sensors[id] + " ";
65 for (var id in data.actuators) {
66 al += id + ":" + data.actuators[id] + " ";
73 QUnit.test("Invalid module", function () {
74 query("dontexist", {callback : function(data) {
79 QUnit.module("Sensors");
80 QUnit.test("Existence", function() {
81 query("identify", {params : {sensors : 1}, callback : function(data) {
82 ok(data.status > 0, "Identification");
83 var hasSensor = false;
84 for (var id in data.sensors) {
86 query("sensors", {params : {id : id}, callback : function(data) {
87 ok(data.status > 0, "Sensor " + id);
88 ok(data.data !== undefined, "Data field existence");
89 var result = "Data: ";
90 for (var i = 0; i < data.data.length; i++) {
91 result += data.data[i][0] + ":" + data.data[i][1] + ", ";
96 ok(hasSensor, "Has at least one sensor");
101 QUnit.test("Invalid sensor ids", function() {
102 query("sensors", {params : {id : ""}, callback : function(data) {
103 ok(data.status < 0, "No id");
105 query("sensors", {params : {id : 999}, callback : function(data) {
106 ok(data.status < 0, "Id too large");
108 query("sensors", {params : {id : "-1"}, callback : function(data) {
109 ok(data.status < 0, "Negative id");
113 QUnit.module("Controls and access");
114 QUnit.asyncTest("Setting actuator value", function () {
115 $.when(ut.controlcb).done(function () {
119 query("control", {params : {action : "start", force : true},
120 username : $("#username").val(), password : $("#password").val(),
122 callback : function(data) {
123 ok(data.status > 0, "Gaining access key");
124 ok(data.key, "Access key - " + data.key);
127 query("control", {params : {action : "set", id : 0,
128 username : $("#username").val(), password : $("#password").val(),
129 value : 200, key : key},
130 callback : function(data) {
131 ok(data.status > 0, "Setting actuator");
132 ok(true, data.description);
137 $(document).ready(function(){
138 $("#control").submit(function () {
139 ut.controlcb.resolve();