Commit 6391a573 authored by Thomas Lechauve's avatar Thomas Lechauve

Initialisation of jQuery library for slapOs

This first version contains:

The library (static/js/slapOs.js):
	A jQuery plugin which permit to discuss with a slapOs master.
	All methods are listed here: <http://packages.python.org/slapos.core/rest.html>
library tests (static/js/test.js):
	For slapOs tests, we used others libraries:
		Qunit for units tests
		SinonJs to simulate server responses (no need a real server)
a Flask server (server.py):
	A real server to principally test cross-domain requests with ajax methods
and some html files to test all of it
parent 8eeb1724
from flask import Flask
from flask import render_template, abort, request, make_response
app = Flask(__name__)
@app.route('/')
def index():
return "index"
@app.route('/test-mobile')
def test():
return render_template('test-mobile.html')
@app.route('/request', methods=["POST", "GET"])
def request():
response = make_response("HELLO", 409)
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = '*'
return response
if __name__ == '__main__':
app.run(debug=True)
\ No newline at end of file
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="static/css/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="static/js/jquery-1.7.2.js"></script>
<script type="text/javascript" src="static/js/qunit.js"></script>
<script type="text/javascript" src="static/js/sinon-1.3.2.js"></script>
<script type="text/javascript" src="static/js/sinon-qunit-1.0.0.js"></script>
<script type="text/javascript" src="static/js/slapOs.js"></script>
<script type="text/javascript" src="static/js/test.js"></script>
<title>Test Slapos</title>
</head>
<body>
<div class="container">
<h1 id="qunit-header">QUnit SlapOs tests</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</div>
</body>
\ No newline at end of file
(function(window, $) {
var SlapOs = function(elem, options){
this.elem = elem;
this.$elem = $(elem);
this.options = options;
this.metadata = this.$elem.data('plugin-options');
};
SlapOs.prototype = {
host: '',
init: function(){
this.config = $.extends({}, this.defaults, this.options, this.metadata);
return this;
},
request: function(type, url, callback, data){
data = data || '';
return $.ajax({
url: this.host+url,
dataType: 'json',
data: data,
type: type,
statusCode: {
409: function(){console.log('Status Code : 409')},
},
success: function(data){ callback(data); }
});
},
newInstance: function(data, callback){
this.request('POST', '/request', callback, data);
},
deleteInstance: function(id, callback){
this.request('DELETE', '/instance/'+id, callback);
},
getInstance: function(id, callback){
this.request('GET', '/instance/'+id, callback);
},
getInstanceCert: function(id, callback){
this.request('GET', '/instance/'+id+'/certificate', callback);
},
bangInstance: function(id, log, callback){
this.request('POST', '/instance/'+id+'/bang', callback, log);
},
editInstance: function(id, data, callback){
this.request('PUT', '/instance/'+id, callback, data);
},
newComputer: function(data, callback){
this.request('POST', '/computer', callback, data);
},
getComputer: function(id, callback){
this.request('GET', '/computer/'+id, callback, data);
},
editComputer: function(id, data, callback){
this.request('PUT', '/computer/'+id, callback, data);
},
newSoftware: function(computerId, data, callback){
this.request('POST', '/computer/'+computerId+'/supply', callback, data);
},
bangComputer: function(id, log, callback){
this.request('POST', '/computer/'+id+'/bang', callback, log);
},
computerReport: function(id, data, callback){
this.request('POST', '/computer/'+id+'/report', callback, data);
}
};
$.fn.slapos = function(options){
return this.each(function(){
new SlapOs(this, options).init();
});
};
window.SlapOs = SlapOs;
})(window, jQuery);
\ No newline at end of file
$(function(){
module("Instance & Computer Methods Tests", {
setup: function(){
this.server = sinon.sandbox.useFakeServer();
this.header = {"Content-Type":"application/json; charset=utf-8"};
this.error = [409, this.header, '']
this.slap = new SlapOs();
},
tearDown: function(){
this.server.restore();
}
});
test("Requesting a new instance - Success Response", function(){
expect(2);
callback = this.spy();
responseBody = [{instance_id: "anId",status: "started",connection: {}}];
response = [201, this.header, JSON.stringify(responseBody)];
this.server.respondWith("POST", "/request", response);
data = '{"title": "My unique instance","software_release": "http://example.com/example.cfg","software_type": "type_provided_by_the_software","slave": False,"status": "started","sla": {"computer_id": "COMP-0"}';
this.slap.newInstance(data, callback);
this.server.respond();
ok(callback.calledOnce, "callback call");
ok(callback.calledWith(responseBody), 'callback check right parameters');
});
test("Requesting a new instance - Fail Response", function(){
expect(1);
callback = this.spy();
this.server.respondWith("POST", "/request", this.error);
data = '{"title": "My unique instance","software_release": "http://example.com/example.cfg","software_type": "type_provided_by_the_software","slave": False,"status": "started","sla": {"computer_id": "COMP-0"}';
this.slap.newInstance(data, callback);
this.server.respond();
ok(!callback.calledOnce, "callback not call");
});
test("Deleting an instance", function(){
expect(1);
callback = this.spy();
response = [202, this.header, ''];
this.server.respondWith("DELETE", /\/instance\/(\w+)/, response);
this.slap.deleteInstance('id', callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
test("Get instance information", function(){
expect(1);
callback = this.spy();
response = [200, this.header, ''];
this.server.respondWith("GET", /\/instance\/(\w+)/, response);
this.slap.getInstance('id', callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
test("Get instance authentication certificates", function(){
expect(1);
callback = this.spy();
response = [200, this.header, ''];
this.server.respondWith("GET", /\/instance\/(\w+)\/certificate/, response);
this.slap.getInstanceCert('id', callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
test("Bang instance", function(){
expect(1);
callback = this.spy();
response = [200, this.header, ''];
this.server.respondWith("GET", /\/instance\/(\w+)\/bang/, response);
data = '';
this.slap.bangInstance('id', data, callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
test("Modifying instance", function(){
expect(1);
callback = this.spy();
response = [200, this.header, ''];
this.server.respondWith("PUT", /\/instance\/(\w+)/, response);
data = '';
this.slap.editInstance('id', data, callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
test("Register a new computer", function(){
expect(1);
callback = this.spy();
response = [201, this.header, ''];
this.server.respondWith("POST", /\/computer/, response);
data = '';
this.slap.newComputer(data, callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
test("Getting computer information", function(){
expect(1);
callback = this.spy();
response = [200, this.header, ''];
this.server.respondWith("GET", /\/computer\/(\w+)/, response);
data = '';
this.slap.getComputer('id', callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
test("Modifying computer", function(){
expect(1);
callback = this.spy();
response = [200, this.header, ''];
this.server.respondWith("PUT", /\/computer\/(\w+)/, response);
data = '';
this.slap.editComputer('id', data, callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
test("Supplying new software", function(){
expect(1);
callback = this.spy();
response = [200, this.header, ''];
this.server.respondWith("POST", /\/computer\/(\w+)\/supply/, response);
data = '';
this.slap.newSoftware('computerId', data, callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
test("Bang computer", function(){
expect(1);
callback = this.spy();
response = [200, this.header, ''];
this.server.respondWith("POST", /\/computer\/(\w+)\/bang/, response);
data = '';
this.slap.bangComputer('id', data, callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
test("Report computer usage", function(){
expect(1);
callback = this.spy();
response = [200, this.header, ''];
this.server.respondWith("POST", /\/computer\/(\w+)\/report/, response);
data = '';
this.slap.newComputer('id', data, callback);
this.server.respond();
equal(1, this.server.requests.length, 'A request has been sent');
});
});
\ No newline at end of file
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
<script type="text/javascript" src="{{url_for('static', filename='js/slapOs.js')}}"></script>
<title>Test Slapos</title>
</head>
<body>
<script type="text/javascript">
$.mobile.allowCrossDomainPages = true;
$.mobile.pushStateEnabled = false;
var s = new SlapOs();
s.request('GET', 'http://127.0.0.1:5000/request', null);
</script>
</body>
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment