1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-21 23:08:50 -05:00
mail/test/unit/email/email-dao-test.js

2314 lines
78 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-10-07 14:32:23 -04:00
var mailreader = require('mailreader'),
ImapClient = require('imap-client'),
PgpMailer = require('pgpmailer'),
PgpBuilder = require('pgpbuilder'),
cfg = require('../../../src/js/app-config').config,
EmailDAO = require('../../../src/js/email/email'),
KeychainDAO = require('../../../src/js/service/keychain'),
PGP = require('../../../src/js/crypto/pgp'),
DeviceStorageDAO = require('../../../src/js/service/devicestorage'),
appConfig = require('../../../src/js/app-config'),
Auth = require('../../../src/js/service/auth'),
Dialog = require('../../../src/js/util/dialog');
2014-10-07 14:32:23 -04:00
describe('Email DAO unit tests', function() {
// show the stack trace when an error occurred
2014-11-05 08:27:34 -05:00
chai.config.includeStack = true;
2013-08-28 08:12:39 -04:00
2014-10-07 14:32:23 -04:00
// SUT
var dao;
2014-10-07 14:32:23 -04:00
// mocks
var keychainStub, imapClientStub, pgpMailerStub, pgpBuilderStub, pgpStub, devicestorageStub, parseStub, dialogStub, authStub;
2014-10-07 14:32:23 -04:00
// config
var emailAddress, passphrase, asymKeySize, account;
2014-10-07 14:32:23 -04:00
// test data
var folders, inboxFolder, sentFolder, draftsFolder, outboxFolder, trashFolder, flaggedFolder, otherFolder, mockKeyPair;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
//
// test data
2014-10-07 14:32:23 -04:00
//
emailAddress = 'asdf@asdf.com';
passphrase = 'asdf';
asymKeySize = 2048;
inboxFolder = {
name: 'Inbox',
type: 'Inbox',
path: 'INBOX',
2015-03-31 09:43:42 -04:00
messages: [],
uids: [],
modseq: 123
2014-10-07 14:32:23 -04:00
};
sentFolder = {
name: 'Sent',
type: 'Sent',
path: 'SENT',
2015-03-31 09:43:42 -04:00
messages: [],
uids: [],
modseq: 123
2014-10-07 14:32:23 -04:00
};
draftsFolder = {
name: 'Drafts',
type: 'Drafts',
path: 'DRAFTS',
2015-03-31 09:43:42 -04:00
messages: [],
uids: [],
modseq: 123
2014-10-07 14:32:23 -04:00
};
outboxFolder = {
name: 'Outbox',
type: 'Outbox',
path: 'OUTBOX',
2015-03-31 09:43:42 -04:00
messages: [],
uids: [],
modseq: 123
2014-10-07 14:32:23 -04:00
};
trashFolder = {
name: 'Trash',
type: 'Trash',
path: 'TRASH',
2015-03-31 09:43:42 -04:00
messages: [],
uids: [],
modseq: 123
2014-10-07 14:32:23 -04:00
};
flaggedFolder = {
name: 'Flagged',
type: 'Flagged',
path: 'FLAGGED',
2015-03-31 09:43:42 -04:00
messages: [],
uids: [],
modseq: 123
};
otherFolder = {
name: 'Other',
type: 'Other',
path: 'OTHER',
2015-03-31 09:43:42 -04:00
messages: [],
uids: [],
modseq: 123
};
folders = [inboxFolder, outboxFolder, trashFolder, sentFolder, otherFolder];
2014-10-07 14:32:23 -04:00
account = {
emailAddress: emailAddress,
asymKeySize: asymKeySize,
folders: folders,
online: true
};
mockKeyPair = {
publicKey: {
_id: 1234,
userId: emailAddress,
publicKey: 'publicpublicpublicpublic'
},
privateKey: {
_id: 1234,
userId: emailAddress,
encryptedKey: 'privateprivateprivateprivate'
}
};
//
// setup the mocks
//
keychainStub = sinon.createStubInstance(KeychainDAO);
imapClientStub = sinon.createStubInstance(ImapClient);
pgpMailerStub = sinon.createStubInstance(PgpMailer);
pgpBuilderStub = sinon.createStubInstance(PgpBuilder);
pgpStub = sinon.createStubInstance(PGP);
parseStub = sinon.stub(mailreader, 'parse');
devicestorageStub = sinon.createStubInstance(DeviceStorageDAO);
dialogStub = sinon.createStubInstance(Dialog);
authStub = sinon.createStubInstance(Auth);
2014-10-07 14:32:23 -04:00
//
// setup the SUT
//
dao = new EmailDAO(keychainStub, pgpStub, devicestorageStub, pgpBuilderStub, mailreader, dialogStub, appConfig, authStub);
2014-10-07 14:32:23 -04:00
dao._account = account;
dao._pgpMailer = pgpMailerStub;
dao._imapClient = imapClientStub;
//
// check configuration
//
expect(dao._keychain).to.equal(keychainStub);
expect(dao._pgp).to.equal(pgpStub);
expect(dao._devicestorage).to.equal(devicestorageStub);
expect(dao._mailreader).to.equal(mailreader);
expect(dao._pgpbuilder).to.equal(pgpBuilderStub);
});
2014-10-07 14:32:23 -04:00
afterEach(function() {
mailreader.parse.restore();
});
2014-11-04 14:31:09 -05:00
describe('#init', function() {
beforeEach(function() {
delete dao._account;
});
it('should initialize folders', function(done) {
2015-03-31 09:43:42 -04:00
devicestorageStub.listItems.withArgs('folders', true).returns(resolves([
[inboxFolder]
]));
2014-11-04 14:31:09 -05:00
dao.init({
account: account
2014-12-16 08:12:30 -05:00
}).then(function() {
2015-03-31 09:43:42 -04:00
expect(devicestorageStub.listItems.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
describe('#unlock', function() {
it('should unlock', function(done) {
pgpStub.getKeyParams.returns({
_id: mockKeyPair.publicKey._id,
userId: emailAddress,
userIds: [{
emailAddress: emailAddress
}]
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
pgpStub.importKeys.withArgs({
passphrase: passphrase,
privateKeyArmored: mockKeyPair.privateKey.encryptedKey,
publicKeyArmored: mockKeyPair.publicKey.publicKey
2014-12-16 08:12:30 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
pgpStub._privateKey = {
foo: 'bar'
};
2014-11-04 14:31:09 -05:00
dao.unlock({
passphrase: passphrase,
keypair: mockKeyPair
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-11-04 14:31:09 -05:00
expect(pgpStub.importKeys.calledOnce).to.be.true;
expect(dao._pgpbuilder._privateKey).to.equal(pgpStub._privateKey);
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should generate a keypair and unlock', function(done) {
var keypair = {
keyId: 123,
publicKeyArmored: mockKeyPair.publicKey.publicKey,
privateKeyArmored: mockKeyPair.privateKey.encryptedKey
};
2015-01-20 06:55:19 -05:00
var name = 'Hans Dampf';
2014-11-04 14:31:09 -05:00
pgpStub.generateKeys.withArgs({
emailAddress: emailAddress,
2015-01-20 06:55:19 -05:00
realname: name,
2014-11-04 14:31:09 -05:00
keySize: asymKeySize,
passphrase: passphrase
2014-12-16 08:12:30 -05:00
}).returns(resolves(keypair));
2014-11-04 14:31:09 -05:00
pgpStub.importKeys.withArgs({
passphrase: passphrase,
privateKeyArmored: mockKeyPair.privateKey.encryptedKey,
publicKeyArmored: mockKeyPair.publicKey.publicKey
2014-12-16 08:12:30 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
dao.unlock({
2015-01-20 06:55:19 -05:00
realname: name,
2014-11-04 14:31:09 -05:00
passphrase: passphrase
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-11-04 14:31:09 -05:00
expect(pgpStub.generateKeys.calledOnce).to.be.true;
expect(pgpStub.importKeys.calledOnce).to.be.true;
2013-10-21 07:10:42 -04:00
2014-11-04 14:31:09 -05:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
it('should fail when import fails', function(done) {
var keypair = {
keyId: 123,
publicKeyArmored: 'qwerty',
privateKeyArmored: 'asdfgh'
};
2014-12-16 08:12:30 -05:00
pgpStub.generateKeys.withArgs().returns(resolves(keypair));
pgpStub.importKeys.withArgs().returns(rejects({}));
2014-11-04 14:31:09 -05:00
dao.unlock({
passphrase: passphrase
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
2014-11-04 14:31:09 -05:00
expect(pgpStub.generateKeys.calledOnce).to.be.true;
expect(pgpStub.importKeys.calledOnce).to.be.true;
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should fail when generation fails', function(done) {
2014-12-16 08:12:30 -05:00
pgpStub.generateKeys.returns(rejects({}));
2014-11-04 14:31:09 -05:00
dao.unlock({
passphrase: passphrase
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(pgpStub.generateKeys.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
describe('#openFolder', function() {
it('should open an imap mailbox', function(done) {
imapClientStub.selectMailbox.withArgs({
path: inboxFolder.path
2015-02-12 10:10:46 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
dao.openFolder({
folder: inboxFolder
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-11-04 14:31:09 -05:00
expect(imapClientStub.selectMailbox.calledOnce).to.be.true;
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2013-10-21 07:10:42 -04:00
2014-12-16 08:12:30 -05:00
it('should not open the virtual outbox folder in imap', function(done) {
2014-11-04 14:31:09 -05:00
dao.openFolder({
folder: outboxFolder
2014-12-16 08:12:30 -05:00
}).then(function() {
expect(imapClientStub.selectMailbox.called).to.be.false;
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should not do anything in offline mode', function(done) {
account.online = false;
2014-11-04 14:31:09 -05:00
dao.openFolder({
folder: inboxFolder
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(imapClientStub.selectMailbox.called).to.be.false;
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
});
2013-10-21 07:10:42 -04:00
2015-03-31 09:43:42 -04:00
describe('#refreshOutbox', function() {
2014-11-04 14:31:09 -05:00
var localListStub, mail;
2013-10-21 07:10:42 -04:00
2014-11-04 14:31:09 -05:00
beforeEach(function() {
2015-03-31 09:43:42 -04:00
account.folders = [outboxFolder];
2014-11-04 14:31:09 -05:00
localListStub = sinon.stub(dao, '_localListMessages');
mail = {
uid: 123,
unread: true
};
2014-10-07 14:32:23 -04:00
});
2013-10-11 21:19:01 -04:00
2014-11-04 14:31:09 -05:00
it('should add messages from disk', function(done) {
localListStub.withArgs({
2015-03-31 09:43:42 -04:00
folder: outboxFolder,
exactmatch: false
2014-12-16 08:12:30 -05:00
}).returns(resolves([mail]));
2014-10-07 14:32:23 -04:00
2015-03-31 09:43:42 -04:00
dao.refreshOutbox().then(function() {
expect(outboxFolder.count).to.equal(1);
expect(outboxFolder.messages).to.contain(mail);
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-09-26 13:14:53 -04:00
2014-11-04 14:31:09 -05:00
it('should not add messages from disk', function(done) {
2015-03-31 09:43:42 -04:00
outboxFolder.messages = [mail];
2014-11-04 14:31:09 -05:00
localListStub.withArgs({
2015-03-31 09:43:42 -04:00
folder: outboxFolder,
exactmatch: false
2014-12-16 08:12:30 -05:00
}).returns(resolves([mail]));
2015-03-31 09:43:42 -04:00
dao.refreshOutbox().then(function() {
expect(outboxFolder.count).to.equal(1);
expect(outboxFolder.messages).to.contain(mail);
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should remove messages from memory', function(done) {
2015-03-31 09:43:42 -04:00
outboxFolder.messages = [mail];
2014-11-04 14:31:09 -05:00
localListStub.withArgs({
2015-03-31 09:43:42 -04:00
folder: outboxFolder,
exactmatch: false
2014-12-16 08:12:30 -05:00
}).returns(resolves([]));
2013-10-21 07:10:42 -04:00
2015-03-31 09:43:42 -04:00
dao.refreshOutbox().then(function() {
expect(outboxFolder.count).to.equal(0);
expect(outboxFolder.messages).to.be.empty;
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
});
2014-11-04 14:31:09 -05:00
describe('#deleteMessage', function() {
var imapDeleteStub, localDeleteStub, message;
beforeEach(function() {
message = {
uid: 1234
};
imapDeleteStub = sinon.stub(dao, '_imapDeleteMessage');
localDeleteStub = sinon.stub(dao, '_localDeleteMessage');
inboxFolder.messages = [message];
outboxFolder.messages = [message];
});
2014-11-04 14:31:09 -05:00
it('should delete from imap, local, memory', function(done) {
var deleteOpts = {
folder: inboxFolder,
uid: message.uid
};
2014-12-16 08:12:30 -05:00
imapDeleteStub.withArgs(deleteOpts).returns(resolves());
localDeleteStub.withArgs(deleteOpts).returns(resolves());
2014-11-04 14:31:09 -05:00
dao.deleteMessage({
folder: inboxFolder,
message: message
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-11-04 14:31:09 -05:00
expect(imapDeleteStub.calledOnce).to.be.true;
expect(localDeleteStub.calledOnce).to.be.true;
expect(inboxFolder.messages).to.not.contain(message);
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should delete from local, memory', function(done) {
var deleteOpts = {
folder: inboxFolder,
uid: message.uid
};
2014-12-16 08:12:30 -05:00
localDeleteStub.withArgs(deleteOpts).returns(resolves());
2014-11-04 14:31:09 -05:00
dao.deleteMessage({
folder: inboxFolder,
message: message,
localOnly: true
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-11-04 14:31:09 -05:00
expect(imapDeleteStub.called).to.be.false;
expect(localDeleteStub.calledOnce).to.be.true;
expect(inboxFolder.messages).to.not.contain(message);
2014-11-04 14:31:09 -05:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
it('should delete from outbox from local, memory', function(done) {
var deleteOpts = {
folder: outboxFolder,
uid: message.uid
};
2014-10-07 14:32:23 -04:00
2014-12-16 08:12:30 -05:00
localDeleteStub.withArgs(deleteOpts).returns(resolves());
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
dao.deleteMessage({
folder: outboxFolder,
message: message
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-11-04 14:31:09 -05:00
expect(imapDeleteStub.called).to.be.false;
expect(localDeleteStub.calledOnce).to.be.true;
expect(outboxFolder.messages).to.not.contain(message);
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should fail at delete from local', function(done) {
var deleteOpts = {
folder: inboxFolder,
uid: message.uid
};
2014-12-16 08:12:30 -05:00
imapDeleteStub.withArgs(deleteOpts).returns(resolves());
localDeleteStub.withArgs(deleteOpts).returns(rejects({}));
2014-11-04 14:31:09 -05:00
dao.deleteMessage({
folder: inboxFolder,
message: message
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(imapDeleteStub.calledOnce).to.be.true;
expect(localDeleteStub.calledOnce).to.be.true;
expect(inboxFolder.messages).to.contain(message);
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should fail at delete from imap', function(done) {
var deleteOpts = {
folder: inboxFolder,
uid: message.uid
};
2014-10-07 14:32:23 -04:00
2014-12-16 08:12:30 -05:00
imapDeleteStub.withArgs(deleteOpts).returns(rejects({}));
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
dao.deleteMessage({
folder: inboxFolder,
message: message
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(imapDeleteStub.calledOnce).to.be.true;
expect(localDeleteStub.called).to.be.false;
expect(inboxFolder.messages).to.contain(message);
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should fail at delete from imap in offline', function(done) {
account.online = false;
dao.deleteMessage({
folder: inboxFolder,
message: message
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(imapDeleteStub.called).to.be.false;
expect(localDeleteStub.called).to.be.false;
expect(inboxFolder.messages).to.contain(message);
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
describe('#setFlags', function() {
var imapMark, localListStub, localStoreStub, message;
beforeEach(function() {
message = {
uid: 1234
};
imapMark = sinon.stub(dao, '_imapMark');
localListStub = sinon.stub(dao, '_localListMessages');
localStoreStub = sinon.stub(dao, '_localStoreMessages');
inboxFolder.messages = [message];
outboxFolder.messages = [message];
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
it('should set flags for imap, disk, memory', function(done) {
imapMark.withArgs({
folder: inboxFolder,
uid: message.uid,
unread: message.unread,
2014-11-05 08:25:05 -05:00
answered: message.answered,
flagged: message.flagged
2014-12-16 08:12:30 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
localListStub.withArgs({
folder: inboxFolder,
uid: message.uid
2014-12-16 08:12:30 -05:00
}).returns(resolves([message]));
2014-11-04 14:31:09 -05:00
localStoreStub.withArgs({
folder: inboxFolder,
emails: [message]
2014-12-16 08:12:30 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
dao.setFlags({
folder: inboxFolder,
message: message
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-11-04 14:31:09 -05:00
expect(imapMark.calledOnce).to.be.true;
expect(localListStub.calledOnce).to.be.true;
expect(localStoreStub.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
it('should not explode when message has been deleted during imap roundtrip', function(done) {
imapMark.withArgs({
folder: inboxFolder,
uid: message.uid,
unread: message.unread,
answered: message.answered,
flagged: message.flagged
}).returns(resolves());
localListStub.withArgs({
folder: inboxFolder,
uid: message.uid
}).returns(resolves([]));
dao.setFlags({
folder: inboxFolder,
message: message
}).then(function() {
expect(imapMark.calledOnce).to.be.true;
expect(localListStub.calledOnce).to.be.true;
expect(localStoreStub.called).to.be.false;
done();
});
});
2014-11-04 14:31:09 -05:00
it('should set flags for outbox for disk, memory', function(done) {
localListStub.withArgs({
folder: outboxFolder,
uid: message.uid
2014-12-16 08:12:30 -05:00
}).returns(resolves([message]));
2014-11-04 14:31:09 -05:00
localStoreStub.withArgs({
folder: outboxFolder,
emails: [message]
2014-12-16 08:12:30 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
dao.setFlags({
folder: outboxFolder,
message: message
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-11-04 14:31:09 -05:00
expect(imapMark.called).to.be.false;
expect(localListStub.calledOnce).to.be.true;
expect(localStoreStub.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-02-17 08:31:14 -05:00
2014-11-04 14:31:09 -05:00
it('should set flags for disk, memory', function(done) {
localListStub.withArgs({
folder: inboxFolder,
uid: message.uid
2014-12-16 08:12:30 -05:00
}).returns(resolves([message]));
2014-11-04 14:31:09 -05:00
localStoreStub.withArgs({
folder: inboxFolder,
emails: [message]
2014-12-16 08:12:30 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
dao.setFlags({
folder: inboxFolder,
message: message,
localOnly: true
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-11-04 14:31:09 -05:00
expect(imapMark.called).to.be.false;
expect(localListStub.calledOnce).to.be.true;
expect(localStoreStub.calledOnce).to.be.true;
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should fail to set flags for imap', function(done) {
2014-12-16 08:12:30 -05:00
imapMark.returns(rejects({}));
localListStub.returns(resolves([message]));
localStoreStub.returns(resolves());
2014-11-04 14:31:09 -05:00
dao.setFlags({
folder: inboxFolder,
message: message
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(imapMark.calledOnce).to.be.true;
expect(localListStub.called).to.be.false;
expect(localStoreStub.called).to.be.false;
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
it('should fail to set flags for imap in offline mode', function(done) {
account.online = false;
2014-12-16 08:12:30 -05:00
localListStub.returns(resolves([message]));
localStoreStub.returns(resolves());
2014-11-04 14:31:09 -05:00
dao.setFlags({
folder: inboxFolder,
message: message
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(imapMark.called).to.be.false;
expect(localListStub.called).to.be.false;
expect(localStoreStub.called).to.be.false;
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
});
2014-02-20 09:42:51 -05:00
2014-11-04 14:31:09 -05:00
describe('#moveMessage', function() {
var localDeleteStub, imapMoveStub, message;
2014-02-20 09:42:51 -05:00
2014-11-04 14:31:09 -05:00
beforeEach(function() {
localDeleteStub = sinon.stub(dao, '_localDeleteMessage');
imapMoveStub = sinon.stub(dao, '_imapMoveMessage');
message = {
uid: 123
};
inboxFolder.messages.push(message);
});
2014-11-04 14:31:09 -05:00
it('should move a message to a destination folder', function(done) {
imapMoveStub.withArgs({
folder: inboxFolder,
destination: sentFolder,
uid: message.uid
2014-12-16 08:12:30 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
localDeleteStub.withArgs({
folder: inboxFolder,
uid: message.uid
2014-12-16 08:12:30 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
dao.moveMessage({
folder: inboxFolder,
destination: sentFolder,
message: message
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-11-04 14:31:09 -05:00
expect(imapMoveStub.calledOnce).to.be.true;
expect(localDeleteStub.calledOnce).to.be.true;
expect(inboxFolder.messages).to.not.contain(message);
done();
});
});
2014-11-04 14:31:09 -05:00
it('should not a message if IMAP errors', function(done) {
imapMoveStub.withArgs({
folder: inboxFolder,
destination: sentFolder,
uid: message.uid
2014-12-16 08:12:30 -05:00
}).returns(rejects(new Error()));
2014-11-04 14:31:09 -05:00
dao.moveMessage({
folder: inboxFolder,
destination: sentFolder,
message: message
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(imapMoveStub.calledOnce).to.be.true;
expect(localDeleteStub.called).to.be.false;
expect(inboxFolder.messages).to.contain(message);
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should fail at delete from imap in offline', function(done) {
account.online = false;
2014-11-04 14:31:09 -05:00
dao.moveMessage({
folder: inboxFolder,
destination: sentFolder,
message: message
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(imapMoveStub.called).to.be.false;
expect(localDeleteStub.called).to.be.false;
expect(inboxFolder.messages).to.contain(message);
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
done();
});
});
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
describe('#getBody', function() {
2015-03-31 09:43:42 -04:00
var localListStub, localStoreStub, fetchMessagesStub, uid;
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
beforeEach(function() {
uid = 12345;
2014-11-04 14:31:09 -05:00
localListStub = sinon.stub(dao, '_localListMessages');
localStoreStub = sinon.stub(dao, '_localStoreMessages');
2015-03-31 09:43:42 -04:00
fetchMessagesStub = sinon.stub(dao, '_fetchMessages');
2014-11-04 14:31:09 -05:00
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
it('should not do anything if the message already has content', function() {
var message = {
body: 'bender is great!'
};
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
dao.getBody({
2015-03-31 09:43:42 -04:00
messages: [message]
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
// should do nothing
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
it('should read an unencrypted body from the device', function(done) {
2015-03-31 09:43:42 -04:00
var body = 'bender is great! bender is great!';
var message = {
2014-11-04 14:31:09 -05:00
uid: uid
};
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
localListStub.withArgs({
folder: inboxFolder,
2015-03-31 09:43:42 -04:00
uid: [uid]
2014-12-16 08:12:30 -05:00
}).returns(resolves([{
2015-03-31 09:43:42 -04:00
uid: uid,
2014-11-04 14:31:09 -05:00
bodyParts: [{
2014-10-07 14:32:23 -04:00
type: 'text',
content: body
2014-11-04 14:31:09 -05:00
}]
2014-12-16 08:12:30 -05:00
}]));
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
dao.getBody({
2015-03-31 09:43:42 -04:00
messages: [message],
2014-11-04 14:31:09 -05:00
folder: inboxFolder
2015-03-31 09:43:42 -04:00
}).then(function() {
expect(message.body).to.equal(body);
expect(message.loadingBody).to.be.false;
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
expect(localListStub.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
expect(message.loadingBody).to.be.true;
});
2014-11-04 14:31:09 -05:00
it('should read a pgp/mime from the device', function(done) {
2015-03-31 09:43:42 -04:00
var pt = 'bender is great!';
var ct = '-----BEGIN PGP MESSAGE-----asdasdasd-----END PGP MESSAGE-----';
var message = {
2014-11-04 14:31:09 -05:00
uid: uid,
encrypted: true
};
localListStub.withArgs({
folder: inboxFolder,
2015-03-31 09:43:42 -04:00
uid: [uid]
2014-12-16 08:12:30 -05:00
}).returns(resolves([{
2015-03-31 09:43:42 -04:00
uid: uid,
2014-11-04 14:31:09 -05:00
bodyParts: [{
2014-10-07 14:32:23 -04:00
type: 'text',
content: pt
}, {
type: 'encrypted',
content: ct
2014-11-04 14:31:09 -05:00
}]
2014-12-16 08:12:30 -05:00
}]));
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
dao.getBody({
2015-03-31 09:43:42 -04:00
messages: [message],
2014-11-04 14:31:09 -05:00
folder: inboxFolder
2015-03-31 09:43:42 -04:00
}).then(function() {
expect(message.body).to.equal(ct);
expect(message.encrypted).to.be.true;
2014-11-04 14:31:09 -05:00
expect(message.loadingBody).to.be.false;
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
expect(localListStub.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
expect(message.loadingBody).to.be.true;
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
it('should read a signed pgp/mime from the device', function(done) {
2015-03-31 09:43:42 -04:00
var pt = 'bender is great!';
var signed = 'omg signed text';
var signedMimeTree = 'trallalalalala';
var signature = 'ugauga';
var message = {
2014-11-04 14:31:09 -05:00
uid: uid,
signed: true,
from: [{
address: 'asdasdasd'
}]
};
localListStub.withArgs({
folder: inboxFolder,
2015-03-31 09:43:42 -04:00
uid: [uid]
2014-12-16 08:12:30 -05:00
}).returns(resolves([{
2015-03-31 09:43:42 -04:00
uid: uid,
2014-11-04 14:31:09 -05:00
bodyParts: [{
2014-10-07 14:32:23 -04:00
type: 'text',
2014-11-04 14:31:09 -05:00
content: pt
}, {
type: 'signed',
content: [{
type: 'text',
content: signed
}],
signedMessage: signedMimeTree,
signature: signature
}]
2014-12-16 08:12:30 -05:00
}]));
keychainStub.getReceiverPublicKey.withArgs(message.from[0].address).returns(resolves(mockKeyPair.publicKey));
pgpStub.verifySignedMessage.withArgs(signedMimeTree, signature, mockKeyPair.publicKey.publicKey).returns(resolves(true));
2014-11-04 14:31:09 -05:00
dao.getBody({
2015-03-31 09:43:42 -04:00
messages: [message],
2014-11-04 14:31:09 -05:00
folder: inboxFolder
2015-03-31 09:43:42 -04:00
}).then(function() {
expect(message.body).to.equal(signed);
2014-11-04 14:31:09 -05:00
expect(message.signed).to.be.true;
expect(message.signaturesValid).to.be.true;
expect(message.loadingBody).to.be.false;
expect(localListStub.calledOnce).to.be.true;
expect(pgpStub.verifySignedMessage.calledOnce).to.be.true;
expect(keychainStub.getReceiverPublicKey.calledOnce).to.be.true;
done();
});
expect(message.loadingBody).to.be.true;
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
it('should read a pgp/inline from the device', function(done) {
2015-03-31 09:43:42 -04:00
var ct = '-----BEGIN PGP MESSAGE-----\nasdasdasd\n-----END PGP MESSAGE-----';
var pt = 'bla bla yadda yadda';
var message = {
2014-11-04 14:31:09 -05:00
uid: uid
};
2014-12-16 08:12:30 -05:00
localListStub.returns(resolves([{
2015-03-31 09:43:42 -04:00
uid: uid,
2014-11-04 14:31:09 -05:00
bodyParts: [{
type: 'text',
content: pt
}, {
type: 'text',
content: ct
}, {
type: 'text',
content: pt
}]
2014-12-16 08:12:30 -05:00
}]));
2014-11-04 14:31:09 -05:00
dao.getBody({
2015-03-31 09:43:42 -04:00
messages: [message],
2014-11-04 14:31:09 -05:00
folder: inboxFolder
2015-03-31 09:43:42 -04:00
}).then(function() {
expect(message.body).to.equal(ct);
expect(message.bodyParts[0].type).to.equal('encrypted');
expect(message.bodyParts[0].content).to.equal(ct);
expect(message.encrypted).to.be.true;
2014-11-04 14:31:09 -05:00
expect(message.loadingBody).to.be.false;
2014-11-04 14:31:09 -05:00
expect(localListStub.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
done();
});
2014-11-04 14:31:09 -05:00
expect(message.loadingBody).to.be.true;
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
it('should read a signed pgp/inline from the device', function(done) {
2015-03-31 09:43:42 -04:00
var expected = 'Lorem ipsum Aliquip tempor veniam proident.\n\nafguab;igab;igubalw\n\nLorem ipsum Dolor sed irure sint in non.\n\n\n';
var pt = '-----BEGIN PGP SIGNED MESSAGE-----\nHash: WTFHASH\n\n' + expected + '\n-----BEGIN PGP SIGNATURE----------END PGP SIGNATURE-----';
var message = {
2014-11-04 14:31:09 -05:00
uid: uid,
from: [{
address: 'asdasdasd'
}]
};
2014-12-16 08:12:30 -05:00
localListStub.returns(resolves([{
2015-03-31 09:43:42 -04:00
uid: uid,
2014-11-04 14:31:09 -05:00
bodyParts: [{
type: 'text',
content: pt
}]
2014-12-16 08:12:30 -05:00
}]));
keychainStub.getReceiverPublicKey.withArgs(message.from[0].address).returns(resolves(mockKeyPair.publicKey));
pgpStub.verifyClearSignedMessage.withArgs(pt, mockKeyPair.publicKey.publicKey).returns(resolves(true));
2014-11-04 14:31:09 -05:00
dao.getBody({
2015-03-31 09:43:42 -04:00
messages: [message],
2014-11-04 14:31:09 -05:00
folder: inboxFolder
2015-03-31 09:43:42 -04:00
}).then(function() {
expect(message.body).to.equal(expected);
2014-11-04 14:31:09 -05:00
expect(message.signed).to.be.true;
expect(message.signaturesValid).to.be.true;
expect(message.loadingBody).to.be.false;
expect(localListStub.calledOnce).to.be.true;
expect(pgpStub.verifyClearSignedMessage.calledOnce).to.be.true;
expect(keychainStub.getReceiverPublicKey.calledOnce).to.be.true;
done();
});
expect(message.loadingBody).to.be.true;
});
2014-10-07 14:32:23 -04:00
2015-03-31 09:43:42 -04:00
it('should stream from imap and set body', function(done) {
var body = 'bender is great! bender is great!';
var uid = 1234;
var message = {
uid: uid
2014-11-04 14:31:09 -05:00
};
localListStub.withArgs({
folder: inboxFolder,
2015-03-31 09:43:42 -04:00
uid: [uid]
}).returns(resolves([]));
2014-11-04 14:31:09 -05:00
2015-03-31 09:43:42 -04:00
fetchMessagesStub.withArgs({
messages: [message],
2014-11-04 14:31:09 -05:00
folder: inboxFolder
2015-03-31 09:43:42 -04:00
}).returns(resolves([{
2014-11-04 14:31:09 -05:00
uid: uid,
2015-03-31 09:43:42 -04:00
encrypted: false,
2014-11-04 14:31:09 -05:00
bodyParts: [{
2015-03-31 09:43:42 -04:00
type: 'text',
content: body
2014-11-04 14:31:09 -05:00
}]
2014-12-16 08:12:30 -05:00
}]));
2014-11-04 14:31:09 -05:00
dao.getBody({
2015-03-31 09:43:42 -04:00
messages: [message],
2014-11-04 14:31:09 -05:00
folder: inboxFolder
2015-03-31 09:43:42 -04:00
}).then(function() {
expect(message.body).to.equal(body);
expect(message.loadingBody).to.be.false;
2014-11-04 14:31:09 -05:00
expect(localListStub.calledOnce).to.be.true;
2015-03-31 09:43:42 -04:00
expect(fetchMessagesStub.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
done();
});
expect(message.loadingBody).to.be.true;
});
2015-03-31 09:43:42 -04:00
it('should not error when message is not available in imap', function(done) {
var message = {
uid: uid,
encrypted: true,
bodyParts: [{
type: 'text'
}]
};
localListStub.withArgs({
folder: inboxFolder,
2015-03-31 09:43:42 -04:00
uid: [uid]
}).returns(resolves());
2015-03-31 09:43:42 -04:00
fetchMessagesStub.withArgs({
messages: [message],
folder: inboxFolder
2015-03-31 09:43:42 -04:00
}).returns(rejects(new Error()));
2014-11-04 14:31:09 -05:00
dao.getBody({
2015-03-31 09:43:42 -04:00
messages: [message],
2014-11-04 14:31:09 -05:00
folder: inboxFolder
2015-03-31 09:43:42 -04:00
}).then(function(msgs) {
expect(msgs).to.be.empty;
expect(message.body).to.not.exist;
2014-11-04 14:31:09 -05:00
expect(message.loadingBody).to.be.false;
expect(localListStub.calledOnce).to.be.true;
2015-03-31 09:43:42 -04:00
expect(fetchMessagesStub.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
done();
2014-05-13 07:13:36 -04:00
});
2015-03-31 09:43:42 -04:00
expect(message.loadingBody).to.be.true;
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-05-13 07:13:36 -04:00
2014-11-04 14:31:09 -05:00
describe('#getAttachment', function() {
var imapGetStub, uid;
2014-11-04 14:31:09 -05:00
beforeEach(function() {
uid = 123456;
imapGetStub = sinon.stub(dao, '_getBodyParts');
});
2014-11-04 14:31:09 -05:00
it('should fetch an attachment from imap', function(done) {
var attmt = {};
imapGetStub.withArgs({
folder: inboxFolder,
uid: uid,
bodyParts: [attmt]
2014-12-16 08:12:30 -05:00
}).returns(resolves([{
2014-11-04 14:31:09 -05:00
content: 'CONTENT!!!'
2014-12-16 08:12:30 -05:00
}]));
2014-11-04 14:31:09 -05:00
dao.getAttachment({
folder: inboxFolder,
uid: uid,
attachment: attmt
2014-12-16 08:12:30 -05:00
}).then(function(fetchedAttmt) {
2014-11-04 14:31:09 -05:00
expect(fetchedAttmt).to.equal(attmt);
expect(attmt.content).to.not.be.empty;
expect(imapGetStub.calledOnce).to.be.true;
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should error during fetch', function(done) {
var attmt = {};
2014-12-16 08:12:30 -05:00
imapGetStub.returns(resolves());
2014-11-04 14:31:09 -05:00
dao.getAttachment({
folder: inboxFolder,
uid: uid,
attachment: attmt
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(imapGetStub.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
});
2014-11-04 14:31:09 -05:00
describe('#decryptBody', function() {
it('should do nothing when the message is not encrypted', function(done) {
var message = {
encrypted: false,
decrypted: true,
body: 'asd'
};
dao.decryptBody({
message: message
2014-12-16 08:12:30 -05:00
}).then(function(msg) {
expect(msg).to.equal(message);
done();
});
2014-11-04 14:31:09 -05:00
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
it('should do nothing when the message is already decrypted', function(done) {
var message = {
encrypted: true,
decrypted: true,
body: 'asd'
};
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
dao.decryptBody({
message: message
2014-12-16 08:12:30 -05:00
}).then(function(msg) {
expect(msg).to.equal(message);
done();
});
2014-11-04 14:31:09 -05:00
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
it('should do nothing when the message has no body', function(done) {
var message = {
encrypted: true,
decrypted: false,
body: ''
};
2014-11-04 14:31:09 -05:00
dao.decryptBody({
message: message
2014-12-16 08:12:30 -05:00
}).then(function(msg) {
expect(msg).to.equal(message);
done();
});
2014-11-04 14:31:09 -05:00
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
it('should do nothing when the message is decrypting', function(done) {
var message = {
encrypted: true,
decrypted: false,
body: 'asd',
decryptingBody: true
};
dao.decryptBody({
message: message
2014-12-16 08:12:30 -05:00
}).then(function(msg) {
expect(msg).to.equal(message);
done();
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('decrypt a pgp/mime message', function(done) {
var message, ct, pt, parsed;
pt = 'bender is great';
ct = '-----BEGIN PGP MESSAGE-----asdasdasd-----END PGP MESSAGE-----';
parsed = 'bender! bender! bender!';
message = {
from: [{
address: 'asdasdasd'
}],
body: ct,
encrypted: true,
bodyParts: [{
2014-10-07 14:32:23 -04:00
type: 'encrypted',
2014-11-04 14:31:09 -05:00
content: ct
}]
};
2014-10-07 14:32:23 -04:00
2014-12-16 08:12:30 -05:00
keychainStub.getReceiverPublicKey.withArgs(message.from[0].address).returns(resolves(mockKeyPair.publicKey));
pgpStub.decrypt.withArgs(ct, mockKeyPair.publicKey.publicKey).returns(resolves({
decrypted: pt,
signaturesValid: true
}));
2014-11-04 14:31:09 -05:00
parseStub.withArgs({
bodyParts: [{
type: 'encrypted',
content: ct,
raw: pt
}]
}).yieldsAsync(null, [{
type: 'encrypted',
content: [{
type: 'text',
content: parsed
}]
}]);
dao.decryptBody({
message: message
2014-12-16 08:12:30 -05:00
}).then(function(msg) {
2014-11-04 14:31:09 -05:00
expect(msg).to.equal(message);
expect(message.decrypted).to.be.true;
expect(message.signed).to.be.true;
expect(message.signaturesValid).to.be.true;
expect(message.body).to.equal(parsed);
expect(message.decryptingBody).to.be.false;
expect(keychainStub.getReceiverPublicKey.calledOnce).to.be.true;
expect(pgpStub.decrypt.calledOnce).to.be.true;
expect(parseStub.calledOnce).to.be.true;
done();
});
expect(message.decryptingBody).to.be.true;
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
it('decrypt a pgp/mime message with inner signature', function(done) {
var message, ct, pt, parsed, signed, signedMimeTree, signature;
pt = 'bender is great';
ct = '-----BEGIN PGP MESSAGE-----asdasdasd-----END PGP MESSAGE-----';
signedMimeTree = 'trallalalalala';
signature = 'ugauga';
signed = 'omg signed text';
parsed = 'bender! bender! bender!';
message = {
from: [{
address: 'asdasdasd'
}],
body: ct,
encrypted: true,
bodyParts: [{
type: 'encrypted',
content: ct
}]
};
2014-12-16 08:12:30 -05:00
keychainStub.getReceiverPublicKey.withArgs(message.from[0].address).returns(resolves(mockKeyPair.publicKey));
pgpStub.decrypt.withArgs(ct, mockKeyPair.publicKey.publicKey).returns(resolves({
decrypted: pt,
signaturesValid: undefined
}));
pgpStub.verifySignedMessage.withArgs(signedMimeTree, signature, mockKeyPair.publicKey.publicKey).returns(resolves(true));
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
parseStub.withArgs({
bodyParts: [{
type: 'encrypted',
content: ct,
raw: pt
}]
}).yieldsAsync(null, [{
type: 'encrypted',
content: [{
type: 'signed',
content: [{
type: 'text',
content: signed
2014-10-07 14:32:23 -04:00
}],
2014-11-04 14:31:09 -05:00
signedMessage: signedMimeTree,
signature: signature
}]
}]);
dao.decryptBody({
message: message
2014-12-16 08:12:30 -05:00
}).then(function(msg) {
2014-11-04 14:31:09 -05:00
expect(msg).to.equal(message);
expect(message.decrypted).to.be.true;
expect(message.body).to.equal(signed);
expect(message.signed).to.be.true;
expect(message.signaturesValid).to.be.true;
expect(message.decryptingBody).to.be.false;
expect(keychainStub.getReceiverPublicKey.calledTwice).to.be.true;
expect(pgpStub.decrypt.calledOnce).to.be.true;
expect(pgpStub.verifySignedMessage.calledOnce).to.be.true;
expect(parseStub.calledOnce).to.be.true;
done();
});
expect(message.decryptingBody).to.be.true;
});
2014-02-17 08:31:14 -05:00
2014-11-04 14:31:09 -05:00
it('decrypt a pgp/inline message', function(done) {
var message, ct, pt;
pt = 'bender is great';
ct = '-----BEGIN PGP MESSAGE-----asdasdasd-----END PGP MESSAGE-----';
message = {
from: [{
address: 'asdasdasd'
}],
body: ct,
encrypted: true,
bodyParts: [{
type: 'encrypted',
content: ct,
_isPgpInline: true
}]
};
2014-12-16 08:12:30 -05:00
keychainStub.getReceiverPublicKey.withArgs(message.from[0].address).returns(resolves(mockKeyPair.publicKey));
pgpStub.decrypt.withArgs(ct, mockKeyPair.publicKey.publicKey).returns(resolves({
decrypted: pt,
signaturesValid: true
}));
2014-11-04 14:31:09 -05:00
dao.decryptBody({
message: message
2014-12-16 08:12:30 -05:00
}).then(function(msg) {
2014-11-04 14:31:09 -05:00
expect(msg).to.equal(message);
expect(message.decrypted).to.be.true;
expect(message.body).to.equal(pt);
expect(message.decryptingBody).to.be.false;
expect(message.signed).to.be.true;
expect(message.signaturesValid).to.be.true;
expect(keychainStub.getReceiverPublicKey.calledOnce).to.be.true;
expect(pgpStub.decrypt.calledOnce).to.be.true;
expect(parseStub.called).to.be.false;
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
expect(message.decryptingBody).to.be.true;
});
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
it('should fail during decryption message', function(done) {
var message = {
from: [{
address: 'asdasdasd'
}],
body: 'asdjafuad',
encrypted: true,
bodyParts: [{
type: 'encrypted',
content: '-----BEGIN PGP MESSAGE-----asdasdasd-----END PGP MESSAGE-----'
}]
};
2014-12-16 08:12:30 -05:00
keychainStub.getReceiverPublicKey.returns(resolves(mockKeyPair.publicKey));
pgpStub.decrypt.returns(rejects(new Error('fail.')));
2014-11-04 14:31:09 -05:00
dao.decryptBody({
message: message
2014-12-16 08:12:30 -05:00
}).then(function(msg) {
2014-11-04 14:31:09 -05:00
expect(msg.body).to.equal('fail.');
expect(msg).to.exist;
expect(message.decryptingBody).to.be.false;
expect(keychainStub.getReceiverPublicKey.calledOnce).to.be.true;
expect(pgpStub.decrypt.calledOnce).to.be.true;
expect(parseStub.called).to.be.false;
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-05-12 16:07:25 -04:00
2014-11-04 14:31:09 -05:00
it('should fail during key export', function(done) {
var message = {
from: [{
address: 'asdasdasd'
}],
encrypted: true,
body: 'asdjafuad',
bodyParts: [{
type: 'encrypted',
content: '-----BEGIN PGP MESSAGE-----asdasdasd-----END PGP MESSAGE-----'
}]
};
2014-12-16 08:12:30 -05:00
keychainStub.getReceiverPublicKey.returns(resolves());
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
dao.decryptBody({
message: message
2014-12-16 08:12:30 -05:00
}).then(function(msg) {
expect(msg).to.equal(message);
2014-11-04 14:31:09 -05:00
expect(message.decryptingBody).to.be.false;
expect(keychainStub.getReceiverPublicKey.calledOnce).to.be.true;
2014-12-16 08:12:30 -05:00
expect(pgpStub.decrypt.called).to.be.true;
2014-11-04 14:31:09 -05:00
expect(parseStub.called).to.be.false;
2014-10-07 14:32:23 -04:00
2014-11-04 14:31:09 -05:00
done();
2014-05-12 16:07:25 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-05-12 16:07:25 -04:00
2014-11-04 14:31:09 -05:00
describe('#sendEncrypted', function() {
var credentials,
publicKeys,
dummyMail,
msg;
beforeEach(function() {
credentials = {
smtp: {
host: 'foo.io'
}
};
publicKeys = ["PUBLIC KEY"];
2014-11-04 14:31:09 -05:00
dummyMail = {
publicKeysArmored: publicKeys
};
2014-11-04 14:31:09 -05:00
msg = 'wow. such message. much rfc2822.';
});
2014-05-13 07:13:36 -04:00
2014-11-04 14:31:09 -05:00
it('should send encrypted and upload to sent', function(done) {
imapClientStub.uploadMessage.withArgs({
path: sentFolder.path,
message: msg
2015-02-12 10:10:46 -05:00
}).returns(resolves());
authStub.getCredentials.returns(resolves(credentials));
2014-11-04 14:31:09 -05:00
pgpMailerStub.send.withArgs({
encrypt: true,
mail: dummyMail,
smtpclient: undefined,
publicKeysArmored: publicKeys
2015-02-12 10:10:46 -05:00
}).returns(resolves(msg));
2014-05-13 07:13:36 -04:00
2014-11-04 14:31:09 -05:00
dao.sendEncrypted({
email: dummyMail
}, pgpMailerStub).then(function() {
expect(authStub.getCredentials.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
expect(pgpMailerStub.send.calledOnce).to.be.true;
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
expect(dao.ignoreUploadOnSent).to.be.false;
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should send encrypted and not upload to sent', function(done) {
credentials.smtp.host = 'smtp.gmail.com';
authStub.getCredentials.returns(resolves(credentials));
2014-11-04 14:31:09 -05:00
pgpMailerStub.send.withArgs({
encrypt: true,
mail: dummyMail,
smtpclient: undefined,
publicKeysArmored: publicKeys
2015-02-12 10:10:46 -05:00
}).returns(resolves(msg));
2014-11-04 14:31:09 -05:00
dao.sendEncrypted({
email: dummyMail
}, pgpMailerStub).then(function() {
expect(authStub.getCredentials.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
expect(pgpMailerStub.send.calledOnce).to.be.true;
expect(imapClientStub.uploadMessage.called).to.be.false;
expect(dao.ignoreUploadOnSent).to.be.true;
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should send encrypted and ignore error on upload', function(done) {
2015-02-12 10:10:46 -05:00
imapClientStub.uploadMessage.returns(rejects(new Error()));
pgpMailerStub.send.returns(resolves(msg));
authStub.getCredentials.returns(resolves(credentials));
2014-11-04 14:31:09 -05:00
dao.sendEncrypted({
email: dummyMail
}, pgpMailerStub).then(function() {
2014-11-04 14:31:09 -05:00
expect(pgpMailerStub.send.calledOnce).to.be.true;
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
expect(authStub.getCredentials.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
done();
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should not send when pgpmailer fails', function(done) {
2015-02-12 10:10:46 -05:00
pgpMailerStub.send.returns(rejects({}));
authStub.getCredentials.returns(resolves(credentials));
2014-05-13 07:13:36 -04:00
2014-11-04 14:31:09 -05:00
dao.sendEncrypted({
email: dummyMail
}, pgpMailerStub).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(authStub.getCredentials.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
expect(pgpMailerStub.send.calledOnce).to.be.true;
expect(imapClientStub.uploadMessage.called).to.be.false;
2014-11-04 14:31:09 -05:00
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should not send in offline mode', function(done) {
account.online = false;
2014-12-16 08:12:30 -05:00
dao.sendEncrypted({
email: dummyMail
}, pgpMailerStub).catch(function(err) {
2014-12-16 08:12:30 -05:00
expect(err.code).to.equal(42);
expect(authStub.getCredentials.called).to.be.false;
2014-11-04 14:31:09 -05:00
expect(pgpMailerStub.send.called).to.be.false;
expect(imapClientStub.uploadMessage.called).to.be.false;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
describe('#sendPlaintext', function() {
var credentials,
dummyMail,
msg;
beforeEach(function() {
credentials = {
smtp: {
host: 'foo.io'
}
};
dummyMail = {};
msg = 'wow. such message. much rfc2822.';
});
2014-11-04 14:31:09 -05:00
it('should send in the plain and upload to sent', function(done) {
pgpMailerStub.send.withArgs({
smtpclient: undefined,
mail: dummyMail
2015-02-12 10:10:46 -05:00
}).returns(resolves(msg));
authStub.getCredentials.returns(resolves(credentials));
2014-11-04 14:31:09 -05:00
imapClientStub.uploadMessage.withArgs({
path: sentFolder.path,
message: msg
2015-02-12 10:10:46 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
dao.sendPlaintext({
email: dummyMail
}, pgpMailerStub).then(function() {
expect(authStub.getCredentials.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
expect(pgpMailerStub.send.calledOnce).to.be.true;
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should send in the plain and not upload to sent', function(done) {
dao.ignoreUploadOnSent = true;
credentials.smtp.host = 'smtp.gmail.com';
2014-11-04 14:31:09 -05:00
pgpMailerStub.send.withArgs({
smtpclient: undefined,
mail: dummyMail
2015-02-12 10:10:46 -05:00
}).returns(resolves(msg));
authStub.getCredentials.returns(resolves(credentials));
2014-11-04 14:31:09 -05:00
dao.sendPlaintext({
email: dummyMail
}, pgpMailerStub).then(function() {
expect(authStub.getCredentials.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
expect(pgpMailerStub.send.calledOnce).to.be.true;
expect(imapClientStub.uploadMessage.called).to.be.false;
done();
2014-10-07 14:32:23 -04:00
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should send and ignore error on upload', function(done) {
2015-02-12 10:10:46 -05:00
imapClientStub.uploadMessage.returns(rejects(new Error()));
pgpMailerStub.send.returns(resolves(msg));
authStub.getCredentials.returns(resolves(credentials));
dao.sendPlaintext({
2014-11-04 14:31:09 -05:00
email: dummyMail
}, pgpMailerStub).then(function() {
expect(authStub.getCredentials.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
expect(pgpMailerStub.send.calledOnce).to.be.true;
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
done();
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should not send due to error', function(done) {
2015-02-12 10:10:46 -05:00
pgpMailerStub.send.returns(rejects({}));
authStub.getCredentials.returns(resolves(credentials));
2014-11-04 14:31:09 -05:00
dao.sendPlaintext({
email: dummyMail
}, pgpMailerStub).catch(function(err) {
2014-11-04 14:31:09 -05:00
expect(err).to.exist;
expect(authStub.getCredentials.calledOnce).to.be.true;
2014-11-04 14:31:09 -05:00
expect(pgpMailerStub.send.calledOnce).to.be.true;
expect(imapClientStub.uploadMessage.called).to.be.false;
done();
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
it('should not send in offline mode', function(done) {
account.online = false;
2014-12-16 08:12:30 -05:00
dao.sendPlaintext({
email: dummyMail
}, pgpMailerStub).catch(function(err) {
2014-12-16 08:12:30 -05:00
expect(err.code).to.equal(42);
expect(authStub.getCredentials.called).to.be.false;
2014-11-04 14:31:09 -05:00
expect(pgpMailerStub.send.called).to.be.false;
expect(imapClientStub.uploadMessage.called).to.be.false;
done();
});
});
2014-11-04 14:31:09 -05:00
});
2014-11-04 14:31:09 -05:00
describe('#encrypt', function() {
it('should encrypt', function(done) {
2015-02-12 10:10:46 -05:00
pgpBuilderStub.encrypt.returns(resolves());
2014-10-07 14:32:23 -04:00
2014-12-16 08:12:30 -05:00
dao.encrypt({}).then(function() {
2014-11-04 14:31:09 -05:00
expect(pgpBuilderStub.encrypt.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
});
2014-10-07 14:32:23 -04:00
describe('event handlers', function() {
2014-09-26 13:14:53 -04:00
2014-10-07 14:32:23 -04:00
describe('#onConnect', function() {
var initFoldersStub, credentials;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
2015-03-31 09:43:42 -04:00
initFoldersStub = sinon.stub(dao, '_updateFolders');
sinon.stub(dao, 'isOnline');
2014-10-07 14:32:23 -04:00
delete dao._imapClient;
credentials = {
imap: {}
};
2014-10-07 14:32:23 -04:00
});
it('should connect', function(done) {
2015-03-31 09:43:42 -04:00
account.folders = [inboxFolder];
inboxFolder.uids = [123];
dao.isOnline.returns(true);
authStub.getCredentials.returns(resolves(credentials));
2015-02-12 10:10:46 -05:00
imapClientStub.login.returns(resolves());
imapClientStub.selectMailbox.returns(resolves());
imapClientStub.listenForChanges.returns(resolves());
2014-12-16 08:12:30 -05:00
initFoldersStub.returns(resolves());
2014-10-07 14:32:23 -04:00
dao.onConnect(imapClientStub).then(function() {
2014-10-07 14:32:23 -04:00
expect(imapClientStub.login.calledOnce).to.be.true;
2014-12-11 09:02:08 -05:00
expect(imapClientStub.selectMailbox.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
expect(initFoldersStub.calledOnce).to.be.true;
expect(imapClientStub.mailboxCache).to.deep.equal({
'INBOX': {
exists: 123,
uidNext: 124,
uidlist: [123],
highestModseq: '123'
2014-10-07 14:32:23 -04:00
}
});
done();
});
2014-05-13 07:13:36 -04:00
});
it('should not connect when user agent is offline', function(done) {
dao.isOnline.returns(false);
dao.onConnect(imapClientStub).then(function() {
expect(authStub.getCredentials.called).to.be.false;
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-05-13 07:13:36 -04:00
2014-10-07 14:32:23 -04:00
describe('#onDisconnect', function() {
it('should discard imapClient and pgpMailer', function(done) {
2015-02-12 10:10:46 -05:00
imapClientStub.stopListeningForChanges.returns(resolves());
imapClientStub.logout.returns(resolves());
2014-12-16 08:12:30 -05:00
dao.onDisconnect().then(function() {
expect(imapClientStub.stopListeningForChanges.calledOnce).to.be.true;
expect(imapClientStub.logout.calledOnce).to.be.true;
expect(dao._account.online).to.be.false;
expect(dao._imapClient).to.not.exist;
expect(dao._pgpMailer).to.not.exist;
done();
});
2014-10-07 14:32:23 -04:00
});
});
2014-05-13 07:13:36 -04:00
2014-10-07 14:32:23 -04:00
describe('#_onSyncUpdate', function() {
2015-03-31 09:43:42 -04:00
var getBodyStub, deleteMessagesStub, setFlagsStub, localStoreFoldersStub;
var msgs;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
msgs = [{
uid: 5,
2015-03-31 09:43:42 -04:00
flags: ['\\Answered', '\\Seen'],
bodyParts: []
2014-10-07 14:32:23 -04:00
}];
inboxFolder.messages = msgs;
2015-03-31 09:43:42 -04:00
inboxFolder.uids = [5];
getBodyStub = sinon.stub(dao, 'getBody');
2014-10-07 14:32:23 -04:00
deleteMessagesStub = sinon.stub(dao, 'deleteMessage');
setFlagsStub = sinon.stub(dao, 'setFlags');
2015-03-31 09:43:42 -04:00
localStoreFoldersStub = sinon.stub(dao, '_localStoreFolders');
2014-10-07 14:32:23 -04:00
});
2014-05-13 07:13:36 -04:00
2015-02-12 10:10:46 -05:00
it('should get new message', function(done) {
2015-03-31 09:43:42 -04:00
getBodyStub.withArgs({
2014-10-07 14:32:23 -04:00
folder: inboxFolder,
2015-03-31 09:43:42 -04:00
messages: [{
uid: 7
}, {
uid: 8
}],
notifyNew: true
2015-02-12 10:10:46 -05:00
}).returns(resolves());
2014-10-07 14:32:23 -04:00
2015-03-31 09:43:42 -04:00
localStoreFoldersStub.returns(resolves());
2014-10-07 14:32:23 -04:00
dao._onSyncUpdate({
type: 'new',
path: inboxFolder.path,
2015-03-31 09:43:42 -04:00
list: [1, 7, 8]
});
2014-11-21 11:25:55 -05:00
2015-02-12 10:10:46 -05:00
setTimeout(function() {
2015-03-31 09:43:42 -04:00
expect(getBodyStub.calledOnce).to.be.true;
expect(localStoreFoldersStub.calledOnce).to.be.true;
2015-02-12 10:10:46 -05:00
done();
}, 0);
2014-10-07 14:32:23 -04:00
});
2015-02-12 10:10:46 -05:00
it('should delete message', function(done) {
2014-10-07 14:32:23 -04:00
deleteMessagesStub.withArgs({
folder: inboxFolder,
message: msgs[0],
localOnly: true
2015-02-12 10:10:46 -05:00
}).returns(resolves());
2014-05-13 07:13:36 -04:00
2014-10-07 14:32:23 -04:00
dao._onSyncUpdate({
type: 'deleted',
path: inboxFolder.path,
list: [5]
});
2014-11-21 11:25:55 -05:00
2015-02-12 10:10:46 -05:00
setTimeout(function() {
expect(deleteMessagesStub.calledOnce).to.be.true;
done();
}, 0);
2014-10-07 14:32:23 -04:00
});
2015-02-12 10:10:46 -05:00
it('should fetch flags', function(done) {
2014-10-07 14:32:23 -04:00
setFlagsStub.withArgs({
folder: inboxFolder,
message: msgs[0],
localOnly: true
2015-02-12 10:10:46 -05:00
}).returns(resolves());
2014-10-07 14:32:23 -04:00
dao._onSyncUpdate({
type: 'messages',
path: inboxFolder.path,
list: msgs
});
2014-11-21 11:25:55 -05:00
2015-02-12 10:10:46 -05:00
setTimeout(function() {
expect(setFlagsStub.calledOnce).to.be.true;
done();
}, 0);
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('internal API', function() {
describe('#_checkSignatures', function() {
it('should check signatures in clearsigned message', function(done) {
var message = {
from: [{
address: 'asdasdasd'
}],
clearSignedMessage: 'trallalalalala'
};
2014-12-16 08:12:30 -05:00
keychainStub.getReceiverPublicKey.withArgs(message.from[0].address).returns(resolves(mockKeyPair.publicKey));
pgpStub.verifyClearSignedMessage.withArgs(message.clearSignedMessage, mockKeyPair.publicKey.publicKey).returns(resolves(true));
2014-12-16 08:12:30 -05:00
dao._checkSignatures(message).then(function(signaturesValid) {
2014-10-07 14:32:23 -04:00
expect(signaturesValid).to.be.true;
expect(keychainStub.getReceiverPublicKey.calledOnce).to.be.true;
expect(pgpStub.verifyClearSignedMessage.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should check signatures in pgp/mime signed message', function(done) {
var message = {
from: [{
address: 'asdasdasd'
}],
signedMessage: 'trallalalalala',
signature: 'ugauga'
};
2014-12-16 08:12:30 -05:00
keychainStub.getReceiverPublicKey.withArgs(message.from[0].address).returns(resolves(mockKeyPair.publicKey));
pgpStub.verifySignedMessage.withArgs(message.signedMessage, message.signature, mockKeyPair.publicKey.publicKey).returns(resolves(true));
2014-10-07 14:32:23 -04:00
2014-12-16 08:12:30 -05:00
dao._checkSignatures(message).then(function(signaturesValid) {
2014-10-07 14:32:23 -04:00
expect(signaturesValid).to.be.true;
expect(keychainStub.getReceiverPublicKey.calledOnce).to.be.true;
expect(pgpStub.verifySignedMessage.calledOnce).to.be.true;
done();
});
});
2014-10-07 14:32:23 -04:00
it('should error while checking signatures', function(done) {
var message = {
from: [{
address: 'asdasdasd'
}],
signedMessage: 'trallalalalala',
signature: 'ugauga'
};
2014-12-16 08:12:30 -05:00
keychainStub.getReceiverPublicKey.withArgs(message.from[0].address).returns(resolves(mockKeyPair.publicKey));
pgpStub.verifySignedMessage.returns(rejects(new Error()));
2014-10-07 14:32:23 -04:00
2014-12-16 08:12:30 -05:00
dao._checkSignatures(message).catch(function(error) {
2014-10-07 14:32:23 -04:00
expect(error).to.exist;
expect(keychainStub.getReceiverPublicKey.calledOnce).to.be.true;
expect(pgpStub.verifySignedMessage.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should error while fetching public key', function(done) {
var message = {
from: [{
address: 'asdasdasd'
}],
signedMessage: 'trallalalalala',
signature: 'ugauga'
};
2014-12-16 08:12:30 -05:00
keychainStub.getReceiverPublicKey.returns(rejects(new Error()));
2014-12-16 08:12:30 -05:00
dao._checkSignatures(message).catch(function(error) {
2014-10-07 14:32:23 -04:00
expect(error).to.exist;
expect(keychainStub.getReceiverPublicKey.calledOnce).to.be.true;
expect(pgpStub.verifySignedMessage.called).to.be.false;
done();
});
});
2014-10-07 14:32:23 -04:00
});
2015-03-31 09:43:42 -04:00
describe('#_updateFolders', function() {
2014-10-07 14:32:23 -04:00
beforeEach(function() {
2015-03-31 09:43:42 -04:00
sinon.stub(dao, 'getBody');
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should initialize from imap if online', function(done) {
account.folders = [];
2015-03-31 09:43:42 -04:00
inboxFolder.uids = [7];
inboxFolder.messages = undefined;
2015-02-12 10:10:46 -05:00
imapClientStub.listWellKnownFolders.returns(resolves({
2014-10-07 14:32:23 -04:00
Inbox: [inboxFolder],
Sent: [sentFolder],
Drafts: [draftsFolder],
Trash: [trashFolder],
Flagged: [flaggedFolder],
Other: [otherFolder]
2015-02-12 10:10:46 -05:00
}));
2014-10-07 14:32:23 -04:00
devicestorageStub.storeList.withArgs(sinon.match(function(arg) {
expect(arg[0][0].name).to.deep.equal(inboxFolder.name);
expect(arg[0][0].path).to.deep.equal(inboxFolder.path);
expect(arg[0][0].type).to.deep.equal(inboxFolder.type);
expect(arg[0][1].name).to.deep.equal(sentFolder.name);
expect(arg[0][1].path).to.deep.equal(sentFolder.path);
expect(arg[0][1].type).to.deep.equal(sentFolder.type);
expect(arg[0][2].name).to.deep.equal(outboxFolder.name);
expect(arg[0][2].path).to.deep.equal(outboxFolder.path);
expect(arg[0][2].type).to.deep.equal(outboxFolder.type);
expect(arg[0][3].name).to.deep.equal(draftsFolder.name);
expect(arg[0][3].path).to.deep.equal(draftsFolder.path);
expect(arg[0][3].type).to.deep.equal(draftsFolder.type);
expect(arg[0][4].name).to.deep.equal(trashFolder.name);
expect(arg[0][4].path).to.deep.equal(trashFolder.path);
expect(arg[0][4].type).to.deep.equal(trashFolder.type);
expect(arg[0][5].name).to.deep.equal(flaggedFolder.name);
expect(arg[0][5].path).to.deep.equal(flaggedFolder.path);
expect(arg[0][5].type).to.deep.equal(flaggedFolder.type);
expect(arg[0][6].name).to.deep.equal(otherFolder.name);
expect(arg[0][6].path).to.deep.equal(otherFolder.path);
expect(arg[0][6].type).to.deep.equal(otherFolder.type);
2014-10-07 14:32:23 -04:00
return true;
2014-12-16 08:12:30 -05:00
}), 'folders').returns(resolves());
2014-10-07 14:32:23 -04:00
2015-03-31 09:43:42 -04:00
dao.getBody.withArgs({
folder: inboxFolder,
messages: [{
uid: 7
}]
}).returns(resolves());
2014-10-07 14:32:23 -04:00
2015-03-31 09:43:42 -04:00
dao._updateFolders().then(function() {
2014-10-07 14:32:23 -04:00
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
2015-03-31 09:43:42 -04:00
expect(dao.getBody.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
});
2014-10-07 14:32:23 -04:00
it('should update folders from imap', function(done) {
account.folders = [inboxFolder, outboxFolder, trashFolder, {
name: 'foo',
type: 'Sent',
path: 'bar',
}];
2015-02-12 10:10:46 -05:00
imapClientStub.listWellKnownFolders.returns(resolves({
2014-10-07 14:32:23 -04:00
Inbox: [inboxFolder],
Sent: [sentFolder],
Drafts: [draftsFolder],
Trash: [trashFolder],
Flagged: [flaggedFolder],
Other: [otherFolder]
2015-02-12 10:10:46 -05:00
}));
2014-10-07 14:32:23 -04:00
devicestorageStub.storeList.withArgs(sinon.match(function(arg) {
2015-03-31 09:43:42 -04:00
expect(arg[0][0].name).to.deep.equal(inboxFolder.name);
expect(arg[0][0].path).to.deep.equal(inboxFolder.path);
expect(arg[0][0].type).to.deep.equal(inboxFolder.type);
expect(arg[0][1].name).to.deep.equal(sentFolder.name);
expect(arg[0][1].path).to.deep.equal(sentFolder.path);
expect(arg[0][1].type).to.deep.equal(sentFolder.type);
expect(arg[0][2].name).to.deep.equal(outboxFolder.name);
expect(arg[0][2].path).to.deep.equal(outboxFolder.path);
expect(arg[0][2].type).to.deep.equal(outboxFolder.type);
expect(arg[0][3].name).to.deep.equal(draftsFolder.name);
expect(arg[0][3].path).to.deep.equal(draftsFolder.path);
expect(arg[0][3].type).to.deep.equal(draftsFolder.type);
expect(arg[0][4].name).to.deep.equal(trashFolder.name);
expect(arg[0][4].path).to.deep.equal(trashFolder.path);
expect(arg[0][4].type).to.deep.equal(trashFolder.type);
expect(arg[0][5].name).to.deep.equal(flaggedFolder.name);
expect(arg[0][5].path).to.deep.equal(flaggedFolder.path);
expect(arg[0][5].type).to.deep.equal(flaggedFolder.type);
expect(arg[0][6].name).to.deep.equal(otherFolder.name);
expect(arg[0][6].path).to.deep.equal(otherFolder.path);
expect(arg[0][6].type).to.deep.equal(otherFolder.type);
2014-10-07 14:32:23 -04:00
return true;
2014-12-16 08:12:30 -05:00
}), 'folders').returns(resolves());
2015-03-31 09:43:42 -04:00
dao._updateFolders().then(function() {
2014-10-07 14:32:23 -04:00
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
2015-03-31 09:43:42 -04:00
expect(dao.getBody.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
});
2014-10-07 14:32:23 -04:00
describe('#_imapMark', function() {
it('should flag a mail', function(done) {
imapClientStub.updateFlags.withArgs({
path: inboxFolder.path,
folder: inboxFolder,
uid: 1,
unread: false,
answered: false
2015-02-12 10:10:46 -05:00
}).returns(resolves());
2014-10-07 14:32:23 -04:00
dao._imapMark({
folder: inboxFolder,
uid: 1,
unread: false,
answered: false
2014-12-16 08:12:30 -05:00
}).then(function() {
2014-10-07 14:32:23 -04:00
expect(imapClientStub.updateFlags.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
});
2014-11-04 14:31:09 -05:00
describe('#_imapMoveMessage', function() {
it('should move a message to a destination folder', function(done) {
imapClientStub.moveMessage.withArgs({
path: inboxFolder.path,
destination: sentFolder.path,
uid: 123
2015-02-12 10:10:46 -05:00
}).returns(resolves());
2014-11-04 14:31:09 -05:00
dao._imapMoveMessage({
folder: inboxFolder,
destination: sentFolder,
uid: 123
2014-12-16 08:12:30 -05:00
}).then(done);
2014-11-04 14:31:09 -05:00
});
});
2014-10-07 14:32:23 -04:00
describe('#_imapDeleteMessage', function() {
var uid = 1337;
2013-12-12 07:14:49 -05:00
2014-10-07 14:32:23 -04:00
it('should fail when disconnected', function(done) {
dao._account.online = false;
2014-12-16 08:12:30 -05:00
dao._imapDeleteMessage({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err.code).to.equal(42);
done();
});
});
2014-10-07 14:32:23 -04:00
it('should move to trash', function(done) {
imapClientStub.moveMessage.withArgs({
path: inboxFolder.path,
uid: uid,
destination: trashFolder.path
2015-02-12 10:10:46 -05:00
}).returns(resolves());
2014-10-07 14:32:23 -04:00
dao._imapDeleteMessage({
folder: inboxFolder,
uid: uid
2014-12-16 08:12:30 -05:00
}).then(done);
2014-10-07 14:32:23 -04:00
});
it('should purge message', function(done) {
imapClientStub.deleteMessage.withArgs({
path: trashFolder.path,
uid: uid
2015-02-12 10:10:46 -05:00
}).returns(resolves());
2014-10-07 14:32:23 -04:00
dao._imapDeleteMessage({
folder: trashFolder,
uid: uid
2014-12-16 08:12:30 -05:00
}).then(done);
2014-10-07 14:32:23 -04:00
});
});
2015-03-31 09:43:42 -04:00
describe('#_fetchMessages', function() {
var localStoreStub, localStoreFoldersStub, getBodyPartsStub;
var messages;
beforeEach(function() {
localStoreStub = sinon.stub(dao, '_localStoreMessages');
localStoreFoldersStub = sinon.stub(dao, '_localStoreFolders');
getBodyPartsStub = sinon.stub(dao, '_getBodyParts');
messages = [{
uid: 1337,
subject: 'asdasd',
unread: true,
modseq: '124',
bodyParts: [{
type: 'text'
}]
}, {
uid: 1339,
subject: 'asdasd',
unread: true,
modseq: '124',
bodyParts: [{
type: 'attachment'
}]
}];
});
2014-10-07 14:32:23 -04:00
it('should list messages', function(done) {
imapClientStub.listMessages.withArgs({
path: inboxFolder.path,
2015-03-31 09:43:42 -04:00
uids: [1337, 1339]
}).returns(resolves(messages));
localStoreStub.withArgs({
folder: inboxFolder,
emails: [messages[0]]
}).returns(resolves());
localStoreStub.withArgs({
folder: inboxFolder,
emails: [messages[1]]
}).returns(resolves());
localStoreFoldersStub.returns(resolves());
getBodyPartsStub.withArgs({
folder: inboxFolder,
uid: messages[0].uid,
bodyParts: messages[0].bodyParts
}).returns(resolves(messages[0].bodyParts));
2015-03-31 09:43:42 -04:00
dao._fetchMessages({
2014-10-07 14:32:23 -04:00
folder: inboxFolder,
2015-03-31 09:43:42 -04:00
messages: messages
2014-12-16 08:12:30 -05:00
}).then(function(msgs) {
2014-10-07 14:32:23 -04:00
expect(msgs).to.exist;
expect(imapClientStub.listMessages.calledOnce).to.be.true;
2015-03-31 09:43:42 -04:00
expect(localStoreStub.calledOnce).to.be.true;
expect(getBodyPartsStub.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when listMessages fails', function(done) {
2015-02-12 10:10:46 -05:00
imapClientStub.listMessages.returns(rejects({}));
2015-03-31 09:43:42 -04:00
dao._fetchMessages({
2014-10-07 14:32:23 -04:00
folder: inboxFolder,
2015-03-31 09:43:42 -04:00
messages: messages
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(imapClientStub.listMessages.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when disconnected', function(done) {
dao._account.online = false;
2015-03-31 09:43:42 -04:00
dao._fetchMessages({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err.code).to.equal(42);
done();
});
});
2014-10-07 14:32:23 -04:00
});
describe('#_imapUploadMessage', function() {
it('should upload a message', function(done) {
var msg = 'wow. such message. much rfc2822.';
imapClientStub.uploadMessage.withArgs({
path: draftsFolder.path,
message: msg
2015-02-12 10:10:46 -05:00
}).returns(resolves());
dao._imapUploadMessage({
folder: draftsFolder,
message: msg
2014-12-16 08:12:30 -05:00
}).then(function() {
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
done();
});
});
});
2014-10-07 14:32:23 -04:00
describe('#_getBodyParts', function() {
it('should get bodyParts', function(done) {
var bp = [{
type: 'text',
content: 'bender is great! bender is great!'
}];
2014-10-07 14:32:23 -04:00
imapClientStub.getBodyParts.withArgs({
folder: inboxFolder,
path: inboxFolder.path,
uid: 123,
bodyParts: bp
}).returns(resolves(bp));
2014-10-07 14:32:23 -04:00
parseStub.yieldsAsync(null, []);
dao._getBodyParts({
folder: inboxFolder,
uid: 123,
bodyParts: bp
2014-12-16 08:12:30 -05:00
}).then(function(parts) {
2014-10-07 14:32:23 -04:00
expect(parts).to.exist;
expect(imapClientStub.getBodyParts.calledOnce).to.be.true;
expect(parseStub.calledOnce).to.be.true;
done();
});
});
it('should fail when deleted on IMAP', function(done) {
var bp = [{
type: 'text'
}];
imapClientStub.getBodyParts.withArgs({
folder: inboxFolder,
path: inboxFolder.path,
uid: 123,
bodyParts: bp
}).returns(resolves());
parseStub.yieldsAsync(null, []);
dao._getBodyParts({
folder: inboxFolder,
uid: 123,
bodyParts: bp
}).catch(function(err) {
expect(err).to.exist;
expect(imapClientStub.getBodyParts.calledOnce).to.be.true;
expect(parseStub.called).to.be.false;
done();
});
});
it('should fail when getBody fails', function(done) {
imapClientStub.getBodyParts.returns(rejects({}));
dao._getBodyParts({
folder: inboxFolder,
uid: 123,
bodyParts: []
}).catch(function(err) {
expect(err).to.exist;
expect(imapClientStub.getBodyParts.calledOnce).to.be.true;
expect(parseStub.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when getBody fails', function(done) {
2015-02-12 10:10:46 -05:00
imapClientStub.getBodyParts.returns(rejects({}));
2014-10-07 14:32:23 -04:00
dao._getBodyParts({
folder: inboxFolder,
uid: 123,
bodyParts: []
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
2014-10-07 14:32:23 -04:00
expect(imapClientStub.getBodyParts.calledOnce).to.be.true;
expect(parseStub.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when disconnected', function(done) {
dao._account.online = false;
2014-12-16 08:12:30 -05:00
dao._getBodyParts({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err.code).to.equal(42);
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('#_localListMessages', function() {
var uid = 123;
2014-01-18 05:42:28 -05:00
2014-10-07 14:32:23 -04:00
it('should list without uid', function(done) {
2015-03-31 09:43:42 -04:00
devicestorageStub.listItems.withArgs('email_' + inboxFolder.path, true).returns(resolves([{}]));
2014-01-18 05:42:28 -05:00
2014-10-07 14:32:23 -04:00
dao._localListMessages({
folder: inboxFolder
2014-12-16 08:12:30 -05:00
}).then(function(messages) {
expect(messages.length).to.exist;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-01-18 05:42:28 -05:00
2014-10-07 14:32:23 -04:00
it('should list with uid', function(done) {
2015-03-31 09:43:42 -04:00
devicestorageStub.listItems.withArgs('email_' + inboxFolder.path + '_' + uid, true).returns(resolves([{}]));
2014-10-07 14:32:23 -04:00
dao._localListMessages({
folder: inboxFolder,
uid: uid
2014-12-16 08:12:30 -05:00
}).then(function(messages) {
expect(messages.length).to.exist;
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('#_localStoreMessages', function() {
it('should store messages', function(done) {
2014-12-16 08:12:30 -05:00
devicestorageStub.storeList.withArgs([{}], 'email_' + inboxFolder.path).returns(resolves());
2014-10-07 14:32:23 -04:00
dao._localStoreMessages({
folder: inboxFolder,
emails: [{}]
2014-12-16 08:12:30 -05:00
}).then(done);
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('#_localDeleteMessage', function() {
var uid = 1337;
2014-10-07 14:32:23 -04:00
it('should delete message', function(done) {
2014-12-16 08:12:30 -05:00
devicestorageStub.removeList.withArgs('email_' + inboxFolder.path + '_' + uid).returns(resolves());
2014-10-07 14:32:23 -04:00
dao._localDeleteMessage({
folder: inboxFolder,
uid: uid
2014-12-16 08:12:30 -05:00
}).then(done);
2014-10-07 14:32:23 -04:00
});
2013-12-03 14:24:12 -05:00
2014-10-07 14:32:23 -04:00
it('should fail when uid is missing', function(done) {
dao._localDeleteMessage({
folder: inboxFolder
2014-12-16 08:12:30 -05:00
}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
2013-12-03 14:24:12 -05:00
});
});
2014-10-07 14:32:23 -04:00
});
describe('#_uploadToSent', function() {
it('should upload', function(done) {
var msg = 'wow. such message. much rfc2822.';
imapClientStub.uploadMessage.withArgs({
path: sentFolder.path,
message: msg
2015-02-12 10:10:46 -05:00
}).returns(resolves());
dao._uploadToSent({
message: msg
2014-12-16 08:12:30 -05:00
}).then(function() {
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
done();
});
});
});
describe('#checkIgnoreUploadOnSent', function() {
it('should ignore upload on gmail', function() {
expect(dao.checkIgnoreUploadOnSent('bla.gmail.com')).to.be.true;
expect(dao.checkIgnoreUploadOnSent('bla.googlemail.com')).to.be.true;
});
it('should not ignore upload on other domain', function() {
expect(dao.checkIgnoreUploadOnSent('imap.foo.com')).to.be.false;
});
});
});
});