From fa3abc34873ac92501b5cdf1bbf7e654199b273f Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Wed, 21 Aug 2013 13:43:19 +0200 Subject: [PATCH] implement get message from imap in email dao and app controller --- src/js/app-controller.js | 12 ++++--- src/js/dao/email-dao.js | 27 ++++++++++++++-- src/tpl/messagelistitem.html | 2 +- test/new-unit/app-controller-test.js | 15 +++++++++ test/new-unit/email-dao-test.js | 48 +++++++++++++++++++++++++--- 5 files changed, 92 insertions(+), 12 deletions(-) diff --git a/src/js/app-controller.js b/src/js/app-controller.js index b60fbda..814a6dc 100644 --- a/src/js/app-controller.js +++ b/src/js/app-controller.js @@ -85,10 +85,14 @@ define(function(require) { } else if (cmd === 'getEmail') { // list emails from folder - var mail = self._emailDao.getItem(args.folder, args.messageId); - callback({ - err: null, - email: mail + self._emailDao.imapGetMessage({ + folder: args.folder, + uid: args.messageId + }, function(err, email) { + callback({ + err: err, + email: email + }); }); } else if (cmd === 'sendEmail') { diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 62c1487..bd9db48 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -156,9 +156,30 @@ define(function(require) { * @param {String} options.messageId The */ EmailDAO.prototype.imapGetMessage = function(options, callback) { - callback({ - errMsg: 'Not yet implemented!' - }); + var self = this, + message; + + // validate options + if (!options.folder || !options.uid) { + callback({ + errMsg: 'Invalid options!' + }); + return; + } + + function messageReady(err, gottenMessage) { + message = gottenMessage; + } + + function attachmentReady(err, attmt) { + message.parsedAttachment = attmt; + callback(null, message); + } + + self._imapClient.getMessage({ + path: options.folder, + uid: options.uid + }, messageReady, attachmentReady); }; // diff --git a/src/tpl/messagelistitem.html b/src/tpl/messagelistitem.html index 3af5006..26f7983 100644 --- a/src/tpl/messagelistitem.html +++ b/src/tpl/messagelistitem.html @@ -1,4 +1,4 @@ - +

<%- from[0].name || from[0].address %>

<%- subject %>

diff --git a/test/new-unit/app-controller-test.js b/test/new-unit/app-controller-test.js index b3bcb49..65e686d 100644 --- a/test/new-unit/app-controller-test.js +++ b/test/new-unit/app-controller-test.js @@ -108,6 +108,21 @@ define(function(require) { }); }); }); + + describe('getEmail', function() { + it('should work', function(done) { + controller._emailDao.imapGetMessage.yields(null, {}); + controller.execute('getEmail', { + folder: 'INBOX', + messageId: 415 + }, function(resArgs) { + expect(resArgs.err).to.not.exist; + expect(resArgs.email).to.a('Object'); + expect(controller._emailDao.imapGetMessage.calledOnce).to.be.true; + done(); + }); + }); + }); }); }); diff --git a/test/new-unit/email-dao-test.js b/test/new-unit/email-dao-test.js index d4c7dfe..2f8c372 100644 --- a/test/new-unit/email-dao-test.js +++ b/test/new-unit/email-dao-test.js @@ -108,7 +108,7 @@ define(function(require) { }); }); - describe('send email via SMTP', function() { + describe('SMTP: send email', function() { it('should fail due to back input', function(done) { emailDao.smtpSend({}, function(err) { expect(smtpClientStub.send.called).to.be.false; @@ -117,7 +117,7 @@ define(function(require) { }); }); - it('send an email via STMP good case', function(done) { + it('should work', function(done) { smtpClientStub.send.yields(); emailDao.smtpSend(dummyMail, function(err) { expect(smtpClientStub.send.calledOnce).to.be.true; @@ -127,7 +127,7 @@ define(function(require) { }); }); - describe('list IMAP folders', function() { + describe('IMAP: list folders', function() { it('should work', function(done) { imapClientStub.listFolders.yields(); emailDao.imapListFolders(function(err) { @@ -138,7 +138,7 @@ define(function(require) { }); }); - describe('list IMAP messages from folder', function() { + describe('IMAP: list messages from folder', function() { it('should fail due to bad options', function(done) { emailDao.imapListMessages({}, function(err) { expect(imapClientStub.listMessages.called).to.be.false; @@ -160,6 +160,46 @@ define(function(require) { }); }); }); + + describe('IMAP: get message content from', function() { + it('should fail due to bad options', function(done) { + emailDao.imapGetMessage({ + folder: 'INBOX' + }, function(err) { + expect(imapClientStub.getMessage.called).to.be.false; + expect(err).to.exist; + done(); + }); + }); + + it('should parse message body and attachement', function(done) { + var uid = 415, + newImapClientStub = { + getMessage: function() {} + }; + sinon.stub(newImapClientStub, 'getMessage', function(options, messageReady, attachmentReady) { + messageReady(null, { + uid: uid + }); + attachmentReady(null, { + uint8Array: new Uint8Array(42) + }); + }); + emailDao._imapClient = newImapClientStub; + + emailDao.imapGetMessage({ + folder: 'INBOX', + uid: uid + }, function(err, message) { + expect(newImapClientStub.getMessage.calledOnce).to.be.true; + expect(err).to.not.exist; + expect(message.uid).to.equal(uid); + expect(message.parsedAttachment.uint8Array.length).to.equal(42); + emailDao._imapClient = imapClientStub; + done(); + }); + }); + }); }); });