2013-08-19 15:13:32 -04:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var KeychainDAO = require('js/dao/keychain-dao'),
|
|
|
|
EmailDAO = require('js/dao/email-dao'),
|
|
|
|
SmtpClient = require('SmtpClient'),
|
|
|
|
ImapClient = require('ImapClient'),
|
|
|
|
app = require('js/app-config'),
|
|
|
|
expect = chai.expect;
|
|
|
|
|
|
|
|
var emaildaoTest = {
|
|
|
|
user: "whiteout.test@t-online.de",
|
|
|
|
passphrase: 'asdf',
|
|
|
|
asymKeySize: 512
|
|
|
|
};
|
|
|
|
|
2013-08-20 07:30:35 -04:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2013-08-19 15:13:32 -04:00
|
|
|
describe('Email DAO unit tests', function() {
|
|
|
|
|
|
|
|
var emailDao, account,
|
|
|
|
keychainStub, imapClientStub, smtpClientStub;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2013-08-20 07:30:35 -04:00
|
|
|
account = {
|
2013-08-19 15:13:32 -04:00
|
|
|
emailAddress: emaildaoTest.user,
|
|
|
|
symKeySize: app.config.symKeySize,
|
|
|
|
symIvSize: app.config.symIvSize,
|
|
|
|
asymKeySize: emaildaoTest.asymKeySize
|
2013-08-20 07:30:35 -04:00
|
|
|
};
|
2013-08-19 15:13:32 -04:00
|
|
|
|
|
|
|
keychainStub = sinon.createStubInstance(KeychainDAO);
|
|
|
|
imapClientStub = sinon.createStubInstance(ImapClient);
|
|
|
|
smtpClientStub = sinon.createStubInstance(SmtpClient);
|
|
|
|
|
|
|
|
emailDao = new EmailDAO(keychainStub, imapClientStub, smtpClientStub);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {});
|
|
|
|
|
|
|
|
describe('init', function() {
|
|
|
|
it('should fail due to error in imap login', function(done) {
|
|
|
|
imapClientStub.login.yields(42);
|
|
|
|
|
|
|
|
emailDao.init(account, emaildaoTest.passphrase, function(err) {
|
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to error in getUserKeyPair', function(done) {
|
|
|
|
imapClientStub.login.yields();
|
|
|
|
keychainStub.getUserKeyPair.yields(42);
|
|
|
|
|
|
|
|
emailDao.init(account, emaildaoTest.passphrase, function(err) {
|
|
|
|
expect(imapClientStub.login.calledOnce).to.be.true;
|
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-20 07:30:35 -04:00
|
|
|
it('should init with new keygen', function(done) {
|
2013-08-19 15:13:32 -04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-20 07:30:35 -04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-21 07:43:19 -04:00
|
|
|
describe('SMTP: send email', function() {
|
2013-08-20 11:22:08 -04:00
|
|
|
it('should fail due to back input', function(done) {
|
|
|
|
emailDao.smtpSend({}, function(err) {
|
|
|
|
expect(smtpClientStub.send.called).to.be.false;
|
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-21 07:43:19 -04:00
|
|
|
it('should work', function(done) {
|
2013-08-20 11:22:08 -04:00
|
|
|
smtpClientStub.send.yields();
|
|
|
|
emailDao.smtpSend(dummyMail, function(err) {
|
|
|
|
expect(smtpClientStub.send.calledOnce).to.be.true;
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
2013-08-20 07:30:35 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-22 10:18:48 -04:00
|
|
|
describe('IMAP Caching', function() {
|
|
|
|
describe('write cache', function() {
|
|
|
|
it('should work if cache is empty', function() {
|
|
|
|
expect(emailDao._account.folders).to.not.exist;
|
|
|
|
emailDao.cacheItem('INBOX', {
|
|
|
|
uid: 42
|
|
|
|
});
|
|
|
|
expect(emailDao._account.folders.INBOX[42]).to.exist;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work if cache is not empty', function() {
|
|
|
|
expect(emailDao._account.folders).to.not.exist;
|
|
|
|
emailDao.cacheItem('INBOX', {
|
|
|
|
uid: 42
|
|
|
|
});
|
|
|
|
emailDao.cacheItem('INBOX', {
|
|
|
|
uid: 43
|
|
|
|
});
|
|
|
|
expect(emailDao._account.folders.INBOX[42]).to.exist;
|
|
|
|
expect(emailDao._account.folders.INBOX[43]).to.exist;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('read cache', function() {
|
|
|
|
it('should work if cache is empty', function() {
|
|
|
|
expect(emailDao._account.folders).to.not.exist;
|
|
|
|
var item = emailDao.readCache('INBOX', 42);
|
|
|
|
expect(item).to.not.exist;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work if cache is not empty', function() {
|
|
|
|
expect(emailDao._account.folders).to.not.exist;
|
|
|
|
emailDao.cacheItem('INBOX', {
|
|
|
|
uid: 42
|
|
|
|
});
|
|
|
|
expect(emailDao._account.folders.INBOX[42]).to.exist;
|
|
|
|
|
|
|
|
var item = emailDao.readCache('INBOX', 42);
|
|
|
|
expect(item.uid).to.equal(42);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-21 07:43:19 -04:00
|
|
|
describe('IMAP: list folders', function() {
|
2013-08-20 11:22:08 -04:00
|
|
|
it('should work', function(done) {
|
|
|
|
imapClientStub.listFolders.yields();
|
|
|
|
emailDao.imapListFolders(function(err) {
|
|
|
|
expect(imapClientStub.listFolders.calledOnce).to.be.true;
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
2013-08-20 07:30:35 -04:00
|
|
|
});
|
|
|
|
});
|
2013-08-20 13:48:49 -04:00
|
|
|
|
2013-08-21 07:43:19 -04:00
|
|
|
describe('IMAP: list messages from folder', function() {
|
2013-08-20 13:48:49 -04:00
|
|
|
it('should fail due to bad options', function(done) {
|
|
|
|
emailDao.imapListMessages({}, function(err) {
|
|
|
|
expect(imapClientStub.listMessages.called).to.be.false;
|
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function(done) {
|
|
|
|
imapClientStub.listMessages.yields();
|
|
|
|
emailDao.imapListMessages({
|
|
|
|
folder: 'INBOX',
|
|
|
|
offset: 0,
|
|
|
|
num: 10
|
|
|
|
}, function(err) {
|
|
|
|
expect(imapClientStub.listMessages.calledOnce).to.be.true;
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-08-21 07:43:19 -04:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-21 10:07:59 -04:00
|
|
|
it('should parse message body without attachement', function(done) {
|
|
|
|
var uid = 415;
|
|
|
|
|
|
|
|
imapClientStub.getMessage.yields(null, {
|
|
|
|
uid: uid
|
|
|
|
});
|
|
|
|
emailDao.imapGetMessage({
|
|
|
|
folder: 'INBOX',
|
|
|
|
uid: uid
|
|
|
|
}, function(err, message) {
|
|
|
|
expect(imapClientStub.getMessage.calledOnce).to.be.true;
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(message.uid).to.equal(uid);
|
|
|
|
expect(message.attachments).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-21 07:43:19 -04:00
|
|
|
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, {
|
2013-08-21 10:07:59 -04:00
|
|
|
uid: uid,
|
|
|
|
attachments: ['file.txt']
|
2013-08-21 07:43:19 -04:00
|
|
|
});
|
|
|
|
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);
|
2013-08-22 14:18:52 -04:00
|
|
|
expect(message.attachments[0].uint8Array).to.exist;
|
2013-08-21 07:43:19 -04:00
|
|
|
emailDao._imapClient = imapClientStub;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-08-20 07:30:35 -04:00
|
|
|
});
|
2013-08-19 15:13:32 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|