Commit bbf74f1f authored by Tristan Cavelier's avatar Tristan Cavelier

Update DavStorage and Jio tests

parent 27016e9e
var newDAVStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'base' ), priv = {};
priv.secureDocId = function (string) {
var split = string.split('/'), i;
if (split[0] === '') {
split = split.slice(1);
}
for (i = 0; i < split.length; i+= 1) {
if (split[i] === '') { return ''; }
}
return split.join('%2F');
};
priv.convertSlashes = function (string) {
return string.split('/').join('%2F');
};
priv.restoreSlashes = function (string) {
return string.split('%2F').join('/');
};
priv.username = spec.username || '';
priv.secured_username = priv.convertSlashes(priv.username);
priv.applicationname = spec.applicationname || 'untitled';
priv.secured_applicationname = priv.convertSlashes(priv.applicationname);
priv.url = spec.url || '';
priv.password = spec.password || ''; // TODO : is it secured ?
......@@ -22,7 +43,7 @@ var newDAVStorage = function ( spec, my ) {
* @return {string} '' -> ok, 'message' -> error
*/
that.validateState = function() {
if (priv.username && priv.url) {
if (priv.secured_username && priv.url) {
return '';
}
return 'Need at least 2 parameters: "username" and "url".';
......@@ -54,50 +75,55 @@ var newDAVStorage = function ( spec, my ) {
return async;
};
that.post = function (command) {
that.put(command);
};
/**
* Saves a document in the distant dav storage.
* @method saveDocument
* @method put
*/
that.saveDocument = function (command) {
that.put = function (command) {
var secured_docid = priv.secureDocId(command.getDocId());
// TODO if path of /dav/user/applic does not exists, it won't work!
//// save on dav
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/' +
command.getPath(),
url: priv.url + '/' +
priv.secured_username + '/' +
priv.secured_applicationname + '/' +
secured_docid,
type: 'PUT',
data: command.getContent(),
data: command.getDocContent(),
async: true,
dataType: 'text', // TODO is it necessary ?
headers: {'Authorization':'Basic '+Base64.encode(
priv.username+':'+priv.password)},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function () {
that.success();
that.success({ok:true,id:command.getDocId()});
},
error: function (type) {
type.message = 'Cannot save "' + command.getPath() +
'" into DAVStorage.';
// TODO : make statusText to lower case and add '_'
type.error = type.statusText;
type.reason = 'Cannot save "' + command.getDocId() + '"';
type.message = type.reason + '.';
that.retry(type);
}
} );
//// end saving on dav
};
}; // end put
/**
* Loads a document from a distant dav storage.
* @method loadDocument
* @method get
*/
that.loadDocument = function (command) {
var doc = {},
getContent = function () {
that.get = function (command) {
var secured_docid = priv.secureDocId(command.getDocId()),
doc = {}, getContent = function () {
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/' +
command.getPath(),
url: priv.url + '/' +
priv.secured_username + '/' +
priv.secured_applicationname + '/' +
secured_docid,
type: "GET",
async: true,
dataType: 'text', // TODO is it necessary ?
......@@ -109,28 +135,31 @@ var newDAVStorage = function ( spec, my ) {
that.success(doc);
},
error: function (type) {
type.error = type.statusText; // TODO : to lower case
if (type.status === 404) {
type.message = 'Document "' +
command.getPath() +
'" not found in localStorage.';
command.getDocId() +
'" not found.';
type.reason = 'missing';
that.error(type);
} else {
type.message =
'Cannot load "' + command.getPath() +
'" from DAVStorage.';
type.reason =
'An error occured when trying to get "' +
command.getDocId() + '"';
type.message = type.reason + '.';
that.retry(type);
}
}
} );
};
doc.name = command.getPath(); // TODO : basename
doc._id = command.getDocId();
// NOTE : if (command.getOption('content_only') { return getContent(); }
// Get properties
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/' +
command.getPath(),
url: priv.url + '/' +
priv.secured_username + '/' +
priv.secured_applicationname + '/' +
secured_docid,
type: "PROPFIND",
async: true,
dataType: 'xml',
......@@ -141,12 +170,14 @@ var newDAVStorage = function ( spec, my ) {
$(xmlData).find(
'lp1\\:getlastmodified, getlastmodified'
).each( function () {
doc.last_modified = $(this).text();
doc._last_modified =
new Date($(this).text()).getTime();
});
$(xmlData).find(
'lp1\\:creationdate, creationdate'
).each( function () {
doc.creation_date = $(this).text();
doc._creation_date =
new Date($(this).text()).getTime();
});
if (!command.getOption('metadata_only')) {
getContent();
......@@ -155,11 +186,15 @@ var newDAVStorage = function ( spec, my ) {
}
},
error: function (type) {
type.message = 'Cannot load "' + command.getPath() +
'" informations from DAVStorage.';
if (type.status === 404) {
type.message = 'Cannot find "' + command.getDocId() +
'" informations.';
type.reason = 'missing';
that.error(type);
} else {
type.reason = 'Cannot get "' + command.getDocId() +
'" informations';
type.message = type.reason + '.';
that.retry(type);
}
}
......@@ -168,18 +203,18 @@ var newDAVStorage = function ( spec, my ) {
/**
* Gets a document list from a distant dav storage.
* @method getDocumentList
* @method allDocs
*/
that.getDocumentList = function (command) {
var document_array = [], file = {}, path_array = [],
that.allDocs = function (command) {
var rows = [],
am = priv.newAsyncModule(), o = {};
o.getContent = function (file) {
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/' +
file.name,
url: priv.url + '/' +
priv.secured_username + '/' +
priv.secured_applicationname + '/' +
priv.secureDocId(file.id),
type: "GET",
async: true,
dataType: 'text', // TODO : is it necessary ?
......@@ -187,24 +222,26 @@ var newDAVStorage = function ( spec, my ) {
Base64.encode(priv.username +':'+
priv.password)},
success: function (content) {
file.content = content;
file.value.content = content;
// WARNING : files can be disordered because
// of asynchronous action
document_array.push (file);
rows.push (file);
am.call(o,'success');
},
error: function (type) {
type.message = 'Cannot get a document '+
'content from DAVStorage.';
type.error = type.statusText; // TODO : to lower case
type.reason = 'Cannot get a document '+
'content from DAVStorage';
type.message = type.message + '.';
am.call(o,'error',[type]);
}
});
};
o.getDocumentList = function () {
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/',
url: priv.url + '/' +
priv.secured_username + '/' +
priv.secured_applicationname + '/',
async: true,
type: 'PROPFIND',
dataType: 'xml',
......@@ -222,41 +259,46 @@ var newDAVStorage = function ( spec, my ) {
}
response.each( function(i,data){
if(i>0) { // exclude parent folder
file = {};
var file = {value:{}};
$(data).find('D\\:href, href').each(function(){
path_array = $(this).text().split('/');
file.name =
(path_array[path_array.length-1] ?
path_array[path_array.length-1] :
path_array[path_array.length-2]+'/');
var split = $(this).text().split('/');
file.id = split[split.length-1];
file.id = priv.restoreSlashes(file.id);
file.key = file.id;
});
if (file.name === '.htaccess' ||
file.name === '.htpasswd') { return; }
if (file.id === '.htaccess' ||
file.id === '.htpasswd') { return; }
$(data).find(
'lp1\\:getlastmodified, getlastmodified'
).each(function () {
file.last_modified = $(this).text();
file.value._last_modified =
new Date($(this).text()).getTime();
});
$(data).find(
'lp1\\:creationdate, creationdate'
).each(function () {
file.creation_date = $(this).text();
file.value._creation_date =
new Date($(this).text()).getTime();
});
if (!command.getOption ('metadata_only')) {
am.call(o,'getContent',[file]);
} else {
document_array.push (file);
rows.push (file);
am.call(o,'success');
}
}
});
},
error: function (type) {
type.message =
'Cannot get a document list from DAVStorage.';
if (type.status === 404) {
type.error = 'not_found';
type.reason = 'missing';
am.call(o,'error',[type]);
} else {
type.error = type.statusText; // TODO : to lower case
type.reason =
'Cannot get a document list from DAVStorage';
type.message = type.reason + '.';
am.call(o,'retry',[type]);
}
}
......@@ -278,36 +320,43 @@ var newDAVStorage = function ( spec, my ) {
am.neverCall(o,'retry');
am.neverCall(o,'success');
am.neverCall(o,'error');
that.success(document_array);
that.success({total_rows:rows.length,
rows:rows});
};
am.call (o,'getDocumentList');
};
}; // end allDocs
/**
* Removes a document from a distant dav storage.
* @method removeDocument
* @method remove
*/
that.removeDocument = function (command) {
that.remove = function (command) {
var secured_docid = priv.secureDocId(command.getDocId());
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/' +
command.getPath(),
url: priv.url + '/' +
priv.secured_username + '/' +
priv.secured_applicationname + '/' +
secured_docid,
type: "DELETE",
async: true,
headers: {'Authorization':'Basic '+Base64.encode(
priv.username + ':' + priv.password )},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function () {
that.success();
success: function (data,state,type) {
that.success({ok:true,id:command.getDocId()});
},
error: function (type) {
error: function (type,state,statusText) {
if (type.status === 404) {
that.success();
//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.message = 'Cannot remove "' + that.getFileName() +
'" from DAVStorage.';
type.reason = 'Cannot remove "' + that.getDocId() + '"';
type.message = type.reason + '.';
that.retry(type);
}
}
......
......@@ -640,20 +640,20 @@ test ('Document load', function () {
o.mytest = function (message,doc,errprop,errget) {
var server = o.t.sandbox.useFakeServer();
server.respondWith (
"PROPFIND","https://ca-davstorage:8080/dav/davload/jiotests/file",
"PROPFIND","https://ca-davstorage:8080/davload/jiotests/file",
[errprop,{'Content-Type':'text/xml; charset="utf-8"'},
o.davload]);
server.respondWith (
"GET","https://ca-davstorage:8080/dav/davload/jiotests/file",
"GET","https://ca-davstorage:8080/davload/jiotests/file",
[errget,{},'content']);
o.f = function (result) {
if (result && result.status) {
result = undefined;
o.f = function (err,val) {
if (err) {
err = err.status;
}
deepEqual (result,doc,message);
deepEqual (err || val,doc,message);
};
o.t.spy(o,'f');
o.jio.loadDocument('file',{success:o.f,error:o.f,max_retry:1});
o.jio.get('file',{max_retry:1},o.f);
o.clock.tick(1000);
server.respond();
if (!o.f.calledOnce) {
......@@ -676,11 +676,11 @@ test ('Document load', function () {
// 403 Forbidden
// 404 Not Found
// load an inexistant document.
o.mytest ('load inexistant document',undefined,404,404);
o.mytest ('load inexistant document',404,404,404);
// load a document.
o.mytest ('load document',{name:'file',content:'content',
last_modified:1335953199000,
creation_date:1335953202000},207,200);
o.mytest ('load document',{_id:'file',content:'content',
_last_modified:1335953199000,
_creation_date:1335953202000},207,200);
o.jio.stop();
});
......@@ -696,16 +696,16 @@ test ('Document save', function () {
var server = o.t.sandbox.useFakeServer();
server.respondWith (
// lastmodified = 7000, creationdate = 5000
"PROPFIND","https://ca-davstorage:8080/dav/davsave/jiotests/file",
"PROPFIND","https://ca-davstorage:8080/davsave/jiotests/file",
[errnoprop,{'Content-Type':'text/xml; charset="utf-8"'},
o.davsave]);
server.respondWith (
"PUT",
"https://ca-davstorage:8080/dav/davsave/jiotests/file",
"https://ca-davstorage:8080/davsave/jiotests/file",
[errnoput, {'Content-Type':'x-www-form-urlencoded'},
'content']);
server.respondWith (
"GET","https://ca-davstorage:8080/dav/davsave/jiotests/file",
"GET","https://ca-davstorage:8080/davsave/jiotests/file",
[errnoprop===207?200:errnoprop,{},'content']);
// server.respondWith ("MKCOL","https://ca-davstorage:8080/dav",
// [200,{},'']);
......@@ -714,16 +714,9 @@ test ('Document save', function () {
// server.respondWith ("MKCOL",
// "https://ca-davstorage:8080/dav/davsave/jiotests",
// [200,{},'']);
o.f = function (result) {
if (result && result.status) {
result = 'fail';
} else {
result = 'done';
}
deepEqual (result,value,message);
};
o.f = basic_test_function_generator(o,'value',value,message);
o.t.spy(o,'f');
o.jio.saveDocument('file','content',{success:o.f,error:o.f});
o.jio.put({_id:'file',content:'content'},o.f);
o.clock.tick(1000);
server.respond();
if (!o.f.calledOnce) {
......@@ -749,10 +742,10 @@ test ('Document save', function () {
// mytest('create path if not exists, and create document',
// true,201,404);
// the document does not exist, we want to create it
o.mytest('create document','done',201,404);
o.mytest('create document',{ok:true,id:'file'},201,404);
o.clock.tick(8000);
// the document already exists, we want to overwrite it
o.mytest('overwrite document','done',204,207);
o.mytest('overwrite document',{ok:true,id:'file'},204,207);
o.jio.stop();
});
......@@ -767,28 +760,27 @@ test ('Get Document List', function () {
o.mytest = function (message,metadata_only,value,errnoprop) {
var server = o.t.sandbox.useFakeServer();
server.respondWith (
"PROPFIND",'https://ca-davstorage:8080/dav/davlist/jiotests/',
"PROPFIND",'https://ca-davstorage:8080/davlist/jiotests/',
[errnoprop,{'Content-Type':'text/xml; charset="utf-8"'},
o.davlist]);
server.respondWith (
"GET","https://ca-davstorage:8080/dav/davlist/jiotests/file",
"GET","https://ca-davstorage:8080/davlist/jiotests/file",
[200,{},'content']);
server.respondWith (
"GET","https://ca-davstorage:8080/dav/davlist/jiotests/memo",
"GET","https://ca-davstorage:8080/davlist/jiotests/memo",
[200,{},'content2']);
o.f = function (result) {
if (result && result.status) {
o.f = function (err,val) {
if (err) {
result = undefined;
} else {
deepEqual (objectifyDocumentArray(result),
deepEqual (objectifyDocumentArray(val.rows),
objectifyDocumentArray(value),message);
return;
}
deepEqual (result, value, message);
};
o.t.spy(o,'f');
o.jio.getDocumentList('.',{success:o.f,error:o.f,
metadata_only:metadata_only});
o.jio.allDocs({metadata_only:metadata_only},o.f);
o.clock.tick(1000);
server.respond();
if (!o.f.calledOnce) {
......@@ -804,16 +796,34 @@ test ('Get Document List', function () {
url:'https://ca-davstorage:8080',
applicationname:'jiotests'});
o.mytest('fail to get list',true,undefined,404);
o.mytest('getting list',true,[{name:'file',creation_date:1335962911000,
last_modified:1335962907000},
{name:'memo',creation_date:1335894073000,
last_modified:1335955713000}],207);
o.mytest('getting list',false,[{name:'file',content:'content',
creation_date:1335962911000,
last_modified:1335962907000},
{name:'memo',content:'content2',
creation_date:1335894073000,
last_modified:1335955713000}],207);
o.mytest('getting list',true,[{
id:'file',key:'file',
value:{
_creation_date:1335962911000,
_last_modified:1335962907000
}
},{
id:'memo',key:'memo',
value:{
_creation_date:1335894073000,
_last_modified:1335955713000
}
}],207);
o.mytest('getting list',false,[{
id:'file',key:'file',
value:{
content:'content',
_creation_date:1335962911000,
_last_modified:1335962907000
}
},{
id:'memo',key:'memo',
value:{
content:'content2',
_creation_date:1335894073000,
_last_modified:1335955713000
}
}],207);
o.jio.stop();
});
......@@ -825,18 +835,16 @@ test ('Remove document', function () {
o.mytest = function (message,value,errnodel) {
var server = o.t.sandbox.useFakeServer();
server.respondWith (
"DELETE","https://ca-davstorage:8080/dav/davremove/jiotests/file",
"DELETE","https://ca-davstorage:8080/davremove/jiotests/file",
[errnodel,{},'']);
o.f = function (result) {
if (result && result.status) {
result = 'fail';
} else {
result = 'done';
o.f = function (err,val) {
if (err) {
err = err.status;
}
deepEqual (result,value,message);
deepEqual (err || val,value,message);
};
o.t.spy(o,'f');
o.jio.removeDocument('file',{success:o.f,error:o.f});
o.jio.remove({_id:'file'},o.f);
o.clock.tick(1000);
server.respond();
if (!o.f.calledOnce) {
......@@ -850,10 +858,10 @@ test ('Remove document', function () {
o.jio = JIO.newJio({type:'dav',username:'davremove',
password:'checkpwd',
url:'https://ca-davstorage:8080',
appliactionname:'jiotests'});
applicationname:'jiotests'});
o.mytest('remove document','done',204);
o.mytest('remove an already removed document','done',404);
o.mytest('remove document',{ok:true,id:'file'},204);
o.mytest('remove an already removed document',404,404);
o.jio.stop();
});
......
<?xml version="1.0" encoding="utf-8"?>
<D:multistatus xmlns:D="DAV:">
<D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/">
<D:href>/dav/davgetlist/jiotests/</D:href>
<D:href>/davgetlist/jiotests/</D:href>
<D:propstat>
<D:prop>
<lp1:resourcetype><D:collection/></lp1:resourcetype>
......@@ -25,7 +25,7 @@
</D:propstat>
</D:response>
<D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/">
<D:href>/dav/davgetlist/jiotests/file</D:href>
<D:href>/davgetlist/jiotests/file</D:href>
<D:propstat>
<D:prop>
<lp1:resourcetype/>
......@@ -50,7 +50,7 @@
</D:propstat>
</D:response>
<D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/">
<D:href>/dav/davgetlist/jiotests/memo</D:href>
<D:href>/davgetlist/jiotests/memo</D:href>
<D:propstat>
<D:prop>
<lp1:resourcetype/>
......
<?xml version="1.0" encoding="utf-8"?>
<D:multistatus xmlns:D="DAV:">
<D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/">
<D:href>/dav/davload/jiotests/file</D:href>
<D:href>/davload/jiotests/file</D:href>
<D:propstat>
<D:prop>
<lp1:resourcetype/>
......@@ -25,4 +25,4 @@
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>
</D:multistatus>
\ No newline at end of file
</D:multistatus>
<?xml version="1.0" encoding="utf-8"?>
<D:multistatus xmlns:D="DAV:">
<D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/">
<D:href>/dav/davsave/jiotests/file</D:href>
<D:href>/davsave/jiotests/file</D:href>
<D:propstat>
<D:prop>
<lp1:resourcetype/>
......@@ -25,4 +25,4 @@
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>
</D:multistatus>
\ No newline at end of file
</D:multistatus>
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