2013-11-11 11:56:51 -05:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var expect = chai.expect,
|
|
|
|
angular = require('angular'),
|
|
|
|
mocks = require('angularMocks'),
|
|
|
|
WriteCtrl = require('js/controller/write'),
|
|
|
|
EmailDAO = require('js/dao/email-dao'),
|
2014-02-24 04:14:07 -05:00
|
|
|
OutboxBO = require('js/bo/outbox'),
|
2013-11-11 11:56:51 -05:00
|
|
|
KeychainDAO = require('js/dao/keychain-dao'),
|
|
|
|
appController = require('js/app-controller');
|
|
|
|
|
|
|
|
describe('Write controller unit test', function() {
|
2014-02-24 04:14:07 -05:00
|
|
|
var ctrl, scope,
|
2014-05-23 04:52:34 -04:00
|
|
|
origEmailDao, origOutbox, origKeychain,
|
2014-02-24 04:14:07 -05:00
|
|
|
emailDaoMock, keychainMock, outboxMock, emailAddress;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-02-24 04:14:07 -05:00
|
|
|
// the app controller is a singleton, we need to remember the
|
|
|
|
// outbox and email dao to restore it after the tests
|
2013-11-11 11:56:51 -05:00
|
|
|
origEmailDao = appController._emailDao;
|
2014-02-24 04:14:07 -05:00
|
|
|
origOutbox = appController._outboxBo;
|
2014-05-23 04:52:34 -04:00
|
|
|
origKeychain = appController._keychain;
|
2014-02-24 04:14:07 -05:00
|
|
|
|
|
|
|
outboxMock = sinon.createStubInstance(OutboxBO);
|
|
|
|
appController._outboxBo = outboxMock;
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
emailDaoMock = sinon.createStubInstance(EmailDAO);
|
|
|
|
appController._emailDao = emailDaoMock;
|
2013-11-21 11:37:07 -05:00
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
emailAddress = 'fred@foo.com';
|
|
|
|
emailDaoMock._account = {
|
|
|
|
emailAddress: emailAddress,
|
|
|
|
};
|
|
|
|
|
|
|
|
keychainMock = sinon.createStubInstance(KeychainDAO);
|
2014-05-23 04:52:34 -04:00
|
|
|
appController._keychain = keychainMock;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
angular.module('writetest', []);
|
|
|
|
mocks.module('writetest');
|
|
|
|
mocks.inject(function($rootScope, $controller) {
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
|
|
|
ctrl = $controller(WriteCtrl, {
|
|
|
|
$scope: scope
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
2014-02-24 04:14:07 -05:00
|
|
|
// restore the app controller
|
2013-11-11 11:56:51 -05:00
|
|
|
appController._emailDao = origEmailDao;
|
2014-02-24 04:14:07 -05:00
|
|
|
appController._outboxBo = origOutbox;
|
2014-05-23 04:52:34 -04:00
|
|
|
appController._keychain = origKeychain;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('scope variables', function() {
|
|
|
|
it('should be set correctly', function() {
|
|
|
|
expect(scope.state.writer).to.exist;
|
2014-04-24 10:45:09 -04:00
|
|
|
expect(scope.state.lightbox).to.be.undefined;
|
2013-11-11 11:56:51 -05:00
|
|
|
expect(scope.state.writer.write).to.exist;
|
|
|
|
expect(scope.state.writer.close).to.exist;
|
2014-01-14 10:11:59 -05:00
|
|
|
expect(scope.verify).to.exist;
|
|
|
|
expect(scope.onAddressUpdate).to.exist;
|
|
|
|
expect(scope.checkSendStatus).to.exist;
|
2013-11-11 11:56:51 -05:00
|
|
|
expect(scope.updatePreview).to.exist;
|
|
|
|
expect(scope.sendToOutbox).to.exist;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('close', function() {
|
|
|
|
it('should close the writer', function() {
|
2014-04-24 10:45:09 -04:00
|
|
|
scope.state.lightbox = 'write';
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
scope.state.writer.close();
|
|
|
|
|
2014-04-24 10:45:09 -04:00
|
|
|
expect(scope.state.lightbox).to.be.undefined;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('write', function() {
|
|
|
|
it('should prepare write view', function() {
|
2014-01-14 10:11:59 -05:00
|
|
|
var verifyMock = sinon.stub(scope, 'verify');
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
scope.state.writer.write();
|
|
|
|
|
|
|
|
expect(scope.writerTitle).to.equal('New email');
|
2014-01-14 10:11:59 -05:00
|
|
|
expect(scope.to).to.deep.equal([{
|
|
|
|
address: ''
|
|
|
|
}]);
|
2013-11-11 11:56:51 -05:00
|
|
|
expect(scope.subject).to.equal('');
|
|
|
|
expect(scope.body).to.equal('');
|
|
|
|
expect(scope.ciphertextPreview).to.equal('');
|
2014-01-14 10:11:59 -05:00
|
|
|
expect(verifyMock.calledOnce).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-01-14 10:11:59 -05:00
|
|
|
scope.verify.restore();
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should prefill write view for response', function() {
|
2014-01-14 10:11:59 -05:00
|
|
|
var verifyMock = sinon.stub(scope, 'verify'),
|
2013-11-11 11:56:51 -05:00
|
|
|
address = 'pity@dafool',
|
|
|
|
subject = 'Ermahgerd!',
|
|
|
|
body = 'so much body!',
|
|
|
|
re = {
|
2014-05-21 08:19:18 -04:00
|
|
|
id: 'abc',
|
2013-11-11 11:56:51 -05:00
|
|
|
from: [{
|
|
|
|
address: address
|
|
|
|
}],
|
|
|
|
subject: subject,
|
|
|
|
sentDate: new Date(),
|
2014-05-21 08:19:18 -04:00
|
|
|
body: body,
|
|
|
|
references: ['ghi', 'def']
|
2013-11-11 11:56:51 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
scope.state.writer.write(re);
|
|
|
|
|
|
|
|
expect(scope.writerTitle).to.equal('Reply');
|
2014-01-14 10:11:59 -05:00
|
|
|
expect(scope.to).to.deep.equal([{
|
|
|
|
address: address,
|
|
|
|
}, {
|
|
|
|
address: ''
|
|
|
|
}]);
|
2013-11-11 11:56:51 -05:00
|
|
|
expect(scope.subject).to.equal('Re: ' + subject);
|
|
|
|
expect(scope.body).to.contain(body);
|
2014-05-21 08:19:18 -04:00
|
|
|
expect(scope.references).to.deep.equal(['ghi', 'def', 'abc']);
|
2013-11-11 11:56:51 -05:00
|
|
|
expect(scope.ciphertextPreview).to.not.be.empty;
|
2014-04-02 13:47:50 -04:00
|
|
|
expect(verifyMock.called).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-01-14 10:11:59 -05:00
|
|
|
scope.verify.restore();
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2014-05-14 11:29:27 -04:00
|
|
|
it('should prefill write view for forward', function() {
|
|
|
|
var verifyMock = sinon.stub(scope, 'verify'),
|
|
|
|
address = 'pity@dafool',
|
|
|
|
subject = 'Ermahgerd!',
|
|
|
|
body = 'so much body!',
|
|
|
|
re = {
|
|
|
|
from: [{
|
|
|
|
address: address
|
|
|
|
}],
|
|
|
|
to: [{
|
|
|
|
address: address
|
|
|
|
}],
|
|
|
|
subject: subject,
|
|
|
|
sentDate: new Date(),
|
|
|
|
body: body,
|
|
|
|
attachments: [{}]
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.state.writer.write(re, null, true);
|
|
|
|
|
|
|
|
expect(scope.writerTitle).to.equal('Forward');
|
|
|
|
expect(scope.to).to.deep.equal([{
|
|
|
|
address: ''
|
|
|
|
}]);
|
|
|
|
expect(scope.subject).to.equal('Fwd: ' + subject);
|
|
|
|
expect(scope.body).to.contain(body);
|
|
|
|
expect(scope.ciphertextPreview).to.not.be.empty;
|
|
|
|
expect(verifyMock.called).to.be.true;
|
|
|
|
expect(scope.attachments).to.not.equal(re.attachments); // not the same reference
|
|
|
|
expect(scope.attachments).to.deep.equal(re.attachments); // but the same content
|
|
|
|
|
|
|
|
scope.verify.restore();
|
|
|
|
});
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2014-01-14 10:11:59 -05:00
|
|
|
describe('onAddressUpdate', function() {
|
|
|
|
var verifyMock;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-01-14 10:11:59 -05:00
|
|
|
beforeEach(function() {
|
|
|
|
verifyMock = sinon.stub(scope, 'verify');
|
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-01-14 10:11:59 -05:00
|
|
|
afterEach(function() {
|
|
|
|
scope.verify.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do nothing for normal address', function() {
|
|
|
|
var to = [{
|
|
|
|
address: 'asdf@asdf.de'
|
|
|
|
}];
|
|
|
|
scope.onAddressUpdate(to, 0);
|
|
|
|
|
|
|
|
expect(to.length).to.equal(1);
|
|
|
|
expect(to[0].address).to.equal('asdf@asdf.de');
|
|
|
|
expect(verifyMock.calledOnce).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('verify', function() {
|
|
|
|
var checkSendStatusMock;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
checkSendStatusMock = sinon.stub(scope, 'checkSendStatus');
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2014-01-14 10:11:59 -05:00
|
|
|
afterEach(function() {
|
|
|
|
scope.checkSendStatus.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not work for invalid email addresses', function() {
|
|
|
|
var recipient = {
|
|
|
|
address: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.verify(recipient);
|
|
|
|
|
|
|
|
expect(recipient.key).to.be.undefined;
|
|
|
|
expect(recipient.secure).to.be.undefined;
|
2014-02-28 08:45:10 -05:00
|
|
|
expect(scope.checkSendStatus.callCount).to.equal(2);
|
2014-01-14 10:11:59 -05:00
|
|
|
expect(keychainMock.getReceiverPublicKey.called).to.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not work for error in keychain', function(done) {
|
|
|
|
var recipient = {
|
|
|
|
address: 'asds@example.com'
|
|
|
|
};
|
|
|
|
|
2014-05-23 04:52:34 -04:00
|
|
|
keychainMock.refreshKeyForUserId.withArgs(recipient.address).yields({
|
2013-11-11 11:56:51 -05:00
|
|
|
errMsg: '404 not found yadda yadda'
|
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2013-11-14 14:13:27 -05:00
|
|
|
scope.onError = function() {
|
2014-01-14 10:11:59 -05:00
|
|
|
expect(recipient.key).to.be.undefined;
|
|
|
|
expect(recipient.secure).to.be.false;
|
2014-02-28 08:45:10 -05:00
|
|
|
expect(scope.checkSendStatus.callCount).to.equal(1);
|
2014-05-23 04:52:34 -04:00
|
|
|
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
|
2014-01-14 10:11:59 -05:00
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.verify(recipient);
|
|
|
|
});
|
|
|
|
|
2014-06-18 04:02:33 -04:00
|
|
|
it('should work for main userId', function(done) {
|
2014-01-14 10:11:59 -05:00
|
|
|
var recipient = {
|
|
|
|
address: 'asdf@example.com'
|
|
|
|
};
|
|
|
|
|
2014-05-23 04:52:34 -04:00
|
|
|
keychainMock.refreshKeyForUserId.withArgs(recipient.address).yields(null, {
|
2014-01-14 10:11:59 -05:00
|
|
|
userId: 'asdf@example.com'
|
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-04-24 10:45:09 -04:00
|
|
|
scope.$digest = function() {
|
2014-01-14 10:11:59 -05:00
|
|
|
expect(recipient.key).to.deep.equal({
|
|
|
|
userId: 'asdf@example.com'
|
|
|
|
});
|
|
|
|
expect(recipient.secure).to.be.true;
|
2014-02-28 08:45:10 -05:00
|
|
|
expect(scope.checkSendStatus.callCount).to.equal(2);
|
2014-05-23 04:52:34 -04:00
|
|
|
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
|
2013-11-14 14:13:27 -05:00
|
|
|
done();
|
|
|
|
};
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-01-14 10:11:59 -05:00
|
|
|
scope.verify(recipient);
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
2014-06-18 04:02:33 -04:00
|
|
|
|
|
|
|
it('should work for secondary userId', function(done) {
|
|
|
|
var recipient = {
|
|
|
|
address: 'asdf@example.com'
|
|
|
|
};
|
|
|
|
var key = {
|
|
|
|
userId: 'qwer@example.com',
|
|
|
|
userIds: [{
|
|
|
|
emailAddress: 'asdf@example.com'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
keychainMock.refreshKeyForUserId.withArgs(recipient.address).yields(null, key);
|
|
|
|
|
|
|
|
scope.$digest = function() {
|
|
|
|
expect(recipient.key).to.deep.equal(key);
|
|
|
|
expect(recipient.secure).to.be.true;
|
|
|
|
expect(scope.checkSendStatus.callCount).to.equal(2);
|
|
|
|
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.verify(recipient);
|
|
|
|
});
|
2014-01-14 10:11:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('checkSendStatus', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
scope.state.writer.write();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {});
|
|
|
|
|
|
|
|
it('should not be able to send with no recipients', function() {
|
|
|
|
scope.checkSendStatus();
|
|
|
|
|
|
|
|
expect(scope.okToSend).to.be.false;
|
|
|
|
expect(scope.sendBtnText).to.be.undefined;
|
|
|
|
expect(scope.sendBtnSecure).to.be.undefined;
|
|
|
|
});
|
|
|
|
|
2014-02-28 08:45:10 -05:00
|
|
|
it('should be able to send plaintext', function() {
|
2014-01-14 10:11:59 -05:00
|
|
|
scope.to = [{
|
|
|
|
address: 'asdf@asdf.de'
|
|
|
|
}];
|
|
|
|
scope.checkSendStatus();
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-01-14 10:11:59 -05:00
|
|
|
expect(scope.okToSend).to.be.true;
|
2014-02-28 08:45:10 -05:00
|
|
|
expect(scope.sendBtnText).to.equal('Send');
|
2014-01-14 10:11:59 -05:00
|
|
|
expect(scope.sendBtnSecure).to.be.false;
|
|
|
|
});
|
|
|
|
|
2014-02-28 08:45:10 -05:00
|
|
|
it('should send plaintext if one receiver is not secure', function() {
|
2014-01-14 10:11:59 -05:00
|
|
|
scope.to = [{
|
2014-02-28 08:45:10 -05:00
|
|
|
address: 'asdf@asdf.de',
|
|
|
|
secure: true
|
2014-01-14 10:11:59 -05:00
|
|
|
}, {
|
|
|
|
address: 'asdf@asdfg.de'
|
|
|
|
}];
|
|
|
|
scope.checkSendStatus();
|
|
|
|
|
2014-02-28 08:45:10 -05:00
|
|
|
expect(scope.okToSend).to.be.true;
|
|
|
|
expect(scope.sendBtnText).to.equal('Send');
|
|
|
|
expect(scope.sendBtnSecure).to.be.false;
|
2014-01-14 10:11:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to send securely to multiple recipients', function() {
|
|
|
|
scope.to = [{
|
|
|
|
address: 'asdf@asdf.de',
|
|
|
|
secure: true
|
|
|
|
}, {
|
|
|
|
address: 'asdf@asdfg.de',
|
|
|
|
secure: true
|
|
|
|
}];
|
|
|
|
scope.checkSendStatus();
|
|
|
|
|
|
|
|
expect(scope.okToSend).to.be.true;
|
|
|
|
expect(scope.sendBtnText).to.equal('Send securely');
|
|
|
|
expect(scope.sendBtnSecure).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('send to outbox', function() {
|
2014-02-24 04:14:07 -05:00
|
|
|
it('should work', function() {
|
2014-02-25 08:19:01 -05:00
|
|
|
scope.to = [{
|
2014-02-24 04:14:07 -05:00
|
|
|
address: 'pity@dafool'
|
|
|
|
}];
|
2014-02-25 08:19:01 -05:00
|
|
|
scope.cc = [];
|
|
|
|
scope.bcc = [];
|
2014-02-24 04:14:07 -05:00
|
|
|
scope.subject = 'Ermahgerd!';
|
|
|
|
scope.body = 'wow. much body! very text!';
|
|
|
|
scope.attachments = [];
|
2013-12-04 10:33:10 -05:00
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: 'currentFolder'
|
|
|
|
};
|
|
|
|
|
2014-02-24 04:14:07 -05:00
|
|
|
scope.replyTo = {};
|
2013-12-05 12:50:03 -05:00
|
|
|
|
2014-03-06 12:02:05 -05:00
|
|
|
outboxMock.put.withArgs(sinon.match(function(mail) {
|
|
|
|
expect(mail.from).to.deep.equal([{
|
|
|
|
address: emailAddress
|
|
|
|
}]);
|
|
|
|
expect(mail.to).to.deep.equal(scope.to);
|
|
|
|
expect(mail.cc).to.deep.equal(scope.cc);
|
|
|
|
expect(mail.bcc).to.deep.equal(scope.bcc);
|
|
|
|
expect(mail.body).to.contain(scope.body);
|
|
|
|
expect(mail.subject).to.equal(scope.subject);
|
|
|
|
expect(mail.attachments).to.be.empty;
|
|
|
|
expect(mail.sentDate).to.exist;
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})).yields();
|
2014-05-23 08:23:50 -04:00
|
|
|
emailDaoMock.setFlags.yields();
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2013-12-09 13:21:52 -05:00
|
|
|
scope.onError = function(err) {
|
2014-02-24 04:14:07 -05:00
|
|
|
expect(err).to.not.exist;
|
2013-12-09 13:21:52 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
scope.sendToOutbox();
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-02-24 04:14:07 -05:00
|
|
|
expect(outboxMock.put.calledOnce).to.be.true;
|
2014-05-23 08:23:50 -04:00
|
|
|
expect(emailDaoMock.setFlags.calledOnce).to.be.true;
|
2014-04-24 10:45:09 -04:00
|
|
|
expect(scope.state.lightbox).to.be.undefined;
|
2014-02-24 04:14:07 -05:00
|
|
|
expect(scope.replyTo.answered).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|