Commit 27016e9e authored by Tristan Cavelier's avatar Tristan Cavelier

Update LocalStorage and Jio tests

parent 148121db
...@@ -5,12 +5,32 @@ ...@@ -5,12 +5,32 @@
var newLocalStorage = function ( spec, my ) { var newLocalStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'base' ), priv = {}; 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.username = spec.username || '';
priv.secured_username = priv.convertSlashes(priv.username);
priv.applicationname = spec.applicationname || 'untitled'; priv.applicationname = spec.applicationname || 'untitled';
priv.secured_applicationname = priv.convertSlashes(priv.applicationname);
var storage_user_array_name = 'jio/local_user_array'; var storage_user_array_name = 'jio/local_user_array';
var storage_file_array_name = 'jio/local_file_name_array/' + var storage_file_array_name = 'jio/local_file_name_array/' +
priv.username + '/' + priv.applicationname; priv.secured_username + '/' + priv.secured_applicationname;
var super_serialized = that.serialized; var super_serialized = that.serialized;
that.serialized = function() { that.serialized = function() {
...@@ -21,7 +41,7 @@ var newLocalStorage = function ( spec, my ) { ...@@ -21,7 +41,7 @@ var newLocalStorage = function ( spec, my ) {
}; };
that.validateState = function() { that.validateState = function() {
if (priv.username) { if (priv.secured_username) {
return ''; return '';
} }
return 'Need at least one parameter: "username".'; return 'Need at least one parameter: "username".';
...@@ -103,42 +123,64 @@ var newLocalStorage = function ( spec, my ) { ...@@ -103,42 +123,64 @@ var newLocalStorage = function ( spec, my ) {
new_array); new_array);
}; };
priv.checkSecuredDocId = function (secured_docid,docid,method) {
if (!secured_docid) {
that.error({
status:403,statusText:'Method Not Allowed',
error:'method_not_allowed',
message:'Cannot '+method+' "'+docid+
'", file name is incorrect.',
reason:'Cannot '+method+' "'+docid+
'", file name is incorrect'
});
return false;
}
return true;
};
that.post = function (command) {
that.put(command);
};
/** /**
* Saves a document in the local storage. * Saves a document in the local storage.
* It will store the file in 'jio/local/USR/APP/FILE_NAME'. * It will store the file in 'jio/local/USR/APP/FILE_NAME'.
* @method saveDocument * @method put
*/ */
that.saveDocument = function (command) { that.put = function (command) {
// wait a little in order to simulate asynchronous saving // wait a little in order to simulate asynchronous saving
setTimeout (function () { setTimeout (function () {
var doc = null, path = var secured_docid = priv.secureDocId(command.getDocId()),
'jio/local/'+priv.username+'/'+ doc = null, path =
priv.applicationname+'/'+ 'jio/local/'+priv.secured_username+'/'+
command.getPath(); priv.secured_applicationname+'/'+
secured_docid;
if (!priv.checkSecuredDocId(
secured_docid,command.getDocId(),'put')) {return;}
// reading // reading
doc = LocalOrCookieStorage.getItem(path); doc = LocalOrCookieStorage.getItem(path);
if (!doc) { if (!doc) {
// create document // create document
doc = { doc = {
'name': command.getPath(), _id: command.getDocId(),
'content': command.getContent(), content: command.getDocContent(),
'creation_date': Date.now(), _creation_date: Date.now(),
'last_modified': Date.now() _last_modified: Date.now()
}; };
if (!priv.userExists(priv.username)) { if (!priv.userExists(priv.secured_username)) {
priv.addUser (priv.username); priv.addUser (priv.secured_username);
} }
priv.addFileName(command.getPath()); priv.addFileName(secured_docid);
} else { } else {
// overwriting // overwriting
doc.last_modified = Date.now(); doc.content = command.getDocContent();
doc.content = command.getContent(); doc._last_modified = Date.now();
} }
LocalOrCookieStorage.setItem(path, doc); LocalOrCookieStorage.setItem(path, doc);
that.success (); that.success ({ok:true,id:command.getDocId()});
}); });
}; // end saveDocument }; // end put
/** /**
* Loads a document from the local storage. * Loads a document from the local storage.
...@@ -146,22 +188,25 @@ var newLocalStorage = function ( spec, my ) { ...@@ -146,22 +188,25 @@ var newLocalStorage = function ( spec, my ) {
* You can add an 'options' object to the job, it can contain: * You can add an 'options' object to the job, it can contain:
* - metadata_only {boolean} default false, retrieve the file metadata * - metadata_only {boolean} default false, retrieve the file metadata
* only if true. * only if true.
* @method loadDocument * @method get
*/ */
that.loadDocument = function (command) { that.get = function (command) {
// document object is {'name':string,'content':string,
// 'creation_date':date,'last_modified':date}
setTimeout(function () { setTimeout(function () {
var doc = null; var secured_docid = priv.secureDocId(command.getDocId()),
doc = null;
if (!priv.checkSecuredDocId(
secured_docid,command.getDocId(),'get')) {return;}
doc = LocalOrCookieStorage.getItem( doc = LocalOrCookieStorage.getItem(
'jio/local/'+priv.username+'/'+ 'jio/local/'+priv.secured_username+'/'+
priv.applicationname+'/'+command.getPath()); priv.secured_applicationname+'/'+secured_docid);
if (!doc) { if (!doc) {
that.error ({status:404,statusText:'Not Found.', that.error ({status:404,statusText:'Not Found.',
message:'Document "'+ command.getPath() + error:'not_found',
'" not found in localStorage.'}); message:'Document "'+ command.getDocId() +
'" not found.',
reason:'missing'});
} else { } else {
if (command.getOption('metadata_only')) { if (command.getOption('metadata_only')) {
delete doc.content; delete doc.content;
...@@ -169,22 +214,20 @@ var newLocalStorage = function ( spec, my ) { ...@@ -169,22 +214,20 @@ var newLocalStorage = function ( spec, my ) {
that.success (doc); that.success (doc);
} }
}); });
}; // end loadDocument }; // end get
/** /**
* Gets a document list from the local storage. * Gets a document list from the local storage.
* It will retreive an array containing files meta data owned by * It will retreive an array containing files meta data owned by
* the user. * the user.
* @method getDocumentList * @method allDocs
*/ */
that.getDocumentList = function (command) { that.allDocs = function (command) {
// the list is [object,object] -> object = {'name':string,
// 'last_modified':date,'creation_date':date}
setTimeout(function () { setTimeout(function () {
var new_array = [], array = [], i, l, k = 'key', var new_array = [], array = [], i, l, k = 'key',
path = 'jio/local/'+priv.username+'/'+ priv.applicationname, path = 'jio/local/'+priv.secured_username+'/'+
file_object = {}; priv.secured_applicationname, file_object = {};
array = priv.getFileNameArray(); array = priv.getFileNameArray();
for (i = 0, l = array.length; i < l; i += 1) { for (i = 0, l = array.length; i < l; i += 1) {
...@@ -193,39 +236,43 @@ var newLocalStorage = function ( spec, my ) { ...@@ -193,39 +236,43 @@ var newLocalStorage = function ( spec, my ) {
if (file_object) { if (file_object) {
if (command.getOption('metadata_only')) { if (command.getOption('metadata_only')) {
new_array.push ({ new_array.push ({
name:file_object.name, id:file_object._id,key:file_object._id,value:{
creation_date:file_object.creation_date, _creation_date:file_object._creation_date,
last_modified:file_object.last_modified}); _last_modified:file_object._last_modified}});
} else { } else {
new_array.push ({ new_array.push ({
name:file_object.name, id:file_object._id,key:file_object._id,value:{
content:file_object.content, content:file_object.content,
creation_date:file_object.creation_date, _creation_date:file_object._creation_date,
last_modified:file_object.last_modified}); _last_modified:file_object._last_modified}});
} }
} }
} }
that.success (new_array); that.success ({total_rows:new_array.length,rows:new_array});
}); });
}; // end getDocumentList }; // end allDocs
/** /**
* Removes a document from the local storage. * Removes a document from the local storage.
* It will also remove the path from the local file array. * It will also remove the path from the local file array.
* @method removeDocument * @method remove
*/ */
that.removeDocument = function (command) { that.remove = function (command) {
setTimeout (function () { setTimeout (function () {
var path = 'jio/local/'+ var secured_docid = priv.secureDocId(command.getDocId()),
priv.username+'/'+ path = 'jio/local/'+
priv.applicationname+'/'+ priv.secured_username+'/'+
command.getPath(); priv.secured_applicationname+'/'+
secured_docid;
if (!priv.checkSecuredDocId(
secured_docid,command.getDocId(),'remove')) {return;}
// deleting // deleting
LocalOrCookieStorage.deleteItem(path); LocalOrCookieStorage.deleteItem(path);
priv.removeFileName(command.getPath()); priv.removeFileName(secured_docid);
that.success (); that.success ({ok:true,id:command.getDocId()});
}); });
}; }; // end remove
return that; return that;
}; };
Jio.addStorageType('local', newLocalStorage); Jio.addStorageType('local', newLocalStorage);
...@@ -105,21 +105,21 @@ addFileToLocalStorage = function (user,appid,file) { ...@@ -105,21 +105,21 @@ addFileToLocalStorage = function (user,appid,file) {
userarray.push(user); userarray.push(user);
LocalOrCookieStorage.setItem('jio/local_user_array',userarray); LocalOrCookieStorage.setItem('jio/local_user_array',userarray);
LocalOrCookieStorage.setItem( LocalOrCookieStorage.setItem(
'jio/local_file_name_array/'+user+'/'+appid,[file.name]); 'jio/local_file_name_array/'+user+'/'+appid,[file._id]);
} else { } else {
filenamearray = filenamearray =
LocalOrCookieStorage.getItem( LocalOrCookieStorage.getItem(
'jio/local_file_name_array/'+user+'/'+appid) || []; 'jio/local_file_name_array/'+user+'/'+appid) || [];
filenamearray.push(file.name); filenamearray.push(file._id);
LocalOrCookieStorage.setItem( LocalOrCookieStorage.setItem(
'jio/local_file_name_array/'+user+'/'+appid, 'jio/local_file_name_array/'+user+'/'+appid,
filenamearray); filenamearray);
LocalOrCookieStorage.setItem( LocalOrCookieStorage.setItem(
'jio/local/'+user+'/'+appid+'/'+file.name, 'jio/local/'+user+'/'+appid+'/'+file._id,
file); file);
} }
LocalOrCookieStorage.setItem( LocalOrCookieStorage.setItem(
'jio/local/'+user+'/'+appid+'/'+file.name, 'jio/local/'+user+'/'+appid+'/'+file._id,
file); file);
}, },
removeFileFromLocalStorage = function (user,appid,file) { removeFileFromLocalStorage = function (user,appid,file) {
...@@ -128,14 +128,14 @@ removeFileFromLocalStorage = function (user,appid,file) { ...@@ -128,14 +128,14 @@ removeFileFromLocalStorage = function (user,appid,file) {
LocalOrCookieStorage.getItem( LocalOrCookieStorage.getItem(
'jio/local_file_name_array/'+user+'/'+appid) || []; 'jio/local_file_name_array/'+user+'/'+appid) || [];
for (i = 0, l = filenamearray.length; i < l; i+= 1) { for (i = 0, l = filenamearray.length; i < l; i+= 1) {
if (filenamearray[i] !== file.name) { if (filenamearray[i] !== file._id) {
newarray.push(filenamearray[i]); newarray.push(filenamearray[i]);
} }
} }
LocalOrCookieStorage.setItem('jio/local_file_name_array/'+user+'/'+appid, LocalOrCookieStorage.setItem('jio/local_file_name_array/'+user+'/'+appid,
newarray); newarray);
LocalOrCookieStorage.deleteItem( LocalOrCookieStorage.deleteItem(
'jio/local/'+user+'/'+appid+'/'+file.name); 'jio/local/'+user+'/'+appid+'/'+file._id);
}; };
//// end tools //// end tools
...@@ -489,37 +489,18 @@ test ('Document save', function () { ...@@ -489,37 +489,18 @@ test ('Document save', function () {
// We launch a saving to localstorage and we check if the file is // We launch a saving to localstorage and we check if the file is
// realy saved. Then save again and check if // realy saved. Then save again and check if
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this; var o = {}; o.t = this; o.clock = o.t.sandbox.useFakeTimers();
o.clock.tick(base_tick); o.clock.tick(base_tick);
o.spy = function(res,value,message) { o.spy = basic_spy_function;
o.f = function(result) { o.tick = function (o, tick, value, fun) {
if (res === 'status') { basic_tick_function(o,tick,fun);
if (result && result.status) {
result = 'fail';
} else {
result = 'done';
}
}
deepEqual (result,value,message);
};
o.t.spy(o,'f');
};
o.tick = function (value, tick) {
o.clock.tick(tick || 1000);
if (!o.f.calledOnce) {
if (o.f.called) {
ok(false, 'too much results');
} else {
ok(false, 'no response');
}
}
o.tmp = o.tmp =
LocalOrCookieStorage.getItem ('jio/local/MrSaveName/jiotests/file'); LocalOrCookieStorage.getItem ('jio/local/MrSaveName/jiotests/file');
if (o.tmp) { if (o.tmp) {
o.tmp.lmcd = (o.tmp.last_modified === o.tmp.creation_date); o.tmp.lmcd = (o.tmp._last_modified === o.tmp._creation_date);
delete o.tmp.last_modified; delete o.tmp._last_modified;
delete o.tmp.creation_date; delete o.tmp._creation_date;
deepEqual (o.tmp,{name:'file',content:'content',lmcd:value}, deepEqual (o.tmp,{_id:'file',content:'content',lmcd:value},
'check saved document'); 'check saved document');
} else { } else {
ok (false, 'document is not saved!'); ok (false, 'document is not saved!');
...@@ -529,13 +510,13 @@ test ('Document save', function () { ...@@ -529,13 +510,13 @@ test ('Document save', function () {
o.jio = JIO.newJio({type:'local',username:'MrSaveName', o.jio = JIO.newJio({type:'local',username:'MrSaveName',
applicationname:'jiotests'}); applicationname:'jiotests'});
// save and check document existence // save and check document existence
o.spy('status','done','saving document'); o.spy (o,'value',{ok:true,id:'file'},'saving document');
o.jio.saveDocument('file','content',{success:o.f,error:o.f}); o.jio.put({_id:'file',content:'content'},o.f);
o.tick(true); o.tick(o,null,true);
o.spy('status','done','saving document'); o.spy (o,'value',{ok:true,id:'file'},'saving document');
o.jio.saveDocument('file','content',{success:o.f,error:o.f}); o.jio.put({_id:'file',content:'content'},o.f);
o.tick(false); o.tick(o,null,false);
o.jio.stop(); o.jio.stop();
}); });
...@@ -547,44 +528,23 @@ test ('Document load', function () { ...@@ -547,44 +528,23 @@ test ('Document load', function () {
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this; var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this;
o.clock.tick(base_tick); o.clock.tick(base_tick);
o.spy = function(res,value,message) { o.spy = basic_spy_function;
o.f = function(result) { o.tick = basic_tick_function;
if (res === 'status') {
if (result && result.status) {
result = 'fail';
} else {
result = 'done';
}
}
deepEqual (result,value,message);
};
o.t.spy(o,'f');
};
o.tick = function (value, tick) {
o.clock.tick(tick || 1000);
if (!o.f.calledOnce) {
if (o.f.called) {
ok(false, 'too much results');
} else {
ok(false, 'no response');
}
}
};
o.jio = JIO.newJio({type:'local',username:'MrLoadName', o.jio = JIO.newJio({type:'local',username:'MrLoadName',
applicationname:'jiotests'}); applicationname:'jiotests'});
// save and check document existence // save and check document existence
o.doc = {name:'file',content:'content', o.doc = {_id:'file',content:'content',
last_modified:1234,creation_date:1000}; _last_modified:1234,_creation_date:1000};
o.spy('status','fail','loading document failure'); o.spy(o,'status',404,'loading document failure');
o.jio.loadDocument('file',{success:o.f,error:o.f}); o.jio.get('file',o.f);
o.tick(); o.tick(o);
addFileToLocalStorage('MrLoadName','jiotests',o.doc); addFileToLocalStorage('MrLoadName','jiotests',o.doc);
o.spy('value',o.doc,'loading document success'); o.spy(o,'value',o.doc,'loading document success');
o.jio.loadDocument('file',{success:o.f,error:o.f}); o.jio.get('file',o.f);
o.tick(); o.tick(o);
o.jio.stop(); o.jio.stop();
}); });
...@@ -596,12 +556,16 @@ test ('Get document list', function () { ...@@ -596,12 +556,16 @@ test ('Get document list', function () {
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this; var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this;
o.clock.tick(base_tick); o.clock.tick(base_tick);
o.mytest = function (value){ o.mytest = function (value){
o.f = function (result) { o.f = function (err,val) {
deepEqual (objectifyDocumentArray(result), if (val) {
objectifyDocumentArray(value),'getting list'); deepEqual (objectifyDocumentArray(val.rows),
objectifyDocumentArray(value),'getting list');
} else {
deepEqual (err,value,'getting list');
}
}; };
o.t.spy(o,'f'); o.t.spy(o,'f');
o.jio.getDocumentList('.',{success: o.f,error:o.f}); o.jio.allDocs(o.f);
o.clock.tick(1000); o.clock.tick(1000);
if (!o.f.calledOnce) { if (!o.f.calledOnce) {
if (o.f.called) { if (o.f.called) {
...@@ -613,15 +577,25 @@ test ('Get document list', function () { ...@@ -613,15 +577,25 @@ test ('Get document list', function () {
}; };
o.jio = JIO.newJio({type:'local',username:'MrListName', o.jio = JIO.newJio({type:'local',username:'MrListName',
applicationname:'jiotests'}); applicationname:'jiotests'});
o.doc1 = {name:'file',content:'content', o.doc1 = {_id:'file',content:'content',
last_modified:1,creation_date:0}; _last_modified:1,_creation_date:0};
o.doc2 = {name:'memo',content:'test', o.doc2 = {_id:'memo',content:'test',
last_modified:5,creation_date:2}; _last_modified:5,_creation_date:2};
addFileToLocalStorage ('MrListName','jiotests',o.doc1); addFileToLocalStorage ('MrListName','jiotests',o.doc1);
addFileToLocalStorage ('MrListName','jiotests',o.doc2); addFileToLocalStorage ('MrListName','jiotests',o.doc2);
delete o.doc1.content; o.mytest ([{
delete o.doc2.content; id:o.doc2._id,key:o.doc2._id,
o.mytest ([o.doc1,o.doc2]); value:{
_creation_date:o.doc2._creation_date,
_last_modified:o.doc2._last_modified
}
},{
id:o.doc1._id,key:o.doc1._id,
value:{
_last_modified:o.doc1._last_modified,
_creation_date:o.doc1._creation_date
}
}]);
o.jio.stop(); o.jio.stop();
}); });
...@@ -633,32 +607,22 @@ test ('Document remove', function () { ...@@ -633,32 +607,22 @@ test ('Document remove', function () {
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this; var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this;
o.clock.tick(base_tick); o.clock.tick(base_tick);
o.mytest = function (){ o.spy = basic_spy_function;
o.f = function (result) { o.tick = function () {
if (result) { basic_tick_function.apply(basic_tick_function,arguments);
result = 'fail'; // check if the file is still there
} else { o.tmp = LocalOrCookieStorage.getItem (
result = 'done'; 'jio/local/MrRemoveName/jiotests/file');
} ok (!o.tmp, 'check no content');
deepEqual(result,'done','removing document');
};
o.t.spy(o,'f');
o.jio.removeDocument('file',{success:o.f,error:o.f});
o.clock.tick(1000);
if (!o.f.calledOnce) {
ok(false, 'no response / too much results');
} else {
// check if the file is still there
o.tmp = LocalOrCookieStorage.getItem (
'jio/local/MrRemoveName/jiotests/file');
ok (!o.tmp, 'check no content');
}
}; };
o.jio = JIO.newJio({type:'local',username:'MrRemoveName', o.jio = JIO.newJio({type:'local',username:'MrRemoveName',
applicationname:'jiotests'}); applicationname:'jiotests'});
// test removing a file // test removing a file
addFileToLocalStorage ('MrRemoveName','jiotests',{name:'file'}); o.spy (o,'value',{ok:true,id:'file'},'removing document');
o.mytest (); addFileToLocalStorage ('MrRemoveName','jiotests',{_id:'file'});
o.jio.remove({_id:'file'},o.f);
o.tick (o);
o.jio.stop(); o.jio.stop();
}); });
......
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