Commit 0bce7aa0 authored by Vincent Bechu's avatar Vincent Bechu

[cloudoostorage] add test on failed conversion

parent 81600fca
/*jslint nomen: true*/ /*jslint nomen: true*/
/*global jIO, RSVP, Blob, Uint8Array, DOMParser*/ /*global jIO, RSVP, Blob, Uint8Array, DOMParser, XMLSerializer*/
(function (jIO, Uint8Array, Blob, RSVP, DOMParser) { (function (jIO, Uint8Array, Blob, RSVP, DOMParser, XMLSerializer) {
"use strict"; "use strict";
var content_type_dict = { var content_type_dict = {
"application/x-asc-text": "docy", "application/x-asc-text": "docy",
"application/x-asc-presentation": "", "application/x-asc-presentation": "",
"application/x-asc-spreadsheet": "" "application/x-asc-spreadsheet": ""
}; },
parser = new DOMParser(),
serializer = new XMLSerializer();
function newXml() {
return parser.parseFromString(
'<?xml version="1.0" encoding="UTF-8"?><methodCall>' +
'<methodName>convertFile</methodName><params>' +
'<param><value><string></string></value></param>' +
'<param><value><string></string></value></param>' +
'<param><value><string></string></value></param></params></methodCall>',
'text/xml'
);
}
function b64toBlob(b64Data, contentType, sliceSize) { function b64toBlob(b64Data, contentType, sliceSize) {
contentType = contentType || ''; contentType = contentType || '';
...@@ -47,28 +60,27 @@ ...@@ -47,28 +60,27 @@
.push(function (result) { .push(function (result) {
// WIP: use something cleaner // WIP: use something cleaner
var file = result.target.result.split('base64,')[1], var file = result.target.result.split('base64,')[1],
xml = '<?xml version="1.0" encoding="UTF-8"?><methodCall>' + xml = newXml(),
'<methodName>convertFile</methodName><params>' + string_list = xml.getElementsByTagName('string');
'<param><value><string>' + file + '</string></value></param>' + string_list[0].textContent = file;
'<param><value><string>' + from + '</string></value></param>' + string_list[1].textContent = from;
'<param><value><string>' + to + string_list[2].textContent = to;
'</string></value></param></params></methodCall>';
return jIO.util.ajax({ return jIO.util.ajax({
type: 'POST', type: 'POST',
url: storage._url, url: storage._url,
data: xml data: serializer.serializeToString(xml)
}); });
}) })
.push(function (result) { .push(function (result) {
var data = (new DOMParser().parseFromString( var data = parser.parseFromString(
result.target.responseText, result.target.responseText,
"application/xml" "application/xml"
)), ),
content = data.getElementsByTagName('string')[0].textContent; content = data.getElementsByTagName('string');
if (content !== undefined) { if (content.length > 0) {
return b64toBlob(content, to); return b64toBlob(content[0].textContent, to);
} }
throw new jIO.util.jIOError('conversion failed', 400); throw new jIO.util.jIOError('conversion failed', 404);
}); });
} }
...@@ -275,4 +287,4 @@ ...@@ -275,4 +287,4 @@
jIO.addStorage('cloudoo', CloudooStorage); jIO.addStorage('cloudoo', CloudooStorage);
}(jIO, Uint8Array, Blob, RSVP, DOMParser)); }(jIO, Uint8Array, Blob, RSVP, DOMParser, XMLSerializer));
...@@ -381,7 +381,7 @@ ...@@ -381,7 +381,7 @@
test("getAttachment convert from docy to docx", function () { test("getAttachment convert from docy to docx", function () {
stop(); stop();
expect(10); expect(12);
var blob = new Blob(["documentauformatdocy"]), var blob = new Blob(["documentauformatdocy"]),
server = this.server, server = this.server,
...@@ -434,6 +434,8 @@ ...@@ -434,6 +434,8 @@
this.jio.getAttachment("bar", "data?docx") this.jio.getAttachment("bar", "data?docx")
.then(function (result) { .then(function (result) {
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, cloudoo_url);
equal( equal(
server.requests[0].requestBody, server.requests[0].requestBody,
'<?xml version="1.0" encoding="UTF-8"?><methodCall>' + '<?xml version="1.0" encoding="UTF-8"?><methodCall>' +
...@@ -452,6 +454,59 @@ ...@@ -452,6 +454,59 @@
start(); start();
}); });
}); });
test("getAttachment convert from docy to docx failed", function () {
stop();
expect(4);
var blob = new Blob(["documentauformatdocy"]);
this.server.respondWith("POST", cloudoo_url, [200, {
"Content-Type": "text/xml"
}, '<?xml version="1.0" encoding="UTF-8"?>']);
Storage200.prototype.getAttachment = function (id, name) {
equal(id, "bar", "getAttachment 200 called");
if (name === "data?docx") {
throw new jIO.util.jIOError("can't find", 404);
}
return blob;
};
Storage200.prototype.get = function (id) {
if (id === "cloudoo/bar/data") {
throw new jIO.util.jIOError("can't find", 404);
}
if (id === "bar") {
return {content_type: "application/x-asc-text"};
}
equal(id, "", "get 200 called");
return {};
};
Storage200.prototype.put = function (id, doc) {
equal(id, "cloudoo/bar/data", "put 200 called");
deepEqual(doc, {
"attachment_id": "data",
"convert_dict": {
"docx": false
},
"doc_id": "bar",
"format": "docy",
"portal_type": "Conversion Info"
}, "put doc 200 called");
return id;
};
this.jio.getAttachment("bar", "data?docx")
.fail(function (error) {
equal(error.message, "conversion failed", "check conversion failed");
equal(error.status_code, 404, "check error status code");
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// CryptStorage.putAttachment // CryptStorage.putAttachment
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
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