1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-10 11:15:01 -05:00
mail/test/new-unit/email-dao-test.js

431 lines
18 KiB
JavaScript
Raw Normal View History

define(function(require) {
'use strict';
var KeychainDAO = require('js/dao/keychain-dao'),
EmailDAO = require('js/dao/email-dao'),
2013-09-26 07:26:57 -04:00
DeviceStorageDAO = require('js/dao/devicestorage-dao'),
2013-09-19 12:35:12 -04:00
SmtpClient = require('smtp-client'),
ImapClient = require('imap-client'),
2013-09-26 07:26:57 -04:00
Crypto = require('js/crypto/crypto'),
app = require('js/app-config'),
expect = chai.expect;
var emaildaoTest = {
user: "whiteout.test@t-online.de",
passphrase: 'asdf',
asymKeySize: 512
};
2013-08-28 08:12:39 -04:00
var dummyMail;
var publicKey = "-----BEGIN PUBLIC KEY-----\r\n" + "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxy+Te5dyeWd7g0P+8LNO7fZDQ\r\n" + "g96xTb1J6pYE/pPTMlqhB6BRItIYjZ1US5q2vk5Zk/5KasBHAc9RbCqvh9v4XFEY\r\n" + "JVmTXC4p8ft1LYuNWIaDk+R3dyYXmRNct/JC4tks2+8fD3aOvpt0WNn3R75/FGBt\r\n" + "h4BgojAXDE+PRQtcVQIDAQAB\r\n" + "-----END PUBLIC KEY-----";
describe('Email DAO unit tests', function() {
var emailDao, account,
2013-09-26 07:26:57 -04:00
keychainStub, imapClientStub, smtpClientStub, cryptoStub, devicestorageStub;
beforeEach(function() {
2013-09-26 07:26:57 -04:00
// init dummy object
2013-08-28 08:12:39 -04:00
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
};
account = {
emailAddress: emaildaoTest.user,
symKeySize: app.config.symKeySize,
symIvSize: app.config.symIvSize,
asymKeySize: emaildaoTest.asymKeySize
};
keychainStub = sinon.createStubInstance(KeychainDAO);
imapClientStub = sinon.createStubInstance(ImapClient);
smtpClientStub = sinon.createStubInstance(SmtpClient);
2013-09-26 07:26:57 -04:00
cryptoStub = sinon.createStubInstance(Crypto);
devicestorageStub = sinon.createStubInstance(DeviceStorageDAO);
2013-09-26 07:26:57 -04:00
emailDao = new EmailDAO(keychainStub, imapClientStub, smtpClientStub, cryptoStub, devicestorageStub);
});
afterEach(function() {});
describe('init', function() {
it('should fail due to error in getUserKeyPair', function(done) {
2013-09-26 07:26:57 -04:00
devicestorageStub.init.yields();
keychainStub.getUserKeyPair.yields(42);
emailDao.init(account, emaildaoTest.passphrase, function(err) {
2013-09-26 07:26:57 -04:00
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(err).to.equal(42);
done();
});
});
it('should init with new keygen', function(done) {
2013-09-26 07:26:57 -04:00
devicestorageStub.init.yields();
keychainStub.getUserKeyPair.yields();
2013-09-26 07:26:57 -04:00
cryptoStub.init.yields(null, {});
keychainStub.putUserKeyPair.yields();
emailDao.init(account, emaildaoTest.passphrase, function(err) {
2013-09-26 07:26:57 -04:00
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
2013-09-26 07:26:57 -04:00
expect(cryptoStub.init.calledOnce).to.be.true;
expect(keychainStub.putUserKeyPair.calledOnce).to.be.true;
expect(err).to.not.exist;
done();
});
});
});
describe('login', function() {
it('should fail due to error in imap login', function(done) {
imapClientStub.login.yields(42);
emailDao.imapLogin(function(err) {
expect(err).to.equal(42);
done();
});
});
it('should work', function(done) {
imapClientStub.login.yields();
emailDao.imapLogin(function(err) {
expect(err).to.not.exist;
done();
});
});
});
describe('IMAP/SMTP tests', function() {
beforeEach(function(done) {
2013-09-26 07:26:57 -04:00
devicestorageStub.init.yields();
keychainStub.getUserKeyPair.yields();
2013-09-26 07:26:57 -04:00
cryptoStub.init.yields(null, {});
keychainStub.putUserKeyPair.yields();
emailDao.init(account, emaildaoTest.passphrase, function(err) {
2013-09-26 07:26:57 -04:00
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
2013-09-26 07:26:57 -04:00
expect(cryptoStub.init.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();
});
});
describe('SMTP: send email', function() {
it('should fail due to bad input', function(done) {
emailDao.smtpSend({}, function(err) {
expect(smtpClientStub.send.called).to.be.false;
expect(keychainStub.getReveiverPublicKey.called).to.be.false;
expect(err).to.exist;
done();
});
});
2013-08-28 08:12:39 -04:00
it('should fail due to invalid email address input', function(done) {
2013-08-28 08:12:39 -04:00
dummyMail.to = [{
address: 'asfd'
}];
emailDao.smtpSend(dummyMail, function(err) {
expect(smtpClientStub.send.called).to.be.false;
expect(keychainStub.getReveiverPublicKey.called).to.be.false;
expect(err).to.exist;
done();
});
});
2013-08-31 11:13:08 -04:00
it('should work for a new user', function(done) {
keychainStub.getReveiverPublicKey.yields(null, null);
smtpClientStub.send.yields();
emailDao.smtpSend(dummyMail, function(err) {
expect(keychainStub.getReveiverPublicKey.calledOnce).to.be.true;
2013-09-26 07:26:57 -04:00
// expect(smtpClientStub.send.called).to.be.true;
// smtpClientStub.send.calledWith(sinon.match(function(o) {
// return typeof o.attachments === 'undefined';
// }));
expect(err).to.exist;
2013-08-31 11:13:08 -04:00
done();
});
});
2013-08-28 08:12:39 -04:00
it('should work without attachments', function(done) {
keychainStub.getReveiverPublicKey.yields(null, {
_id: "fcf8b4aa-5d09-4089-8b4f-e3bc5091daf3",
userId: "safewithme.testuser@gmail.com",
publicKey: publicKey
});
smtpClientStub.send.yields();
2013-09-26 07:26:57 -04:00
cryptoStub.encryptListForUser.yields(null, []);
2013-08-28 08:12:39 -04:00
emailDao.smtpSend(dummyMail, function(err) {
expect(keychainStub.getReveiverPublicKey.calledOnce).to.be.true;
expect(smtpClientStub.send.calledOnce).to.be.true;
smtpClientStub.send.calledWith(sinon.match(function(o) {
return typeof o.attachments === 'undefined';
}));
expect(err).to.not.exist;
done();
});
});
2013-08-28 08:12:39 -04:00
it('should work with attachments', function(done) {
dummyMail.attachments = [{
fileName: 'bar.txt',
contentType: 'text/plain',
binStr: 'barbarbarbarbar'
}];
keychainStub.getReveiverPublicKey.yields(null, {
_id: "fcf8b4aa-5d09-4089-8b4f-e3bc5091daf3",
userId: "safewithme.testuser@gmail.com",
publicKey: publicKey
});
smtpClientStub.send.yields();
2013-09-26 07:26:57 -04:00
cryptoStub.encryptListForUser.yields(null, [{}, {}]);
emailDao.smtpSend(dummyMail, function(err) {
expect(keychainStub.getReveiverPublicKey.calledOnce).to.be.true;
expect(smtpClientStub.send.calledOnce).to.be.true;
2013-08-28 08:12:39 -04:00
smtpClientStub.send.calledWith(sinon.match(function(o) {
var ptAt = dummyMail.attachments[0];
var ctAt = o.attachments[0];
return ctAt.uint8Array && !ctAt.binStr && ctAt.fileName && ctAt.fileName !== ptAt.fileName;
}));
expect(err).to.not.exist;
done();
});
});
});
describe('IMAP: list folders', function() {
it('should work', function(done) {
imapClientStub.listAllFolders.yields();
emailDao.imapListFolders(function(err) {
expect(imapClientStub.listAllFolders.calledOnce).to.be.true;
expect(err).to.not.exist;
done();
});
});
});
2013-10-04 11:02:27 -04:00
describe('IMAP: get unread message count for folder', function() {
it('should work', function(done) {
imapClientStub.unreadMessages.yields();
emailDao.unreadMessages(function(err) {
expect(imapClientStub.unreadMessages.calledOnce).to.be.true;
expect(err).to.not.exist;
done();
});
});
});
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;
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();
});
});
});
describe('IMAP: get message content', 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 without attachement', function(done) {
var uid = 415;
2013-09-11 17:31:08 -04:00
imapClientStub.getMessage.yields(null, {
uid: uid,
body: ''
});
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-28 14:21:15 -04:00
// it('should parse message body and attachement', function(done) {
// var uid = 415,
// newImapClientStub = {
// getMessage: function() {}
// };
// sinon.stub(newImapClientStub, 'getMessage', function(options) {
// options.onMessageBody(null, {
// uid: uid,
// body: '',
// attachments: ['file.txt']
// });
// options.onAttachment(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.attachments[0].uint8Array).to.exist;
// emailDao._imapClient = imapClientStub;
// done();
// });
// });
});
2013-09-26 07:26:57 -04:00
describe('IMAP: sync messages to local storage', function() {
it('should not list unencrypted messages', function(done) {
imapClientStub.listMessages.yields(null, [{
uid: 413,
}, {
uid: 414,
}]);
imapClientStub.getMessage.yields(null, {
body: 'asdf'
});
devicestorageStub.removeList.yields();
devicestorageStub.storeList.yields();
emailDao.imapSync({
folder: 'INBOX',
offset: 0,
num: 2
}, function(err) {
expect(err).to.not.exist;
expect(imapClientStub.listMessages.calledOnce).to.be.true;
expect(imapClientStub.getMessage.called).to.be.false;
expect(devicestorageStub.removeList.calledOnce).to.be.true;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
done();
});
});
2013-09-26 07:26:57 -04:00
it('should work', function(done) {
imapClientStub.listMessages.yields(null, [{
uid: 413,
subject: app.string.subject
2013-09-26 07:26:57 -04:00
}, {
uid: 414,
subject: app.string.subject
2013-09-26 07:26:57 -04:00
}]);
imapClientStub.getMessage.yields(null, {
body: 'asdf'
});
devicestorageStub.removeList.yields();
2013-09-26 09:48:32 -04:00
devicestorageStub.storeList.yields();
2013-09-26 07:26:57 -04:00
emailDao.imapSync({
folder: 'INBOX',
offset: 0,
num: 2
}, function(err) {
expect(err).to.not.exist;
expect(imapClientStub.listMessages.calledOnce).to.be.true;
expect(imapClientStub.getMessage.calledTwice).to.be.true;
expect(devicestorageStub.removeList.calledOnce).to.be.true;
2013-09-26 09:48:32 -04:00
expect(devicestorageStub.storeList.calledOnce).to.be.true;
2013-09-26 07:26:57 -04:00
done();
});
});
});
describe('IMAP: list messages from local storage', function() {
it('should work', function(done) {
2013-09-26 09:48:32 -04:00
devicestorageStub.listItems.yields(null, [{
2013-10-04 09:47:30 -04:00
body: app.string.cryptPrefix + btoa(JSON.stringify({})) + app.string.cryptSuffix
2013-09-26 07:26:57 -04:00
}]);
keychainStub.getPublicKeys.yields(null, [{
_id: "fcf8b4aa-5d09-4089-8b4f-e3bc5091daf3",
userId: "safewithme.testuser@gmail.com",
publicKey: publicKey
}]);
2013-10-04 09:47:30 -04:00
cryptoStub.decryptListForUser.yields(null, [{
body: 'test body',
subject: 'test subject'
}]);
2013-09-26 07:26:57 -04:00
emailDao.listMessages({
folder: 'INBOX',
offset: 0,
num: 2
}, function(err, emails) {
2013-09-26 09:48:32 -04:00
expect(devicestorageStub.listItems.calledOnce).to.be.true;
2013-09-26 07:26:57 -04:00
expect(keychainStub.getPublicKeys.calledOnce).to.be.true;
expect(cryptoStub.decryptListForUser.calledOnce).to.be.true;
expect(err).to.not.exist;
2013-10-04 09:47:30 -04:00
expect(emails.length).to.equal(1);
2013-09-26 07:26:57 -04:00
done();
});
});
});
2013-10-04 11:02:27 -04:00
});
describe('IMAP: mark message as read', function() {
it('should work', function(done) {
imapClientStub.updateFlags.yields();
2013-09-26 07:26:57 -04:00
2013-10-04 11:02:27 -04:00
emailDao.imapMarkMessageRead({
folder: 'asdf',
uid: 1
}, function(err) {
expect(imapClientStub.updateFlags.calledOnce).to.be.true;
expect(err).to.not.exist;
done();
});
});
});
2013-10-04 11:02:27 -04:00
});
});