mail/test/new-unit/outbox-bo-test.js

247 lines
8.9 KiB
JavaScript
Raw Normal View History

define(function(require) {
'use strict';
var expect = chai.expect,
OutboxBO = require('js/bo/outbox'),
KeychainDAO = require('js/dao/keychain-dao'),
EmailDAO = require('js/dao/email-dao'),
DeviceStorageDAO = require('js/dao/devicestorage-dao');
2014-02-24 04:14:07 -05:00
chai.Assertion.includeStack = true;
describe('Outbox Business Object unit test', function() {
2014-02-28 08:45:10 -05:00
var outbox, emailDaoStub, devicestorageStub, keychainStub,
dummyUser = 'spiderpig@springfield.com';
beforeEach(function() {
emailDaoStub = sinon.createStubInstance(EmailDAO);
emailDaoStub._account = {
emailAddress: dummyUser,
folders: [{
type: 'Outbox'
2014-01-20 05:03:01 -05:00
}],
online: true
};
devicestorageStub = sinon.createStubInstance(DeviceStorageDAO);
keychainStub = sinon.createStubInstance(KeychainDAO);
2014-02-28 08:45:10 -05:00
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;
});
});
2014-02-24 04:14:07 -05:00
describe('put', function() {
it('should not encrypt and store a mail', function(done) {
2014-02-24 04:14:07 -05:00
var mail, senderKey, receiverKey;
senderKey = {
publicKey: 'SENDER PUBLIC KEY'
};
receiverKey = {
publicKey: 'RECEIVER PUBLIC KEY'
};
mail = {
from: [{
name: 'member',
address: 'member@whiteout.io'
}],
to: [{
name: 'member',
address: 'member'
}, {
name: 'notamember',
address: 'notamember'
}],
cc: [],
bcc: []
};
keychainStub.getReceiverPublicKey.withArgs(mail.from[0].address).yieldsAsync(null, senderKey);
keychainStub.getReceiverPublicKey.withArgs(mail.to[0].address).yieldsAsync(null, receiverKey);
keychainStub.getReceiverPublicKey.withArgs(mail.to[1].address).yieldsAsync();
devicestorageStub.storeList.withArgs([mail]).yieldsAsync();
outbox.put(mail, function(error) {
expect(error).to.not.exist;
expect(mail.publicKeysArmored.length).to.equal(2);
expect(emailDaoStub.encrypt.called).to.be.false;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
done();
});
});
it('should 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'
}],
to: [{
name: 'member',
address: 'member'
}, {
name: 'notamember',
address: 'notamember'
}],
cc: [],
bcc: []
};
keychainStub.getReceiverPublicKey.withArgs(mail.from[0].address).yieldsAsync(null, senderKey);
keychainStub.getReceiverPublicKey.withArgs(mail.to[0].address).yieldsAsync(null, receiverKey);
keychainStub.getReceiverPublicKey.withArgs(mail.to[1].address).yieldsAsync(null, receiverKey);
2014-02-24 04:14:07 -05:00
emailDaoStub.encrypt.withArgs({
mail: mail,
publicKeysArmored: [senderKey.publicKey, receiverKey.publicKey, receiverKey.publicKey]
2014-02-24 04:14:07 -05:00
}).yieldsAsync();
devicestorageStub.storeList.withArgs([mail]).yieldsAsync();
outbox.put(mail, function(error) {
expect(error).to.not.exist;
expect(mail.publicKeysArmored.length).to.equal(3);
expect(emailDaoStub.encrypt.calledOnce).to.be.true;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
2014-02-24 04:14:07 -05:00
done();
});
});
});
describe('process outbox', function() {
it('should send to registered users and update pending mails', function(done) {
2014-02-24 04:14:07 -05:00
var from, member, invited, notinvited, newlyjoined, dummyMails, newlyjoinedKey;
2014-02-24 04:14:07 -05:00
from = [{
name: 'member',
address: 'member@whiteout.io'
}];
member = {
2014-02-24 04:14:07 -05:00
id: '12',
from: from,
to: [{
name: 'member',
2014-02-24 04:14:07 -05:00
address: 'member'
}],
2014-02-28 08:45:10 -05:00
encrypted: true,
2014-02-24 04:14:07 -05:00
publicKeysArmored: ['ARMORED KEY OF MEMBER'],
unregisteredUsers: []
};
invited = {
2014-02-24 04:14:07 -05:00
id: '34',
from: from,
to: [{
name: 'invited',
2014-02-24 04:14:07 -05:00
address: 'invited'
}],
publicKeysArmored: [],
unregisteredUsers: [{
name: 'invited',
address: 'invited'
}]
};
notinvited = {
2014-02-24 04:14:07 -05:00
id: '56',
from: from,
to: [{
name: 'notinvited',
2014-02-24 04:14:07 -05:00
address: 'notinvited'
}],
publicKeysArmored: [],
unregisteredUsers: [{
name: 'notinvited',
address: 'notinvited'
}]
};
newlyjoined = {
id: '78',
from: from,
to: [{
name: 'newlyjoined',
address: 'newlyjoined'
}],
2014-02-28 08:45:10 -05:00
encrypted: true,
2014-02-24 04:14:07 -05:00
publicKeysArmored: [],
unregisteredUsers: [{
name: 'newlyjoined',
address: 'newlyjoined'
}]
};
2014-02-24 04:14:07 -05:00
newlyjoinedKey = {
publicKey: 'THIS IS THE NEWLY JOINED PUBLIC KEY!'
};
dummyMails = [member, invited, notinvited, newlyjoined];
devicestorageStub.listItems.yieldsAsync(null, dummyMails);
emailDaoStub.sendPlaintext.yieldsAsync();
2014-01-20 05:03:01 -05:00
2014-02-24 04:14:07 -05:00
emailDaoStub.sendEncrypted.withArgs({
email: newlyjoined
}).yieldsAsync();
emailDaoStub.sendEncrypted.withArgs({
email: member
}).yieldsAsync();
2014-02-28 08:45:10 -05:00
devicestorageStub.removeList.yieldsAsync();
function onOutboxUpdate(err, count) {
expect(err).to.not.exist;
2014-02-28 08:45:10 -05:00
expect(count).to.equal(0);
2014-02-24 04:14:07 -05:00
expect(outbox._outboxBusy).to.be.false;
2014-02-28 08:45:10 -05:00
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);
2014-02-24 04:14:07 -05:00
done();
}
outbox._processOutbox(onOutboxUpdate);
});
2013-12-05 09:22:44 -05:00
2014-01-20 05:03:01 -05:00
it('should not process outbox in offline mode', function(done) {
emailDaoStub._account.online = false;
2014-02-24 04:14:07 -05:00
devicestorageStub.listItems.yieldsAsync(null, [{}]);
2014-01-20 05:03:01 -05:00
outbox._processOutbox(function(err, count) {
expect(err).to.not.exist;
expect(count).to.equal(1);
2014-02-24 04:14:07 -05:00
expect(devicestorageStub.listItems.callCount).to.equal(1);
2014-01-20 05:03:01 -05:00
expect(outbox._outboxBusy).to.be.false;
done();
});
});
});
});
});