Commit 42bdbc93 authored by Thomas Lechauve's avatar Thomas Lechauve

Fix: keeping context in ajax request

Also modified all tests to use slapos plugin new structure
parent 674bb1bc
(function($) {
;(function($) {
var methods = {
defaults: {
host: ''
},
init: function( options ){
var settings = $.extend({
'host': ''
}, options);
return this.each(function(){
methods.config = $.extend({}, methods.defaults, methods.options, methods.metadata);
methods['store'] = Modernizr.localstorage ? methods.lStore : methods.cStore;
methods.store('host', methods.config.host);
$(this).slapos('store', 'host', settings.host);
});
},
......@@ -41,67 +44,68 @@
}
},
request: function(context, type, url, callback, statusEvent, data){
request: function(type, url, callback, statusEvent, data){
data = data || '';
statusEvent = statusEvent || this.statusDefault;
return this.each(function(){
$.ajax({
url: methods.store('host')+url,
dataType: 'json',
data: data,
context: context,
context: $(this),
type: type,
statusCode: statusEvent
}).done(callback);
statusCode: statusEvent,
success: callback
});
});
},
newInstance: function(data, callback, statusEvent){
return this.request('POST', '/request', callback, statusEvent, data);
return $(this).slapos('request', 'POST', '/request', callback, statusEvent, data);
},
deleteInstance: function(id, callback, statusEvent){
return this.request('DELETE', '/instance/'+id, callback, statusEvent);
return $(this).slapos('request', 'DELETE', '/instance/'+id, callback, statusEvent);
},
getInstance: function(id, callback, statusEvent){
return this.each(function(){
methods.request(this, 'GET', '/instance/'+id, callback, statusEvent);
});
return $(this).slapos('request', 'GET', '/instance/'+id, callback, statusEvent);
},
getInstanceCert: function(id, callback, statusEvent){
return this.request('GET', '/instance/'+id+'/certificate', callback, statusEvent);
return $(this).slapos('request', 'GET', '/instance/'+id+'/certificate', callback, statusEvent);
},
bangInstance: function(id, log, callback, statusEvent){
return this.request('POST', '/instance/'+id+'/bang', callback, statusEvent, log);
return $(this).slapos('request', 'POST', '/instance/'+id+'/bang', callback, statusEvent, log);
},
editInstance: function(id, data, callback, statusEvent){
return this.request('PUT', '/instance/'+id, callback, statusEvent, data);
return $(this).slapos('request', 'PUT', '/instance/'+id, callback, statusEvent, data);
},
newComputer: function(data, callback, statusEvent){
return this.request('POST', '/computer', callback, statusEvent, data);
return $(this).slapos('request', 'POST', '/computer', callback, statusEvent, data);
},
getComputer: function(id, callback, statusEvent){
return this.request('GET', '/computer/'+id, callback, statusEvent);
return $(this).slapos('request', 'GET', '/computer/'+id, callback, statusEvent);
},
editComputer: function(id, data, callback, statusEvent){
return this.request('PUT', '/computer/'+id, callback, statusEvent, data);
return $(this).slapos('request', 'PUT', '/computer/'+id, callback, statusEvent, data);
},
newSoftware: function(computerId, data, callback, statusEvent){
return this.request('POST', '/computer/'+computerId+'/supply', callback, statusEvent, data);
return $(this).slapos('request', 'POST', '/computer/'+computerId+'/supply', callback, statusEvent, data);
},
bangComputer: function(id, log, callback, statusEvent){
return this.request('POST', '/computer/'+id+'/bang', callback, statusEvent, log);
return $(this).slapos('request', 'POST', '/computer/'+id+'/bang', callback, statusEvent, log);
},
computerReport: function(id, data, callback, statusEvent){
return this.request('POST', '/computer/'+id+'/report', callback, statusEvent, data);
return $(this).slapos('request', 'POST', '/computer/'+id+'/report', callback, statusEvent, data);
}
};
......@@ -114,5 +118,4 @@
$.error( 'Method ' + method + ' does not exist on jQuery.slapos' );
}
};
})(jQuery);
\ No newline at end of file
$(function(){
var h = getParameterByName("login");
var slap = new SlapOs(document, {host: h}).init();
$(document).slapos();
module("Cross-domain Tests");
test("200 response", function(){
expect(1);
stop(1);
$.ajax({
url: 'http://192.168.242.92:5000/200',
url: 'http://sheldy.com:5000/200',
complete: function() { start(); },
statusCode: {
200: function(){ ok(true, "should get 200 status");},
200: function(){ ok(true, "should get 200 status");}
}});
});
......@@ -39,7 +38,7 @@ $(function(){
});
test("Requesting a new instance", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
url = "/request";
......@@ -47,59 +46,59 @@ $(function(){
response = [201, this.header, JSON.stringify(responseBody)];
this.server.respondWith("POST", url, response);
slap.newInstance('', callback);
$(document).slapos('newInstance', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+url);
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+url);
ok(callback.calledOnce, "callback should be called");
ok(callback.calledWith(responseBody), 'should return mainly id and status of an instance');
});
test("Requesting a new instance - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
url = "/request";
this.server.respondWith("POST", url, this.error);
slap.newInstance('', callback);
$(document).slapos('newInstance', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+url);
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+url);
ok(!callback.calledOnce, "callback should not be called");
});
test("Deleting an instance", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [202, this.header, ''];
this.server.respondWith("DELETE", /\/instance\/(\w+)/, response);
slap.deleteInstance('id', callback);
$(document).slapos('deleteInstance', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/instance/id');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/instance/id');
ok(callback.calledOnce, "callback should be called");
});
test("Deleting an instance - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("DELETE", /\/instance\/(\w+)/, this.error);
slap.deleteInstance('id', callback);
$(document).slapos('deleteInstance', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/instance/id');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/instance/id');
ok(!callback.calledOnce, "callback should not be called");
});
test("Get instance information", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
responseBody = [{instance_id: "anId", status: "start", software_release: "http://example.com/example.cfg",
......@@ -107,328 +106,327 @@ $(function(){
custom_connection_parameter_1: "foo",
custom_connection_parameter_2: "bar"},
parameter: {Custom1: "one string", Custom2: "one float",
Custom3: ["abc", "def"],},
sla: {computer_id: "COMP-0",},
Custom3: ["abc", "def"]},
sla: {computer_id: "COMP-0"},
children_id_list: ["subinstance1", "subinstance2"],
partition: {public_ip: ["::1", "91.121.63.94"], private_ip: ["127.0.0.1"],
tap_interface: "tap2",},}];
tap_interface: "tap2"}}];
response = [200, this.header, JSON.stringify(responseBody)];
this.server.respondWith("GET", /\/instance\/(\w+)/, response);
slap.getInstance('id', callback);
$(document).slapos('getInstance', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/instance/id');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/instance/id');
ok(callback.calledOnce, "callback should be call");
ok(callback.calledWith(responseBody), "should return informations of an instance");
});
test("Get instance information - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("GET", /\/instance\/(\w+)/, this.error);
slap.getInstance('id', callback);
$(document).slapos('getInstance', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/instance/id');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/instance/id');
ok(!callback.calledOnce, "callback should not be called");
});
test("Get instance authentication certificates", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
responseBody = [{ ssl_key: "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADAN...h2VSZRlSN\n-----END PRIVATE KEY-----",
ssl_certificate: "-----BEGIN CERTIFICATE-----\nMIIEAzCCAuugAwIBAgICHQI...ulYdXJabLOeCOA=\n-----END CERTIFICATE-----",}];
ssl_certificate: "-----BEGIN CERTIFICATE-----\nMIIEAzCCAuugAwIBAgICHQI...ulYdXJabLOeCOA=\n-----END CERTIFICATE-----"}];
response = [200, this.header, JSON.stringify(responseBody)];
this.server.respondWith("GET", /\/instance\/(\w+)\/certificate/, response);
slap.getInstanceCert('id', callback);
$(document).slapos('getInstanceCert', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/instance/id/certificate');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/instance/id/certificate');
ok(callback.calledOnce, "callback call");
ok(callback.calledWith(responseBody));
});
test("Get instance authentication certificates - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("GET", /\/instance\/(\w+)\/certificate/, this.error);
slap.getInstanceCert('id', callback);
$(document).slapos('getInstanceCert', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/instance/id/certificate');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/instance/id/certificate');
ok(!callback.calledOnce, "callback should not be called");
});
test("Bang instance", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("POST", /\/instance\/(\w+)\/bang/, response);
data = '';
slap.bangInstance('id', data, callback);
$(document).slapos('bangInstance', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/instance/id/bang');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/instance/id/bang');
ok(callback.calledOnce, "callback should be called");
});
test("Bang instance - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", /\/instance\/(\w+)\/bang/, this.error);
slap.bangInstance('id', data, callback);
$(document).slapos('bangInstance', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/instance/id/bang');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/instance/id/bang');
ok(!callback.calledOnce, "callback should not be called");
});
test("Modifying instance", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("PUT", /\/instance\/(\w+)/, response);
data = '';
slap.editInstance('id', data, callback);
$(document).slapos('editInstance', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/instance/id');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/instance/id');
ok(callback.calledOnce, "callback should be called");
});
test("Modifying instance - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("PUT", /\/instance\/(\w+)/, this.error);
slap.editInstance('id', '', callback);
$(document).slapos('editInstance', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/instance/id');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/instance/id');
ok(!callback.calledOnce, "callback should not be called");
});
test("Register a new computer", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
responseBody = [{computer_id: "COMP-0",
ssl_key: "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADAN...h2VSZRlSN\n-----END PRIVATE KEY-----",
ssl_certificate: "-----BEGIN CERTIFICATE-----\nMIIEAzCCAuugAwIBAgICHQI...ulYdXJabLOeCOA=\n-----END CERTIFICATE-----",}];
ssl_certificate: "-----BEGIN CERTIFICATE-----\nMIIEAzCCAuugAwIBAgICHQI...ulYdXJabLOeCOA=\n-----END CERTIFICATE-----"}];
response = [201, this.header, JSON.stringify(responseBody)];
this.server.respondWith("POST", "/computer", response);
slap.newComputer('', callback);
$(document).slapos('newComputer', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer');
ok(callback.calledOnce, "callback should be called");
ok(callback.calledWith(responseBody), "should return a computerID, ssl key and ssl certificates");
});
test("Register a new computer - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", "/computer", this.error);
slap.newComputer('', callback);
$(document).slapos('newComputer', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer');
ok(!callback.calledOnce, "callback should not be called");
});
test("Getting computer information", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
responseBody = [{computer_id: "COMP-0",
software: [{software_release: "http://example.com/example.cfg",
status: "install"},],
status: "install"}],
partition: [{title: "slapart1",instance_id: "foo",status: "start",
software_release: "http://example.com/example.cfg"},
{title: "slapart2",instance_id: "bar",status: "stop",
software_release: "http://example.com/example.cfg"},],}];
software_release: "http://example.com/example.cfg"}]}];
response = [200, this.header, JSON.stringify(responseBody)];
this.server.respondWith("GET", /\/computer\/(\w+)/, response);
slap.getComputer('id', callback);
$(document).slapos('getComputer', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer/id');
ok(callback.calledOnce, "callback should be called");
ok(callback.calledWith(responseBody), "should return informations of a computer");
});
test("Getting computer information - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("GET", /\/computer\/(\w+)/, this.error);
slap.getComputer('id', callback);
$(document).slapos('getComputer', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer/id');
ok(!callback.calledOnce, "callback should not be called");
});
test("Modifying computer", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("PUT", /\/computer\/(\w+)/, response);
slap.editComputer('id', '', callback);
$(document).slapos('editComputer', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer/id');
ok(callback.calledOnce, "callback should be called");
});
test("Modifying computer - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("PUT", /\/computer\/(\w+)/, this.error);
slap.editComputer('id', '', callback);
$(document).slapos('editComputer', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer/id');
ok(!callback.calledOnce, "callback should not be called");
});
test("Supplying new software", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("POST", /\/computer\/(\w+)\/supply/, response);
data = '';
slap.newSoftware('id', data, callback);
$(document).slapos('newSoftware', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id/supply');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer/id/supply');
ok(callback.calledOnce, "callback should be called");
});
test("Supplying new software - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", /\/computer\/(\w+)\/supply/, this.error);
slap.newSoftware('id', '', callback);
$(document).slapos('newSoftware', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id/supply');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer/id/supply');
ok(!callback.calledOnce, "callback should not be called");
});
test("Bang computer", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("POST", /\/computer\/(\w+)\/bang/, response);
data = '';
slap.bangComputer('id', data, callback);
$(document).slapos('bangComputer', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id/bang');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer/id/bang');
ok(callback.calledOnce, "callback should be called");
});
test("Bang computer - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", /\/computer\/(\w+)\/bang/, this.error);
slap.bangComputer('id', '', callback);
$(document).slapos('bangComputer', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id/bang');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer/id/bang');
ok(!callback.calledOnce, "callback should not be called");
});
test("Report computer usage", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("POST", /\/computer\/(\w+)\/report/, response);
data = '';
slap.computerReport('id', data, callback);
$(document).slapos('computerReport', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id/report');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer/id/report');
ok(callback.calledOnce, "callback call");
});
test("Report computer usage - Fail", function(){
callback = this.spy();
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", /\/computer\/(\w+)\/report/, this.error);
slap.computerReport('id', '', callback);
$(document).slapos('computerReport', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id/report');
equal(jQuery.ajax.getCall(0).args[0].url, $(document).slapos('store', 'host')+'/computer/id/report');
ok(!callback.calledOnce, "callback should not be called");
});
module("Common Tests");
test("Check if host has been saved", function(){
newS = new SlapOs(document, {host: "http://foo.com"}).init();
equal(newS.store('host'), "http://foo.com", "should contains host whatever is the method")
$(document).slapos({host: "http://foo.com"});
equal($(document).slapos('store', 'host'), "http://foo.com", "should contains host whatever is the method")
});
test("Modifying host after initialisation at start", function(){
newS = new SlapOs(document, {host: "http://foo.com"}).init();
newS.store('host', 'http://examples.com');
equal(newS.store('host'), "http://examples.com", "should contains modified host")
$(document).slapos('store', 'host', 'http://examples.com');
equal($(document).slapos('store', 'host'), "http://examples.com", "should contains modified host")
});
});
......
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