Fix conrtoller unit tests

This commit is contained in:
Tankred Hase 2014-12-18 15:19:06 +01:00
parent 383761e6cb
commit 6a8c24d813
14 changed files with 335 additions and 315 deletions

View File

@ -85,7 +85,6 @@ var ContactsCtrl = function($scope, $q, keychain, pgp, dialog) {
return $scope.listKeys(); return $scope.listKeys();
}).catch(dialog.error); }).catch(dialog.error);
}; };
}; };
module.exports = ContactsCtrl; module.exports = ContactsCtrl;

View File

@ -52,6 +52,9 @@ var PrivateKeyUploadCtrl = function($scope, $q, keychain, pgp, dialog, auth) {
keyId: keyParams._id keyId: keyParams._id
}); });
}).then(function(privateKeySynced) {
return privateKeySynced ? privateKeySynced : undefined;
}).catch(dialog.error); }).catch(dialog.error);
}; };

View File

@ -43,6 +43,7 @@ describe('Account Controller unit test', function() {
scope.state = {}; scope.state = {};
accountCtrl = $controller(AccountCtrl, { accountCtrl = $controller(AccountCtrl, {
$scope: scope, $scope: scope,
$q: window.qMock,
auth: authStub, auth: authStub,
keychain: keychainStub, keychain: keychainStub,
pgp: pgpStub, pgp: pgpStub,
@ -63,8 +64,8 @@ describe('Account Controller unit test', function() {
}); });
}); });
describe('export to key file', function() { describe('export to key file', function() {
it('should work', function() { it('should work', function(done) {
keychainStub.getUserKeyPair.withArgs(emailAddress).yields(null, { keychainStub.getUserKeyPair.withArgs(emailAddress).returns(resolves({
publicKey: { publicKey: {
_id: dummyKeyId, _id: dummyKeyId,
publicKey: 'a' publicKey: 'a'
@ -72,24 +73,26 @@ describe('Account Controller unit test', function() {
privateKey: { privateKey: {
encryptedKey: 'b' encryptedKey: 'b'
} }
}); }));
downloadStub.createDownload.withArgs(sinon.match(function(arg) { downloadStub.createDownload.withArgs(sinon.match(function(arg) {
return arg.content === 'a\r\nb' && arg.filename === 'whiteout_mail_' + emailAddress + '_' + expectedKeyId + '.asc' && arg.contentType === 'text/plain'; return arg.content === 'a\r\nb' && arg.filename === 'whiteout_mail_' + emailAddress + '_' + expectedKeyId + '.asc' && arg.contentType === 'text/plain';
})).returns(); })).returns();
scope.exportKeyFile(); scope.exportKeyFile().then(function() {
expect(scope.state.lightbox).to.equal(undefined);
expect(scope.state.lightbox).to.equal(undefined); expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true; expect(downloadStub.createDownload.calledOnce).to.be.true;
expect(downloadStub.createDownload.calledOnce).to.be.true; done();
});
}); });
it('should not work when key export failed', function() { it('should not work when key export failed', function(done) {
keychainStub.getUserKeyPair.yields(new Error()); keychainStub.getUserKeyPair.returns(rejects(new Error()));
scope.exportKeyFile(); scope.exportKeyFile().then(function() {
expect(dialogStub.error.calledOnce).to.be.true;
expect(dialogStub.error.calledOnce).to.be.true; done();
});
}); });
}); });
}); });

View File

@ -33,6 +33,7 @@ describe('Action Bar Controller unit test', function() {
actionBarCtrl = $controller(ActionBarCtrl, { actionBarCtrl = $controller(ActionBarCtrl, {
$scope: scope, $scope: scope,
$q: window.qMock,
email: emailMock, email: emailMock,
dialog: dialogMock, dialog: dialogMock,
status: statusMock status: statusMock
@ -47,13 +48,14 @@ describe('Action Bar Controller unit test', function() {
scope.deleteMessage(); scope.deleteMessage();
}); });
it('should delete the selected mail', function() { it('should delete the selected mail', function(done) {
emailMock.deleteMessage.yields(); emailMock.deleteMessage.returns(resolves());
scope.deleteMessage({}); scope.deleteMessage({}).then(function() {
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true;
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true; expect(emailMock.deleteMessage.calledOnce).to.be.true;
expect(emailMock.deleteMessage.calledOnce).to.be.true; done();
});
}); });
}); });
@ -79,13 +81,14 @@ describe('Action Bar Controller unit test', function() {
scope.moveMessage(); scope.moveMessage();
}); });
it('should move the selected mail', function() { it('should move the selected mail', function(done) {
emailMock.moveMessage.yields(); emailMock.moveMessage.returns(resolves());
scope.moveMessage({}, {}); scope.moveMessage({}, {}).then(function() {
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true;
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true; expect(emailMock.moveMessage.calledOnce).to.be.true;
expect(emailMock.moveMessage.calledOnce).to.be.true; done();
});
}); });
}); });
@ -144,22 +147,24 @@ describe('Action Bar Controller unit test', function() {
}, true); }, true);
}); });
it('should mark the selected mail', function() { it('should mark the selected mail', function(done) {
emailMock.setFlags.yields(); emailMock.setFlags.returns(resolves());
scope.markMessage({}, true); scope.markMessage({}, true).then(function() {
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true;
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true; expect(emailMock.setFlags.calledOnce).to.be.true;
expect(emailMock.setFlags.calledOnce).to.be.true; done();
});
}); });
it('should mark the selected mail and close read mode', function() { it('should mark the selected mail and close read mode', function(done) {
emailMock.setFlags.yields(); emailMock.setFlags.returns(resolves());
scope.markMessage({}, true, true); scope.markMessage({}, true, true).then(function() {
expect(statusMock.setReading.calledOnce).to.be.false;
expect(statusMock.setReading.calledOnce).to.be.false; expect(emailMock.setFlags.calledOnce).to.be.true;
expect(emailMock.setFlags.calledOnce).to.be.true; done();
});
}); });
}); });
@ -196,11 +201,11 @@ describe('Action Bar Controller unit test', function() {
}); });
it('should flag the selected mail', function() { it('should flag the selected mail', function() {
emailMock.setFlags.yields(); emailMock.setFlags.returns(resolves());
scope.flagMessage({}, true); scope.flagMessage({}, true).then(function() {
expect(emailMock.setFlags.calledOnce).to.be.true;
expect(emailMock.setFlags.calledOnce).to.be.true; });
}); });
}); });

