2013-11-19 10:14:48 -05:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var expect = chai.expect,
|
|
|
|
OutboxBO = require('js/bo/outbox'),
|
2013-11-20 13:13:39 -05:00
|
|
|
KeychainDAO = require('js/dao/keychain-dao'),
|
2013-11-19 10:14:48 -05:00
|
|
|
EmailDAO = require('js/dao/email-dao'),
|
|
|
|
DeviceStorageDAO = require('js/dao/devicestorage-dao'),
|
|
|
|
InvitationDAO = require('js/dao/invitation-dao');
|
|
|
|
|
2014-02-24 04:14:07 -05:00
|
|
|
chai.Assertion.includeStack = true;
|
|
|
|
|
2013-11-19 10:14:48 -05:00
|
|
|
describe('Outbox Business Object unit test', function() {
|
2013-11-20 13:13:39 -05:00
|
|
|
var outbox, emailDaoStub, devicestorageStub, invitationDaoStub, keychainStub,
|
|
|
|
dummyUser = 'spiderpig@springfield.com';
|
2013-11-19 10:14:48 -05:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
emailDaoStub = sinon.createStubInstance(EmailDAO);
|
2013-11-20 13:13:39 -05:00
|
|
|
emailDaoStub._account = {
|
2013-12-04 11:13:45 -05:00
|
|
|
emailAddress: dummyUser,
|
|
|
|
folders: [{
|
|
|
|
type: 'Outbox'
|
2014-01-20 05:03:01 -05:00
|
|
|
}],
|
|
|
|
online: true
|
2013-11-20 13:13:39 -05:00
|
|
|
};
|
2013-11-21 11:37:07 -05:00
|
|
|
devicestorageStub = sinon.createStubInstance(DeviceStorageDAO);
|
|
|
|
keychainStub = sinon.createStubInstance(KeychainDAO);
|
2013-11-19 10:14:48 -05:00
|
|
|
invitationDaoStub = sinon.createStubInstance(InvitationDAO);
|
2013-11-21 11:37:07 -05:00
|
|
|
outbox = new OutboxBO(emailDaoStub, keychainStub, devicestorageStub, invitationDaoStub);
|
2013-11-19 10:14:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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 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();
|
|
|
|
|
|
|
|
emailDaoStub.encrypt.withArgs({
|
|
|
|
mail: mail,
|
|
|
|
publicKeysArmored: [senderKey.publicKey, receiverKey.publicKey]
|
|
|
|
}).yieldsAsync();
|
|
|
|
|
|
|
|
devicestorageStub.storeList.withArgs([mail]).yieldsAsync();
|
|
|
|
|
|
|
|
outbox.put(mail, function(error) {
|
|
|
|
expect(error).to.not.exist;
|
|
|
|
|
|
|
|
expect(mail.publicKeysArmored.length).to.equal(2);
|
|
|
|
expect(mail.unregisteredUsers.length).to.equal(1);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-20 13:13:39 -05:00
|
|
|
describe('process outbox', function() {
|
2013-11-21 11:37:07 -05:00
|
|
|
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;
|
2013-11-21 11:37:07 -05:00
|
|
|
|
2014-02-24 04:14:07 -05:00
|
|
|
from = [{
|
|
|
|
name: 'member',
|
|
|
|
address: 'member@whiteout.io'
|
|
|
|
}];
|
2013-11-21 11:37:07 -05:00
|
|
|
member = {
|
2014-02-24 04:14:07 -05:00
|
|
|
id: '12',
|
|
|
|
from: from,
|
2013-11-20 13:13:39 -05:00
|
|
|
to: [{
|
|
|
|
name: 'member',
|
2014-02-24 04:14:07 -05:00
|
|
|
address: 'member'
|
|
|
|
}],
|
|
|
|
publicKeysArmored: ['ARMORED KEY OF MEMBER'],
|
|
|
|
unregisteredUsers: []
|
2013-11-21 11:37:07 -05:00
|
|
|
};
|
|
|
|
invited = {
|
2014-02-24 04:14:07 -05:00
|
|
|
id: '34',
|
|
|
|
from: from,
|
2013-11-20 13:13:39 -05:00
|
|
|
to: [{
|
|
|
|
name: 'invited',
|
2014-02-24 04:14:07 -05:00
|
|
|
address: 'invited'
|
|
|
|
}],
|
|
|
|
publicKeysArmored: [],
|
|
|
|
unregisteredUsers: [{
|
|
|
|
name: 'invited',
|
|
|
|
address: 'invited'
|
2013-11-20 13:13:39 -05:00
|
|
|
}]
|
2013-11-21 11:37:07 -05:00
|
|
|
};
|
|
|
|
notinvited = {
|
2014-02-24 04:14:07 -05:00
|
|
|
id: '56',
|
|
|
|
from: from,
|
2013-11-20 13:13:39 -05:00
|
|
|
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'
|
|
|
|
}],
|
|
|
|
publicKeysArmored: [],
|
|
|
|
unregisteredUsers: [{
|
|
|
|
name: 'newlyjoined',
|
|
|
|
address: 'newlyjoined'
|
2013-11-20 13:13:39 -05:00
|
|
|
}]
|
2013-11-21 11:37:07 -05:00
|
|
|
};
|
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);
|
|
|
|
|
|
|
|
keychainStub.getReceiverPublicKey.withArgs(invited.unregisteredUsers[0].address).yieldsAsync();
|
|
|
|
keychainStub.getReceiverPublicKey.withArgs(notinvited.unregisteredUsers[0].address).yieldsAsync();
|
|
|
|
keychainStub.getReceiverPublicKey.withArgs(newlyjoined.unregisteredUsers[0].address).yieldsAsync(null, newlyjoinedKey);
|
|
|
|
|
|
|
|
invitationDaoStub.check.withArgs({
|
|
|
|
recipient: invited.to[0].address,
|
|
|
|
sender: invited.from[0].address
|
|
|
|
}).yieldsAsync(null, InvitationDAO.INVITE_PENDING);
|
|
|
|
|
|
|
|
invitationDaoStub.check.withArgs({
|
|
|
|
recipient: notinvited.to[0].address,
|
|
|
|
sender: notinvited.from[0].address
|
|
|
|
}).yieldsAsync(null, InvitationDAO.INVITE_MISSING);
|
|
|
|
|
|
|
|
invitationDaoStub.invite.withArgs({
|
|
|
|
recipient: notinvited.to[0].address,
|
|
|
|
sender: notinvited.from[0].address
|
|
|
|
}).yieldsAsync(null, InvitationDAO.INVITE_SUCCESS);
|
2013-11-20 13:13:39 -05:00
|
|
|
|
2013-12-03 13:21:50 -05:00
|
|
|
emailDaoStub.sendPlaintext.yieldsAsync();
|
2014-01-20 05:03:01 -05:00
|
|
|
|
2014-02-24 04:14:07 -05:00
|
|
|
emailDaoStub.reEncrypt.withArgs({
|
|
|
|
mail: newlyjoined,
|
|
|
|
publicKeysArmored: [newlyjoinedKey.publicKey]
|
|
|
|
}).yieldsAsync(null, newlyjoined);
|
|
|
|
|
|
|
|
emailDaoStub.sendEncrypted.withArgs({
|
|
|
|
email: newlyjoined
|
|
|
|
}).yieldsAsync();
|
|
|
|
|
|
|
|
emailDaoStub.sendEncrypted.withArgs({
|
|
|
|
email: member
|
|
|
|
}).yieldsAsync();
|
|
|
|
|
|
|
|
devicestorageStub.storeList.withArgs([newlyjoined]).yieldsAsync();
|
|
|
|
|
|
|
|
devicestorageStub.removeList.withArgs('email_OUTBOX_' + member.id).yieldsAsync();
|
|
|
|
devicestorageStub.removeList.withArgs('email_OUTBOX_' + newlyjoined.id).yieldsAsync();
|
2013-11-19 10:14:48 -05:00
|
|
|
|
|
|
|
function onOutboxUpdate(err, count) {
|
|
|
|
expect(err).to.not.exist;
|
2014-02-24 04:14:07 -05:00
|
|
|
expect(count).to.equal(2);
|
|
|
|
|
|
|
|
expect(outbox._outboxBusy).to.be.false;
|
|
|
|
expect(emailDaoStub.sendEncrypted.calledTwice).to.be.true;
|
|
|
|
expect(emailDaoStub.reEncrypt.calledOnce).to.be.true;
|
|
|
|
expect(emailDaoStub.sendPlaintext.calledOnce).to.be.true;
|
|
|
|
expect(devicestorageStub.listItems.calledOnce).to.be.true;
|
|
|
|
expect(keychainStub.getReceiverPublicKey.calledThrice).to.be.true;
|
|
|
|
expect(invitationDaoStub.check.calledTwice).to.be.true;
|
|
|
|
|
|
|
|
done();
|
2013-11-19 10:14:48 -05:00
|
|
|
}
|
|
|
|
|
2013-11-20 13:13:39 -05:00
|
|
|
outbox._processOutbox(onOutboxUpdate);
|
2013-11-19 10:14:48 -05:00
|
|
|
});
|
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();
|
|
|
|
});
|
|
|
|
});
|
2013-11-19 10:14:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|