Commit d97cbe76 authored by Thomas Lechauve's avatar Thomas Lechauve

Cross-domain tests added

This tests are usefull to check if a browser support or not cross-domain
requests
parent 4d098c6b
...@@ -6,9 +6,16 @@ app = Flask(__name__) ...@@ -6,9 +6,16 @@ app = Flask(__name__)
def index(): def index():
return render_template("slapos.html") return render_template("slapos.html")
@app.route('/200', methods=["POST", "GET"])
def request200():
response = make_response("HELLO", 200)
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = '*'
return response
@app.route('/request', methods=["POST", "GET"]) @app.route('/request', methods=["POST", "GET"])
def request(): def request404():
response = make_response("HELLO", 408) response = make_response("Not Found", 404)
response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = '*' response.headers['Access-Control-Allow-Methods'] = '*'
return response return response
......
...@@ -42,12 +42,15 @@ ...@@ -42,12 +42,15 @@
request: function(type, url, callback, data){ request: function(type, url, callback, data){
data = data || ''; data = data || '';
$.ajax({ return $.ajax({
url: this.config.host+url, url: this.store('host')+url,
dataType: 'json', dataType: 'json',
data: data, data: data,
context: this.$elem, context: this.$elem,
type: type, type: type,
statusCode: {
0: function(){ console.log('status code 0') }
}
}).done(callback).fail(this.failCallback); }).done(callback).fail(this.failCallback);
}, },
...@@ -56,7 +59,7 @@ ...@@ -56,7 +59,7 @@
}, },
newInstance: function(data, callback){ newInstance: function(data, callback){
this.request('POST', '/request', callback, data); return this.request('POST', '/request', callback, data);
}, },
deleteInstance: function(id, callback){ deleteInstance: function(id, callback){
......
...@@ -2,8 +2,32 @@ $(function(){ ...@@ -2,8 +2,32 @@ $(function(){
var h = getParameterByName("login"); var h = getParameterByName("login");
var slap = new SlapOs(document, {host: h}).init(); var slap = new SlapOs(document, {host: h}).init();
module("Cross-domain Tests");
test("200 response", function(){
expect(1);
stop(1);
$.ajax({
url: 'http://192.168.242.92:5000/200',
complete: function() { start(); },
statusCode: {
200: function(){ ok(true, "should get 200 status");},
}});
});
test("404 response", function(){
expect(1);
stop(1);
$.ajax({
url: 'http://sheldy.com:5000/request',
complete: function() { start(); },
statusCode: {
404: function(xhr){ ok(true, "should get 404 error status status="+xhr.status); },
0: function(){ ok(false, "should get 404 not but receive 0"); }
}});
});
module("Ajax Tests", { module("Local Ajax 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; charset=utf-8"};
...@@ -15,60 +39,68 @@ $(function(){ ...@@ -15,60 +39,68 @@ $(function(){
}); });
test("Requesting a new instance", function(){ test("Requesting a new instance", function(){
expect(2);
callback = this.spy(); callback = this.spy();
this.spy(jQuery, 'ajax');
url = "/request";
responseBody = [{instance_id: "anId",status: "started",connection: {}}]; responseBody = [{instance_id: "anId",status: "started",connection: {}}];
response = [201, this.header, JSON.stringify(responseBody)]; response = [201, this.header, JSON.stringify(responseBody)];
this.server.respondWith("POST", "/request", response); this.server.respondWith("POST", url, response);
slap.newInstance('', callback); slap.newInstance('', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", "/request", this.error); url = "/request";
this.server.respondWith("POST", url, this.error);
slap.newInstance('', callback); slap.newInstance('', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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); slap.deleteInstance('id', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("DELETE", /\/instance\/(\w+)/, this.error); this.server.respondWith("DELETE", /\/instance\/(\w+)/, this.error);
slap.deleteInstance('id', callback); slap.deleteInstance('id', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(2);
callback = this.spy(); callback = this.spy();
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",
software_type: "type_provided_by_the_software", slave: "False", connection: { software_type: "type_provided_by_the_software", slave: "False", connection: {
...@@ -86,25 +118,29 @@ $(function(){ ...@@ -86,25 +118,29 @@ $(function(){
slap.getInstance('id', callback); slap.getInstance('id', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("GET", /\/instance\/(\w+)/, this.error); this.server.respondWith("GET", /\/instance\/(\w+)/, this.error);
slap.getInstance('id', callback); slap.getInstance('id', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(2);
callback = this.spy(); callback = this.spy();
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-----",}];
...@@ -114,25 +150,29 @@ $(function(){ ...@@ -114,25 +150,29 @@ $(function(){
slap.getInstanceCert('id', callback); slap.getInstanceCert('id', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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); slap.getInstanceCert('id', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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);
...@@ -141,24 +181,28 @@ $(function(){ ...@@ -141,24 +181,28 @@ $(function(){
slap.bangInstance('id', data, callback); slap.bangInstance('id', data, callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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); slap.bangInstance('id', data, callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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);
...@@ -167,24 +211,28 @@ $(function(){ ...@@ -167,24 +211,28 @@ $(function(){
slap.editInstance('id', data, callback); slap.editInstance('id', data, callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("PUT", /\/instance\/(\w+)/, this.error); this.server.respondWith("PUT", /\/instance\/(\w+)/, this.error);
slap.editInstance('id', '', callback); slap.editInstance('id', '', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(2);
callback = this.spy(); callback = this.spy();
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-----",
...@@ -195,25 +243,29 @@ $(function(){ ...@@ -195,25 +243,29 @@ $(function(){
slap.newComputer('', callback); slap.newComputer('', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", "/computer", this.error); this.server.respondWith("POST", "/computer", this.error);
slap.newComputer('', callback); slap.newComputer('', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(2);
callback = this.spy(); callback = this.spy();
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",
...@@ -228,77 +280,88 @@ $(function(){ ...@@ -228,77 +280,88 @@ $(function(){
slap.getComputer('id', callback); slap.getComputer('id', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("GET", /\/computer\/(\w+)/, this.error); this.server.respondWith("GET", /\/computer\/(\w+)/, this.error);
slap.getComputer('id', callback); slap.getComputer('id', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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);
data = ''; slap.editComputer('id', '', callback);
slap.editComputer('id', data, callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("PUT", /\/computer\/(\w+)/, this.error); this.server.respondWith("PUT", /\/computer\/(\w+)/, this.error);
slap.editComputer('id', '', callback); slap.editComputer('id', '', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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('computerId', data, callback); slap.newSoftware('id', data, callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
this.spy(jQuery, 'ajax');
this.server.respondWith("POST", /\/computer\/(\w+)\/supply/, this.error); this.server.respondWith("POST", /\/computer\/(\w+)\/supply/, this.error);
slap.newSoftware('computerId', '', callback); slap.newSoftware('id', '', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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);
...@@ -307,24 +370,28 @@ $(function(){ ...@@ -307,24 +370,28 @@ $(function(){
slap.bangComputer('id', data, callback); slap.bangComputer('id', data, callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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); slap.bangComputer('id', '', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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);
...@@ -333,18 +400,21 @@ $(function(){ ...@@ -333,18 +400,21 @@ $(function(){
slap.computerReport('id', data, callback); slap.computerReport('id', data, callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.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(){
expect(1);
callback = this.spy(); callback = this.spy();
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); slap.computerReport('id', '', callback);
this.server.respond(); this.server.respond();
equal(jQuery.ajax.getCall(0).args[0].url, slap.store('host')+'/computer/id/report');
ok(!callback.calledOnce, "callback should not be called"); ok(!callback.calledOnce, "callback should not be called");
}); });
......
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