Commit 4ccd05a0 authored by Sven Franck's avatar Sven Franck

webDav Storage REMOVE new API and qunit tests

parent b8faaed2
...@@ -31,6 +31,21 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -31,6 +31,21 @@ jIO.addStorageType('dav', function (spec, my) {
return string.split('%2F').join('/'); return string.split('%2F').join('/');
}; };
/**
* Checks if an object has no enumerable keys
* @method objectIsEmpty
* @param {object} obj The object
* @return {boolean} true if empty, else false
*/
priv.objectIsEmpty = function (obj) {
var k;
for (k in obj) {
if (obj.hasOwnProperty(k)) {
return false;
}
}
return true;
};
// ==================== Attributes ==================== // ==================== Attributes ====================
priv.username = spec.username || ''; priv.username = spec.username || '';
...@@ -406,7 +421,7 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -406,7 +421,7 @@ jIO.addStorageType('dav', function (spec, my) {
*/ */
that.get = function (command) { that.get = function (command) {
var docid = command.getDocId(), doc, var docid = command.getDocId(), doc,
secured_docid; secured_docid, secured_attachmentid, attachment_url;
// no docId // no docId
if (!(typeof docid === "string" && docid !== "")) { if (!(typeof docid === "string" && docid !== "")) {
...@@ -430,8 +445,6 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -430,8 +445,6 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
return; return;
} }
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid;
if (typeof command.getAttachmentId() === "string") { if (typeof command.getAttachmentId() === "string") {
secured_attachmentid = priv.secureDocId(command.getAttachmentId()); secured_attachmentid = priv.secureDocId(command.getAttachmentId());
...@@ -463,6 +476,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -463,6 +476,9 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}); });
} else { } else {
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid;
// get document // get document
$.ajax({ $.ajax({
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
...@@ -495,6 +511,259 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -495,6 +511,259 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}; };
/**
* Remove a document or attachment
* @method remove
* @param {object} command The JIO command
*/
that.remove = function (command) {
var docid = command.getDocId(), doc,
secured_docid, secured_attachmentid, attachment_url,
attachment_list = [], i, j, k = 1;
// no docId
if (!(typeof docid === "string" && docid !== "")) {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Cannot create document which id is undefined",
"reason": "Document id is undefined"
});
return;
}
// no cors support
if (priv.checkCors === false) {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined"
});
return;
}
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid;
if (typeof command.getAttachmentId() === "string") {
secured_attachmentid = priv.secureDocId(command.getAttachmentId());
attachment_url = url + '/' + secured_attachmentid;
// remove attachment
$.ajax({
url: attachment_url + '?_=' + Date.now(),
type: 'REMOVE',
async: true,
crossdomain : true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
success: function (response) {
// retrieve underlying document
$.ajax({
url: url + '?_=' + Date.now(),
type: 'GET',
async: true,
dataType: 'text',
crossdomain : true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
success: function (response) {
// underlying document
doc = JSON.parse(response);
// update doc._attachments
if (typeof doc._attachments === "object") {
if (typeof doc._attachments[command.getAttachmentId()] ===
"object") {
delete doc._attachments[command.getAttachmentId()];
if (priv.objectIsEmpty(doc._attachments)) {
delete doc._attachments;
}
// PUT back to server
$.ajax({
url: url + '?_=' + Date.now(),
type: 'PUT',
data: doc,
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function (response) {
that.success({
"ok": true,
"id": command.getDocId()+'/'+command.getAttachmentId()
});
},
error: function (type) {
that.error({
"status": 409,
"statusText": "Conflicts",
"error": "conflicts",
"message": "Cannot modify document",
"reason": "Error trying to update document attachments"
});
return;
}
});
} else {
// sure this if-else is needed?
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the document",
"reason": "Error trying to update document attachments"
});
}
} else {
// no attachments, we are done
that.success({
"ok": true,
"id": command.getDocId() + '/' + command.getAttachmentId()
});
}
},
error: function (type) {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the document",
"reason": "Document does not exist"
});
}
});
},
error: function (type) {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the attachment",
"reason": "Error trying to remove attachment"
});
}
});
} else {
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid;
// get document to also remove all attachments
$.ajax({
url: url + '?_=' + Date.now(),
type: 'GET',
async: true,
dataType: 'text',
crossdomain : true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
success: function (response) {
doc = JSON.parse(response);
// prepare attachment loop
if (typeof doc._attachments === "object") {
// prepare list of attachments
for (i in doc._attachments) {
if (doc._attachments.hasOwnProperty(i)) {
attachment_list.push(i);
}
}
}
// delete document
$.ajax({
url: url + '?_=' + Date.now(),
type: 'REMOVE',
async: true,
crossdomain : true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
success: function (response) {
j = attachment_list.length;
// no attachments, done
if (j === 0) {
that.success({
"ok": true,
"id": command.getDocId()
});
} else {
for (i = 0; i < j; i += 1) {
secured_attachmentid = priv.secureDocId(attachment_list[i]);
attachment_url = url + '/' + secured_attachmentid;
$.ajax({
url: attachment_url + '?_=' + Date.now(),
type: 'REMOVE',
async: true,
crossdomain : true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
success: function (response) {
// all deleted, return response, need k as async couter
if (j === k){
that.success({
"ok": true,
"id": command.getDocId()
});
} else {
k += 1;
}
},
error: function (type) {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the attachment",
"reason": "Error trying to remove attachment"
});
}
});
};
}
},
error: function (type) {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the document",
"reason": "Error trying to remove document"
});
}
});
},
error: function (type) {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the document",
"reason": "Document does not exist"
});
}
});
}
};
/** /**
* Gets a document list from a distant dav storage. * Gets a document list from a distant dav storage.
* @method allDocs * @method allDocs
...@@ -629,46 +898,5 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -629,46 +898,5 @@ jIO.addStorageType('dav', function (spec, my) {
am.call(o, 'getDocumentList'); am.call(o, 'getDocumentList');
}; // end allDocs }; // end allDocs
/**
* Removes a document from a distant dav storage.
* @method remove
*/
that.remove = function (command) {
var secured_docid = priv.secureDocId(command.getDocId());
$.ajax({
url: priv.url + '/' + priv.secured_username + '/' +
priv.secured_application_name + '/' + secured_docid + '?_=' +
Date.now(),
type: "DELETE",
async: true,
headers: {
'Authorization': 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'}, // cross domain
// jslint: removed params data, state, type
success: function () {
that.success({
ok: true,
id: command.getDocId()
});
},
error: function (type) {
if (type.status === 404) {
//that.success({ok:true,id:command.getDocId()});
type.error = 'not_found';
type.reason = 'missing';
type.message = 'Cannot remove missing file.';
that.error(type);
} else {
type.reason = 'Cannot remove "' + that.getDocId() + '"';
type.message = type.reason + '.';
that.retry(type);
}
}
});
};
return that; return that;
}); });
\ No newline at end of file
...@@ -2232,32 +2232,97 @@ test ("Get", function(){ ...@@ -2232,32 +2232,97 @@ test ("Get", function(){
// check for credentials in sinon // check for credentials in sinon
}); });
test ("Remove", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type": "dav",
"username": "davremove",
"password": "checkpwd",
"url": "https://ca-davstorage:8080"
});
// remove inexistent document
o.addFakeServerResponse("GET", "remove1", 404, "HTML RESPONSE");
o.spy(o, "status", 404, "Remove non existening document");
o.jio.remove({"_id": "remove1"}, o.f);
o.clock.tick(5000);
o.server.respond();
// remove inexistent document/attachment
o.addFakeServerResponse("GET", "remove1/remove2", 404, "HTML RESPONSE");
o.spy(o, "status", 404, "Remove inexistent document/attachment");
o.jio.remove({"_id": "remove1/remove2"}, o.f);
o.clock.tick(5000);
o.server.respond();
// remove document
o.answer = JSON.stringify({"_id": "remove3", "title": "some doc"});
o.addFakeServerResponse("GET", "remove3", 200, o.answer);
o.addFakeServerResponse("REMOVE", "remove3", 200, "HTML RESPONSE");
o.spy(o, "value", {"ok": true, "id": "remove3"}, "Remove document");
o.jio.remove({"_id": "remove3"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.answer = JSON.stringify({
"_id": "remove4",
"title": "some doc",
"_attachments": {
"remove5": {
"length": 4,
"digest": "md5-d41d8cd98f00b204e9800998ecf8427e"
}
}
});
// remove attachment
o.addFakeServerResponse("GET", "remove4", 200, o.answer);
o.addFakeServerResponse("PUT", "remove4", 201, "HTML RESPONSE");
o.addFakeServerResponse("REMOVE", "remove4/remove5", 200, "HTML RESPONSE");
o.spy(o, "value", {"ok": true, "id": "remove4/remove5"},
"Remove attachment");
o.jio.remove({"_id": "remove4/remove5"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.answer = JSON.stringify({
"_id": "remove6",
"title": "some other doc",
"_attachments": {
"remove7": {
"length": 4,
"digest": "md5-d41d8cd98f00b204e9800998ecf8427e"
},
"remove8": {
"length": 4,
"digest": "md5-e41d8cd98f00b204e9800998ecf8427e"
},
"remove9": {
"length": 4,
"digest": "md5-f41d8cd98f00b204e9800998ecf8427e"
}
}
});
// remove document with multiple attachments
o.addFakeServerResponse("GET", "remove6", 200, o.answer);
o.addFakeServerResponse("REMOVE", "remove6/remove7", 200, "HTML RESPONSE");
o.addFakeServerResponse("REMOVE", "remove6/remove8", 200, "HTML RESPONSE");
o.addFakeServerResponse("REMOVE", "remove6/remove9", 200, "HTML RESPONSE");
o.addFakeServerResponse("REMOVE", "remove6", 200, "HTML RESPONSE");
o.spy(o, "value", {"ok": true, "id": "remove6"},
"Remove document with multiple attachments");
o.jio.remove({"_id": "remove6"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.jio.stop();
// do the same tests live webDav-Server/simulate CORS
// check for credentials in sinon
});
/*
// note: http errno:
// 200 OK
// 201 Created
// 204 No Content
// 207 Multi Status
// 403 Forbidden
// 404 Not Found
// 405 Not Allowed
server.respondWith (
// lastmodified = 7000, creationdate = 5000
"PROPFIND",
/https:\/\/ca-davstorage:8080\/davpost\/myFile(\?.*|$)/,
[errnoprop,{'Content-Type':'text/xml; charset="utf-8"'},
o.davpost]);
server.respondWith ("MKCOL","https://ca-davstorage:8080/dav",
[200,{},'']);
server.respondWith ("MKCOL","https://ca-davstorage:8080/dav/davpost",
[200,{},'']);
server.respondWith ("MKCOL",
"https://ca-davstorage:8080/dav/davpost/jiotests",
[200,{},'']);
*/
/* /*
test ('Get Document List', function () { test ('Get Document List', function () {
...@@ -2341,46 +2406,6 @@ test ('Get Document List', function () { ...@@ -2341,46 +2406,6 @@ test ('Get Document List', function () {
o.jio.stop(); o.jio.stop();
}); });
test ('Remove document', function () {
// Test if DavStorage can remove documents.
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this;
o.clock.tick(base_tick);
o.mytest = function (message,value,errnodel) {
var server = o.t.sandbox.useFakeServer();
server.respondWith (
"DELETE",
/https:\/\/ca-davstorage:8080\/davremove\/jiotests\/file(\?.*|$)/,
[errnodel,{},'']);
o.f = function (err,val) {
if (err) {
err = err.status;
}
deepEqual (err || val,value,message);
};
o.t.spy(o,'f');
o.jio.remove({_id:'file'},o.f);
o.clock.tick(1000);
server.respond();
if (!o.f.calledOnce) {
if (o.f.called) {
ok(false, 'too much results');
} else {
ok(false, 'no response');
}
}
};
o.jio = JIO.newJio({type:'dav',username:'davremove',
password:'checkpwd',
url:'https://ca-davstorage:8080',
application_name:'jiotests'});
o.mytest('remove document',{ok:true,id:'file'},204);
o.mytest('remove an already removed document',404,404);
o.jio.stop();
});
*/ */
/* /*
module ('Jio ReplicateStorage'); module ('Jio ReplicateStorage');
......
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