Switch back to HTTP status codes for fastcgi + update unit tests
[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  * 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
23  * @returns JSON data
24  */
25 function query(module, opts, username, password, callback) {
26   if (typeof opts === 'string') {
27     callback = password;
28     password = username;
29     username = opts;
30     opts = undefined;
31   } else if (typeof opts === 'function') {
32     callback = opts;
33     opts = undefined;
34   } else if (typeof username === 'function') {
35     callback = username;
36     username = undefined;
37   }
38   
39   function buildQuery(opts) {
40     var result = "?";
41     var first = true;
42     
43     for (key in opts) {
44       if (!first) 
45         result += "&";
46       else 
47         first = false;
48       result += encodeURIComponent(key) + 
49                 (opts.key ? "=" + encodeURIComponent(opts.key) : "");
50     }
51     return result;
52   }
53   
54   var queryurl = api + module;
55   if (opts)
56     queryurl += buildQuery(opts);
57   
58   var authfunc;
59   if (username) {
60     authfunc = function(xhr) {
61       xhr.setRequestHeader("Authorization",
62         "Basic " + btoa(username + ":" + password));
63     };
64   }
65   
66   $.ajax({
67     url: queryurl,
68     type: 'GET',
69     dataType: 'json',
70     beforeSend: authfunc
71   }).done(callback)
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});
76       } else {
77         try {
78           callback($.parseJSON(jqXHR.responseText));
79         } catch (err) {
80           callback({"status" : jqXHR.status, "description" : jqXHR.statusText});
81         }
82       }
83     });
84 }
85
86
87 QUnit.asyncTest("API Existence", function () {
88   query("test", function(data) {
89    start();
90    //TODO:Change fastcgi error codes
91    equal(parseInt(data.status, 10), -1, "Nonexistent module"); //Magic numbers!
92   });
93 });
94
95 QUnit.asyncTest("Login test", function() {
96   query("login", {"force" : true}, "mctxadmin", "admin", function(data) {
97    start();
98    equal(parseInt(data.status, 10), 0, "Login ok"); //Magic numbers!
99   });
100 });
101
102 QUnit.test("Sensors module", function() {
103   
104 });
105
106 /*QUnit.test("Login module", function () {
107   
108 });*/
109
110 QUnit.test("Access control", function () {
111   
112 });

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