1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-23 09:22:23 -05:00
mail/test/unit/email/outbox-bo-test.js

296 lines
9.7 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
var OutboxBO = require('../../../src/js/email/outbox'),
KeychainDAO = require('../../../src/js/service/keychain'),
EmailDAO = require('../../../src/js/email/email'),
DeviceStorageDAO = require('../../../src/js/service/devicestorage');
2014-10-07 14:32:23 -04:00
2014-12-15 11:37:21 -05:00
describe('Outbox unit test', function() {
2014-10-07 14:32:23 -04:00
var outbox, emailDaoStub, devicestorageStub, keychainStub,
dummyUser = 'spiderpig@springfield.com';
2014-11-05 08:27:34 -05:00
chai.config.includeStack = true;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
emailDaoStub = sinon.createStubInstance(EmailDAO);
emailDaoStub._account = {
emailAddress: dummyUser,
folders: [{
type: 'Outbox'
}],
online: true
};
devicestorageStub = sinon.createStubInstance(DeviceStorageDAO);
keychainStub = sinon.createStubInstance(KeychainDAO);
outbox = new OutboxBO(emailDaoStub, keychainStub, devicestorageStub);
});
afterEach(function() {});
describe('start/stop checking', function() {
it('should work', function() {
function onOutboxUpdate(err) {
expect(err).to.not.exist;
}
outbox.startChecking(onOutboxUpdate);
expect(outbox._intervalId).to.exist;
outbox.stopChecking();
expect(outbox._intervalId).to.not.exist;
});
});
describe('put', function() {
2014-12-15 11:37:21 -05:00
beforeEach(function() {
sinon.stub(outbox, '_processOutbox');
});
afterEach(function() {
outbox._processOutbox.restore();
});
it('should throw error for message without recipients', function(done) {
var mail = {
from: [{
name: 'member',
address: 'member@whiteout.io'
}],
to: [],
cc: [],
bcc: []
};
outbox.put(mail).catch(function(err) {
expect(err).to.exist;
expect(keychainStub.getReceiverPublicKey.called).to.be.false;
done();
});
});
2014-10-07 14:32:23 -04:00
it('should not encrypt and store a mail', function(done) {
var mail, senderKey, receiverKey;
senderKey = {
publicKey: 'SENDER PUBLIC KEY'
};
receiverKey = {
publicKey: 'RECEIVER PUBLIC KEY'
};
mail = {
from: [{
name: 'member',
address: 'member@whiteout.io'
2014-01-20 05:03:01 -05:00
}],
2014-10-07 14:32:23 -04:00
to: [{
name: 'member',
address: 'member'
}, {
name: 'notamember',
address: 'notamember'
}],
cc: [],
bcc: []
};
2014-12-15 11:37:21 -05:00
keychainStub.getReceiverPublicKey.withArgs(mail.from[0].address).returns(resolves(senderKey));
keychainStub.getReceiverPublicKey.withArgs(mail.to[0].address).returns(resolves(receiverKey));
keychainStub.getReceiverPublicKey.withArgs(mail.to[1].address).returns(resolves());
2014-10-07 14:32:23 -04:00
2014-12-15 11:37:21 -05:00
devicestorageStub.storeList.withArgs([mail]).returns(resolves());
2014-12-15 11:37:21 -05:00
outbox.put(mail).then(function() {
2014-10-07 14:32:23 -04:00
expect(mail.publicKeysArmored.length).to.equal(2);
expect(emailDaoStub.encrypt.called).to.be.false;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
});
2014-10-07 14:32:23 -04:00
it('should not encrypt a mail with bcc and store a mail', function(done) {
var mail;
2014-10-07 14:32:23 -04:00
mail = {
from: [{
name: 'member',
address: 'member@whiteout.io'
}],
to: [{
name: 'member',
address: 'member@whiteout.io'
}],
cc: [],
bcc: [{
name: 'member',
address: 'member@whiteout.io'
}]
};
2014-12-15 11:37:21 -05:00
devicestorageStub.storeList.withArgs([mail]).returns(resolves());
2014-12-15 11:37:21 -05:00
outbox.put(mail).then(function() {
2014-10-07 14:32:23 -04:00
expect(mail.publicKeysArmored.length).to.equal(0);
expect(keychainStub.getReceiverPublicKey.called).to.be.false;
expect(emailDaoStub.encrypt.called).to.be.false;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
done();
2014-02-24 04:14:07 -05:00
});
});
2014-10-07 14:32:23 -04:00
it('should encrypt and store a mail', function(done) {
var mail, senderKey, receiverKey;
2014-10-07 14:32:23 -04:00
senderKey = {
publicKey: 'SENDER PUBLIC KEY'
};
receiverKey = {
publicKey: 'RECEIVER PUBLIC KEY'
};
mail = {
from: [{
2014-02-24 04:14:07 -05:00
name: 'member',
address: 'member@whiteout.io'
2014-10-07 14:32:23 -04:00
}],
to: [{
name: 'member',
address: 'member'
}, {
name: 'notamember',
address: 'notamember'
}],
cc: [],
bcc: []
};
2014-12-15 11:37:21 -05:00
keychainStub.getReceiverPublicKey.withArgs(mail.from[0].address).returns(resolves(senderKey));
keychainStub.getReceiverPublicKey.withArgs(mail.to[0].address).returns(resolves(receiverKey));
keychainStub.getReceiverPublicKey.withArgs(mail.to[1].address).returns(resolves(receiverKey));
2014-10-07 14:32:23 -04:00
emailDaoStub.encrypt.withArgs({
mail: mail,
publicKeysArmored: [senderKey.publicKey, receiverKey.publicKey, receiverKey.publicKey]
2014-12-15 11:37:21 -05:00
}).returns(resolves());
2014-10-07 14:32:23 -04:00
2014-12-15 11:37:21 -05:00
devicestorageStub.storeList.withArgs([mail]).returns(resolves());
2014-10-07 14:32:23 -04:00
2014-12-15 11:37:21 -05:00
outbox.put(mail).then(function() {
2014-10-07 14:32:23 -04:00
expect(mail.publicKeysArmored.length).to.equal(3);
expect(emailDaoStub.encrypt.calledOnce).to.be.true;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
});
describe('process outbox', function() {
it('should send to registered users and update pending mails', function(done) {
var from, member, invited, notinvited, newlyjoined, dummyMails, newlyjoinedKey;
from = [{
name: 'member',
address: 'member@whiteout.io'
}];
member = {
id: '12',
from: from,
to: [{
name: 'member',
address: 'member'
}],
encrypted: true,
publicKeysArmored: ['ARMORED KEY OF MEMBER'],
unregisteredUsers: []
};
invited = {
id: '34',
from: from,
to: [{
name: 'invited',
address: 'invited'
}],
publicKeysArmored: [],
unregisteredUsers: [{
name: 'invited',
address: 'invited'
}]
};
notinvited = {
id: '56',
from: from,
to: [{
name: 'notinvited',
address: 'notinvited'
}],
publicKeysArmored: [],
unregisteredUsers: [{
name: 'notinvited',
address: 'notinvited'
}]
};
newlyjoined = {
id: '78',
from: from,
to: [{
name: 'newlyjoined',
address: 'newlyjoined'
}],
encrypted: true,
publicKeysArmored: [],
unregisteredUsers: [{
name: 'newlyjoined',
address: 'newlyjoined'
}]
};
newlyjoinedKey = {
publicKey: 'THIS IS THE NEWLY JOINED PUBLIC KEY!'
};
dummyMails = [member, invited, notinvited, newlyjoined];
2014-12-15 11:37:21 -05:00
devicestorageStub.listItems.returns(resolves(dummyMails));
2014-10-07 14:32:23 -04:00
2014-12-15 11:37:21 -05:00
emailDaoStub.sendPlaintext.returns(resolves());
2014-10-07 14:32:23 -04:00
emailDaoStub.sendEncrypted.withArgs({
email: newlyjoined
2014-12-15 11:37:21 -05:00
}).returns(resolves());
2014-10-07 14:32:23 -04:00
emailDaoStub.sendEncrypted.withArgs({
email: member
2014-12-15 11:37:21 -05:00
}).returns(resolves());
2014-10-07 14:32:23 -04:00
2014-12-15 11:37:21 -05:00
devicestorageStub.removeList.returns(resolves());
2014-10-07 14:32:23 -04:00
function onOutboxUpdate(err, count) {
expect(err).to.not.exist;
expect(count).to.equal(0);
expect(outbox._outboxBusy).to.be.false;
expect(emailDaoStub.sendEncrypted.callCount).to.equal(2);
expect(emailDaoStub.sendPlaintext.callCount).to.equal(2);
expect(devicestorageStub.listItems.callCount).to.equal(1);
expect(devicestorageStub.removeList.callCount).to.equal(4);
expect(keychainStub.getReceiverPublicKey.callCount).to.equal(0);
done();
}
outbox._processOutbox(onOutboxUpdate);
});
it('should not process outbox in offline mode', function(done) {
emailDaoStub._account.online = false;
2014-12-15 11:37:21 -05:00
devicestorageStub.listItems.returns(resolves([{}]));
2013-12-05 09:22:44 -05:00
2014-10-07 14:32:23 -04:00
outbox._processOutbox(function(err, count) {
expect(err).to.not.exist;
expect(count).to.equal(1);
expect(devicestorageStub.listItems.callCount).to.equal(1);
expect(outbox._outboxBusy).to.be.false;
done();
2014-01-20 05:03:01 -05:00
});
});
});
});