Merge branch 'master' of https://github.com/szmoore/MCTX3420.git
authorJeremy Tan <[email protected]>
Thu, 29 Aug 2013 01:35:29 +0000 (09:35 +0800)
committerJeremy Tan <[email protected]>
Thu, 29 Aug 2013 01:35:29 +0000 (09:35 +0800)
Conflicts:
irc/log

nginx-configs/README
nginx-configs/sites-enabled/mctxconfig
server/fastcgi.h
testing/qunit/index.html [new file with mode: 0644]
testing/qunit/unit-tests.js [new file with mode: 0644]

index 1c6ade5..99499a5 100644 (file)
@@ -16,3 +16,8 @@ To get the login functionality working, you need to place a .htpasswd file
 under /usr/share/nginx/access (create folder if it doesn't exist). To generate
 the htpasswd file, install the apache2-utils package and use the 'htpasswd'
 executable.
+
+
+P.S: (I always forget these)
+Set file permissions to: 644
+Set folder permissions to: 755
\ No newline at end of file
index 541f6d1..dfdf2b9 100644 (file)
@@ -70,18 +70,17 @@ server {
                deny all;
        }
        
-       #Login area
-       location ^~ /api/login {
-               auth_basic "Restricted Access";
-               auth_basic_user_file /usr/share/nginx/access/.htpasswd;
-       
-               fastcgi_pass 127.0.0.1:9005;
-               fastcgi_param DOCUMENT_URI_LOCAL login;
-               include fastcgi_params;
-       }
-       
        #MCTX API
        location /api {
+               #Login area
+               location ^~ /api/login {
+                       auth_basic "Restricted Access";
+                       auth_basic_user_file /usr/share/nginx/access/.htpasswd;
+       
+                       fastcgi_pass 127.0.0.1:9005;
+                       fastcgi_param DOCUMENT_URI_LOCAL login;
+                       include fastcgi_params;
+               }
                location ~ ^/api/?([^?]*) {
                        fastcgi_pass 127.0.0.1:9005;
                        fastcgi_param DOCUMENT_URI_LOCAL $1;
index 1efdef7..3030b75 100644 (file)
@@ -6,11 +6,11 @@
 #ifndef _FASTCGI_H
 #define _FASTCGI_H
  
-/**Status codes that fcgi module handlers can return**/
+/**(HTTP) Status codes that fcgi module handlers can return**/
 typedef enum StatusCodes {
-       STATUS_OK = 0,
-       STATUS_ERROR = -1,
-       STATUS_UNAUTHORIZED = -2
+       STATUS_OK = 200,
+       STATUS_ERROR = 400,
+       STATUS_UNAUTHORIZED = 401
 } StatusCodes;
 
 typedef struct FCGIContext FCGIContext;
diff --git a/testing/qunit/index.html b/testing/qunit/index.html
new file mode 100644 (file)
index 0000000..e666fce
--- /dev/null
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html>
+       <head>
+       <meta charset="utf-8">
+               <title>MCTX3420 2013 Server API unit tests</title>
+               <link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
+        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
+        <script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
+    </head>
+<body>
+       <div id="qunit"></div>
+       <div id="qunit-fixture"></div>
+       <script src="unit-tests.js"></script>
+</body>
+</html>
\ No newline at end of file
diff --git a/testing/qunit/unit-tests.js b/testing/qunit/unit-tests.js
new file mode 100644 (file)
index 0000000..8aec28f
--- /dev/null
@@ -0,0 +1,112 @@
+/**
+ * MCTX3420 2013 - Remote pressurised can experiment.
+ * Unit testing for the server API.
+ * These unit tests use the QUnit unit testing framework.
+ * @requires QUnit and jQuery
+ * @date 28/8/13
+ * @author Jeremy Tan
+ */
+
+var api = location.protocol + "//" +  location.host + "/api/";
+
+/**
+ * Sends an AJAX query to the API
+ * query(module, username, password, callback);
+ * query(module, callback);
+ * query(module, opts, callback);
+ * query(module, opts, username, password, callback);
+ * @param {string} module The name of the module to be queried
+ * @param {Object} opts Object containing parameters to pass to module 
+ * @param {string} username Optional
+ * @param {string} password Required if username specified
+ * @param {function} callback Function that receives JSON data
+ * @returns JSON data
+ */
+function query(module, opts, username, password, callback) {
+  if (typeof opts === 'string') {
+    callback = password;
+    password = username;
+    username = opts;
+    opts = undefined;
+  } else if (typeof opts === 'function') {
+    callback = opts;
+    opts = undefined;
+  } else if (typeof username === 'function') {
+    callback = username;
+    username = undefined;
+  }
+  
+  function buildQuery(opts) {
+    var result = "?";
+    var first = true;
+    
+    for (key in opts) {
+      if (!first) 
+        result += "&";
+      else 
+        first = false;
+      result += encodeURIComponent(key) + 
+                (opts.key ? "=" + encodeURIComponent(opts.key) : "");
+    }
+    return result;
+  }
+  
+  var queryurl = api + module;
+  if (opts)
+    queryurl += buildQuery(opts);
+  
+  var authfunc;
+  if (username) {
+    authfunc = function(xhr) {
+      xhr.setRequestHeader("Authorization",
+        "Basic " + btoa(username + ":" + password));
+    };
+  }
+  
+  $.ajax({
+    url: queryurl,
+    type: 'GET',
+    dataType: 'json',
+    beforeSend: authfunc
+  }).done(callback)
+    .fail(function(jqXHR) {
+      //Note:Callback must be called so the QUnit test can run.
+      if (jqXHR.status !== 400) {
+        callback({"status" : jqXHR.status, "description" : jqXHR.statusText});
+      } else {
+        try {
+          callback($.parseJSON(jqXHR.responseText));
+        } catch (err) {
+          callback({"status" : jqXHR.status, "description" : jqXHR.statusText});
+        }
+      }
+    });
+}
+
+
+QUnit.asyncTest("API Existence", function () {
+  query("test", function(data) {
+   start();
+   //TODO:Change fastcgi error codes
+   equal(parseInt(data.status, 10), 400, "Nonexistent module"); //Magic numbers!
+  });
+});
+
+QUnit.asyncTest("Login test", function() {
+  query("login", {"force" : true}, "mctxadmin", "admin", function(data) {
+   start();
+   equal(parseInt(data.status, 10), 200, "Login ok"); //Magic numbers!
+  });
+});
+
+QUnit.test("Sensors module", function() {
+  
+});
+
+/*QUnit.test("Login module", function () {
+  
+});*/
+
+QUnit.test("Access control", function () {
+  
+});
\ No newline at end of file

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