View File

@ -20,6 +20,7 @@ describe('Contacts Controller unit test', function() {
scope.state = {}; scope.state = {};
contactsCtrl = $controller(ContactsCtrl, { contactsCtrl = $controller(ContactsCtrl, {
$scope: scope, $scope: scope,
$q: window.qMock,
keychain: keychainStub, keychain: keychainStub,
pgp: pgpStub, pgp: pgpStub,
dialog: dialogStub dialog: dialogStub
@ -36,28 +37,30 @@ describe('Contacts Controller unit test', function() {
}); });
describe('listKeys', function() { describe('listKeys', function() {
it('should fail due to error in keychain.listLocalPublicKeys', function() { it('should fail due to error in keychain.listLocalPublicKeys', function(done) {
keychainStub.listLocalPublicKeys.yields(42); keychainStub.listLocalPublicKeys.returns(rejects(42));
scope.listKeys(); scope.listKeys().then(function() {
expect(dialogStub.error.calledOnce).to.be.true;
expect(dialogStub.error.calledOnce).to.be.true; done();
});
}); });
it('should work', function() { it('should work', function(done) {
keychainStub.listLocalPublicKeys.yields(null, [{ keychainStub.listLocalPublicKeys.returns(resolves([{
_id: '12345' _id: '12345'
}]); }]));
pgpStub.getKeyParams.returns({ pgpStub.getKeyParams.returns({
fingerprint: 'asdf' fingerprint: 'asdf'
}); });
expect(scope.keys).to.not.exist; expect(scope.keys).to.not.exist;
scope.listKeys(); scope.listKeys().then(function() {
expect(scope.keys.length).to.equal(1);
expect(scope.keys.length).to.equal(1); expect(scope.keys[0]._id).to.equal('12345');
expect(scope.keys[0]._id).to.equal('12345'); expect(scope.keys[0].fingerprint).to.equal('asdf');
expect(scope.keys[0].fingerprint).to.equal('asdf'); done();
});
}); });
}); });
@ -89,13 +92,13 @@ describe('Contacts Controller unit test', function() {
userIds: [], userIds: [],
publicKey: '-----BEGIN PGP PUBLIC KEY BLOCK-----', publicKey: '-----BEGIN PGP PUBLIC KEY BLOCK-----',
imported: true imported: true
}).yields(); }).returns(resolves());
scope.listKeys = function() { scope.listKeys = function() {};
scope.importKey(keyArmored).then(function() {
done(); done();
}; });
scope.importKey(keyArmored);
}); });
it('should fail due to invalid armored key', function() { it('should fail due to invalid armored key', function() {
@ -115,7 +118,7 @@ describe('Contacts Controller unit test', function() {
expect(dialogStub.error.calledOnce).to.be.true; expect(dialogStub.error.calledOnce).to.be.true;
}); });
it('should fail due to error in keychain.saveLocalPublicKey', function() { it('should fail due to error in keychain.saveLocalPublicKey', function(done) {
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----'; var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
pgpStub.getKeyParams.returns({ pgpStub.getKeyParams.returns({
@ -123,11 +126,12 @@ describe('Contacts Controller unit test', function() {
userId: 'max@example.com' userId: 'max@example.com'
}); });
keychainStub.saveLocalPublicKey.yields(42); keychainStub.saveLocalPublicKey.returns(rejects(42));
scope.importKey(keyArmored); scope.importKey(keyArmored).then(function() {
expect(dialogStub.error.calledOnce).to.be.true;
expect(dialogStub.error.calledOnce).to.be.true; done();
});
}); });
}); });
@ -137,25 +141,26 @@ describe('Contacts Controller unit test', function() {
_id: '12345' _id: '12345'
}; };
keychainStub.removeLocalPublicKey.withArgs('12345').yields(); keychainStub.removeLocalPublicKey.withArgs('12345').returns(resolves());
scope.listKeys = function() { scope.listKeys = function() {};
scope.removeKey(key).then(function() {
done(); done();
}; });
scope.removeKey(key);
}); });
it('should fail due to error in keychain.removeLocalPublicKey', function() { it('should fail due to error in keychain.removeLocalPublicKey', function(done) {
var key = { var key = {
_id: '12345' _id: '12345'
}; };
keychainStub.removeLocalPublicKey.withArgs('12345').yields(42); keychainStub.removeLocalPublicKey.withArgs('12345').returns(rejects(42));
scope.removeKey(key); scope.removeKey(key).then(function() {
expect(dialogStub.error.calledOnce).to.be.true;
expect(dialogStub.error.calledOnce).to.be.true; done();
});
}); });
}); });
}); });

View File

