Commit 77635b7a authored by Tristan Cavelier's avatar Tristan Cavelier

Adding Qunit tests and corrections to conflict and crypt storage.

parent b43c7be2
This diff is collapsed.
This diff is collapsed.
var newCryptedStorage = function ( spec, my ) { var newCryptedStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'handler' ), priv = {}; var that = Jio.storage( spec, my, 'handler' ), priv = {};
var is_valid_storage = spec.storage || false;
priv.username = spec.username || ''; priv.username = spec.username || '';
priv.password = spec.password || ''; priv.password = spec.password || '';
priv.secondstorage_spec = spec.storage || {type:'base'}; priv.secondstorage_spec = spec.storage || {type:'base'};
priv.secondstorage_string = JSON.stringify (priv.secondstorage_string);
var super_serialized = that.serialized; var super_serialized = that.serialized;
that.serialized = function () { that.serialized = function () {
var o = super_serialized(); var o = super_serialized();
o.username = priv.username; o.username = priv.username;
o.password = priv.password; o.password = priv.password;
o.storage = priv.secondstorage_string;
return o; return o;
}; };
that.validateState = function () { that.validateState = function () {
if (priv.username && if (priv.username && is_valid_storage) {
JSON.stringify (priv.secondstorage_spec) ===
JSON.stringify ({type:'base'})) {
return ''; return '';
} }
return 'Need at least two parameters: "username" and "storage".'; return 'Need at least two parameters: "username" and "storage".';
...@@ -44,8 +46,8 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -44,8 +46,8 @@ var newCryptedStorage = function ( spec, my ) {
priv.encrypt = function (data,callback,index) { priv.encrypt = function (data,callback,index) {
// end with a callback in order to improve encrypt to an // end with a callback in order to improve encrypt to an
// asynchronous encryption. // asynchronous encryption.
var tmp = sjcl.encrypt (that.getStorageUserName()+':'+ var tmp = sjcl.encrypt (priv.username+':'+
that.getStoragePassword(), data, priv.password, data,
priv.encrypt_param_object); priv.encrypt_param_object);
callback(JSON.parse(tmp).ct,index); callback(JSON.parse(tmp).ct,index);
}; };
...@@ -54,8 +56,8 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -54,8 +56,8 @@ var newCryptedStorage = function ( spec, my ) {
param.ct = data || ''; param.ct = data || '';
param = JSON.stringify (param); param = JSON.stringify (param);
try { try {
tmp = sjcl.decrypt (that.getStorageUserName()+':'+ tmp = sjcl.decrypt (priv.username+':'+
that.getStoragePassword(), priv.password,
param); param);
} catch (e) { } catch (e) {
callback({status:0,statusText:'Decrypt Fail', callback({status:0,statusText:'Decrypt Fail',
...@@ -70,7 +72,7 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -70,7 +72,7 @@ var newCryptedStorage = function ( spec, my ) {
* @method saveDocument * @method saveDocument
*/ */
that.saveDocument = function (command) { that.saveDocument = function (command) {
var new_file_name, newfilecontent, var new_file_name, new_file_content,
_1 = function () { _1 = function () {
priv.encrypt(command.getPath(),function(res) { priv.encrypt(command.getPath(),function(res) {
new_file_name = res; new_file_name = res;
...@@ -79,21 +81,21 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -79,21 +81,21 @@ var newCryptedStorage = function ( spec, my ) {
}, },
_2 = function () { _2 = function () {
priv.encrypt(command.getContent(),function(res) { priv.encrypt(command.getContent(),function(res) {
newfilecontent = res; new_file_content = res;
_3(); _3();
}); });
}, },
_3 = function () { _3 = function () {
var settings = that.cloneOption(), newcommand, newstorage; var settings = command.cloneOption(), newcommand;
settings.onResponse = function (){}; settings.onResponse = function (){};
settings.onDone = function () { that.done(); }; settings.onDone = function () { that.done(); };
settings.onFail = function (r) { that.fail(r); }; settings.onFail = function (r) { that.fail(r); };
newcommand = that.newCommand( newcommand = that.newCommand(
{path:new_file_name, 'saveDocument',
content:newfilecontent, {path:new_file_name,content:new_file_content,option:settings});
option:settings}); that.addJob (
newstorage = that.newStorage( priv.secondstorage_spec ); that.newStorage( priv.secondstorage_spec ),
that.addJob ( newstorage, newcommand ); newcommand );
}; };
_1(); _1();
}; // end saveDocument }; // end saveDocument
...@@ -111,22 +113,22 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -111,22 +113,22 @@ var newCryptedStorage = function ( spec, my ) {
}); });
}, },
_2 = function () { _2 = function () {
var settings = command.cloneOption(), newcommand, newstorage; var settings = command.cloneOption(), newcommand;
settings.onResponse = function(){}; settings.onResponse = function(){};
settings.onFail = loadOnFail; settings.onFail = loadOnFail;
settings.onDone = loadOnDone; settings.onDone = loadOnDone;
newcommand = that.newCommand ( newcommand = that.newCommand (
{path:new_file_name, 'loadDocument',
option:settings}); {path:new_file_name,option:settings});
newstorage = that.newStorage ( priv.secondstorage_spec ); that.addJob (
that.addJob ( newstorage, newcommand ); that.newStorage ( priv.secondstorage_spec ), newcommand );
}, },
loadOnDone = function (result) { loadOnDone = function (result) {
result.name = command.getPath(); result.name = command.getPath();
if (command.getOption('metadata_only')) { if (command.getOption('metadata_only')) {
that.done(result); that.done(result);
} else { } else {
priv.decrypt (result.content,function(res){ priv.decrypt (result.content, function(res){
if (typeof res === 'object') { if (typeof res === 'object') {
that.fail({status:0,statusText:'Decrypt Fail', that.fail({status:0,statusText:'Decrypt Fail',
message:'Unable to decrypt'}); message:'Unable to decrypt'});
...@@ -140,11 +142,11 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -140,11 +142,11 @@ var newCryptedStorage = function ( spec, my ) {
}); });
} }
}, },
loadOnFail = function (result) { loadOnFail = function (error) {
// NOTE : we can re create an error object instead of // NOTE : we can re create an error object instead of
// keep the old ex:status=404,message="document 1y59gyl8g // keep the old ex:status=404,message="document 1y59gyl8g
// not found in localStorage"... // not found in localStorage"...
that.fail(result); that.fail(error);
}; };
_1(); _1();
}; // end loadDocument }; // end loadDocument
...@@ -156,16 +158,16 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -156,16 +158,16 @@ var newCryptedStorage = function ( spec, my ) {
that.getDocumentList = function (command) { that.getDocumentList = function (command) {
var new_job, i, l, cpt = 0, array, ok = true, var new_job, i, l, cpt = 0, array, ok = true,
_1 = function () { _1 = function () {
var newcommand = command.clone(), var newcommand = command.clone();
newstorage = that.newStorage ( priv.secondstorage_spec );
newcommand.onResponseDo (getListOnResponse); newcommand.onResponseDo (getListOnResponse);
newcommand.onDoneDo (function(){}); newcommand.onDoneDo (function(){});
newcommand.onFailDo (function(){}); newcommand.onFailDo (function(){});
that.addJob ( new_job ); that.addJob (
that.newStorage ( priv.secondstorage_spec ), newcommand );
}, },
getListOnResponse = function (result) { getListOnResponse = function (result) {
if (result.status.isDone()) { if (result.status.isDone()) {
array = result.return_value; array = result.value;
for (i = 0, l = array.length; i < l; i+= 1) { for (i = 0, l = array.length; i < l; i+= 1) {
// cpt--; // cpt--;
priv.decrypt (array[i].name, priv.decrypt (array[i].name,
...@@ -182,6 +184,7 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -182,6 +184,7 @@ var newCryptedStorage = function ( spec, my ) {
cpt++; cpt++;
if (typeof res === 'object') { if (typeof res === 'object') {
if (ok) { if (ok) {
ok = false;
that.fail({status:0,statusText:'Decrypt Fail', that.fail({status:0,statusText:'Decrypt Fail',
message:'Unable to decrypt.'}); message:'Unable to decrypt.'});
} }
...@@ -201,23 +204,27 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -201,23 +204,27 @@ var newCryptedStorage = function ( spec, my ) {
* Removes a document. * Removes a document.
* @method removeDocument * @method removeDocument
*/ */
that.removeDocument = function () { that.removeDocument = function (command) {
var new_job, new_file_name, var new_file_name,
_1 = function () { _1 = function () {
priv.encrypt(that.getFileName(),function(res) { priv.encrypt(command.getPath(),function(res) {
new_file_name = res; new_file_name = res;
_2(); _2();
}); });
}, },
_2 = function () { _2 = function () {
new_job = that.cloneJob(); var cloned_option = command.cloneOption();
new_job.name = new_file_name; cloned_option.onResponse = removeOnResponse;
new_job.storage = that.getSecondStorage(); cloned_option.onFail = function () {};
new_job.onResponse = removeOnResponse; cloned_option.onDone = function () {};
that.addJob(new_job); that.addJob(that.newStorage(priv.secondstorage_spec),
that.newCommand(
'removeDocument',
{path:new_file_name,
option:cloned_option}));
}, },
removeOnResponse = function (result) { removeOnResponse = function (result) {
if (result.status === 'done') { if (result.status.isDone()) {
that.done(); that.done();
} else { } else {
that.fail(result.error); that.fail(result.error);
......
This diff is collapsed.
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