diff --git a/src/js/app-controller.js b/src/js/app-controller.js index 52332ee..a955749 100644 --- a/src/js/app-controller.js +++ b/src/js/app-controller.js @@ -152,15 +152,15 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha keychain = new KeychainDAO(cloudstorage); imapClient = new ImapClient(imapOptions); smtpClient = new SmtpClient(smtpOptions); - emailDao = new EmailDAO(cloudstorage, keychain, imapClient, smtpClient); + emailDao = new EmailDAO(keychain, imapClient, smtpClient); // init email dao - var account = new app.model.Account({ + var account = { emailAddress: userId, symKeySize: app.config.symKeySize, symIvSize: app.config.symIvSize, asymKeySize: app.config.asymKeySize - }); + }; emailDao.init(account, password, callback); } diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 87557fc..93d5a1f 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -7,7 +7,6 @@ define(function(require) { jsonDB = require('js/dao/lawnchair-dao'), devicestorage = require('js/dao/devicestorage-dao'), app = require('js/app-config'); - require('js/model/account-model'); /** * A high-level Data-Access Api for handling Email synchronization @@ -30,7 +29,7 @@ define(function(require) { self.account = account; // validate email address - var emailAddress = account.get('emailAddress'); + var emailAddress = account.emailAddress; if (!validateEmail(emailAddress)) { callback({ errMsg: 'The user email address must be specified!' @@ -70,8 +69,8 @@ define(function(require) { crypto.init({ emailAddress: emailAddress, password: password, - keySize: account.get('symKeySize'), - rsaKeySize: account.get('asymKeySize'), + keySize: account.symKeySize, + rsaKeySize: account.asymKeySize, storedKeypair: storedKeypair }, function(err, generatedKeypair) { if (err) { @@ -89,6 +88,65 @@ define(function(require) { } }; + // + // New IMAP/SMTP implementation + // + + /** + * Cleanup by logging the user off. + */ + EmailDAO.prototype.destroy = function(callback) { + var self = this; + + self._imapClient.logout(callback); + }; + + /** + * Send an email client side via STMP. + */ + EmailDAO.prototype.smtpSend = function(email, callback) { + var self = this; + + // validate the email input + if (!email.to || !email.from || !email.to[0].address || !email.from[0].address) { + callback({ + errMsg: 'Invalid email object!' + }); + return; + } + + self._smtpClient.send(email, callback); + }; + + /** + * List the folders in the user's IMAP mailbox. + */ + EmailDAO.prototype.imapListFolders = function(callback) { + + }; + + /** + * List messages from an imap folder. This will not yet fetch the email body. + * @param {String} options.folderName The name of the imap folder. + * @param {Number} offset The offset of items to fetch (0 is the last stored item) + * @param {Number} num The number of items to fetch (null means fetch all) + */ + EmailDAO.prototype.imapListMessages = function(options, callback) { + + }; + + /** + * Get an email messsage including the email body from imap + * @param {String} options.messageId The + */ + EmailDAO.prototype.imapGetMessage = function(options, callback) { + + }; + + // + // Old cloud storage implementation + // + /** * Fetch an email with the following id */ @@ -333,15 +391,10 @@ define(function(require) { } function send(email) { - if (self._smtpClient) { - // send email directly client side - self._smtpClient.send(email, callback); - } else { - // send email via cloud service - self._cloudstorage.deliverEmail(email, userId, recipient, function(err) { - callback(err); - }); - } + // send email via cloud service + self._cloudstorage.deliverEmail(email, userId, recipient, function(err) { + callback(err); + }); } }; @@ -349,6 +402,10 @@ define(function(require) { // helper functions // + /** + * Validates an email address + */ + function validateEmail(email) { var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); diff --git a/test/new-unit/app-controller-test.js b/test/new-unit/app-controller-test.js new file mode 100644 index 0000000..48ad6dd --- /dev/null +++ b/test/new-unit/app-controller-test.js @@ -0,0 +1,20 @@ +define(function() { + 'use strict'; + + var expect = chai.expect; + + describe('App Controller unit tests', function() { + + beforeEach(function() {}); + + afterEach(function() {}); + + describe('init', function() { + it('should not explode', function() { + expect(true).to.be.ok; + }); + }); + + }); + +}); \ No newline at end of file diff --git a/test/new-unit/cloudstorage-dao-test.js b/test/new-unit/cloudstorage-dao-test.js index 1f25187..739bc2f 100644 --- a/test/new-unit/cloudstorage-dao-test.js +++ b/test/new-unit/cloudstorage-dao-test.js @@ -10,7 +10,7 @@ define(function() { afterEach(function() {}); describe('init', function() { - it('should fail due to error in imap login', function() { + it('should not explode', function() { expect(true).to.be.ok; }); }); diff --git a/test/new-unit/email-dao-test.js b/test/new-unit/email-dao-test.js index bbe43ec..9dceeca 100644 --- a/test/new-unit/email-dao-test.js +++ b/test/new-unit/email-dao-test.js @@ -5,7 +5,6 @@ define(function(require) { EmailDAO = require('js/dao/email-dao'), SmtpClient = require('SmtpClient'), ImapClient = require('ImapClient'), - Account = require('js/model/account-model'), app = require('js/app-config'), expect = chai.expect; @@ -15,18 +14,30 @@ define(function(require) { asymKeySize: 512 }; + var dummyMail = { + from: [{ + name: 'Whiteout Test', + address: 'whiteout.test@t-online.de' + }], // sender address + to: [{ + address: 'safewithme.testuser@gmail.com' + }], // list of receivers + subject: "Hello", // Subject line + body: "Hello world" // plaintext body + }; + describe('Email DAO unit tests', function() { var emailDao, account, keychainStub, imapClientStub, smtpClientStub; beforeEach(function() { - account = new Account({ + account = { emailAddress: emaildaoTest.user, symKeySize: app.config.symKeySize, symIvSize: app.config.symIvSize, asymKeySize: emaildaoTest.asymKeySize - }); + }; keychainStub = sinon.createStubInstance(KeychainDAO); imapClientStub = sinon.createStubInstance(ImapClient); @@ -38,7 +49,6 @@ define(function(require) { afterEach(function() {}); describe('init', function() { - it('should fail due to error in imap login', function(done) { imapClientStub.login.yields(42); @@ -59,7 +69,7 @@ define(function(require) { }); }); - it('should initialize', function(done) { + it('should init with new keygen', function(done) { imapClientStub.login.yields(); keychainStub.getUserKeyPair.yields(); keychainStub.putUserKeyPair.yields(); @@ -74,6 +84,47 @@ define(function(require) { }); }); + describe('IMAP/SMTP tests', function() { + beforeEach(function(done) { + imapClientStub.login.yields(); + keychainStub.getUserKeyPair.yields(); + keychainStub.putUserKeyPair.yields(); + + emailDao.init(account, emaildaoTest.passphrase, function(err) { + expect(imapClientStub.login.calledOnce).to.be.true; + expect(keychainStub.getUserKeyPair.calledOnce).to.be.true; + expect(keychainStub.putUserKeyPair.calledOnce).to.be.true; + expect(err).to.not.exist; + done(); + }); + }); + + afterEach(function(done) { + imapClientStub.logout.yields(); + emailDao.destroy(function(err) { + expect(imapClientStub.logout.calledOnce).to.be.true; + expect(err).to.not.exist; + done(); + }); + }); + + it('send an email via STMP bad case', function(done) { + emailDao.smtpSend({}, function(err) { + expect(smtpClientStub.send.called).to.be.false; + expect(err).to.exist; + done(); + }); + }); + + it('send an email via STMP good case', function(done) { + smtpClientStub.send.yields(); + emailDao.smtpSend(dummyMail, function(err) { + expect(smtpClientStub.send.calledOnce).to.be.true; + expect(err).to.not.exist; + done(); + }); + }); + }); }); }); \ No newline at end of file diff --git a/test/new-unit/main.js b/test/new-unit/main.js index 5f34324..d2295e3 100644 --- a/test/new-unit/main.js +++ b/test/new-unit/main.js @@ -21,7 +21,7 @@ function startTests() { require( [ 'test/new-unit/email-dao-test', - 'test/new-unit/cloudstorage-dao-test' + 'test/new-unit/app-controller-test' ], function() { //Tests loaded, run tests mocha.run();