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

Update LocalStorage and Jio tests

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