2013-11-19 10:14:48 -05:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var expect = chai.expect,
|
2013-11-20 13:13:39 -05:00
|
|
|
_ = require('underscore'),
|
2013-11-19 10:14:48 -05:00
|
|
|
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');
|
|
|
|
|
|
|
|
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'
|
|
|
|
}]
|
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-12-04 11:13:45 -05:00
|
|
|
outbox.init();
|
2013-11-19 10:14:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {});
|
|
|
|
|
|
|
|
describe('init', function() {
|
|
|
|
it('should work', function() {
|
|
|
|
expect(outbox).to.exist;
|
2013-12-02 09:48:59 -05:00
|
|
|
expect(outbox._emailDao).to.equal(emailDaoStub);
|
2013-11-21 11:37:07 -05:00
|
|
|
expect(outbox._keychain).to.equal(keychainStub);
|
|
|
|
expect(outbox._devicestorage).to.equal(devicestorageStub);
|
2013-12-02 09:48:59 -05:00
|
|
|
expect(outbox._invitationDao).to.equal(invitationDaoStub);
|
2013-11-19 10:14:48 -05:00
|
|
|
expect(outbox._outboxBusy).to.be.false;
|
2013-11-21 11:37:07 -05:00
|
|
|
expect(outbox.pendingEmails).to.be.empty;
|
2013-12-04 11:13:45 -05:00
|
|
|
expect(emailDaoStub._account.folders[0].messages).to.equal(outbox.pendingEmails);
|
2013-11-19 10:14:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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) {
|
|
|
|
var member, invited, notinvited, dummyMails, unsentCount;
|
|
|
|
|
|
|
|
member = {
|
2013-11-20 13:13:39 -05:00
|
|
|
id: '123',
|
|
|
|
to: [{
|
|
|
|
name: 'member',
|
|
|
|
address: 'member@whiteout.io'
|
|
|
|
}]
|
2013-11-21 11:37:07 -05:00
|
|
|
};
|
|
|
|
invited = {
|
2013-11-20 13:13:39 -05:00
|
|
|
id: '456',
|
|
|
|
to: [{
|
|
|
|
name: 'invited',
|
|
|
|
address: 'invited@whiteout.io'
|
|
|
|
}]
|
2013-11-21 11:37:07 -05:00
|
|
|
};
|
|
|
|
notinvited = {
|
2013-11-20 13:13:39 -05:00
|
|
|
id: '789',
|
|
|
|
to: [{
|
|
|
|
name: 'notinvited',
|
|
|
|
address: 'notinvited@whiteout.io'
|
|
|
|
}]
|
2013-11-21 11:37:07 -05:00
|
|
|
};
|
|
|
|
dummyMails = [member, invited, notinvited];
|
2013-11-20 13:13:39 -05:00
|
|
|
|
2013-11-21 11:37:07 -05:00
|
|
|
emailDaoStub.list.yieldsAsync(null, dummyMails);
|
2013-12-04 09:36:20 -05:00
|
|
|
emailDaoStub.sendEncrypted.withArgs(sinon.match(function(opts) {
|
|
|
|
return typeof opts.email !== 'undefined' && opts.email.to.address === member.to.address;
|
|
|
|
})).yieldsAsync();
|
2013-12-03 13:21:50 -05:00
|
|
|
emailDaoStub.sendPlaintext.yieldsAsync();
|
2013-11-20 13:13:39 -05:00
|
|
|
devicestorageStub.removeList.yieldsAsync();
|
2013-11-21 11:37:07 -05:00
|
|
|
invitationDaoStub.check.withArgs(sinon.match(function(o) {
|
|
|
|
return o.recipient === 'invited@whiteout.io';
|
|
|
|
})).yieldsAsync(null, InvitationDAO.INVITE_PENDING);
|
|
|
|
invitationDaoStub.check.withArgs(sinon.match(function(o) {
|
|
|
|
return o.recipient === 'notinvited@whiteout.io';
|
|
|
|
})).yieldsAsync(null, InvitationDAO.INVITE_MISSING);
|
|
|
|
invitationDaoStub.invite.withArgs(sinon.match(function(o) {
|
|
|
|
return o.recipient === 'notinvited@whiteout.io';
|
|
|
|
})).yieldsAsync(null, InvitationDAO.INVITE_SUCCESS);
|
|
|
|
keychainStub.getReceiverPublicKey.withArgs(sinon.match(function(o) {
|
|
|
|
return o === 'member@whiteout.io';
|
|
|
|
})).yieldsAsync(null, 'this is not the key you are looking for...');
|
|
|
|
keychainStub.getReceiverPublicKey.withArgs(sinon.match(function(o) {
|
|
|
|
return o === 'invited@whiteout.io' || o === 'notinvited@whiteout.io';
|
|
|
|
})).yieldsAsync();
|
2013-11-20 13:13:39 -05:00
|
|
|
|
|
|
|
var check = _.after(dummyMails.length + 1, function() {
|
2013-11-21 11:37:07 -05:00
|
|
|
expect(unsentCount).to.equal(2);
|
|
|
|
expect(emailDaoStub.list.callCount).to.equal(1);
|
2013-12-03 13:21:50 -05:00
|
|
|
expect(emailDaoStub.sendEncrypted.callCount).to.equal(1);
|
|
|
|
expect(emailDaoStub.sendPlaintext.callCount).to.equal(1);
|
2013-11-20 13:13:39 -05:00
|
|
|
expect(devicestorageStub.removeList.callCount).to.equal(1);
|
|
|
|
expect(invitationDaoStub.check.callCount).to.equal(2);
|
|
|
|
expect(invitationDaoStub.invite.callCount).to.equal(1);
|
2013-11-21 11:37:07 -05:00
|
|
|
|
|
|
|
expect(outbox.pendingEmails.length).to.equal(2);
|
|
|
|
expect(outbox.pendingEmails).to.contain(invited);
|
|
|
|
expect(outbox.pendingEmails).to.contain(notinvited);
|
2013-11-20 13:13:39 -05:00
|
|
|
done();
|
|
|
|
});
|
2013-11-19 10:14:48 -05:00
|
|
|
|
|
|
|
function onOutboxUpdate(err, count) {
|
|
|
|
expect(err).to.not.exist;
|
2013-11-20 13:13:39 -05:00
|
|
|
expect(count).to.exist;
|
2013-11-21 11:37:07 -05:00
|
|
|
unsentCount = count;
|
2013-11-20 13:13:39 -05:00
|
|
|
check();
|
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
|
|
|
|
|
|
|
it('should fire notification', function(done) {
|
|
|
|
outbox.onSent = function(email) {
|
|
|
|
expect(email).to.exist;
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
outbox._onSent({});
|
|
|
|
});
|
2013-11-19 10:14:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|