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