1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-27 03:12:23 -05:00
mail/test/unit/controller/app/write-ctrl-test.js

391 lines
13 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-11-26 07:43:10 -05:00
var WriteCtrl = require('../../../../src/js/controller/app/write'),
Email = require('../../../../src/js/email/email'),
Outbox = require('../../../../src/js/email/outbox'),
Keychain = require('../../../../src/js/service/keychain'),
Auth = require('../../../../src/js/service/auth'),
PGP = require('../../../../src/js/crypto/pgp'),
Status = require('../../../../src/js/util/status'),
2014-11-26 07:43:10 -05:00
Dialog = require('../../../../src/js/util/dialog');
2014-10-07 14:32:23 -04:00
describe('Write controller unit test', function() {
var ctrl, scope,
authMock, pgpMock, dialogMock, emailMock, keychainMock, outboxMock, statusMock,
2014-11-26 07:43:10 -05:00
emailAddress, realname;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
2014-11-26 07:43:10 -05:00
authMock = sinon.createStubInstance(Auth);
pgpMock = sinon.createStubInstance(PGP);
dialogMock = sinon.createStubInstance(Dialog);
outboxMock = sinon.createStubInstance(Outbox);
emailMock = sinon.createStubInstance(Email);
keychainMock = sinon.createStubInstance(Keychain);
statusMock = sinon.createStubInstance(Status);
2014-10-07 14:32:23 -04:00
emailAddress = 'fred@foo.com';
realname = 'Fred Foo';
2014-11-26 07:43:10 -05:00
authMock.emailAddress = emailAddress;
authMock.realname = realname;
2014-10-07 14:32:23 -04:00
2014-11-26 07:43:10 -05:00
angular.module('writetest', ['woEmail', 'woServices', 'woUtil']);
angular.mock.module('writetest');
angular.mock.inject(function($rootScope, $controller) {
2014-10-07 14:32:23 -04:00
scope = $rootScope.$new();
scope.state = {};
ctrl = $controller(WriteCtrl, {
2014-11-26 07:43:10 -05:00
$scope: scope,
2014-12-18 09:19:06 -05:00
$q: window.qMock,
2014-11-26 07:43:10 -05:00
auth: authMock,
keychain: keychainMock,
pgp: pgpMock,
email: emailMock,
outbox: outboxMock,
dialog: dialogMock,
status: statusMock
2014-10-07 14:32:23 -04:00
});
});
});
2014-11-26 07:43:10 -05:00
afterEach(function() {});
2014-10-07 14:32:23 -04:00
describe('scope variables', function() {
it('should be set correctly', function() {
expect(scope.state.writer).to.exist;
expect(scope.state.lightbox).to.be.undefined;
expect(scope.state.writer.write).to.exist;
expect(scope.state.writer.close).to.exist;
expect(scope.verify).to.exist;
expect(scope.checkSendStatus).to.exist;
expect(scope.sendToOutbox).to.exist;
expect(scope.tagStyle).to.exist;
expect(scope.lookupAddressBook).to.exist;
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('close', function() {
it('should close the writer', function() {
scope.state.lightbox = 'write';
scope.state.writer.close();
expect(scope.state.lightbox).to.be.undefined;
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('write', function() {
it('should prepare write view', function() {
var verifyMock = sinon.stub(scope, 'verify');
scope.state.writer.write();
expect(scope.writerTitle).to.equal('New email');
expect(scope.to).to.deep.equal([]);
expect(scope.subject).to.equal('');
expect(scope.body).to.equal('');
expect(verifyMock.calledOnce).to.be.true;
scope.verify.restore();
});
2014-10-07 14:32:23 -04:00
it('should prefill write view for response', function() {
var verifyMock = sinon.stub(scope, 'verify'),
address = 'pity@dafool',
subject = 'Ermahgerd!',
body = 'so much body!',
re = {
id: 'abc',
from: [{
address: address
}],
subject: subject,
sentDate: new Date(),
body: body,
references: ['ghi', 'def']
};
2014-10-07 14:32:23 -04:00
scope.sendBtnSecure = true;
2014-10-07 14:32:23 -04:00
scope.state.writer.write(re);
expect(scope.writerTitle).to.equal('Reply');
expect(scope.to).to.deep.equal([{
address: address,
}]);
expect(scope.subject).to.equal('Re: ' + subject);
expect(scope.body).to.contain(body);
expect(scope.references).to.deep.equal(['ghi', 'def', 'abc']);
expect(verifyMock.called).to.be.true;
scope.verify.restore();
});
2014-10-07 14:32:23 -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: [{}]
};
2014-10-07 14:32:23 -04:00
scope.sendBtnSecure = false;
2014-10-07 14:32:23 -04:00
scope.state.writer.write(re, null, true);
2014-10-07 14:32:23 -04:00
expect(scope.writerTitle).to.equal('Forward');
expect(scope.to).to.deep.equal([]);
expect(scope.subject).to.equal('Fwd: ' + subject);
expect(scope.body).to.contain(body);
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
2014-10-07 14:32:23 -04:00
scope.verify.restore();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('verify', function() {
var checkSendStatusMock;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
checkSendStatusMock = sinon.stub(scope, 'checkSendStatus');
});
2014-10-07 14:32:23 -04:00
afterEach(function() {
scope.checkSendStatus.restore();
});
2014-01-14 10:11:59 -05:00
2014-10-07 14:32:23 -04:00
it('should do nothing if recipient is not provided', function() {
scope.verify(undefined);
});
2014-10-07 14:32:23 -04:00
it('should not work for invalid email addresses', function() {
var recipient = {
address: ''
};
2014-01-14 10:11:59 -05:00
2014-10-07 14:32:23 -04:00
scope.verify(recipient);
2014-09-15 11:19:36 -04:00
2014-10-07 14:32:23 -04:00
expect(recipient.key).to.be.undefined;
expect(recipient.secure).to.be.undefined;
expect(scope.checkSendStatus.callCount).to.equal(2);
expect(keychainMock.getReceiverPublicKey.called).to.be.false;
});
2014-01-14 10:11:59 -05:00
2014-12-18 09:19:06 -05:00
it('should not work for error in keychain', function(done) {
2014-10-07 14:32:23 -04:00
var recipient = {
address: 'asds@example.com'
};
2014-01-14 10:11:59 -05:00
keychainMock.refreshKeyForUserId.withArgs({
userId: recipient.address
2014-12-18 09:19:06 -05:00
}).returns(rejects({
2014-10-07 14:32:23 -04:00
errMsg: '404 not found yadda yadda'
2014-12-18 09:19:06 -05:00
}));
2014-11-26 07:43:10 -05:00
2014-12-18 09:19:06 -05:00
scope.verify(recipient).then(function() {
expect(dialogMock.error.calledOnce).to.be.true;
expect(recipient.key).to.be.undefined;
expect(recipient.secure).to.be.false;
expect(scope.checkSendStatus.callCount).to.equal(1);
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-05-23 04:52:34 -04:00
2014-10-07 14:32:23 -04:00
it('should work for main userId', function(done) {
var recipient = {
address: 'asdf@example.com'
};
2014-01-14 10:11:59 -05:00
keychainMock.refreshKeyForUserId.withArgs({
userId: recipient.address
2014-12-18 09:19:06 -05:00
}).returns(resolves({
2014-10-07 14:32:23 -04:00
userId: 'asdf@example.com'
2014-12-18 09:19:06 -05:00
}));
2014-01-14 10:11:59 -05:00
2014-12-18 09:19:06 -05:00
scope.verify(recipient).then(function() {
2014-10-07 14:32:23 -04:00
expect(recipient.key).to.deep.equal({
2014-01-14 10:11:59 -05:00
userId: 'asdf@example.com'
});
2014-10-07 14:32:23 -04:00
expect(recipient.secure).to.be.true;
expect(scope.checkSendStatus.callCount).to.equal(2);
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
done();
2014-12-18 09:19:06 -05:00
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -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({
userId: recipient.address
2014-12-18 09:19:06 -05:00
}).returns(resolves(key));
2014-12-18 09:19:06 -05:00
scope.verify(recipient).then(function() {
2014-10-07 14:32:23 -04:00
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();
2014-12-18 09:19:06 -05:00
});
2014-01-14 10:11:59 -05:00
});
2014-10-07 14:32:23 -04:00
});
2014-01-14 10:11:59 -05:00
2014-10-07 14:32:23 -04:00
describe('checkSendStatus', function() {
beforeEach(function() {
scope.state.writer.write();
});
2014-01-14 10:11:59 -05:00
2014-10-07 14:32:23 -04:00
afterEach(function() {});
2014-01-14 10:11:59 -05:00
2014-10-07 14:32:23 -04:00
it('should not be able to send with no recipients', function() {
scope.checkSendStatus();
2014-01-14 10:11:59 -05:00
2014-10-07 14:32:23 -04:00
expect(scope.okToSend).to.be.false;
expect(scope.sendBtnText).to.be.undefined;
expect(scope.sendBtnSecure).to.be.undefined;
});
2014-01-14 10:11:59 -05:00
2014-10-07 14:32:23 -04:00
it('should be able to send plaintext', function() {
scope.to = [{
address: 'asdf@asdf.de'
}];
scope.checkSendStatus();
2014-10-07 14:32:23 -04: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
2014-10-07 14:32:23 -04:00
it('should send plaintext if one receiver is not secure', function() {
scope.to = [{
address: 'asdf@asdf.de',
secure: true
}, {
address: 'asdf@asdfg.de'
}];
scope.checkSendStatus();
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
2014-10-07 14:32:23 -04: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;
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('send to outbox', function() {
2014-12-18 09:19:06 -05:00
it('should work', function(done) {
2014-10-07 14:32:23 -04:00
scope.to = [{
address: 'pity@dafool'
}];
scope.cc = [];
scope.bcc = [];
scope.subject = 'Ermahgerd!';
scope.body = 'wow. much body! very text!';
scope.attachments = [];
scope.state.nav = {
currentFolder: 'currentFolder'
};
2013-12-04 10:33:10 -05:00
2014-10-07 14:32:23 -04:00
scope.replyTo = {};
2013-12-05 12:50:03 -05:00
2014-10-07 14:32:23 -04:00
outboxMock.put.withArgs(sinon.match(function(mail) {
expect(mail.from).to.deep.equal([{
address: emailAddress,
name: realname
}]);
2014-11-26 07:43:10 -05:00
2014-10-07 14:32:23 -04:00
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;
2014-03-06 12:02:05 -05:00
2014-10-07 14:32:23 -04:00
return true;
2014-12-18 09:19:06 -05:00
})).returns(resolves());
emailMock.setFlags.returns(resolves());
scope.sendToOutbox().then(function() {
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true;
expect(outboxMock.put.calledOnce).to.be.true;
expect(emailMock.setFlags.calledOnce).to.be.true;
expect(scope.state.lightbox).to.be.undefined;
expect(scope.replyTo.answered).to.be.true;
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-09-15 18:11:44 -04:00
2014-10-07 14:32:23 -04:00
describe('lookupAddressBook', function() {
it('should work', function(done) {
2014-12-18 09:19:06 -05:00
keychainMock.listLocalPublicKeys.returns(resolves([{
2014-10-07 14:32:23 -04:00
userId: 'test@asdf.com',
publicKey: 'KEY'
2014-12-18 09:19:06 -05:00
}]));
2014-09-15 18:11:44 -04:00
2014-10-07 14:32:23 -04:00
var result = scope.lookupAddressBook('test');
2014-09-15 18:11:44 -04:00
2014-10-07 14:32:23 -04:00
result.then(function(response) {
expect(response).to.deep.equal([{
address: 'test@asdf.com'
}]);
done();
2014-09-15 18:11:44 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-09-15 18:11:44 -04:00
2014-10-07 14:32:23 -04:00
it('should work with cache', function(done) {
scope.addressBookCache = [{
address: 'test@asdf.com'
}, {
address: 'tes@asdf.com'
}];
2014-09-15 18:11:44 -04:00
2014-10-07 14:32:23 -04:00
var result = scope.lookupAddressBook('test');
2014-09-15 18:11:44 -04:00
2014-10-07 14:32:23 -04:00
result.then(function(response) {
expect(response).to.deep.equal([{
address: 'test@asdf.com'
}]);
done();
2014-09-15 18:11:44 -04:00
});
});
});
2014-10-07 14:32:23 -04:00
});