2bab3f835d0c37a6a6d742c8052cebd22bfc925f
[matches/MCTX3420.git] / testing / qunit / unit-tests.js
1 /**
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
6  * @date 28/8/13
7  * @author Jeremy Tan
8  */
9
10 var api = location.protocol + "//" +  location.host + "/api/";
11
12 /**
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
17  *                 pairs.
18  * @returns JSON data
19  */
20 function query(module, opts) {
21   function buildQuery(opts) {
22     var result = "?";
23     var first = true;
24     
25     for (key in opts) {
26       if (!first) 
27         result += "&";
28       else 
29         first = false;
30       result += encodeURIComponent(key) + 
31                 ((opts[key] !== undefined) ? "=" + encodeURIComponent(opts[key]) : "");
32     }
33     return result;
34   }
35   
36   var queryurl = api + module;
37   if (opts.params)
38     queryurl += buildQuery(opts.params);
39   
40   var authfunc;
41   if (opts.username) {
42     authfunc = function(xhr) {
43       xhr.setRequestHeader("Authorization",
44         "Basic " + btoa(opts.username + ":" + opts.password));
45     };
46   }
47   
48   $.ajax({
49     url: queryurl,
50     type: 'GET',
51     dataType: 'json',
52     beforeSend: authfunc
53   }).done(opts.callback)
54     .fail(function(jqXHR) {
55       alert("Request Failed!");
56       ok(false, "Request failed: " + jqXHR.status.toString() + " " + jqXHR.statusText);
57       opts.callback(null);
58     });
59 }
60
61 QUnit.module("API basics");
62 QUnit.asyncTest("Existence (identify)", function () {
63   query("identify", {callback : function(data) {
64    start();
65    ok(data.status >= 0, "Return status");
66    ok(data.description, data.description);
67    ok(data.build_date, data.build_date);
68   }});
69 });
70
71 QUnit.asyncTest("Invalid module", function () {
72   query("dontexist", {callback : function(data) {
73    start();
74    ok(data.status < 0);
75   }});
76 });
77
78 QUnit.module("Sensors");
79 QUnit.asyncTest("Existence", function() {
80   query("sensors", {params : {id : 0}, callback : function(data) {
81    start();
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] + ", ";
87    }
88    ok(true, result);
89   }});  
90 });
91
92 QUnit.asyncTest("Invalid sensor id 1", function() {
93   query("sensors", {params : {id : 999}, callback : function(data) {
94    start();
95    ok(data.status < 0, "Return status");
96   }});  
97 });
98
99 QUnit.asyncTest("Invalid sensor id 2", function() {
100   query("sensors", {params : {id : ""}, callback : function(data) {
101    start();
102    ok(data.status < 0, "Return status");
103   }});  
104 });
105
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) {
111    start();
112    ok(data.status >= 0, "Return status");
113    
114    var key = data.key;
115    
116   }});
117 });

UCC git Repository :: git.ucc.asn.au