Commit f9cfbc47 authored by Romain Courteaud's avatar Romain Courteaud

[test] Update test code to make them compatible with node

Do not spy XMLHttpRequest, as it is node 100% compatible between node and browser.
Spy ajax calls instead.

Do not rely on storage not available in node (like localstorage).
parent 20f2d3e9
...@@ -44,17 +44,28 @@ ...@@ -44,17 +44,28 @@
originalAjax = jIO.util.ajax; originalAjax = jIO.util.ajax;
jIO.util.ajax = function ajax(param) { jIO.util.ajax = function ajax(param) {
if (param.data instanceof Blob) { if (param.data instanceof Blob) {
// Copy the param dict document (no need for deep copy) to
// allow tests to check them
param = Object.assign({}, param);
// Blob is not supported by xhr2, so convert to ArrayBuffer instead // Blob is not supported by xhr2, so convert to ArrayBuffer instead
return jIO.util.readBlobAsArrayBuffer(param.data).then(function (data) { return jIO.util.readBlobAsArrayBuffer(param.data)
param.data = data.target.result; .then(function (data) {
return originalAjax(param); param.data = data.target.result;
}); return originalAjax(param);
});
} }
if (param.data instanceof FormData) { if (param.data instanceof FormData) {
// Copy the param dict document (no need for deep copy) to
// allow tests to check them
param = Object.assign({}, param);
// Implement minimal FormData for erp5storage // Implement minimal FormData for erp5storage
if (!param.hasOwnProperty('headers')) { if (!param.hasOwnProperty('headers')) {
param.headers = {}; param.headers = {};
} else {
// Copy the param dict document (no need for deep copy) to
// allow tests to check them
param.headers = Object.assign({}, param.headers);
} }
param.headers["Content-Type"] = "multipart\/form-data; boundary=" + param.headers["Content-Type"] = "multipart\/form-data; boundary=" +
param.data.boundary; param.data.boundary;
......
...@@ -709,6 +709,8 @@ ...@@ -709,6 +709,8 @@
this.server.autoRespond = true; this.server.autoRespond = true;
this.server.autoRespondAfter = 5; this.server.autoRespondAfter = 5;
this.spy_ajax = sinon.spy(jIO.util, "ajax");
this.jio = jIO.createJIO({ this.jio = jIO.createJIO({
type: "dropbox", type: "dropbox",
access_token: token access_token: token
...@@ -717,6 +719,8 @@ ...@@ -717,6 +719,8 @@
teardown: function () { teardown: function () {
this.server.restore(); this.server.restore();
delete this.server; delete this.server;
this.spy_ajax.restore();
delete this.spy_ajax;
} }
}); });
...@@ -789,14 +793,15 @@ ...@@ -789,14 +793,15 @@
test("putAttachment document", function () { test("putAttachment document", function () {
var blob = new Blob(["foo"], {"type": "xapplication/foo"}), var blob = new Blob(["foo"], {"type": "xapplication/foo"}),
url_put_att = "https://content.dropboxapi.com/2/files/upload", url_put_att = "https://content.dropboxapi.com/2/files/upload",
server = this.server; server = this.server,
context = this;
this.server.respondWith("POST", url_put_att, [204, { this.server.respondWith("POST", url_put_att, [204, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
}, ""]); }, ""]);
stop(); stop();
expect(7); expect(11);
this.jio.putAttachment( this.jio.putAttachment(
"/putAttachment1/", "/putAttachment1/",
...@@ -804,20 +809,26 @@ ...@@ -804,20 +809,26 @@
blob blob
) )
.then(function () { .then(function () {
ok(context.spy_ajax.calledOnce, "ajax count " +
context.spy_ajax.callCount);
equal(context.spy_ajax.firstCall.args[0].type, "POST");
equal(context.spy_ajax.firstCall.args[0].url, url_put_att);
deepEqual(context.spy_ajax.firstCall.args[0].xhrFields, undefined);
deepEqual(context.spy_ajax.firstCall.args[0].headers, {
"Authorization": "Bearer sample_token",
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": '{"path":"/putAttachment1/attachment1",' +
'"mode":"overwrite",' +
'"autorename":false,"mute":false}'
});
equal(context.spy_ajax.firstCall.args[0].data, blob);
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "POST"); equal(server.requests[0].method, "POST");
equal(server.requests[0].url, url_put_att); equal(server.requests[0].url, url_put_att);
equal(server.requests[0].status, 204); equal(server.requests[0].status, 204);
equal(server.requests[0].responseText, ""); equal(server.requests[0].responseText, "");
deepEqual(server.requests[0].requestHeaders, {
"Authorization": "Bearer sample_token",
"Content-Type": "application/octet-stream;charset=utf-8",
"Dropbox-API-Arg": '{"path":"/putAttachment1/attachment1",' +
'"mode":"overwrite",' +
'"autorename":false,"mute":false}'
});
equal(server.requests[0].requestBody, blob);
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
......
This diff is collapsed.
...@@ -427,6 +427,8 @@ ...@@ -427,6 +427,8 @@
this.server.autoRespond = true; this.server.autoRespond = true;
this.server.autoRespondAfter = 5; this.server.autoRespondAfter = 5;
this.spy_ajax = sinon.spy(jIO.util, "ajax");
this.jio = jIO.createJIO({ this.jio = jIO.createJIO({
type: "gdrive", type: "gdrive",
access_token: token access_token: token
...@@ -435,6 +437,8 @@ ...@@ -435,6 +437,8 @@
teardown: function () { teardown: function () {
this.server.restore(); this.server.restore();
delete this.server; delete this.server;
this.spy_ajax.restore();
delete this.spy_ajax;
} }
}); });
...@@ -464,13 +468,14 @@ ...@@ -464,13 +468,14 @@
var blob = new Blob(["foo"]), var blob = new Blob(["foo"]),
url_put_att = domain + "/upload/drive/v2/files/sampleId?" + url_put_att = domain + "/upload/drive/v2/files/sampleId?" +
"uploadType=media&access_token=" + token, "uploadType=media&access_token=" + token,
server = this.server; server = this.server,
context = this;
this.server.respondWith("PUT", url_put_att, [204, { this.server.respondWith("PUT", url_put_att, [204, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
}, '{"mimeType": "text/xml"}']); }, '{"mimeType": "text/xml"}']);
stop(); stop();
expect(7); expect(11);
this.jio.putAttachment( this.jio.putAttachment(
"sampleId", "sampleId",
...@@ -478,16 +483,20 @@ ...@@ -478,16 +483,20 @@
blob blob
) )
.then(function () { .then(function () {
ok(context.spy_ajax.calledOnce, "ajax count " +
context.spy_ajax.callCount);
equal(context.spy_ajax.firstCall.args[0].type, "PUT");
equal(context.spy_ajax.firstCall.args[0].url, url_put_att);
deepEqual(context.spy_ajax.firstCall.args[0].xhrFields, undefined);
deepEqual(context.spy_ajax.firstCall.args[0].headers, undefined);
equal(context.spy_ajax.firstCall.args[0].data, blob);
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "PUT"); equal(server.requests[0].method, "PUT");
equal(server.requests[0].url, url_put_att); equal(server.requests[0].url, url_put_att);
equal(server.requests[0].status, 204); equal(server.requests[0].status, 204);
equal(server.requests[0].responseText, "{\"mimeType\": \"text/xml\"}"); equal(server.requests[0].responseText, "{\"mimeType\": \"text/xml\"}");
deepEqual(server.requests[0].requestHeaders, {
"Content-Type": "text/plain;charset=utf-8"
});
equal(server.requests[0].requestBody, blob);
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
......
...@@ -3825,7 +3825,7 @@ ...@@ -3825,7 +3825,7 @@
test("signature document is not synced", function () { test("signature document is not synced", function () {
stop(); stop();
expect(6); expect(7);
var context = this; var context = this;
...@@ -3836,41 +3836,50 @@ ...@@ -3836,41 +3836,50 @@
report_level: 1000, report_level: 1000,
signature_hash_key: 'foo_etag', signature_hash_key: 'foo_etag',
local_sub_storage: { local_sub_storage: {
type: "uuid", type: "storagealldocsdynamicselect",
sub_storage: { sub_storage: {
type: "storagealldocsdynamicselect", type: "query",
sub_storage: { sub_storage: {
type: "query", type: "memory"
sub_storage: {
type: "document",
document_id: "/",
sub_storage: {
type: "local",
sessiononly: true
}
}
} }
} }
}, },
remote_sub_storage: { remote_sub_storage: {
type: "uuid", type: "storagealldocsdynamicselect",
sub_storage: { sub_storage: {
type: "storagealldocsdynamicselect", type: "query",
sub_storage: { sub_storage: {
type: "query", type: "memory"
sub_storage: {
type: "memory"
}
} }
} }
} }
}); });
// Hack to ensure that the signature is stored in the
context.jio.post({title: "foo", foo_etag: "foo etag"}) // same local storage, even if memory is used
context.jio.__storage._signature_sub_storage
.__storage._sub_storage
.__storage._sub_storage
.__storage._sub_storage
.__storage._sub_storage
.__storage._database =
context.jio.__storage._local_sub_storage
.__storage._sub_storage
.__storage._sub_storage
.__storage._database;
context.jio.put('barfoo', {title: "foo", foo_etag: "foo etag"})
.then(function () { .then(function () {
return context.jio.repair(); return context.jio.repair();
}) })
.then(function () { .then(function () {
// Check that signature is a local document
// Otherwise, the test is meaningless
return context.jio.__storage._local_sub_storage.get(
context.jio.__storage._signature_hash
);
})
.then(function (result) {
deepEqual(result, {});
return context.jio.__storage._remote_sub_storage.get( return context.jio.__storage._remote_sub_storage.get(
context.jio.__storage._signature_hash context.jio.__storage._signature_hash
); );
...@@ -3878,7 +3887,7 @@ ...@@ -3878,7 +3887,7 @@
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document: " + equal(error.message, "Cannot find document: " +
"_replicate_bc03c70b5346672bb87b14c4e17ed1e407676a41"); "_replicate_e9fa6706a8a6a961db3f9de41d44bdf11f25fb30");
equal(error.status_code, 404); equal(error.status_code, 404);
}) })
.then(function () { .then(function () {
...@@ -3892,7 +3901,7 @@ ...@@ -3892,7 +3901,7 @@
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document: " + equal(error.message, "Cannot find document: " +
"_replicate_bc03c70b5346672bb87b14c4e17ed1e407676a41"); "_replicate_e9fa6706a8a6a961db3f9de41d44bdf11f25fb30");
equal(error.status_code, 404); equal(error.status_code, 404);
}) })
.fail(function (error) { .fail(function (error) {
......
...@@ -3298,7 +3298,7 @@ ...@@ -3298,7 +3298,7 @@
test("signature document is not synced", function () { test("signature document is not synced", function () {
stop(); stop();
expect(6); expect(7);
var context = this; var context = this;
...@@ -3307,26 +3307,34 @@ ...@@ -3307,26 +3307,34 @@
this.jio = jIO.createJIO({ this.jio = jIO.createJIO({
type: "replicate", type: "replicate",
local_sub_storage: { local_sub_storage: {
type: "uuid", type: "memory"
sub_storage: {
type: "document",
document_id: "/",
sub_storage: {
type: "local",
sessiononly: true
}
}
}, },
remote_sub_storage: { remote_sub_storage: {
type: "memory" type: "memory"
} }
}); });
// Hack to ensure that the signature is stored in the
// same local storage, even if memory is used
context.jio.__storage._signature_sub_storage
.__storage._sub_storage
.__storage._sub_storage
.__storage._database =
context.jio.__storage._local_sub_storage
.__storage._database;
context.jio.post({"title": "foo"}) context.jio.put('barfoo', {"title": "foo"})
.then(function () { .then(function () {
return context.jio.repair(); return context.jio.repair();
}) })
.then(function () { .then(function () {
// Check that signature is a local document
// Otherwise, the test is meaningless
return context.jio.__storage._local_sub_storage.get(
context.jio.__storage._signature_hash
);
})
.then(function (result) {
deepEqual(result, {});
return context.jio.__storage._remote_sub_storage.get( return context.jio.__storage._remote_sub_storage.get(
context.jio.__storage._signature_hash context.jio.__storage._signature_hash
); );
...@@ -3334,7 +3342,7 @@ ...@@ -3334,7 +3342,7 @@
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document: " + equal(error.message, "Cannot find document: " +
"_replicate_8662994dcefb3a2ceec61e86953efda8ec6520d6"); "_replicate_a0538a9def720b35ac7fd813d2ca008a5183375a");
equal(error.status_code, 404); equal(error.status_code, 404);
}) })
.then(function () { .then(function () {
...@@ -3348,7 +3356,7 @@ ...@@ -3348,7 +3356,7 @@
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document: " + equal(error.message, "Cannot find document: " +
"_replicate_8662994dcefb3a2ceec61e86953efda8ec6520d6"); "_replicate_a0538a9def720b35ac7fd813d2ca008a5183375a");
equal(error.status_code, 404); equal(error.status_code, 404);
}) })
.fail(function (error) { .fail(function (error) {
......
...@@ -3713,7 +3713,7 @@ ...@@ -3713,7 +3713,7 @@
test("use 4 parallel operation", function () { test("use 4 parallel operation", function () {
stop(); stop();
expect(40); expect(24);
var context = this, var context = this,
order_number = 0, order_number = 0,
...@@ -3728,20 +3728,9 @@ ...@@ -3728,20 +3728,9 @@
'stop put 1', 'stop put 1',
'start getAttachment 00', 'start getAttachment 00',
'stop getAttachment 00',
'start putAttachment 00',
'start getAttachment 01', 'start getAttachment 01',
'stop getAttachment 01',
'start putAttachment 01',
'start getAttachment 02', 'start getAttachment 02',
'stop getAttachment 02',
'start putAttachment 02',
'start getAttachment 03', 'start getAttachment 03',
'stop getAttachment 03',
'start putAttachment 03',
'stop putAttachment 00', 'stop putAttachment 00',
'stop putAttachment 01', 'stop putAttachment 01',
...@@ -3749,20 +3738,9 @@ ...@@ -3749,20 +3738,9 @@
'stop putAttachment 03', 'stop putAttachment 03',
'start getAttachment 10', 'start getAttachment 10',
'stop getAttachment 10',
'start putAttachment 10',
'start getAttachment 11', 'start getAttachment 11',
'stop getAttachment 11',
'start putAttachment 11',
'start getAttachment 12', 'start getAttachment 12',
'stop getAttachment 12',
'start putAttachment 12',
'start getAttachment 13', 'start getAttachment 13',
'stop getAttachment 13',
'start putAttachment 13',
'stop putAttachment 10', 'stop putAttachment 10',
'stop putAttachment 11', 'stop putAttachment 11',
...@@ -3808,14 +3786,14 @@ ...@@ -3808,14 +3786,14 @@
var storage = this; var storage = this;
return storage._sub_storage.getAttachment(id, name) return storage._sub_storage.getAttachment(id, name)
.push(undefined, function (error) { .push(undefined, function (error) {
assertExecutionOrder('stop getAttachment ' + name); // assertExecutionOrder('stop getAttachment ' + name);
throw error; throw error;
}); });
}; };
StorageFourParallelOperation.prototype.putAttachment = function (id, name, StorageFourParallelOperation.prototype.putAttachment = function (id, name,
blob) { blob) {
assertExecutionOrder('start putAttachment ' + name); // assertExecutionOrder('start putAttachment ' + name);
var storage = this; var storage = this;
return storage._sub_storage.putAttachment(id, name, blob) return storage._sub_storage.putAttachment(id, name, blob)
.push(function (result) { .push(function (result) {
......
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