Commit 77a8fbc0 authored by lucas.parsy's avatar lucas.parsy Committed by Romain Courteaud

added error handle to DropboxStorage's removeAttachment,

when attempting to remove an inexistent document.

added comments in dropboxstorage.js about the Dropbox API limitations.

updated dropboxstorage.tests.js to suit to previous changes in dropboxstorage.js
parent c43f7171
...@@ -171,9 +171,12 @@ ...@@ -171,9 +171,12 @@
}); });
}; };
//currently, putattachment will fail with files larger than 150MB, //currently, putAttachment will fail with files larger than 150MB,
//due to the Dropbox API. the API provides the "chunked_upload" method //due to the Dropbox API. the API provides the "chunked_upload" method
//to pass this limit, but upload process becomes more complex to implement. //to pass this limit, but upload process becomes more complex to implement.
//
//putAttachment will also create a folder if you try to put an attachment
//to an inexisting foler.
DropboxStorage.prototype.putAttachment = function (id, name, blob) { DropboxStorage.prototype.putAttachment = function (id, name, blob) {
id = restrictDocumentId(id); id = restrictDocumentId(id);
...@@ -225,18 +228,29 @@ ...@@ -225,18 +228,29 @@
}); });
}; };
//removeAttachment removes also directories.(due to Dropbox API)
DropboxStorage.prototype.removeAttachment = function (id, name) { DropboxStorage.prototype.removeAttachment = function (id, name) {
var that = this;
id = restrictDocumentId(id); id = restrictDocumentId(id);
restrictAttachmentId(name); restrictAttachmentId(name);
return jIO.util.ajax({ return new RSVP.Queue()
type: "POST", .push(function () {
url: remote_template.expand({ return jIO.util.ajax({
access_token: this._access_token, type: "POST",
root: this._root, url: remote_template.expand({
path: id + name access_token: that._access_token,
}) root: that._root,
}); path: id + name
})
});
}).push(undefined, function (error) {
if (error.target !== undefined && error.target.status === 404) {
throw new jIO.util.jIOError("Cannot find attachment: " +
id + ", " + name, 404);
}
});
}; };
jIO.addStorage('dropbox', DropboxStorage); jIO.addStorage('dropbox', DropboxStorage);
......
...@@ -50,9 +50,8 @@ ...@@ -50,9 +50,8 @@
}); });
test("put document", function () { test("put document", function () {
var url = "https://api.dropboxapi.com/1/fileops/" + var url = "https://api.dropboxapi.com/1/fileops/create_folder?access_token="
"create_folder?access_token=" + token + "&root=dropbox&path=%2Fput1%2F",
+ token + "&root=dropbox&path=/put1/",
server = this.server; server = this.server;
this.server.respondWith("POST", url, [201, { this.server.respondWith("POST", url, [201, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
...@@ -82,8 +81,8 @@ ...@@ -82,8 +81,8 @@
}); });
test("don't throw error when putting existing directory", function () { test("don't throw error when putting existing directory", function () {
var url = "https://api.dropboxapi.com/1/fileops/create_folder" + var url = "https://api.dropboxapi.com/1/fileops/create_folder?access_token="
"?access_token=" + token + "&root=dropbox&path=/existing/", + token + "&root=dropbox&path=%2Fexisting%2F",
server = this.server; server = this.server;
this.server.respondWith("POST", url, [405, { this.server.respondWith("POST", url, [405, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
...@@ -177,37 +176,25 @@ ...@@ -177,37 +176,25 @@
delete this.server; delete this.server;
} }
}); });
test("remove document", function () { test("remove document", function () {
var url_get = "https://api.dropboxapi.com/1/metadata/dropbox" + var url_delete = "https://api.dropboxapi.com/1/fileops/delete/?" +
"//remove1/?access_token=" + token, "access_token=" + token + "&root=dropbox&path=%2Fremove1%2F",
url_delete = "https://api.dropboxapi.com/1/fileops/delete/?" +
"access_token=" + token + "&root=dropbox&path=/remove1/",
server = this.server; server = this.server;
this.server.respondWith("GET", url_get, [204, {
"Content-Type": "text/xml"
}, '{"is_dir": true}']);
this.server.respondWith("POST", url_delete, [204, { this.server.respondWith("POST", url_delete, [204, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
}, '']); }, '']);
stop(); stop();
expect(13); expect(7);
this.jio.remove("/remove1/") this.jio.remove("/remove1/")
.then(function () { .then(function () {
equal(server.requests.length, 2); equal(server.requests.length, 1);
equal(server.requests[0].method, "GET"); equal(server.requests[0].method, "POST");
equal(server.requests[0].url, url_get); equal(server.requests[0].url, url_delete);
equal(server.requests[0].status, 204); equal(server.requests[0].status, 204);
equal(server.requests[0].requestBody, undefined); equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].responseText, '{"is_dir": true}'); equal(server.requests[0].responseText, '');
deepEqual(server.requests[0].requestHeaders, {}); deepEqual(server.requests[0].requestHeaders, {
equal(server.requests[1].method, "POST");
equal(server.requests[1].url, url_delete);
equal(server.requests[1].status, 204);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].responseText, '');
deepEqual(server.requests[1].requestHeaders, {
"Content-Type": "text/plain;charset=utf-8" "Content-Type": "text/plain;charset=utf-8"
}); });
}) })
...@@ -325,7 +312,7 @@ ...@@ -325,7 +312,7 @@
this.jio.get("/inexistent/") this.jio.get("/inexistent/")
.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: /inexistent/");
equal(error.status_code, 404); equal(error.status_code, 404);
}) })
.fail(function (error) { .fail(function (error) {
...@@ -428,7 +415,7 @@ ...@@ -428,7 +415,7 @@
this.jio.allAttachments("/inexistent/") this.jio.allAttachments("/inexistent/")
.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: /inexistent/");
equal(error.status_code, 404); equal(error.status_code, 404);
}) })
.fail(function (error) { .fail(function (error) {
...@@ -588,53 +575,19 @@ ...@@ -588,53 +575,19 @@
}); });
}); });
test("putAttachment to inexisting directory: expecting a 404", function () {
var blob = new Blob(["foo"]),
url = "/inexistent_dir/attachment1";
this.server.respondWith("PUT", url, [405, {"": ""}, ""]);
stop();
expect(3);
this.jio.putAttachment(
"/inexistent_dir/",
"attachment1",
blob
)
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot access subdocument");
equal(error.status_code, 404);
})
.always(function () {
start();
});
});
test("putAttachment document", function () { test("putAttachment document", function () {
var blob = new Blob(["foo"]), var blob = new Blob(["foo"]),
url_get_id = "https://api.dropboxapi.com/1/metadata/dropbox" + url_put_att = "https://content.dropboxapi.com/1/files_put/dropbox"
"//putAttachment1/?access_token=" + token, + "/putAttachment1/"
url_get_att = "https://api.dropboxapi.com/1/metadata/dropbox" + + "attachment1?access_token=" + token,
"//putAttachment1/attachment1/?access_token=" + token,
url_put_att = "https://content.dropboxapi.com/1/files_put/dropbox/" +
"putAttachment1/attachment1?access_token=" + token,
server = this.server; server = this.server;
this.server.respondWith("GET", url_get_id, [204, {
"Content-Type": "text/xml"
}, '{"is_dir": true, "contents": []}']);
this.server.respondWith("GET", url_get_att, [404, {
"Content-Type": "text/xml"
}, '']);
this.server.respondWith("PUT", url_put_att, [204, { this.server.respondWith("PUT", url_put_att, [204, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
}, ""]); }, ""]);
stop(); stop();
expect(17); expect(7);
this.jio.putAttachment( this.jio.putAttachment(
"/putAttachment1/", "/putAttachment1/",
...@@ -642,28 +595,16 @@ ...@@ -642,28 +595,16 @@
blob blob
) )
.then(function () { .then(function () {
equal(server.requests.length, 3); equal(server.requests.length, 1);
equal(server.requests[0].method, "GET"); equal(server.requests[0].method, "PUT");
equal(server.requests[0].url, url_get_id); 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, "");
"{\"is_dir\": true, \"contents\": []}"); deepEqual(server.requests[0].requestHeaders, {
deepEqual(server.requests[0].requestHeaders, {});
equal(server.requests[1].method, "GET");
equal(server.requests[1].url, url_get_att);
equal(server.requests[1].status, 404);
equal(server.requests[1].responseText, "");
deepEqual(server.requests[1].requestHeaders, {});
equal(server.requests[2].method, "PUT");
equal(server.requests[2].url, url_put_att);
equal(server.requests[2].status, 204);
equal(server.requests[2].responseText, "");
deepEqual(server.requests[2].requestHeaders, {
"Content-Type": "text/plain;charset=utf-8" "Content-Type": "text/plain;charset=utf-8"
}); });
equal(server.requests[2].requestBody, blob); equal(server.requests[0].requestBody, blob);
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -759,42 +700,31 @@ ...@@ -759,42 +700,31 @@
}); });
test("removeAttachment document", function () { test("removeAttachment document", function () {
var url_get_id = "https://api.dropboxapi.com/1/metadata/dropbox" + var url_delete = "https://api.dropboxapi.com/1/fileops/delete/" +
"//removeAttachment1/attachment1/?access_token=" + token,
url_delete = "https://api.dropboxapi.com/1/fileops/delete/" +
"?access_token=" + token + "&root=dropbox" + "?access_token=" + token + "&root=dropbox" +
"&path=/removeAttachment1/attachment1", "&path=%2FremoveAttachment1%2Fattachment1",
server = this.server; server = this.server;
this.server.respondWith("GET", url_get_id, [204, {
"Content-Type": "text/xml"
}, '{"is_dir": false}']);
this.server.respondWith("POST", url_delete, [204, { this.server.respondWith("POST", url_delete, [204, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
}, ""]); }, ""]);
stop(); stop();
expect(13); expect(7);
this.jio.removeAttachment( this.jio.removeAttachment(
"/removeAttachment1/", "/removeAttachment1/",
"attachment1" "attachment1"
) )
.then(function () { .then(function () {
equal(server.requests.length, 2); equal(server.requests.length, 1);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, url_get_id); equal(server.requests[0].method, "POST");
equal(server.requests[0].url, url_delete);
equal(server.requests[0].status, 204); equal(server.requests[0].status, 204);
equal(server.requests[0].requestBody, undefined); equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].responseText, "{\"is_dir\": false}"); equal(server.requests[0].responseText, "");
deepEqual(server.requests[0].requestHeaders, {}); deepEqual(server.requests[0].requestHeaders, {
equal(server.requests[1].method, "POST");
equal(server.requests[1].url, url_delete);
equal(server.requests[1].status, 204);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].responseText, "");
deepEqual(server.requests[1].requestHeaders, {
"Content-Type": "text/plain;charset=utf-8" "Content-Type": "text/plain;charset=utf-8"
}); });
}) })
...@@ -921,8 +851,8 @@ ...@@ -921,8 +851,8 @@
}); });
test("getAttachment document", function () { test("getAttachment document", function () {
var url = "https://content.dropboxapi.com/1/files/dropbox//" + var url = "https://content.dropboxapi.com/1/files/dropbox/" +
"getAttachment1/attachment1?access_token=" + token, "%2FgetAttachment1%2Fattachment1?access_token=" + token,
server = this.server; server = this.server;
this.server.respondWith("GET", url, [200, { this.server.respondWith("GET", url, [200, {
"Content-Type": "text/plain" "Content-Type": "text/plain"
......
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