Commit d71e7e30 authored by Vincent Bechu's avatar Vincent Bechu

[cloudoostorage] Add cloudoo storage

This storage add convertion on getAttachment by name :
conversion_name = name?format
failed conversion will be store and remake on repair.

cloudo storage will be use to convert file from cloudoo server
parent 70754e8f
...@@ -182,7 +182,8 @@ module.exports = function (grunt) { ...@@ -182,7 +182,8 @@ module.exports = function (grunt) {
'src/jio.storage/indexeddbstorage.js', 'src/jio.storage/indexeddbstorage.js',
'src/jio.storage/cryptstorage.js', 'src/jio.storage/cryptstorage.js',
'src/jio.storage/websqlstorage.js', 'src/jio.storage/websqlstorage.js',
'src/jio.storage/fbstorage.js' 'src/jio.storage/fbstorage.js',
'src/jio.storage/cloudoostorage.js'
], ],
dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js' dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
// dest: 'jio.js' // dest: 'jio.js'
......
/*jslint nomen: true*/
/*global jIO, RSVP, Blob, Uint8Array, DOMParser, XMLSerializer*/
(function (jIO, Uint8Array, Blob, RSVP, DOMParser, XMLSerializer) {
"use strict";
var content_type_dict = {
"application/x-asc-text": "docy",
"application/x-asc-presentation": "ppty",
"application/x-asc-spreadsheet": "xlsy"
},
parser = new DOMParser(),
serializer = new XMLSerializer();
function makeXmlRpcRequest(file, from, to) {
var xml = 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'
),
string_list = xml.getElementsByTagName('string');
string_list[0].textContent = file;
string_list[1].textContent = from;
string_list[2].textContent = to;
return serializer.serializeToString(xml);
}
function b64toBlob(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = window.atob(b64Data),
byteArrays = [],
slice,
byteArray,
byteNumbers = [],
offset,
i;
for (offset = 0; offset < byteCharacters.length; offset += sliceSize) {
slice = byteCharacters.slice(offset, offset + sliceSize);
for (i = 0; i < slice.length; i += 1) {
byteNumbers[i] = slice.charCodeAt(i);
}
byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
return new Blob(byteArrays, {type: contentType});
}
/**
* convert a blob
* from a format to another
* return converted blob.
**/
function convert(url, blob, from, to) {
return new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsDataURL(blob);
})
.push(function (result) {
return jIO.util.ajax({
type: 'POST',
url: url,
data: makeXmlRpcRequest(
result.target.result.split('base64,')[1],
from,
to
)
});
})
.push(function (result) {
var data_list = parser.parseFromString(
result.target.responseText,
"application/xml"
).getElementsByTagName('string');
if (data_list.length) {
return b64toBlob(data_list[0].textContent, to);
}
// Parse error. And add case.
throw new jIO.util.jIOError('conversion failed', 404);
});
}
function getCloudooDocumentId(id, attachment_id) {
return 'cloudoo/' + id + '/' + attachment_id;
}
function convertAttachment(storage, cloudoo_id, cloudoo_doc, from, to) {
return storage._sub_storage.getAttachment(
cloudoo_doc.doc_id,
cloudoo_doc.attachment_id
)
.push(function (blob) {
return convert(storage._url, blob, from, to);
})
.push(function (blob) {
var att_id = cloudoo_doc.attachment_id;
if (to !== cloudoo_doc.format) {
att_id += "?" + to;
}
cloudoo_doc.convert_dict[to] = true;
return RSVP.all([
blob,
storage._sub_storage.putAttachment(cloudoo_doc.doc_id, att_id, blob),
storage._sub_storage.put(cloudoo_id, cloudoo_doc)
]);
}, undefined, function (error) {
cloudoo_doc.convert_dict[from] = to;
return storage._sub_storage.put(
cloudoo_id,
cloudoo_doc
)
.push(function () {
throw error;
});
});
}
function createCloudooDocument(storage, id, attachment_id) {
return storage.get(id)
.push(function (doc) {
var format = doc.content_type || undefined;
if (content_type_dict.hasOwnProperty(format)) {
format = content_type_dict[format];
}
return {
portal_type: "Conversion Info",
convert_dict: {},
format: format,
attachment_id: attachment_id,
doc_id: id
};
});
}
function removeConvertedAttachments(storage, id, attachment_id) {
var cloudoo_id = getCloudooDocumentId(id, attachment_id);
return storage._sub_storage.get(cloudoo_id)
.push(function (doc) {
var format, promise_list = [];
for (format in doc.convert_list) {
if (doc.convert_list.hasOwnProperty(format)) {
if (doc.convert_list[format]) {
promise_list.push(
storage._sub_storage.removeAttachment(
id,
attachment_id + '?' + format
)
);
}
}
}
return RSVP.all(promise_list);
})
.push(function () {
return storage._sub_storage.remove(
cloudoo_id
);
})
.push(undefined, function (error) {
if (!(error instanceof jIO.util.jIOError
&& error.status_code === 404)) {
throw error;
}
});
}
/**
* The jIO CloudooStorage extension
*
* Convert attachment : att_id?format
* cloudoo_info :
* {
* portal_type: "Conversion Info"
* base_format: format de base ( content_type du document )
* to_convert_list: [liste des format a convertir pendant la synchro]
* format_avaible_list : [liste des formats deja disponible]
* }
*
* @class CloudooStorage
* @constructor
*/
function CloudooStorage(spec) {
this._url = spec.url;
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
CloudooStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments);
};
CloudooStorage.prototype.post = function () {
return this._sub_storage.post.apply(this._sub_storage, arguments);
};
CloudooStorage.prototype.put = function () {
return this._sub_storage.put.apply(this._sub_storage, arguments);
};
CloudooStorage.prototype.remove = function () {
return this._sub_storage.remove.apply(this._sub_storage, arguments);
};
CloudooStorage.prototype.getAttachment = function (id, attachment_id) {
var storage = this,
att_id_list = attachment_id.split('?'),
cloudoo_id;
if (att_id_list.length === 1) {
return this._sub_storage.getAttachment.apply(
this._sub_storage,
arguments
);
}
cloudoo_id = getCloudooDocumentId(id, att_id_list[0]);
return storage._sub_storage.get(cloudoo_id)
.push(undefined, function (error) {
if (error instanceof jIO.util.jIOError && error.status_code) {
return createCloudooDocument(storage, id, att_id_list[0]);
}
})
.push(function (doc) {
if (doc.convert_dict[att_id_list[0]] === true) {
return storage._sub_storage.getAttachment(id, attachment_id);
}
return convertAttachment(
storage,
cloudoo_id,
doc,
doc.format,
att_id_list[1]
)
.push(function (result) {
return result[0];
});
});
};
CloudooStorage.prototype.putAttachment = function (id, attachment_id, blob) {
var storage = this;
return this._sub_storage.putAttachment.apply(this._sub_storage, arguments)
.push(function (result) {
var att_id_list = attachment_id.split('?'),
cloudoo_id = getCloudooDocumentId(id, att_id_list[0]);
return storage._sub_storage.get(cloudoo_id)
.push(undefined, function (error) {
if (error instanceof jIO.util.jIOError && error.status_code) {
return createCloudooDocument(storage, id, att_id_list[0]);
}
})
.push(function (doc) {
if (att_id_list.length === 1) {
doc.convert_dict = {};
doc.convert_dict[doc.format] = true;
return storage._sub_storage.put(
getCloudooDocumentId(id, attachment_id),
doc
);
}
return convert(storage._url, blob, att_id_list[1], doc.format)
.push(function (result) {
return storage._sub_storage.putAttachment(
id,
att_id_list[0],
result
);
})
.push(function () {
doc.convert_dict[doc.format] = true;
doc.convert_dict[att_id_list[1]] = true;
return storage._sub_storage.put(cloudoo_id, doc);
})
.push(undefined, function (error) {
doc.convert_dict[doc.format] = att_id_list[1];
return storage._sub_storage.put(cloudoo_id, doc)
.push(function () {
throw error;
});
});
})
.push(function () {
return result;
});
});
};
CloudooStorage.prototype.removeAttachment = function (id, attachment_id) {
var storage = this;
return this._sub_storage.removeAttachment.apply(
this._sub_storage,
arguments
)
.push(function () {
return removeConvertedAttachments(storage, id, attachment_id);
});
};
CloudooStorage.prototype.allAttachments = function () {
return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
};
CloudooStorage.prototype.repair = function () {
var storage = this;
return this._sub_storage.repair.apply(this._sub_storage, arguments)
.push(function () {
return storage._sub_storage.allDocs({
query: 'portal_type: "Conversion Info"',
include_docs: true
});
})
.push(function (result) {
var i, promise_list = [], format, doc;
for (i = 0; i < result.data.total_rows; i += 1) {
doc = result.data.rows[i].doc;
for (format in doc.convert_dict) {
if (doc.convert_dict.hasOwnProperty(format) &&
doc.convert_dict[format] !== true) {
promise_list.push(convertAttachment(
storage,
result.data.rows[i].id,
doc,
doc.convert_dict[format],
format
));
}
}
}
return RSVP.all(promise_list);
});
};
CloudooStorage.prototype.hasCapacity = function (name) {
return this._sub_storage.hasCapacity(name);
};
CloudooStorage.prototype.buildQuery = function () {
return this._sub_storage.buildQuery.apply(this._sub_storage,
arguments);
};
jIO.addStorage('cloudoo', CloudooStorage);
}(jIO, Uint8Array, Blob, RSVP, DOMParser, XMLSerializer));
/*jslint nomen: true*/
/*global jIO, Blob, sinon*/
(function (jIO, Blob, sinon) {
"use strict";
var test = QUnit.test,
stop = QUnit.stop,
start = QUnit.start,
ok = QUnit.ok,
expect = QUnit.expect,
deepEqual = QUnit.deepEqual,
equal = QUnit.equal,
throws = QUnit.throws,
module = QUnit.module,
cloudoo_url = 'https://www.cloudooo.com/';
/////////////////////////////////////////////////////////////////
// Custom test substorage definition
/////////////////////////////////////////////////////////////////
function Storage200() {
return this;
}
jIO.addStorage('cloudoostorage200', Storage200);
/////////////////////////////////////////////////////////////////
// CloudooStorage.constructor
/////////////////////////////////////////////////////////////////
module("cloudooStorage.constructor");
test("create substorage", function () {
var jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {type : "cloudoostorage200"}
});
equal(jio.__type, "cloudoo");
equal(jio.__storage._url, cloudoo_url);
equal(jio.__storage._sub_storage.__type, "cloudoostorage200");
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.get
/////////////////////////////////////////////////////////////////
module("cloudooStorage.get");
test("get called substorage get", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
Storage200.prototype.get = function (param) {
equal(param, "bar", "get 200 called");
return {title: "foo"};
};
jio.get("bar")
.then(function (result) {
deepEqual(result, {
"title": "foo"
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.post
/////////////////////////////////////////////////////////////////
module("cloudooStorage.post");
test("post called substorage post", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
Storage200.prototype.post = function (param) {
deepEqual(param, {"title": "foo"}, "post 200 called");
return "youhou";
};
jio.post({"title": "foo"})
.then(function (result) {
equal(result, "youhou");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.put
/////////////////////////////////////////////////////////////////
module("cloudooStorage.put");
test("put called substorage put", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
Storage200.prototype.put = function (id, param) {
equal(id, "bar", "put 200 called");
deepEqual(param, {"title": "foo"}, "put 200 called");
return id;
};
jio.put("bar", {"title": "foo"})
.then(function (result) {
equal(result, "bar");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.remove
/////////////////////////////////////////////////////////////////
module("cloudooStorage.remove");
test("remove called substorage remove", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
Storage200.prototype.remove = function (id) {
deepEqual(id, "bar", "remove 200 called");
return id;
};
jio.remove("bar")
.then(function (result) {
equal(result, "bar");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.hasCapacity
/////////////////////////////////////////////////////////////////
module("cloudooStorage.hasCapacity");
test("hasCapacity return substorage value", function () {
var jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
delete Storage200.prototype.hasCapacity;
throws(
function () {
jio.hasCapacity("foo");
},
function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 501);
equal(error.message,
"Capacity 'foo' is not implemented on 'cloudoostorage200'");
return true;
}
);
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.buildQuery
/////////////////////////////////////////////////////////////////
module("cloudooStorage.buildQuery");
test("buildQuery return substorage buildQuery", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
Storage200.prototype.hasCapacity = function () {
return true;
};
Storage200.prototype.buildQuery = function (options) {
deepEqual(options, {
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
uuid: 'title: "two"'
}, "allDocs parameter");
return "bar";
};
jio.allDocs({
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
uuid: 'title: "two"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: "bar",
total_rows: 3
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.removeAttachment
/////////////////////////////////////////////////////////////////
module("cloudooStorage.removeAttachment");
test("removeAttachment called substorage removeAttachment", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
Storage200.prototype.get = function (id) {
if (id !== "bar") {
throw new jIO.util.jIOError('not found', 404);
}
return {};
};
Storage200.prototype.removeAttachment = function (id, name) {
equal(id, "bar", "removeAttachment 200 called");
equal(name, "foo", "removeAttachment 200 called");
return "Removed";
};
jio.removeAttachment("bar", "foo")
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("cloudooStorage.allAttachments");
test("allAttachments called substorage allAttachments", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
Storage200.prototype.allAttachments = function (id) {
equal(id, "bar", "allAttachments, 200 called");
return {attachmentname: {}};
};
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {
attachmentname: {}
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("cloudooStorage.getAttachment", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("getAttachment called substorage getAttachment", function () {
stop();
expect(3);
var blob = new Blob([""]);
Storage200.prototype.getAttachment = function (id, name) {
equal(id, "bar", "getAttachment 200 called");
equal(name, "foo", "getAttachment 200 called");
return blob;
};
this.jio.getAttachment("bar", "foo")
.then(function (result) {
equal(result, blob);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("getAttachment convert from docy to docx", function () {
stop();
expect(12);
var blob = new Blob(["documentauformatdocy"]),
server = this.server,
blob_convert = new Blob(["documentauformatdocx"], {type: "docx"});
this.server.respondWith("POST", cloudoo_url, [200, {
"Content-Type": "text/xml"
}, '<?xml version="1.0" encoding="UTF-8"?>' +
'<string>ZG9jdW1lbnRhdWZvcm1hdGRvY3g=</string>']);
Storage200.prototype.getAttachment = function (id, name) {
equal(id, "bar", "getAttachment 200 called");
equal(name, "data", "getAttachment 200 called");
return blob;
};
Storage200.prototype.putAttachment = function (id, att_id, blob) {
equal(id, "bar", "putAttachment 200 called");
equal(att_id, "data?docx", "putAttachment 200 called");
deepEqual(blob, blob_convert, "putAttachment 200 called");
};
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": true
},
"doc_id": "bar",
"format": "docy",
"portal_type": "Conversion Info"
}, "put doc 200 called");
return id;
};
this.jio.getAttachment("bar", "data?docx")
.then(function (result) {
equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, cloudoo_url);
equal(
server.requests[0].requestBody,
'<?xml version="1.0" encoding="UTF-8"?><methodCall>' +
'<methodName>convertFile</methodName><params><param><value>' +
'<string>ZG9jdW1lbnRhdWZvcm1hdGRvY3k=</string></value></param>' +
'<param><value><string>docy</string></value></param>' +
'<param><value><string>docx' +
'</string></value></param></params></methodCall>'
);
deepEqual(result, blob_convert, "check result");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
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");
equal(name, "data", "getAttachment 200 called");
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": "docy"
},
"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();
});
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.putAttachment
/////////////////////////////////////////////////////////////////
module("cloudooStorage.putAttachment", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("putAttachment called substorage putAttachment", function () {
stop();
expect(6);
var jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
}),
blob = new Blob([""]);
Storage200.prototype.get = function (id) {
if (id !== 'bar') {
throw new jIO.util.jIOError("can't find", 404);
}
return {};
};
Storage200.prototype.putAttachment = function (id, name, blob2) {
equal(id, "bar", "putAttachment 200 called");
equal(name, "foo", "putAttachment 200 called");
deepEqual(blob2, blob,
"putAttachment 200 called");
return "OK";
};
Storage200.prototype.put = function (id, doc) {
equal(id, "cloudoo/bar/foo", "put id 200 called");
deepEqual(doc, {
"attachment_id": "foo",
"convert_dict": {
'undefined': true
},
"doc_id": "bar",
"format": undefined,
"portal_type": "Conversion Info"
}, "put doc 200 called");
return id;
};
jio.putAttachment("bar", "foo", blob)
.then(function (result) {
equal(result, "OK");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("putAttachment convert from docx to docy", function () {
stop();
expect(10);
var blob = new Blob(["documentauformatdocy"], {type: "docy"}),
server = this.server,
blob_convert = new Blob(["documentauformatdocx"], {type: "docx"});
this.server.respondWith("POST", cloudoo_url, [200, {
"Content-Type": "text/xml"
}, '<?xml version="1.0" encoding="UTF-8"?>' +
'<string>ZG9jdW1lbnRhdWZvcm1hdGRvY3k=</string>']);
Storage200.prototype.putAttachment = function (id, att_id, data) {
equal(id, "bar", "putAttachment 200 called");
if (att_id === "data?docx") {
deepEqual(data, blob_convert, "putAttachment 200 called");
return "OK";
}
if (att_id === "data") {
deepEqual(data, blob, "putAttachment 200 called");
return "OK";
}
};
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": true,
"docy": true
},
"doc_id": "bar",
"format": "docy",
"portal_type": "Conversion Info"
}, "put doc 200 called");
return id;
};
this.jio.putAttachment("bar", "data?docx", blob_convert)
.then(function () {
equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, cloudoo_url);
equal(
server.requests[0].requestBody,
'<?xml version="1.0" encoding=\"UTF-8\"?><methodCall>' +
'<methodName>convertFile</methodName><params><param><value>' +
'<string>ZG9jdW1lbnRhdWZvcm1hdGRvY3g=</string></value></param>' +
'<param><value><string>docx</string></value></param>' +
'<param><value><string>docy' +
'</string></value></param></params></methodCall>'
);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// CloudooStorage.repair
/////////////////////////////////////////////////////////////////
module("cloudooStorage.repair", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "cloudoo",
url: cloudoo_url,
sub_storage: {
type: "cloudoostorage200"
}
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("repair convert attachment", function () {
stop();
expect(13);
var blob = new Blob(["documentauformatdocy"]),
server = this.server,
blob_convert = new Blob(["documentauformatdocx"], {type: "docx"});
this.server.respondWith("POST", cloudoo_url, [200, {
"Content-Type": "text/xml"
}, '<?xml version="1.0" encoding="UTF-8"?>' +
'<string>ZG9jdW1lbnRhdWZvcm1hdGRvY3g=</string>']);
Storage200.prototype.repair = function () {
return "OK";
};
Storage200.prototype.hasCapacity = function () {
return true;
};
Storage200.prototype.get = function (id) {
equal(id, 'cloudoo/bar/data', 'check get 200 called');
return {
"attachment_id": "data",
"convert_dict": {
"docx": "docy",
"docy": true
},
"doc_id": "bar",
"format": "docy",
"portal_type": "Conversion Info"
};
};
Storage200.prototype.put = function (id, doc) {
equal(id, "cloudoo/bar/data", "check put 200 called");
deepEqual(doc, {
"attachment_id": "data",
"convert_dict": {
"docx": true,
"docy": true
},
"doc_id": "bar",
"format": "docy",
"portal_type": "Conversion Info"
}, "check put 200 called");
};
Storage200.prototype.getAttachment = function (id, att_id) {
equal(id, 'bar', "check getAttachment 200 called");
equal(att_id, 'data', "check getAttachment 200 called");
return blob;
};
Storage200.prototype.putAttachment = function (id, att_id, result) {
equal(id, 'bar', 'check putAttachment 200 called');
equal(att_id, 'data?docx', "check puttAttachment 200 called");
deepEqual(result, blob_convert, "check putAttachment 200 called");
return "OK";
};
Storage200.prototype.buildQuery = function (options) {
equal(
options.query,
'portal_type: "Conversion Info"',
"check buildQuery 200 called"
);
equal(options.include_docs, true, 'check buildQuery 200 called');
return [{
id: "cloudoo/bar/data",
doc: {
'convert_dict': {
'docx': 'docy',
'docy': true
},
'doc_id': 'bar',
'attachment_id': 'data',
'format': 'docy',
'portal_type': 'Conversion Info'
},
value: {}
}];
};
this.jio.repair()
.then(function () {
equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, cloudoo_url);
equal(
server.requests[0].requestBody,
'<?xml version="1.0" encoding="UTF-8"?><methodCall>' +
'<methodName>convertFile</methodName><params><param><value>' +
'<string>ZG9jdW1lbnRhdWZvcm1hdGRvY3k=</string></value></param>' +
'<param><value><string>docy</string></value></param>' +
'<param><value><string>docx' +
'</string></value></param></params></methodCall>'
);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, Blob, sinon));
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
<!--script src="jio.storage/qiniustorage.tests.js"></script--> <!--script src="jio.storage/qiniustorage.tests.js"></script-->
<!--script src="jio.storage/indexstorage.tests.js"></script--> <!--script src="jio.storage/indexstorage.tests.js"></script-->
<script src="jio.storage/cryptstorage.tests.js"></script> <script src="jio.storage/cryptstorage.tests.js"></script>
<script src="jio.storage/cloudoostorage.tests.js"></script>
<script src="jio.storage/dropboxstorage.tests.js"></script> <script src="jio.storage/dropboxstorage.tests.js"></script>
<script src="jio.storage/zipstorage.tests.js"></script> <script src="jio.storage/zipstorage.tests.js"></script>
<script src="jio.storage/gdrivestorage.tests.js"></script> <script src="jio.storage/gdrivestorage.tests.js"></script>
......
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