@ -44,6 +44,7 @@ describe('Mail List controller unit test', function() {
ctrl = $controller(MailListCtrl, { ctrl = $controller(MailListCtrl, {
$scope: scope, $scope: scope,
$location: location, $location: location,
$q: window.qMock,
status: statusMock, status: statusMock,
notification: notificationMock, notification: notificationMock,
email: emailMock, email: emailMock,
@ -207,15 +208,17 @@ describe('Mail List controller unit test', function() {
}); });
describe('getBody', function() { describe('getBody', function() {
it('should get the mail content', function() { it('should get the mail content', function(done) {
scope.state.nav = { scope.state.nav = {
currentFolder: { currentFolder: {
type: 'asd', type: 'asd',
} }
}; };
scope.getBody(); scope.getBody().then(function() {
expect(emailMock.getBody.calledOnce).to.be.true; expect(emailMock.getBody.calledOnce).to.be.true;
done();
});
}); });
}); });
@ -245,7 +248,7 @@ describe('Mail List controller unit test', function() {
}); });
describe('select', function() { describe('select', function() {
it('should decrypt, focus mark an unread mail as read', function() { it('should decrypt, focus mark an unread mail as read', function(done) {
scope.pendingNotifications = ['asd']; scope.pendingNotifications = ['asd'];
sinon.stub(notificationMock, 'close'); sinon.stub(notificationMock, 'close');
@ -272,20 +275,21 @@ describe('Mail List controller unit test', function() {
keychainMock.refreshKeyForUserId.withArgs({ keychainMock.refreshKeyForUserId.withArgs({
userId: mail.from[0].address userId: mail.from[0].address
}).yields(); }).returns(resolves());
scope.select(mail); scope.select(mail).then(function() {
expect(emailMock.decryptBody.calledOnce).to.be.true;
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
expect(scope.state.mailList.selected).to.equal(mail);
expect(notificationMock.close.calledWith('asd')).to.be.true;
expect(notificationMock.close.calledOnce).to.be.true;
expect(emailMock.decryptBody.calledOnce).to.be.true; notificationMock.close.restore();
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true; done();
expect(scope.state.mailList.selected).to.equal(mail); });
expect(notificationMock.close.calledWith('asd')).to.be.true;
expect(notificationMock.close.calledOnce).to.be.true;
notificationMock.close.restore();
}); });
it('should decrypt and focus a read mail', function() { it('should decrypt and focus a read mail', function(done) {
var mail = { var mail = {
from: [{ from: [{
address: 'asd' address: 'asd'
@ -307,13 +311,14 @@ describe('Mail List controller unit test', function() {
keychainMock.refreshKeyForUserId.withArgs({ keychainMock.refreshKeyForUserId.withArgs({
userId: mail.from[0].address userId: mail.from[0].address
}).yields(); }).returns(resolves());
scope.select(mail); scope.select(mail).then(function() {
expect(emailMock.decryptBody.calledOnce).to.be.true;
expect(emailMock.decryptBody.calledOnce).to.be.true; expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true; expect(scope.state.mailList.selected).to.equal(mail);
expect(scope.state.mailList.selected).to.equal(mail); done();
});
}); });
}); });

View File

@ -41,6 +41,7 @@ describe('Navigation Controller unit test', function() {
ctrl = $controller(NavigationCtrl, { ctrl = $controller(NavigationCtrl, {
$scope: scope, $scope: scope,
$routeParams: {}, $routeParams: {},
$q: window.qMock,
account: accountMock, account: accountMock,
email: emailDaoMock, email: emailDaoMock,
outbox: outboxBoMock, outbox: outboxBoMock,

View File

@ -23,6 +23,7 @@ describe('Private Key Upload Controller unit test', function() {
ctrl = $controller(PrivateKeyUploadCtrl, { ctrl = $controller(PrivateKeyUploadCtrl, {
$location: location, $location: location,
$scope: scope, $scope: scope,
$q: window.qMock,
keychain: keychainMock, keychain: keychainMock,
pgp: pgpStub, pgp: pgpStub,
dialog: dialogStub, dialog: dialogStub,
@ -41,14 +42,15 @@ describe('Private Key Upload Controller unit test', function() {
_id: 'keyId', _id: 'keyId',
}; };
it('should fail', function() { it('should fail', function(done) {
pgpStub.getKeyParams.returns(keyParams); pgpStub.getKeyParams.returns(keyParams);
keychainMock.hasPrivateKey.yields(42); keychainMock.hasPrivateKey.returns(rejects(42));
scope.checkServerForKey(); scope.checkServerForKey().then(function() {
expect(dialogStub.error.calledOnce).to.be.true;
expect(dialogStub.error.calledOnce).to.be.true; expect(keychainMock.hasPrivateKey.calledOnce).to.be.true;
expect(keychainMock.hasPrivateKey.calledOnce).to.be.true; done();
});
}); });
it('should return true', function(done) { it('should return true', function(done) {
@ -56,9 +58,9 @@ describe('Private Key Upload Controller unit test', function() {
keychainMock.hasPrivateKey.withArgs({ keychainMock.hasPrivateKey.withArgs({
userId: keyParams.userId, userId: keyParams.userId,
keyId: keyParams._id keyId: keyParams._id
}).yields(null, true); }).returns(resolves(true));
scope.checkServerForKey(function(privateKeySynced) { scope.checkServerForKey().then(function(privateKeySynced) {
expect(privateKeySynced).to.be.true; expect(privateKeySynced).to.be.true;
done(); done();
}); });
@ -69,9 +71,9 @@ describe('Private Key Upload Controller unit test', function() {
keychainMock.hasPrivateKey.withArgs({ keychainMock.hasPrivateKey.withArgs({
userId: keyParams.userId, userId: keyParams.userId,
keyId: keyParams._id keyId: keyParams._id
}).yields(null, false); }).returns(resolves(false));
scope.checkServerForKey(function(privateKeySynced) { scope.checkServerForKey().then(function(privateKeySynced) {
expect(privateKeySynced).to.be.undefined; expect(privateKeySynced).to.be.undefined;
done(); done();
}); });
@ -110,27 +112,27 @@ describe('Private Key Upload Controller unit test', function() {
describe('setDeviceName', function() { describe('setDeviceName', function() {
it('should work', function(done) { it('should work', function(done) {
keychainMock.setDeviceName.yields(); keychainMock.setDeviceName.returns(resolves());
scope.setDeviceName(done); scope.setDeviceName().then(done);
}); });
}); });
describe('encryptAndUploadKey', function() { describe('encryptAndUploadKey', function() {
it('should fail due to keychain.registerDevice', function() { it('should fail due to keychain.registerDevice', function(done) {
keychainMock.registerDevice.yields(42); keychainMock.registerDevice.returns(rejects(42));
scope.encryptAndUploadKey(); scope.encryptAndUploadKey().then(function() {
expect(dialogStub.error.calledOnce).to.be.true;
expect(dialogStub.error.calledOnce).to.be.true; expect(keychainMock.registerDevice.calledOnce).to.be.true;
expect(keychainMock.registerDevice.calledOnce).to.be.true; done();
});
}); });
it('should work', function(done) { it('should work', function(done) {
keychainMock.registerDevice.yields(); keychainMock.registerDevice.returns(resolves());
keychainMock.uploadPrivateKey.yields(); keychainMock.uploadPrivateKey.returns(resolves());
scope.encryptAndUploadKey(function(err) { scope.encryptAndUploadKey().then(function() {
expect(err).to.not.exist;
expect(keychainMock.registerDevice.calledOnce).to.be.true; expect(keychainMock.registerDevice.calledOnce).to.be.true;
expect(keychainMock.uploadPrivateKey.calledOnce).to.be.true; expect(keychainMock.uploadPrivateKey.calledOnce).to.be.true;
done(); done();
@ -185,36 +187,39 @@ describe('Private Key Upload Controller unit test', function() {
expect(scope.step).to.equal(2); expect(scope.step).to.equal(2);
}); });
it('should fail for 3 due to error in setDeviceName', function() { it('should fail for 3 due to error in setDeviceName', function(done) {
scope.step = 3; scope.step = 3;
setDeviceNameStub.yields(42); setDeviceNameStub.returns(rejects(42));
scope.goForward(); scope.goForward().then(function() {
expect(dialogStub.error.calledOnce).to.be.true;
expect(dialogStub.error.calledOnce).to.be.true; expect(scope.step).to.equal(3);
expect(scope.step).to.equal(3); done();
});
}); });
it('should fail for 3 due to error in encryptAndUploadKey', function() { it('should fail for 3 due to error in encryptAndUploadKey', function(done) {
scope.step = 3; scope.step = 3;
setDeviceNameStub.yields(); setDeviceNameStub.returns(resolves());
encryptAndUploadKeyStub.yields(42); encryptAndUploadKeyStub.returns(rejects(42));
scope.goForward(); scope.goForward().then(function() {
expect(dialogStub.error.calledOnce).to.be.true;
expect(dialogStub.error.calledOnce).to.be.true; expect(scope.step).to.equal(4);
expect(scope.step).to.equal(4); done();
});
}); });
it('should work for 3', function() { it('should work for 3', function(done) {
scope.step = 3; scope.step = 3;
setDeviceNameStub.yields(); setDeviceNameStub.returns(resolves());
encryptAndUploadKeyStub.yields(); encryptAndUploadKeyStub.returns(resolves());
scope.goForward(); scope.goForward().then(function() {
expect(dialogStub.info.calledOnce).to.be.true;
expect(dialogStub.info.calledOnce).to.be.true; expect(scope.step).to.equal(4);
expect(scope.step).to.equal(4); done();
});
}); });
}); });
}); });

View File

@ -11,8 +11,7 @@ var Keychain = require('../../../../src/js/service/keychain'),
Download = require('../../../../src/js/util/download'); Download = require('../../../../src/js/util/download');
describe('Read Controller unit test', function() { describe('Read Controller unit test', function() {
var scope, ctrl, keychainMock, invitationMock, emailMock, pgpMock, outboxMock, dialogMock, authMock, downloadMock, var scope, ctrl, keychainMock, invitationMock, emailMock, pgpMock, outboxMock, dialogMock, authMock, downloadMock;
emailAddress = 'sender@example.com';
beforeEach(function() { beforeEach(function() {
keychainMock = sinon.createStubInstance(Keychain); keychainMock = sinon.createStubInstance(Keychain);
@ -31,6 +30,7 @@ describe('Read Controller unit test', function() {
scope.state = {}; scope.state = {};
ctrl = $controller(ReadCtrl, { ctrl = $controller(ReadCtrl, {
$scope: scope, $scope: scope,
$q: window.qMock,
email: emailMock, email: emailMock,
invitation: invitationMock, invitation: invitationMock,
outbox: outboxMock, outbox: outboxMock,
@ -66,33 +66,37 @@ describe('Read Controller unit test', function() {
describe('getKeyId', function() { describe('getKeyId', function() {
var address = 'asfd@asdf.com'; var address = 'asfd@asdf.com';
it('should show searching on error', function() { it('should show searching on error', function(done) {
expect(scope.keyId).to.equal('No key found.'); expect(scope.keyId).to.equal('No key found.');
keychainMock.getReceiverPublicKey.yields(42); keychainMock.getReceiverPublicKey.returns(rejects(42));
scope.getKeyId(address); scope.getKeyId(address).then(function() {
expect(dialogMock.error.calledOnce).to.be.true;
expect(dialogMock.error.calledOnce).to.be.true; expect(scope.keyId).to.equal('Searching...');
expect(scope.keyId).to.equal('Searching...'); done();
});
it('should allow invitation on empty key', function() {
keychainMock.getReceiverPublicKey.yields();
scope.getKeyId(address);
expect(scope.keyId).to.equal('User has no key. Click to invite.');
});
it('should show searching on error', function() {
keychainMock.getReceiverPublicKey.yields(null, {
publicKey: 'PUBLIC KEY'
}); });
});
it('should allow invitation on empty key', function(done) {
keychainMock.getReceiverPublicKey.returns(resolves());
scope.getKeyId(address).then(function() {
expect(scope.keyId).to.equal('User has no key. Click to invite.');
done();
});
});
it('should show searching on error', function(done) {
keychainMock.getReceiverPublicKey.returns(resolves({
publicKey: 'PUBLIC KEY'
}));
pgpMock.getFingerprint.returns('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); pgpMock.getFingerprint.returns('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
scope.getKeyId(address); scope.getKeyId(address).then(function() {
expect(scope.keyId).to.equal('PGP key: XXXXXXXX'); expect(scope.keyId).to.equal('PGP key: XXXXXXXX');
done();
});
}); });
}); });
@ -108,36 +112,39 @@ describe('Read Controller unit test', function() {
expect(scope.keyId).to.equal('No key found.'); expect(scope.keyId).to.equal('No key found.');
}); });
it('should show error on invitation dao invite error', function() { it('should show error on invitation dao invite error', function(done) {
invitationMock.invite.yields(42); invitationMock.invite.returns(rejects(42));
scope.invite({ scope.invite({
address: 'asdf@asdf.de' address: 'asdf@asdf.de'
}).then(function() {
expect(dialogMock.error.calledOnce).to.be.true;
done();
}); });
expect(dialogMock.error.calledOnce).to.be.true;
}); });
it('should show error on outbox put error', function() { it('should show error on outbox put error', function(done) {
invitationMock.invite.yields(); invitationMock.invite.returns(resolves());
outboxMock.put.yields(42); outboxMock.put.returns(rejects(42));
scope.invite({ scope.invite({
address: 'asdf@asdf.de' address: 'asdf@asdf.de'
}).then(function() {
expect(dialogMock.error.calledOnce).to.be.true;
done();
}); });
expect(dialogMock.error.calledOnce).to.be.true;
}); });
it('should work', function() { it('should work', function(done) {
invitationMock.invite.yields(); invitationMock.invite.returns(resolves());
outboxMock.put.yields(); outboxMock.put.returns(resolves());
scope.invite({ scope.invite({
address: 'asdf@asdf.de' address: 'asdf@asdf.de'
}).then(function() {
expect(dialogMock.error.calledOnce).to.be.false;
done();
}); });
expect(dialogMock.error.calledOnce).to.be.true;
}); });
}); });

View File

@ -40,6 +40,7 @@ describe('Set Passphrase Controller unit test', function() {
scope.state = {}; scope.state = {};
setPassphraseCtrl = $controller(SetPassphraseCtrl, { setPassphraseCtrl = $controller(SetPassphraseCtrl, {
$scope: scope, $scope: scope,
$q: window.qMock,
pgp: pgpStub, pgp: pgpStub,
keychain: keychainStub, keychain: keychainStub,
dialog: dialogStub dialog: dialogStub
@ -49,31 +50,32 @@ describe('Set Passphrase Controller unit test', function() {
afterEach(function() {}); afterEach(function() {});
describe('setPassphrase', function() { describe('setPassphrase', function(done) {
it('should work', function() { it('should work', function() {
scope.oldPassphrase = 'old'; scope.oldPassphrase = 'old';
scope.newPassphrase = 'new'; scope.newPassphrase = 'new';
keychainStub.lookupPrivateKey.withArgs(dummyKeyId).yields(null, { keychainStub.lookupPrivateKey.withArgs(dummyKeyId).returns(resolves({
encryptedKey: 'encrypted' encryptedKey: 'encrypted'
}); }));
pgpStub.changePassphrase.withArgs({ pgpStub.changePassphrase.withArgs({
privateKeyArmored: 'encrypted', privateKeyArmored: 'encrypted',
oldPassphrase: 'old', oldPassphrase: 'old',
newPassphrase: 'new' newPassphrase: 'new'
}).yields(null, 'newArmoredKey'); }).returns(resolves('newArmoredKey'));
keychainStub.saveLocalPrivateKey.withArgs({ keychainStub.saveLocalPrivateKey.withArgs({
_id: dummyKeyId, _id: dummyKeyId,
userId: emailAddress, userId: emailAddress,
userIds: [], userIds: [],
encryptedKey: 'newArmoredKey' encryptedKey: 'newArmoredKey'
}).yields(); }).returns(resolves());
scope.setPassphrase(); scope.setPassphrase().then(function() {
expect(dialogStub.info.calledOnce).to.be.true;
expect(dialogStub.info.calledOnce).to.be.true; done();
});
}); });
}); });

View File

@ -36,6 +36,7 @@ describe('Write controller unit test', function() {
scope.state = {}; scope.state = {};
ctrl = $controller(WriteCtrl, { ctrl = $controller(WriteCtrl, {
$scope: scope, $scope: scope,
$q: window.qMock,
auth: authMock, auth: authMock,
keychain: keychainMock, keychain: keychainMock,
pgp: pgpMock, pgp: pgpMock,
@ -183,24 +184,25 @@ describe('Write controller unit test', function() {
expect(keychainMock.getReceiverPublicKey.called).to.be.false; expect(keychainMock.getReceiverPublicKey.called).to.be.false;
}); });
it('should not work for error in keychain', function() { it('should not work for error in keychain', function(done) {
var recipient = { var recipient = {
address: 'asds@example.com' address: 'asds@example.com'
}; };
keychainMock.refreshKeyForUserId.withArgs({ keychainMock.refreshKeyForUserId.withArgs({
userId: recipient.address userId: recipient.address
}).yields({ }).returns(rejects({
errMsg: '404 not found yadda yadda' errMsg: '404 not found yadda yadda'
}));
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();
}); });
scope.verify(recipient);
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;
}); });
it('should work for main userId', function(done) { it('should work for main userId', function(done) {
@ -210,11 +212,11 @@ describe('Write controller unit test', function() {
keychainMock.refreshKeyForUserId.withArgs({ keychainMock.refreshKeyForUserId.withArgs({
userId: recipient.address userId: recipient.address
}).yields(null, { }).returns(resolves({
userId: 'asdf@example.com' userId: 'asdf@example.com'
}); }));
scope.$digest = function() { scope.verify(recipient).then(function() {
expect(recipient.key).to.deep.equal({ expect(recipient.key).to.deep.equal({
userId: 'asdf@example.com' userId: 'asdf@example.com'
}); });
@ -222,9 +224,7 @@ describe('Write controller unit test', function() {
expect(scope.checkSendStatus.callCount).to.equal(2); expect(scope.checkSendStatus.callCount).to.equal(2);
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true; expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
done(); done();
}; });
scope.verify(recipient);
}); });
it('should work for secondary userId', function(done) { it('should work for secondary userId', function(done) {
@ -240,17 +240,15 @@ describe('Write controller unit test', function() {
keychainMock.refreshKeyForUserId.withArgs({ keychainMock.refreshKeyForUserId.withArgs({
userId: recipient.address userId: recipient.address
}).yields(null, key); }).returns(resolves(key));
scope.$digest = function() { scope.verify(recipient).then(function() {
expect(recipient.key).to.deep.equal(key); expect(recipient.key).to.deep.equal(key);
expect(recipient.secure).to.be.true; expect(recipient.secure).to.be.true;
expect(scope.checkSendStatus.callCount).to.equal(2); expect(scope.checkSendStatus.callCount).to.equal(2);
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true; expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
done(); done();
}; });
scope.verify(recipient);
}); });
}); });
@ -311,7 +309,7 @@ describe('Write controller unit test', function() {
}); });
describe('send to outbox', function() { describe('send to outbox', function() {
it('should work', function() { it('should work', function(done) {
scope.to = [{ scope.to = [{
address: 'pity@dafool' address: 'pity@dafool'
}]; }];
@ -341,25 +339,26 @@ describe('Write controller unit test', function() {
expect(mail.sentDate).to.exist; expect(mail.sentDate).to.exist;
return true; return true;
})).yields(); })).returns(resolves());
emailMock.setFlags.yields(); emailMock.setFlags.returns(resolves());
scope.sendToOutbox(); scope.sendToOutbox().then(function() {
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true;
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true; expect(outboxMock.put.calledOnce).to.be.true;
expect(outboxMock.put.calledOnce).to.be.true; expect(emailMock.setFlags.calledOnce).to.be.true;
expect(emailMock.setFlags.calledOnce).to.be.true; expect(scope.state.lightbox).to.be.undefined;
expect(scope.state.lightbox).to.be.undefined; expect(scope.replyTo.answered).to.be.true;
expect(scope.replyTo.answered).to.be.true; done();
});
}); });
}); });
describe('lookupAddressBook', function() { describe('lookupAddressBook', function() {
it('should work', function(done) { it('should work', function(done) {
keychainMock.listLocalPublicKeys.yields(null, [{ keychainMock.listLocalPublicKeys.returns(resolves([{
userId: 'test@asdf.com', userId: 'test@asdf.com',
publicKey: 'KEY' publicKey: 'KEY'
}]); }]));
var result = scope.lookupAddressBook('test'); var result = scope.lookupAddressBook('test');
@ -369,7 +368,6 @@ describe('Write controller unit test', function() {
}]); }]);
done(); done();
}); });
scope.$digest();
}); });
it('should work with cache', function(done) { it('should work with cache', function(done) {
@ -387,7 +385,6 @@ describe('Write controller unit test', function() {
}]); }]);
done(); done();
}); });
scope.$digest();
}); });
}); });

View File

@ -29,6 +29,7 @@ describe('Login Private Key Download Controller unit test', function() {
$location: location, $location: location,
$scope: scope, $scope: scope,
$routeParams: {}, $routeParams: {},
$q: window.qMock,
auth: authMock, auth: authMock,
email: emailDaoMock, email: emailDaoMock,
keychain: keychainMock keychain: keychainMock
@ -46,74 +47,57 @@ describe('Login Private Key Download Controller unit test', function() {
}); });
describe('checkToken', function() { describe('checkToken', function() {
var verifyRecoveryTokenStub;
beforeEach(function() {
verifyRecoveryTokenStub = sinon.stub(scope, 'verifyRecoveryToken');
});
afterEach(function() {
verifyRecoveryTokenStub.restore();
});
it('should fail for empty recovery token', function() {
scope.tokenForm.$invalid = true;
scope.checkToken();
expect(verifyRecoveryTokenStub.calledOnce).to.be.false;
expect(scope.errMsg).to.exist;
});
it('should work', function() {
verifyRecoveryTokenStub.yields();
scope.checkToken();
expect(verifyRecoveryTokenStub.calledOnce).to.be.true;
expect(scope.step).to.equal(2);
});
});
describe('verifyRecoveryToken', function() {
var testKeypair = { var testKeypair = {
publicKey: { publicKey: {
_id: 'id' _id: 'id'
} }
}; };
it('should fail in keychain.getUserKeyPair', function() { it('should fail for empty recovery token', function() {
keychainMock.getUserKeyPair.yields(new Error('asdf')); scope.tokenForm.$invalid = true;
scope.verifyRecoveryToken(); scope.checkToken();
expect(keychainMock.getUserKeyPair.calledOnce).to.be.false;
expect(scope.errMsg).to.exist; expect(scope.errMsg).to.exist;
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
}); });
it('should fail in keychain.downloadPrivateKey', function() { it('should fail in keychain.getUserKeyPair', function(done) {
keychainMock.getUserKeyPair.yields(null, testKeypair); keychainMock.getUserKeyPair.returns(rejects(new Error('asdf')));
keychainMock.downloadPrivateKey.yields(new Error('asdf'));
scope.recoveryToken = 'token';
scope.verifyRecoveryToken(); scope.checkToken().then(function() {
expect(scope.errMsg).to.exist;
expect(scope.errMsg).to.exist; expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true; done();
expect(keychainMock.downloadPrivateKey.calledOnce).to.be.true; });
}); });
it('should work', function() { it('should fail in keychain.downloadPrivateKey', function(done) {
keychainMock.getUserKeyPair.yields(null, testKeypair); keychainMock.getUserKeyPair.returns(resolves(testKeypair));
keychainMock.downloadPrivateKey.yields(null, 'encryptedPrivateKey'); keychainMock.downloadPrivateKey.returns(rejects(new Error('asdf')));
scope.recoveryToken = 'token'; scope.recoveryToken = 'token';
scope.verifyRecoveryToken(function() {}); scope.checkToken().then(function() {
expect(scope.errMsg).to.exist;
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
expect(keychainMock.downloadPrivateKey.calledOnce).to.be.true;
done();
});
});
expect(scope.encryptedPrivateKey).to.equal('encryptedPrivateKey'); it('should work', function(done) {
keychainMock.getUserKeyPair.returns(resolves(testKeypair));
keychainMock.downloadPrivateKey.returns(resolves('encryptedPrivateKey'));
scope.recoveryToken = 'token';
scope.checkToken().then(function() {
expect(scope.encryptedPrivateKey).to.equal('encryptedPrivateKey');
done();
});
}); });
}); });
describe('decryptAndStorePrivateKeyLocally', function() { describe('checkCode', function() {
beforeEach(function() { beforeEach(function() {
scope.code = '012345'; scope.code = '012345';
@ -125,48 +109,50 @@ describe('Login Private Key Download Controller unit test', function() {
_id: 'keyId' _id: 'keyId'
} }
}; };
sinon.stub(scope, 'goTo');
});
afterEach(function() {
scope.goTo.restore();
}); });
it('should fail on decryptAndStorePrivateKeyLocally', function() { it('should fail on decryptAndStorePrivateKeyLocally', function(done) {
keychainMock.decryptAndStorePrivateKeyLocally.yields(new Error('asdf')); keychainMock.decryptAndStorePrivateKeyLocally.returns(rejects(new Error('asdf')));
scope.decryptAndStorePrivateKeyLocally(); scope.checkCode().then(function() {
expect(scope.errMsg).to.exist;
expect(scope.errMsg).to.exist; expect(keychainMock.decryptAndStorePrivateKeyLocally.calledOnce).to.be.true;
expect(keychainMock.decryptAndStorePrivateKeyLocally.calledOnce).to.be.true; done();
});
}); });
it('should goto /login-existing on emailDao.unlock fail', function(done) { it('should goto /login-existing on emailDao.unlock fail', function(done) {
keychainMock.decryptAndStorePrivateKeyLocally.yields(null, { keychainMock.decryptAndStorePrivateKeyLocally.returns(resolves({
encryptedKey: 'keyArmored' encryptedKey: 'keyArmored'
}); }));
emailDaoMock.unlock.yields(new Error('asdf')); emailDaoMock.unlock.returns(rejects(new Error('asdf')));
scope.goTo = function(location) { scope.checkCode().then(function() {
expect(location).to.equal('/login-existing'); expect(scope.goTo.withArgs('/login-existing').calledOnce).to.be.true;
expect(keychainMock.decryptAndStorePrivateKeyLocally.calledOnce).to.be.true; expect(keychainMock.decryptAndStorePrivateKeyLocally.calledOnce).to.be.true;
expect(emailDaoMock.unlock.calledOnce).to.be.true; expect(emailDaoMock.unlock.calledOnce).to.be.true;
done(); done();
}; });
scope.decryptAndStorePrivateKeyLocally();
}); });
it('should goto /account on emailDao.unlock success', function(done) { it('should goto /account on emailDao.unlock success', function(done) {
keychainMock.decryptAndStorePrivateKeyLocally.yields(null, { keychainMock.decryptAndStorePrivateKeyLocally.returns(resolves({
encryptedKey: 'keyArmored' encryptedKey: 'keyArmored'
}); }));
emailDaoMock.unlock.yields(); emailDaoMock.unlock.returns(resolves());
authMock.storeCredentials.yields(); authMock.storeCredentials.returns(resolves());
scope.goTo = function(location) { scope.checkCode().then(function() {
expect(location).to.equal('/account'); expect(scope.goTo.withArgs('/account').calledOnce).to.be.true;
expect(keychainMock.decryptAndStorePrivateKeyLocally.calledOnce).to.be.true; expect(keychainMock.decryptAndStorePrivateKeyLocally.calledOnce).to.be.true;
expect(emailDaoMock.unlock.calledOnce).to.be.true; expect(emailDaoMock.unlock.calledOnce).to.be.true;
done(); done();
}; });
scope.decryptAndStorePrivateKeyLocally();
}); });
}); });

View File

@ -40,6 +40,7 @@ describe('Login (Set Credentials) Controller unit test', function() {
setCredentialsCtrl = $controller(SetCredentialsCtrl, { setCredentialsCtrl = $controller(SetCredentialsCtrl, {
$scope: scope, $scope: scope,
$routeParams: {}, $routeParams: {},
$q: window.qMock,
auth: auth, auth: auth,
connectionDoctor: doctor connectionDoctor: doctor
}); });
@ -49,7 +50,7 @@ describe('Login (Set Credentials) Controller unit test', function() {
afterEach(function() {}); afterEach(function() {});
describe('set credentials', function() { describe('set credentials', function() {
it('should work', function() { it('should work', function(done) {
scope.emailAddress = 'emailemailemailemail'; scope.emailAddress = 'emailemailemailemail';
scope.password = 'passwdpasswdpasswdpasswd'; scope.password = 'passwdpasswdpasswdpasswd';
scope.smtpHost = 'hosthosthost'; scope.smtpHost = 'hosthosthost';
@ -80,14 +81,16 @@ describe('Login (Set Credentials) Controller unit test', function() {
} }
}; };
doctor.check.yields(); // synchronous yields! doctor.check.returns(resolves()); // synchronous yields!
scope.test(); scope.test().then(function() {
expect(doctor.check.calledOnce).to.be.true;
expect(doctor.configure.calledOnce).to.be.true;
expect(doctor.configure.calledWith(expectedCredentials)).to.be.true;
expect(auth.setCredentials.calledOnce).to.be.true;
done();
});
expect(doctor.check.calledOnce).to.be.true;
expect(doctor.configure.calledOnce).to.be.true;
expect(doctor.configure.calledWith(expectedCredentials)).to.be.true;
expect(auth.setCredentials.calledOnce).to.be.true;
}); });
}); });
}); });

View File

@ -34,6 +34,7 @@ describe('Validate Phone Controller unit test', function() {
$location: location, $location: location,
$scope: scope, $scope: scope,
$routeParams: {}, $routeParams: {},
$q: window.qMock,
mailConfig: mailConfigMock, mailConfig: mailConfigMock,
auth: authStub, auth: authStub,
admin: adminStub admin: adminStub
@ -59,55 +60,53 @@ describe('Validate Phone Controller unit test', function() {
it('should fail to error creating user', function(done) { it('should fail to error creating user', function(done) {
scope.form.$invalid = false; scope.form.$invalid = false;
scope.token = 'asfd'; scope.token = 'asfd';
adminStub.validateUser.yieldsAsync(new Error('asdf')); adminStub.validateUser.returns(rejects(new Error('asdf')));
scope.$apply = function() { scope.validateUser().then(function() {
expect(scope.busy).to.be.false; expect(scope.busy).to.be.false;
expect(scope.errMsg).to.equal('asdf'); expect(scope.errMsg).to.equal('asdf');
expect(adminStub.validateUser.calledOnce).to.be.true; expect(adminStub.validateUser.calledOnce).to.be.true;
done(); done();
}; });
scope.validateUser();
expect(scope.busy).to.be.true; expect(scope.busy).to.be.true;
}); });
it('should work', function(done) { it('should work', function(done) {
scope.form.$invalid = false; scope.form.$invalid = false;
scope.token = 'asfd'; scope.token = 'asfd';
adminStub.validateUser.yieldsAsync(); adminStub.validateUser.returns(resolves());
scope.login = function() { scope.login = function() {};
scope.validateUser().then(function() {
expect(scope.busy).to.be.true; expect(scope.busy).to.be.true;
expect(scope.errMsg).to.be.undefined; expect(scope.errMsg).to.be.undefined;
expect(adminStub.validateUser.calledOnce).to.be.true; expect(adminStub.validateUser.calledOnce).to.be.true;
done(); done();
}; });
scope.validateUser();
expect(scope.busy).to.be.true; expect(scope.busy).to.be.true;
}); });
}); });
describe('login', function() { describe('login', function() {
it('should work', inject(function($q, $rootScope) { it('should work', function(done) {
scope.form.$invalid = false; scope.form.$invalid = false;
authStub.setCredentials.returns(); authStub.setCredentials.returns();
var deferred = $q.defer(); sinon.stub(mailConfigMock, 'get').returns(resolves({
sinon.stub(mailConfigMock, 'get').returns(deferred.promise);
deferred.resolve({
imap: {}, imap: {},
smtp: {} smtp: {}
}));
scope.login().then(function() {
expect(mailConfigMock.get.calledOnce).to.be.true;
expect(authStub.setCredentials.calledOnce).to.be.true;
expect(location.path.calledWith('/login')).to.be.true;
done();
}); });
});
scope.login();
$rootScope.$apply();
expect(mailConfigMock.get.calledOnce).to.be.true;
expect(authStub.setCredentials.calledOnce).to.be.true;
expect(location.path.calledWith('/login')).to.be.true;
}));
}); });
}); });