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