From 1460bf21289c4eda05feadc19aa9f7af3b7cc25f Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 22 Jun 2015 16:52:01 -0300 Subject: [PATCH] add language & unit to preferences Signed-off-by: Ivan Socolsky --- lib/model/preferences.js | 4 ++++ lib/server.js | 36 ++++++++++++++++++++++++---- test/integration/server.js | 48 ++++++++++++++++++++++++++++---------- 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/lib/model/preferences.js b/lib/model/preferences.js index ab2fb92..353c858 100644 --- a/lib/model/preferences.js +++ b/lib/model/preferences.js @@ -13,6 +13,8 @@ Preferences.create = function(opts) { x.walletId = opts.walletId; x.copayerId = opts.copayerId; x.email = opts.email; + x.language = opts.language; + x.unit = opts.unit; return x; }; @@ -23,6 +25,8 @@ Preferences.fromObj = function(obj) { x.walletId = obj.walletId; x.copayerId = obj.copayerId; x.email = obj.email; + x.language = obj.language; + x.unit = obj.unit; return x; }; diff --git a/lib/server.js b/lib/server.js index 5c137c5..7706b11 100644 --- a/lib/server.js +++ b/lib/server.js @@ -454,16 +454,42 @@ WalletService.prototype.joinWallet = function(opts, cb) { * Save copayer preferences for the current wallet/copayer pair. * @param {Object} opts * @param {string} opts.email - Email address for notifications. + * @param {string} opts.language - Language used for notifications. + * @param {string} opts.unit - Bitcoin unit used to format amounts in notifications. */ WalletService.prototype.savePreferences = function(opts, cb) { var self = this; opts = opts || {}; - if (opts.email) { - if (!EmailValidator.validate(opts.email)) { - return cb(new ClientError('Invalid email address')); - } + var preferences = [{ + name: 'email', + isValid: function(value) { + return EmailValidator.validate(value); + }, + }, { + name: 'language', + isValid: function(value) { + return _.isString(value) && value.length == 2; + }, + }, { + name: 'unit', + isValid: function(value) { + return _.isString(value) && _.contains(['btc', 'bit'], value.toLowerCase()); + }, + }]; + + try { + _.each(preferences, function(preference) { + var value = opts[preference.name]; + if (!value) return; + if (!preference.isValid(value)) { + throw 'Invalid ' + preference.name; + return false; + } + }); + } catch (ex) { + return cb(new ClientError(ex)); } self._runLocked(cb, function(cb) { @@ -471,6 +497,8 @@ WalletService.prototype.savePreferences = function(opts, cb) { walletId: self.walletId, copayerId: self.copayerId, email: opts.email, + language: opts.language, + unit: opts.unit, }); self.storage.storePreferences(preferences, function(err) { return cb(err); diff --git a/test/integration/server.js b/test/integration/server.js index 3b93e23..ed1d21d 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -1099,13 +1099,17 @@ describe('Wallet service', function() { it('should save & retrieve preferences', function(done) { server.savePreferences({ - email: 'dummy@dummy.com' + email: 'dummy@dummy.com', + language: 'es', + unit: 'bit', }, function(err) { should.not.exist(err); server.getPreferences({}, function(err, preferences) { should.not.exist(err); should.exist(preferences); preferences.email.should.equal('dummy@dummy.com'); + preferences.language.should.equal('es'); + preferences.unit.should.equal('bit'); done(); }); }); @@ -1125,20 +1129,40 @@ describe('Wallet service', function() { }); }); it.skip('should save preferences only for requesting wallet', function(done) {}); - it('should validate email address', function(done) { - server.savePreferences({ - email: ' ' - }, function(err) { - should.exist(err); - err.message.should.contain('email'); - server.savePreferences({ + it('should validate entries', function(done) { + var invalid = [{ + preferences: { + email: ' ', + }, + expected: 'email' + }, { + preferences: { email: 'dummy@' + _.repeat('domain', 50), - }, function(err) { + }, + expected: 'email' + }, { + preferences: { + language: 'xxxxx', + }, + expected: 'language' + }, { + preferences: { + language: 123, + }, + expected: 'language' + }, { + preferences: { + unit: 'xxxxx', + }, + expected: 'unit' + }, ]; + async.each(invalid, function(item, next) { + server.savePreferences(item.preferences, function(err) { should.exist(err); - err.message.should.contain('email'); - done(); + err.message.should.contain(item.expected); + next(); }); - }); + }, done); }); });