From bd698257f9835d48349fb8c52485b6846a87f93d Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 3 Dec 2014 08:23:53 -0300 Subject: [PATCH 1/4] add tests to insight storage + remove used code --- js/controllers/home.js | 2 + js/plugins/GoogleDrive.js | 28 --------- js/plugins/InsightStorage.js | 14 +---- js/plugins/LocalStorage.js | 34 ---------- test/plugin.insight.js | 118 +++++++++++++++++++++++++++++------ test/util.log.js | 21 ++++++- 6 files changed, 124 insertions(+), 93 deletions(-) diff --git a/js/controllers/home.js b/js/controllers/home.js index ae8113843..df4c43ab5 100644 --- a/js/controllers/home.js +++ b/js/controllers/home.js @@ -46,6 +46,8 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc $scope.error = 'Invalid email or password'; } else if ((err.toString() || '').match('Connection')) { $scope.error = 'Could not connect to Insight Server'; + } else if ((err.toString() || '').match('Unable')) { + $scope.error = 'Unable to read data from the Insight Server'; } else { $scope.error = 'Unknown error'; } diff --git a/js/plugins/GoogleDrive.js b/js/plugins/GoogleDrive.js index b786fca4b..08634a279 100644 --- a/js/plugins/GoogleDrive.js +++ b/js/plugins/GoogleDrive.js @@ -298,32 +298,4 @@ GoogleDrive.prototype._checkHomeDir = function(cb) { }); }; -GoogleDrive.prototype.allKeys = function(cb) { - var self = this; - - this._checkHomeDir(function(homeId) { - preconditions.checkState(homeId); - - var request = gapi.client.request({ - 'path': '/drive/v2/files', - 'method': 'GET', - 'params': { - 'q': "'" + homeId + "' in parents and trashed = false", - 'fields': 'items(id,title)' - }, - }); - request.execute(function(res) { - // console.log('[googleDrive.js.152:res:]', res); //TODO - if (res.error) - throw new Error(res.error.message); - - var ret = []; - for (var ii in res.items) { - ret.push(res.items[ii].title); - } - return cb(ret); - }); - }); -}; - module.exports = GoogleDrive; diff --git a/js/plugins/InsightStorage.js b/js/plugins/InsightStorage.js index 522444c3e..4b78505bd 100644 --- a/js/plugins/InsightStorage.js +++ b/js/plugins/InsightStorage.js @@ -118,7 +118,7 @@ InsightStorage.prototype._makeGetRequest = function(passphrase, key, callback) { return callback('PNOTFOUND: Profile not found'); } if (response.statusCode !== 200) { - return callback('Connection error'); + return callback('Unable to read item from insight'); } return callback(null, body, InsightStorage.parseResponseHeaders(response.getAllResponseHeaders())); } @@ -217,7 +217,7 @@ InsightStorage.prototype.removeItem = function(key, callback) { 'Authorization': authHeader } }; - log.debug('erase ' + name); + log.debug('Erasing: ' + key); this.request.get(getParams, function(err, response, body) { if (err) { return callback('Connection error'); @@ -236,14 +236,4 @@ InsightStorage.prototype.clear = function(callback) { callback(); }; -InsightStorage.prototype.allKeys = function(callback) { - // TODO: compatibility with localStorage - return callback(null); -}; - -InsightStorage.prototype.getFirst = function(prefix, opts, callback) { - // TODO: compatibility with localStorage - return callback(null, true, true); -}; - module.exports = InsightStorage; diff --git a/js/plugins/LocalStorage.js b/js/plugins/LocalStorage.js index 0692a13ae..12533ef5a 100644 --- a/js/plugins/LocalStorage.js +++ b/js/plugins/LocalStorage.js @@ -46,38 +46,4 @@ LocalStorage.prototype.clear = function(cb) { return cb(); }; -LocalStorage.prototype.allKeys = function(cb) { - var l = localStorage.length; - var ret = []; - - for(var i=0; i Date: Wed, 3 Dec 2014 09:24:52 -0300 Subject: [PATCH 2/4] add tests to plugin.localstorage --- js/plugins/LocalStorage.js | 21 ++++++++------ test/plugin.localstorage.js | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 test/plugin.localstorage.js diff --git a/js/plugins/LocalStorage.js b/js/plugins/LocalStorage.js index 12533ef5a..f60235d10 100644 --- a/js/plugins/LocalStorage.js +++ b/js/plugins/LocalStorage.js @@ -2,11 +2,16 @@ var _ = require('lodash'); var preconditions = require('preconditions').singleton(); -function LocalStorage() { +function LocalStorage(opts) { this.type = 'DB'; + opts = opts || {}; + - preconditions.checkState(typeof localStorage !== 'undefined', - 'localstorage not available, cannot run plugin'); + this.ls = opts.ls + || ( (typeof localStorage !== 'undefined') ? localStorage : null ); + + preconditions.checkState(this.ls, + 'localstorage not available, cannot run plugin'); }; LocalStorage.prototype.init = function() { @@ -18,31 +23,31 @@ LocalStorage.prototype.setCredentials = function(email, password, opts) { }; LocalStorage.prototype.getItem = function(k,cb) { - return cb(null, localStorage.getItem(k)); + return cb(null, this.ls.getItem(k)); }; /** * Same as setItem, but fails if an item already exists */ LocalStorage.prototype.createItem = function(name, value, callback) { - if (localStorage.getItem(name)) { + if (this.ls.getItem(name)) { return callback('EEXISTS'); } return this.setItem(name, value, callback); }; LocalStorage.prototype.setItem = function(k,v,cb) { - localStorage.setItem(k,v); + this.ls.setItem(k,v); return cb(); }; LocalStorage.prototype.removeItem = function(k,cb) { - localStorage.removeItem(k); + this.ls.removeItem(k); return cb(); }; LocalStorage.prototype.clear = function(cb) { - localStorage.clear(); + this.ls.clear(); return cb(); }; diff --git a/test/plugin.localstorage.js b/test/plugin.localstorage.js new file mode 100644 index 000000000..a49e317ce --- /dev/null +++ b/test/plugin.localstorage.js @@ -0,0 +1,56 @@ +var LocalStorage = require('../js/plugins/LocalStorage'); +var assert = require('assert'); + +describe('local storage plugin', function() { + + var storage, storageMock, VALUE=123; + + beforeEach(function() { + storageMock = {}; + storageMock.getItem = sinon.stub().returns(VALUE); + storageMock.setItem = sinon.stub().returns(); + storageMock.removeItem = sinon.stub().returns(); + storageMock.clear = sinon.stub().returns(); + storage = new LocalStorage({ + ls: storageMock + }); + }); + + it('#getItem', function(done) { + storage.getItem('hola', function(err, value) { + assert(!err); + storageMock.getItem.getCall(0).args[0].should.equal('hola'); + value.should.equal(VALUE); + return done(); + }); + }); + + + it('#removeItem', function(done) { + storage.removeItem('pepe', function(err) { + assert(!err); + storageMock.removeItem.getCall(0).args[0].should.equal('pepe'); + return done(); + }); + }); + + it('#setItem', function(done) { + storage.setItem('hola', 'chau', function(err) { + assert(!err); + storageMock.setItem.getCall(0).args[0].should.equal('hola'); + storageMock.setItem.getCall(0).args[1].should.equal('chau'); + return done(); + }); + }); + + it('#clear', function(done) { + storage.clear(function(err) { + assert(!err); + storageMock.clear.calledOnce.should.equal(true); + return done(); + }); + }); + + + +}); From fccc97014699e6e1166aeee81b1022d6ee17b4a9 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 3 Dec 2014 09:40:54 -0300 Subject: [PATCH 3/4] add tests localStorage --- js/plugins/LocalStorage.js | 8 ++++++-- test/plugin.localstorage.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/js/plugins/LocalStorage.js b/js/plugins/LocalStorage.js index f60235d10..8e4ee75e3 100644 --- a/js/plugins/LocalStorage.js +++ b/js/plugins/LocalStorage.js @@ -18,11 +18,11 @@ LocalStorage.prototype.init = function() { }; LocalStorage.prototype.setCredentials = function(email, password, opts) { - this.email = email; - this.password = password; + // NOP }; LocalStorage.prototype.getItem = function(k,cb) { + preconditions.checkArgument(_.isFunction(cb)); return cb(null, this.ls.getItem(k)); }; @@ -30,6 +30,7 @@ LocalStorage.prototype.getItem = function(k,cb) { * Same as setItem, but fails if an item already exists */ LocalStorage.prototype.createItem = function(name, value, callback) { + preconditions.checkArgument(_.isFunction(callback)); if (this.ls.getItem(name)) { return callback('EEXISTS'); } @@ -37,16 +38,19 @@ LocalStorage.prototype.createItem = function(name, value, callback) { }; LocalStorage.prototype.setItem = function(k,v,cb) { + preconditions.checkArgument(_.isFunction(cb)); this.ls.setItem(k,v); return cb(); }; LocalStorage.prototype.removeItem = function(k,cb) { + preconditions.checkArgument(_.isFunction(cb)); this.ls.removeItem(k); return cb(); }; LocalStorage.prototype.clear = function(cb) { + preconditions.checkArgument(_.isFunction(cb)); this.ls.clear(); return cb(); }; diff --git a/test/plugin.localstorage.js b/test/plugin.localstorage.js index a49e317ce..4e58cae3c 100644 --- a/test/plugin.localstorage.js +++ b/test/plugin.localstorage.js @@ -8,6 +8,7 @@ describe('local storage plugin', function() { beforeEach(function() { storageMock = {}; storageMock.getItem = sinon.stub().returns(VALUE); + storageMock.createItem = sinon.stub().returns(); storageMock.setItem = sinon.stub().returns(); storageMock.removeItem = sinon.stub().returns(); storageMock.clear = sinon.stub().returns(); @@ -25,6 +26,22 @@ describe('local storage plugin', function() { }); }); + it('#createItem', function(done) { + storageMock.getItem = sinon.stub().returns(null); + storage.createItem('hola', 'value', function(err) { + assert(!err); + return done(); + }); + }); + + it('#createItem (Exists)', function(done) { + storage.createItem('hola', 'value', function(err) { + err.should.contain('EEXISTS'); + return done(); + }); + }); + + it('#removeItem', function(done) { storage.removeItem('pepe', function(err) { From d5a75b1460c59c033838476141c3f14e706ec716 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 3 Dec 2014 16:30:43 -0300 Subject: [PATCH 4/4] fix plugin path references --- js/models/PluginManager.js | 2 +- js/services/localstorageService.js | 2 +- js/util/log.js | 2 +- util/build.js | 10 +++++----- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/js/models/PluginManager.js b/js/models/PluginManager.js index d2d8d57fc..c4082ecbd 100644 --- a/js/models/PluginManager.js +++ b/js/models/PluginManager.js @@ -17,7 +17,7 @@ function PluginManager(config) { if(config.pluginsPath){ pluginClass = require(config.pluginsPath + pluginName); } else { - pluginClass = require('../plugins/' + pluginName); + pluginClass = require('../js/plugins/' + pluginName); } var pluginObj = new pluginClass(config[pluginName]); pluginObj.init(); diff --git a/js/services/localstorageService.js b/js/services/localstorageService.js index 7642775ea..1dc19d04b 100644 --- a/js/services/localstorageService.js +++ b/js/services/localstorageService.js @@ -3,7 +3,7 @@ angular.module('copayApp.services') .factory('localstorageService', function($rootScope) { - var LS = require('../plugins/LocalStorage'); + var LS = require('../js/plugins/LocalStorage'); var ls = new LS(); return ls; }); diff --git a/js/util/log.js b/js/util/log.js index 27a2f02aa..220fc2a02 100644 --- a/js/util/log.js +++ b/js/util/log.js @@ -3,7 +3,7 @@ var _ = require('lodash'); var ls; try { - var LS = require('../plugins/LocalStorage'); + var LS = require('../js/plugins/LocalStorage'); ls = new LS(); } catch(e) {}; diff --git a/util/build.js b/util/build.js index 1d55781ef..1618dfab0 100644 --- a/util/build.js +++ b/util/build.js @@ -90,22 +90,22 @@ var createBundle = function(opts) { if (!opts.disablePlugins) { b.require('./js/plugins/GoogleDrive', { - expose: '../plugins/GoogleDrive' + expose: '../js/plugins/GoogleDrive' }); b.require('./js/plugins/InsightStorage', { - expose: '../plugins/InsightStorage' + expose: '../js/plugins/InsightStorage' }); b.require('./js/plugins/InsightStorage', { expose: '../js/plugins/InsightStorage' }); b.require('./js/plugins/LocalStorage', { - expose: '../plugins/LocalStorage' + expose: '../js/plugins/LocalStorage' }); b.require('./js/plugins/EncryptedInsightStorage', { - expose: '../plugins/EncryptedInsightStorage' + expose: '../js/plugins/EncryptedInsightStorage' }); b.require('./js/plugins/EncryptedLocalStorage', { - expose: '../plugins/EncryptedLocalStorage' + expose: '../js/plugins/EncryptedLocalStorage' }); }