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>
......
...@@ -13,7 +13,8 @@ jQuery(function(){ ...@@ -13,7 +13,8 @@ jQuery(function(){
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(){ test("404 response", function(){
...@@ -23,411 +24,135 @@ jQuery(function(){ ...@@ -23,411 +24,135 @@ jQuery(function(){
url: 'http://sheldy.com:5000/request', url: 'http://sheldy.com:5000/request',
complete: function() { start(); }, complete: function() { start(); },
statusCode: { statusCode: {
404: function(xhr){ ok(true, "should get 404 error status status="+xhr.status); }, 404: function(xhr){
0: function(){ ok(false, "should get 404 not but receive 0"); } ok(true, "should get 404 error status status="+xhr.status);
},
0: function(){
ok(false, "should get 404 not but receive 0");
}
}}); }});
}); });
module("Local Ajax Tests", { module("Callback Tests", {
setup: function(){ setup: function(){
this.server = sinon.sandbox.useFakeServer(); this.server = sinon.sandbox.useFakeServer();
this.header = {"Content-Type":"application/json; charset=utf-8"}; this.header = {"Content-Type":"application/json"};
this.error = [409, this.header, 'ERROR'];
// Discovery response
var discoBody = {
"request_instance": {
"authentication": true,
"url": "/request_instance",
"method": "POST",
"required": {
"status": "unicode",
"slave": "bool",
"title": "unicode",
"software_release": "unicode",
"software_type": "unicode",
"parameter": "object",
"sla": "object"
},
"optional": {}
}
};
var discoResponse = [200, this.header, JSON.stringify(discoBody)];
this.server.respondWith("GET", 'http://10.8.2.34:12006/erp5/portal_vifib_rest_api_v1', discoResponse);
// Error responses
this.bad_request = [400, this.header, 'Bad Request'];
this.unauthorized = [401, this.header, 'Unauthorized'];
this.payment = [402, this.header, 'Payment required'];
this.not_found = [404, this.header, 'Not found'];
this.server_error = [500, this.header, 'Internal server error'];
}, },
teardown: function(){ teardown: function(){
this.server.restore(); this.server.restore();
} }
}); });
test("Requesting a new instance", function(){ test("Bad request", function(){
var callback = this.spy(); expect(1);
this.spy(jQuery, 'ajax');
url = "/request";
responseBody = [{instance_id: "anId",status: "started",connection: {}}];
response = [201, this.header, JSON.stringify(responseBody)];
this.server.respondWith("POST", url, response);
jQuery(document).slapos('newInstance', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(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(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
url = "/request";
this.server.respondWith("POST", url, this.error);
jQuery(document).slapos('newInstance', '', callback);
this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, jQuery(document).slapos('store', 'host')+url);
ok(!callback.calledOnce, "callback should not be called");
});
test("Deleting an instance", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
response = [202, this.header, ''];
this.server.respondWith("DELETE", /\/instance\/(\w+)/, response);
jQuery(document).slapos('deleteInstance', '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 be called");
});
test("Deleting an instance - Fail", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("DELETE", /\/instance\/(\w+)/, this.error);
jQuery(document).slapos('deleteInstance', '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("Get instance information", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
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",
custom_connection_parameter_2: "bar"},
parameter: {Custom1: "one string", Custom2: "one float",
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"}}];
response = [200, this.header, JSON.stringify(responseBody)];
this.server.respondWith("GET", /\/instance\/(\w+)/, response);
jQuery(document).slapos('getInstance', '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 be call");
ok(callback.calledWith(responseBody), "should return informations of an instance");
});
test("Get instance information - Fail", function(){
var callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("GET", /\/instance\/(\w+)/, this.error);
jQuery(document).slapos('getInstance', '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("Get instance authentication certificates", function(){
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-----"}];
response = [200, this.header, JSON.stringify(responseBody)];
this.server.respondWith("GET", /\/instance\/(\w+)\/certificate/, response);
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 call");
ok(callback.calledWith(responseBody));
});
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'); this.server.respondWith("POST", "/request_instance", this.bad_request);
ok(callback.calledOnce, "callback should be called"); var callback = function(){
ok(callback.calledWith(responseBody), "should return a computerID, ssl key and ssl certificates"); ok(true, "should use 400 callback");
},
statusCode = {
400: callback
};
jQuery(document).slapos('instanceRequest', {
url: "/request_instance",
statusCode: statusCode,
}); });
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(); 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(){ test("Unauthorized", function(){
expect(1);
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'); this.server.respondWith("POST", "/request_instance", this.unauthorized);
ok(!callback.calledOnce, "callback should not be called"); var callback = function(){
ok(true, "should use 401 callback");
},
statusCode = {
401: callback
};
jQuery(document).slapos('instanceRequest', {
url: "/request_instance",
statusCode: statusCode,
}); });
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(); 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(){ test("Payment required", function(){
expect(1);
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'); this.server.respondWith("POST", "/request_instance", this.payment);
ok(callback.calledOnce, "callback should be called"); var callback = function(){
ok(true, "should use 402 callback");
},
statusCode = {
402: callback
};
jQuery(document).slapos('instanceRequest', {
url: "/request_instance",
statusCode: statusCode,
}); });
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(); 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(){ test("Not found", function(){
expect(1);
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'); this.server.respondWith("POST", "/request_instance", this.not_found);
ok(callback.calledOnce, "callback should be called"); var callback = function(){
ok(true, "should use 404 callback");
},
statusCode = {
404: callback
};
jQuery(document).slapos('instanceRequest', {
url: "/request_instance",
statusCode: statusCode,
}); });
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(); 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(){ test("Internal server error", function(){
expect(1);
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'); this.server.respondWith("POST", "/request_instance", this.server_error);
ok(callback.calledOnce, "callback call"); var callback = function(){
ok(true, "should use 500 callback");
},
statusCode = {
500: callback
};
jQuery(document).slapos('instanceRequest', {
url: "/request_instance",
statusCode: statusCode,
}); });
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(); 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