Commit 0d0c819a authored by Thomas Lechauve's avatar Thomas Lechauve

Adapt unit tests to the new library

parent 772b9400
...@@ -82,11 +82,10 @@ ...@@ -82,11 +82,10 @@
datatype: 'json', datatype: 'json',
context: $(this), context: $(this),
beforeSend: function (xhr) { beforeSend: function (xhr) {
xhr.setRequestHeader('REMOTE_USER', 'test_vifib_customer'); //xhr.setRequestHeader('REMOTE_USER', 'test_vifib_customer');
xhr.setRequestHeader('Accept', 'application/json'); xhr.setRequestHeader('Accept', 'application/json');
if ($(this).slapos('access_token') && authentication) { if ($(this).slapos('access_token') && authentication) {
//xhr.setRequestHeader('Authorization', $(this).slapos('store', 'token_type') + ' ' + $(this).slapos('access_token')); xhr.setRequestHeader('Authorization', $(this).slapos('store', 'token_type') + ' ' + $(this).slapos('access_token'));
//xhr.setRequestHeader('Accept', 'application/json');
} }
} }
}; };
...@@ -120,7 +119,7 @@ ...@@ -120,7 +119,7 @@
beforeSend: function (xhr) { beforeSend: function (xhr) {
xhr.setRequestHeader('Accept', 'application/json'); xhr.setRequestHeader('Accept', 'application/json');
}, },
success: callback success: callback,
}); });
}); });
}, },
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<!-- Load local SinonJs --> <!-- Load local SinonJs -->
<script type="text/javascript" src="../libs/sinon/sinon-1.3.2.js"></script> <script type="text/javascript" src="../libs/sinon/sinon-1.3.2.js"></script>
<script type="text/javascript" src="../libs/sinon/sinon-qunit-1.0.0.js"></script> <script type="text/javascript" src="../libs/sinon/sinon-qunit-1.0.0.js"></script>
<script type="text/javascript" src="../libs/sinon/sinon-server-1.3.2.js"></script> <!--<script type="text/javascript" src="../libs/sinon/sinon-server-1.3.2.js"></script>-->
<!-- Load local lib and tests. --> <!-- Load local lib and tests. -->
<script src="../src/jquery.slapos.js"></script> <script src="../src/jquery.slapos.js"></script>
<script src="jquery.slapos_test.js"></script> <script src="jquery.slapos_test.js"></script>
......
jQuery(function(){ jQuery(function(){
var response, responseBody, url, data; var response, responseBody, url, data;
jQuery(document).slapos(); jQuery(document).slapos();
module("Cross-domain Tests"); module("Cross-domain Tests");
test("200 response", function(){ test("200 response", function(){
expect(1); expect(1);
stop(1); stop(1);
jQuery.ajax({ jQuery.ajax({
url: 'http://sheldy.com: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");}
}}); }
}); });
});
test("404 response", function(){
expect(1); test("404 response", function(){
stop(1); expect(1);
jQuery.ajax({ stop(1);
url: 'http://sheldy.com:5000/request', jQuery.ajax({
complete: function() { start(); }, url: 'http://sheldy.com:5000/request',
statusCode: { complete: function() { start(); },
404: function(xhr){ ok(true, "should get 404 error status status="+xhr.status); }, statusCode: {
0: function(){ ok(false, "should get 404 not but receive 0"); } 404: function(xhr){
}}); ok(true, "should get 404 error status status="+xhr.status);
}); },
0: function(){
module("Local Ajax Tests", { ok(false, "should get 404 not but receive 0");
setup: function(){ }
this.server = sinon.sandbox.useFakeServer(); }});
this.header = {"Content-Type":"application/json; charset=utf-8"}; });
this.error = [409, this.header, 'ERROR'];
}, module("Callback Tests", {
teardown: function(){ setup: function(){
this.server.restore(); this.server = sinon.sandbox.useFakeServer();
} this.header = {"Content-Type":"application/json"};
});
// Discovery response
test("Requesting a new instance", function(){ var discoBody = {
var callback = this.spy(); "request_instance": {
this.spy(jQuery, 'ajax'); "authentication": true,
"url": "/request_instance",
url = "/request"; "method": "POST",
responseBody = [{instance_id: "anId",status: "started",connection: {}}]; "required": {
response = [201, this.header, JSON.stringify(responseBody)]; "status": "unicode",
this.server.respondWith("POST", url, response); "slave": "bool",
"title": "unicode",
jQuery(document).slapos('newInstance', '', callback); "software_release": "unicode",
this.server.respond(); "software_type": "unicode",
"parameter": "object",
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+url); "sla": "object"
ok(callback.calledOnce, "callback should be called"); },
ok(callback.calledWith(responseBody), 'should return mainly id and status of an instance'); "optional": {}
}); }
};
test("Requesting a new instance - Fail", function(){ var discoResponse = [200, this.header, JSON.stringify(discoBody)];
var callback = this.spy(); this.server.respondWith("GET", 'http://10.8.2.34:12006/erp5/portal_vifib_rest_api_v1', discoResponse);
this.spy(jQuery, 'ajax');
// Error responses
url = "/request"; this.bad_request = [400, this.header, 'Bad Request'];
this.server.respondWith("POST", url, this.error); this.unauthorized = [401, this.header, 'Unauthorized'];
this.payment = [402, this.header, 'Payment required'];
jQuery(document).slapos('newInstance', '', callback); this.not_found = [404, this.header, 'Not found'];
this.server.respond(); this.server_error = [500, this.header, 'Internal server error'];
},
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+url); teardown: function(){
ok(!callback.calledOnce, "callback should not be called"); this.server.restore();
}); }
});
test("Deleting an instance", function(){
var callback = this.spy(); test("Bad request", function(){
this.spy(jQuery, 'ajax'); expect(1);
response = [202, this.header, '']; this.server.respondWith("POST", "/request_instance", this.bad_request);
this.server.respondWith("DELETE", /\/instance\/(\w+)/, response); var callback = function(){
ok(true, "should use 400 callback");
jQuery(document).slapos('deleteInstance', 'id', callback); },
this.server.respond(); statusCode = {
400: callback
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/instance/id'); };
ok(callback.calledOnce, "callback should be called"); jQuery(document).slapos('instanceRequest', {
}); url: "/request_instance",
statusCode: statusCode,
test("Deleting an instance - Fail", function(){ });
this.server.respond();
var callback = this.spy(); });
this.spy(jQuery, 'ajax');
test("Unauthorized", function(){
this.server.respondWith("DELETE", /\/instance\/(\w+)/, this.error); expect(1);
jQuery(document).slapos('deleteInstance', 'id', callback); this.server.respondWith("POST", "/request_instance", this.unauthorized);
this.server.respond(); var callback = function(){
ok(true, "should use 401 callback");
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/instance/id'); },
ok(!callback.calledOnce, "callback should not be called"); statusCode = {
}); 401: callback
};
test("Get instance information", function(){ jQuery(document).slapos('instanceRequest', {
url: "/request_instance",
var callback = this.spy(); statusCode: statusCode,
this.spy(jQuery, 'ajax'); });
this.server.respond();
responseBody = [{instance_id: "anId", status: "start", software_release: "http://example.com/example.cfg", });
software_type: "type_provided_by_the_software", slave: "False", connection: {
custom_connection_parameter_1: "foo", test("Payment required", function(){
custom_connection_parameter_2: "bar"}, expect(1);
parameter: {Custom1: "one string", Custom2: "one float",
Custom3: ["abc", "def"]}, this.server.respondWith("POST", "/request_instance", this.payment);
sla: {computer_id: "COMP-0"}, var callback = function(){
children_id_list: ["subinstance1", "subinstance2"], ok(true, "should use 402 callback");
partition: {public_ip: ["::1", "91.121.63.94"], private_ip: ["127.0.0.1"], },
tap_interface: "tap2"}}]; statusCode = {
response = [200, this.header, JSON.stringify(responseBody)]; 402: callback
this.server.respondWith("GET", /\/instance\/(\w+)/, response); };
jQuery(document).slapos('instanceRequest', {
jQuery(document).slapos('getInstance', 'id', callback); url: "/request_instance",
this.server.respond(); statusCode: statusCode,
});
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/instance/id'); this.server.respond();
ok(callback.calledOnce, "callback should be call"); });
ok(callback.calledWith(responseBody), "should return informations of an instance");
}); test("Not found", function(){
expect(1);
test("Get instance information - Fail", function(){
this.server.respondWith("POST", "/request_instance", this.not_found);
var callback = this.spy(); var callback = function(){
this.spy(jQuery, 'ajax'); ok(true, "should use 404 callback");
},
this.server.respondWith("GET", /\/instance\/(\w+)/, this.error); statusCode = {
404: callback
jQuery(document).slapos('getInstance', 'id', callback); };
this.server.respond(); jQuery(document).slapos('instanceRequest', {
url: "/request_instance",
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/instance/id'); statusCode: statusCode,
ok(!callback.calledOnce, "callback should not be called"); });
}); this.server.respond();
});
test("Get instance authentication certificates", function(){
test("Internal server error", function(){
var callback = this.spy(); expect(1);
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", "/request_instance", this.server_error);
responseBody = [{ ssl_key: "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADAN...h2VSZRlSN\n-----END PRIVATE KEY-----", var callback = function(){
ssl_certificate: "-----BEGIN CERTIFICATE-----\nMIIEAzCCAuugAwIBAgICHQI...ulYdXJabLOeCOA=\n-----END CERTIFICATE-----"}]; ok(true, "should use 500 callback");
response = [200, this.header, JSON.stringify(responseBody)]; },
this.server.respondWith("GET", /\/instance\/(\w+)\/certificate/, response); statusCode = {
500: callback
jQuery(document).slapos('getInstanceCert', 'id', callback); };
this.server.respond(); jQuery(document).slapos('instanceRequest', {
url: "/request_instance",
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/instance/id/certificate'); statusCode: statusCode,
ok(callback.calledOnce, "callback call"); });
ok(callback.calledWith(responseBody)); this.server.respond();
}); });
test("Get instance authentication certificates - Fail", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("GET", /\/instance\/(\w+)\/certificate/, this.error);
jQuery(document).slapos('getInstanceCert', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/instance/id/certificate');
ok(!callback.calledOnce, "callback should not be called");
});
test("Bang instance", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("POST", /\/instance\/(\w+)\/bang/, response);
data = '';
jQuery(document).slapos('bangInstance', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/instance/id/bang');
ok(callback.calledOnce, "callback should be called");
});
test("Bang instance - Fail", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", /\/instance\/(\w+)\/bang/, this.error);
jQuery(document).slapos('bangInstance', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/instance/id/bang');
ok(!callback.calledOnce, "callback should not be called");
});
test("Modifying instance", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("PUT", /\/instance\/(\w+)/, response);
data = '';
jQuery(document).slapos('editInstance', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/instance/id');
ok(callback.calledOnce, "callback should be called");
});
test("Modifying instance - Fail", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("PUT", /\/instance\/(\w+)/, this.error);
jQuery(document).slapos('editInstance', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/instance/id');
ok(!callback.calledOnce, "callback should not be called");
});
test("Register a new computer", function(){
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-----"}];
response = [201, this.header, JSON.stringify(responseBody)];
this.server.respondWith("POST", "/computer", response);
jQuery(document).slapos('newComputer', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(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(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", "/computer", this.error);
jQuery(document).slapos('newComputer', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/computer');
ok(!callback.calledOnce, "callback should not be called");
});
test("Getting computer information", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
responseBody = [{computer_id: "COMP-0",
software: [{software_release: "http://example.com/example.cfg", 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"}]}];
response = [200, this.header, JSON.stringify(responseBody)];
this.server.respondWith("GET", /\/computer\/(\w+)/, response);
jQuery(document).slapos('getComputer', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(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(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("GET", /\/computer\/(\w+)/, this.error);
jQuery(document).slapos('getComputer', 'id', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/computer/id');
ok(!callback.calledOnce, "callback should not be called");
});
test("Modifying computer", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("PUT", /\/computer\/(\w+)/, response);
jQuery(document).slapos('editComputer', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/computer/id');
ok(callback.calledOnce, "callback should be called");
});
test("Modifying computer - Fail", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("PUT", /\/computer\/(\w+)/, this.error);
jQuery(document).slapos('editComputer', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/computer/id');
ok(!callback.calledOnce, "callback should not be called");
});
test("Supplying new software", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("POST", /\/computer\/(\w+)\/supply/, response);
data = '';
jQuery(document).slapos('newSoftware', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/computer/id/supply');
ok(callback.calledOnce, "callback should be called");
});
test("Supplying new software - Fail", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", /\/computer\/(\w+)\/supply/, this.error);
jQuery(document).slapos('newSoftware', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/computer/id/supply');
ok(!callback.calledOnce, "callback should not be called");
});
test("Bang computer", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("POST", /\/computer\/(\w+)\/bang/, response);
data = '';
jQuery(document).slapos('bangComputer', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/computer/id/bang');
ok(callback.calledOnce, "callback should be called");
});
test("Bang computer - Fail", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", /\/computer\/(\w+)\/bang/, this.error);
jQuery(document).slapos('bangComputer', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/computer/id/bang');
ok(!callback.calledOnce, "callback should not be called");
});
test("Report computer usage", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [200, this.header, ''];
this.server.respondWith("POST", /\/computer\/(\w+)\/report/, response);
data = '';
jQuery(document).slapos('computerReport', 'id', data, callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+'/computer/id/report');
ok(callback.calledOnce, "callback call");
});
test("Report computer usage - Fail", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", /\/computer\/(\w+)\/report/, this.error);
jQuery(document).slapos('computerReport', 'id', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(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(){
jQuery(document).slapos({host: "http://foo.com"});
equal(jQuery(document).slapos('store', 'host'), "http://foo.com", "should contains host whatever is the method");
});
test("Modifying host after initialisation at start", function(){
jQuery(document).slapos('store', 'host', 'http://examples.com');
equal(jQuery(document).slapos('store', 'host'), "http://examples.com", "should contains modified host");
});
}); });
...@@ -82,11 +82,10 @@ ...@@ -82,11 +82,10 @@
datatype: 'json', datatype: 'json',
context: $(this), context: $(this),
beforeSend: function (xhr) { beforeSend: function (xhr) {
xhr.setRequestHeader('REMOTE_USER', 'test_vifib_customer'); //xhr.setRequestHeader('REMOTE_USER', 'test_vifib_customer');
xhr.setRequestHeader('Accept', 'application/json'); xhr.setRequestHeader('Accept', 'application/json');
if ($(this).slapos('access_token') && authentication) { if ($(this).slapos('access_token') && authentication) {
//xhr.setRequestHeader('Authorization', $(this).slapos('store', 'token_type') + ' ' + $(this).slapos('access_token')); xhr.setRequestHeader('Authorization', $(this).slapos('store', 'token_type') + ' ' + $(this).slapos('access_token'));
//xhr.setRequestHeader('Accept', 'application/json');
} }
} }
}; };
...@@ -120,7 +119,7 @@ ...@@ -120,7 +119,7 @@
beforeSend: function (xhr) { beforeSend: function (xhr) {
xhr.setRequestHeader('Accept', 'application/json'); xhr.setRequestHeader('Accept', 'application/json');
}, },
success: callback success: callback,
}); });
}); });
}, },
......
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