From a643819b463a56e5cf6dcbc289a1e886b20ea60d Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 11 May 2015 11:46:28 -0300 Subject: [PATCH] add email validation --- config.js | 2 +- lib/emailservice.js | 8 ++++---- lib/server.js | 8 +++++++- test/integration/server.js | 17 ++++++++++++++++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/config.js b/config.js index 12d471b..db5adf8 100644 --- a/config.js +++ b/config.js @@ -40,7 +40,7 @@ var config = { }, }, // To use email notifications uncomment this: - email: { + emailOpts: { host: 'localhost', port: 25, ignoreTLS: true, diff --git a/lib/emailservice.js b/lib/emailservice.js index 6742071..d5038b8 100644 --- a/lib/emailservice.js +++ b/lib/emailservice.js @@ -33,13 +33,13 @@ var EMAIL_TYPES = { function EmailService(opts) { $.checkArgument(opts); - opts.email = opts.email || {}; + opts.emailOpts = opts.emailOpts || {}; this.storage = opts.storage; this.lock = opts.lock; - this.mailer = opts.mailer || nodemailer.createTransport(opts.email); - this.subjectPrefix = opts.email.subjectPrefix || '[Wallet service]'; - this.from = opts.email.from; + this.mailer = opts.mailer || nodemailer.createTransport(opts.emailOpts); + this.subjectPrefix = opts.emailOpts.subjectPrefix || '[Wallet service]'; + this.from = opts.emailOpts.from; $.checkState(this.mailer); $.checkState(this.from); diff --git a/lib/server.js b/lib/server.js index ce4492b..6eae584 100644 --- a/lib/server.js +++ b/lib/server.js @@ -87,7 +87,7 @@ WalletService.initialize = function(opts, cb) { lock: lock, storage: storage, mailer: opts.mailer, - email: opts.email, + emailOpts: opts.emailOpts, }); return cb(); }; @@ -448,6 +448,12 @@ WalletService.prototype.savePreferences = function(opts, cb) { opts = opts || {}; + if (opts.email) { + if (opts.email.length > 254 || opts.email.indexOf('@') == -1) { + return cb(new ClientError('Invalid email address')); + } + } + self._runLocked(cb, function(cb) { var preferences = Model.Preferences.create({ walletId: self.walletId, diff --git a/test/integration/server.js b/test/integration/server.js index c5018cd..4cf42a4 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -255,7 +255,7 @@ describe('Wallet service', function() { storage: storage, blockchainExplorer: blockchainExplorer, mailer: mailer, - email: { + emailOpts: { from: 'bws@dummy.net', } }, done); @@ -779,6 +779,21 @@ 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({ + email: 'dummy@' + _.repeat('domain', 50), + }, function(err) { + should.exist(err); + err.message.should.contain('email'); + done(); + }); + }); + }); }); describe('#getBalance', function() {