1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 02:42:17 -05:00

Begin fixing controller unit tests

This commit is contained in:
Tankred Hase 2014-11-25 18:19:40 +01:00
parent 2a2058c167
commit c8665bc786
18 changed files with 556 additions and 754 deletions

View File

@ -178,23 +178,23 @@ module.exports = function(grunt) {
'test/unit/email/outbox-bo-test.js',
'test/unit/email/email-dao-test.js',
'test/unit/email/account-test.js',
/*'test/unit/dialog-ctrl-test.js',
'test/unit/add-account-ctrl-test.js',
'test/unit/create-account-ctrl-test.js',
'test/unit/validate-phone-ctrl-test.js',
'test/unit/account-ctrl-test.js',
'test/unit/set-passphrase-ctrl-test.js',
'test/unit/contacts-ctrl-test.js',
'test/unit/login-existing-ctrl-test.js',
'test/unit/login-initial-ctrl-test.js',
'test/unit/login-new-device-ctrl-test.js',
'test/unit/login-privatekey-download-ctrl-test.js',
'test/unit/login-set-credentials-ctrl-test.js',
'test/unit/privatekey-upload-ctrl-test.js',
'test/unit/login-ctrl-test.js',
'test/unit/read-ctrl-test.js',
'test/unit/navigation-ctrl-test.js',
'test/unit/mail-list-ctrl-test.js',
'test/unit/controller/app/dialog-ctrl-test.js',
'test/unit/controller/login/add-account-ctrl-test.js',
'test/unit/controller/login/create-account-ctrl-test.js',
'test/unit/controller/login/validate-phone-ctrl-test.js',
'test/unit/controller/login/login-existing-ctrl-test.js',
'test/unit/controller/login/login-initial-ctrl-test.js',
'test/unit/controller/login/login-new-device-ctrl-test.js',
'test/unit/controller/login/login-privatekey-download-ctrl-test.js',
'test/unit/controller/login/login-set-credentials-ctrl-test.js',
'test/unit/controller/login/login-ctrl-test.js',
'test/unit/controller/app/privatekey-upload-ctrl-test.js',
'test/unit/controller/app/account-ctrl-test.js',
'test/unit/controller/app/set-passphrase-ctrl-test.js',
'test/unit/controller/app/contacts-ctrl-test.js',
'test/unit/controller/app/read-ctrl-test.js',
'test/unit/controller/app/navigation-ctrl-test.js',
/*'test/unit/mail-list-ctrl-test.js',
'test/unit/write-ctrl-test.js',
'test/unit/action-bar-ctrl-test.js',*/
]

View File

@ -1,50 +1,53 @@
'use strict';
var mocks = angular.mock,
AccountCtrl = require('../../src/js/controller/account'),
PGP = require('../../src/js/crypto/pgp'),
dl = require('../../src/js/util/download'),
appController = require('../../src/js/app-controller'),
KeychainDAO = require('../../src/js/dao/keychain-dao');
var AccountCtrl = require('../../../../src/js/controller/app/account'),
PGP = require('../../../../src/js/crypto/pgp'),
Download = require('../../../../src/js/util/download'),
Keychain = require('../../../../src/js/service/keychain'),
Auth = require('../../../../src/js/service/auth'),
Dialog = require('../../../../src/js/util/dialog');
describe('Account Controller unit test', function() {
var scope, accountCtrl,
dummyFingerprint, expectedFingerprint,
dummyKeyId, expectedKeyId,
emailAddress, keySize, pgpMock, keychainMock;
emailAddress, keySize, pgpStub, keychainStub, authStub, dialogStub, downloadStub;
beforeEach(function() {
appController._pgp = pgpMock = sinon.createStubInstance(PGP);
appController._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
pgpStub = sinon.createStubInstance(PGP);
authStub = sinon.createStubInstance(Auth);
keychainStub = sinon.createStubInstance(Keychain);
dialogStub = sinon.createStubInstance(Dialog);
downloadStub = sinon.createStubInstance(Download);
dummyFingerprint = '3A2D39B4E1404190B8B949DE7D7E99036E712926';
expectedFingerprint = '3A2D 39B4 E140 4190 B8B9 49DE 7D7E 9903 6E71 2926';
dummyKeyId = '9FEB47936E712926';
expectedKeyId = '6E712926';
pgpMock.getFingerprint.returns(dummyFingerprint);
pgpMock.getKeyId.returns(dummyKeyId);
pgpStub.getFingerprint.returns(dummyFingerprint);
pgpStub.getKeyId.returns(dummyKeyId);
emailAddress = 'fred@foo.com';
keySize = 1234;
appController._emailDao = {
_account: {
emailAddress: emailAddress,
asymKeySize: keySize
}
};
pgpMock.getKeyParams.returns({
authStub.emailAddress = emailAddress;
pgpStub.getKeyParams.returns({
_id: dummyKeyId,
fingerprint: dummyFingerprint,
userId: emailAddress,
bitSize: keySize
});
angular.module('accounttest', []);
mocks.module('accounttest');
mocks.inject(function($rootScope, $controller) {
angular.module('accounttest', ['woServices']);
angular.mock.module('accounttest');
angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {};
accountCtrl = $controller(AccountCtrl, {
$scope: scope
$scope: scope,
auth: authStub,
keychain: keychainStub,
pgp: pgpStub,
download: downloadStub,
dialog: dialogStub
});
});
});
@ -61,8 +64,7 @@ describe('Account Controller unit test', function() {
});
describe('export to key file', function() {
it('should work', function() {
var createDownloadMock = sinon.stub(dl, 'createDownload');
keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, {
keychainStub.getUserKeyPair.withArgs(emailAddress).yields(null, {
publicKey: {
_id: dummyKeyId,
publicKey: 'a'
@ -71,27 +73,23 @@ describe('Account Controller unit test', function() {
encryptedKey: 'b'
}
});
createDownloadMock.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';
})).returns();
scope.exportKeyFile();
expect(scope.state.lightbox).to.equal(undefined);
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
expect(dl.createDownload.calledOnce).to.be.true;
dl.createDownload.restore();
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(downloadStub.createDownload.calledOnce).to.be.true;
});
it('should not work when key export failed', function(done) {
keychainMock.getUserKeyPair.yields(new Error('Boom!'));
scope.onError = function(err) {
expect(err.message).to.equal('Boom!');
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
done();
};
it('should not work when key export failed', function() {
keychainStub.getUserKeyPair.yields(new Error());
scope.exportKeyFile();
expect(dialogStub.error.calledOnce).to.be.true;
});
});
});

View File

@ -1,38 +1,33 @@
'use strict';
var mocks = angular.mock,
ContactsCtrl = require('../../src/js/controller/contacts'),
appController = require('../../src/js/app-controller'),
KeychainDAO = require('../../src/js/dao/keychain-dao'),
PGP = require('../../src/js/crypto/pgp');
var ContactsCtrl = require('../../../../src/js/controller/app/contacts'),
Keychain = require('../../../../src/js/service/keychain'),
PGP = require('../../../../src/js/crypto/pgp'),
Dialog = require('../../../../src/js/util/dialog');
describe('Contacts Controller unit test', function() {
var scope, contactsCtrl,
origKeychain, keychainMock,
origPgp, pgpMock;
var scope, contactsCtrl, keychainStub, pgpStub, dialogStub;
beforeEach(function() {
origPgp = appController._pgp;
appController._pgp = pgpMock = sinon.createStubInstance(PGP);
origKeychain = appController._keychain;
appController._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
pgpStub = sinon.createStubInstance(PGP);
keychainStub = sinon.createStubInstance(Keychain);
dialogStub = sinon.createStubInstance(Dialog);
angular.module('contactstest', []);
mocks.module('contactstest');
mocks.inject(function($rootScope, $controller) {
angular.module('contactstest', ['woServices']);
angular.mock.module('contactstest');
angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {};
contactsCtrl = $controller(ContactsCtrl, {
$scope: scope
$scope: scope,
keychain: keychainStub,
pgp: pgpStub,
dialog: dialogStub
});
});
});
afterEach(function() {
// restore the module
appController._pgp = origPgp;
appController._keychain = origKeychain;
});
afterEach(function() {});
describe('scope variables', function() {
it('should be set correctly', function() {
@ -41,34 +36,28 @@ describe('Contacts Controller unit test', function() {
});
describe('listKeys', function() {
it('should fail due to error in keychain.listLocalPublicKeys', function(done) {
keychainMock.listLocalPublicKeys.yields(42);
scope.onError = function(err) {
expect(err).to.equal(42);
done();
};
it('should fail due to error in keychain.listLocalPublicKeys', function() {
keychainStub.listLocalPublicKeys.yields(42);
scope.listKeys();
expect(dialogStub.error.calledOnce).to.be.true;
});
it('should work', function(done) {
keychainMock.listLocalPublicKeys.yields(null, [{
it('should work', function() {
keychainStub.listLocalPublicKeys.yields(null, [{
_id: '12345'
}]);
pgpMock.getKeyParams.returns({
pgpStub.getKeyParams.returns({
fingerprint: 'asdf'
});
scope.$apply = function() {
expect(scope.keys).to.not.exist;
scope.listKeys();
expect(scope.keys.length).to.equal(1);
expect(scope.keys[0]._id).to.equal('12345');
expect(scope.keys[0].fingerprint).to.equal('asdf');
done();
};
expect(scope.keys).to.not.exist;
scope.listKeys();
});
});
@ -88,13 +77,13 @@ describe('Contacts Controller unit test', function() {
it('should work', function(done) {
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
pgpMock.getKeyParams.returns({
pgpStub.getKeyParams.returns({
_id: '12345',
userId: 'max@example.com',
userIds: []
});
keychainMock.saveLocalPublicKey.withArgs({
keychainStub.saveLocalPublicKey.withArgs({
_id: '12345',
userId: 'max@example.com',
userIds: [],
@ -109,46 +98,36 @@ describe('Contacts Controller unit test', function() {
scope.importKey(keyArmored);
});
it('should fail due to invalid armored key', function(done) {
it('should fail due to invalid armored key', function() {
var keyArmored = '-----BEGIN PGP PRIVATE KEY BLOCK-----';
scope.onError = function(err) {
expect(err).to.exist;
done();
};
scope.importKey(keyArmored);
expect(dialogStub.error.calledOnce).to.be.true;
});
it('should fail due to error in pgp.getKeyParams', function(done) {
it('should fail due to error in pgp.getKeyParams', function() {
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
pgpMock.getKeyParams.throws(new Error('WAT'));
scope.onError = function(err) {
expect(err).to.exist;
done();
};
pgpStub.getKeyParams.throws(new Error('WAT'));
scope.importKey(keyArmored);
expect(dialogStub.error.calledOnce).to.be.true;
});
it('should fail due to error in keychain.saveLocalPublicKey', function(done) {
it('should fail due to error in keychain.saveLocalPublicKey', function() {
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
pgpMock.getKeyParams.returns({
pgpStub.getKeyParams.returns({
_id: '12345',
userId: 'max@example.com'
});
keychainMock.saveLocalPublicKey.yields(42);
scope.onError = function(err) {
expect(err).to.equal(42);
done();
};
keychainStub.saveLocalPublicKey.yields(42);
scope.importKey(keyArmored);
expect(dialogStub.error.calledOnce).to.be.true;
});
});
@ -158,7 +137,7 @@ describe('Contacts Controller unit test', function() {
_id: '12345'
};
keychainMock.removeLocalPublicKey.withArgs('12345').yields();
keychainStub.removeLocalPublicKey.withArgs('12345').yields();
scope.listKeys = function() {
done();
@ -167,19 +146,16 @@ describe('Contacts Controller unit test', function() {
scope.removeKey(key);
});
it('should fail due to error in keychain.removeLocalPublicKey', function(done) {
it('should fail due to error in keychain.removeLocalPublicKey', function() {
var key = {
_id: '12345'
};
keychainMock.removeLocalPublicKey.withArgs('12345').yields(42);
scope.onError = function(err) {
expect(err).to.equal(42);
done();
};
keychainStub.removeLocalPublicKey.withArgs('12345').yields(42);
scope.removeKey(key);
expect(dialogStub.error.calledOnce).to.be.true;
});
});
});

View File

@ -1,21 +1,22 @@
'use strict';
var mocks = angular.mock,
DialogCtrl = require('../../src/js/controller/dialog');
var DialogCtrl = require('../../../../src/js/controller/app/dialog');
describe('Dialog Controller unit test', function() {
var scope, dialogCtrl;
var scope, dialogCtrl, dialogService;
beforeEach(function() {
angular.module('dialogtest', []);
mocks.module('dialogtest');
mocks.inject(function($rootScope, $controller) {
angular.module('dialogtest', ['woUtil']);
angular.mock.module('dialogtest');
angular.mock.inject(function($rootScope, $controller, dialog) {
scope = $rootScope.$new();
scope.state = {
dialog: {}
};
dialogService = dialog;
dialogCtrl = $controller(DialogCtrl, {
$scope: scope
$scope: scope,
dialog: dialog
});
});
});
@ -24,9 +25,9 @@ describe('Dialog Controller unit test', function() {
describe('confirm', function() {
it('should work', function(done) {
scope.state.dialog.callback = function(confirmed) {
scope.callback = function(confirmed) {
expect(confirmed).to.be.true;
expect(scope.state.dialog.open).to.be.false;
expect(scope.open).to.be.false;
done();
};
scope.confirm(true);
@ -35,9 +36,9 @@ describe('Dialog Controller unit test', function() {
describe('cancel', function() {
it('should work', function(done) {
scope.state.dialog.callback = function(confirmed) {
scope.callback = function(confirmed) {
expect(confirmed).to.be.false;
expect(scope.state.dialog.open).to.be.false;
expect(scope.open).to.be.false;
done();
};
scope.confirm(false);

View File

@ -1,19 +1,17 @@
'use strict';
var mocks = angular.mock,
NavigationCtrl = require('../../src/js/controller/navigation'),
EmailDAO = require('../../src/js/dao/email-dao'),
OutboxBO = require('../../src/js/bo/outbox'),
appController = require('../../src/js/app-controller');
var NavigationCtrl = require('../../../../src/js/controller/app/navigation'),
Email = require('../../../../src/js/email/email'),
Account = require('../../../../src/js/email/account'),
Outbox = require('../../../../src/js/email/outbox'),
Dialog = require('../../../../src/js/util/dialog'),
Notif = require('../../../../src/js/util/notification');
describe('Navigation Controller unit test', function() {
var scope, ctrl, origEmailDao, emailDaoMock, outboxBoMock, outboxFolder, onConnectStub;
var scope, ctrl, emailDaoMock, accountMock, notificationStub, dialogStub, outboxBoMock, outboxFolder;
beforeEach(function(done) {
// remember original module to restore later
origEmailDao = appController._emailDao;
emailDaoMock = sinon.createStubInstance(EmailDAO);
emailDaoMock._account = {
beforeEach(function() {
var account = {
folders: [{
type: 'Inbox',
count: 2,
@ -24,32 +22,34 @@ describe('Navigation Controller unit test', function() {
path: 'OUTBOX'
}]
};
outboxFolder = emailDaoMock._account.folders[1];
appController._emailDao = emailDaoMock;
outboxBoMock = sinon.createStubInstance(OutboxBO);
appController._outboxBo = outboxBoMock;
outboxBoMock.startChecking.returns();
onConnectStub = sinon.stub(appController, 'onConnect');
onConnectStub.yields();
angular.module('navigationtest', []);
mocks.module('navigationtest');
mocks.inject(function($rootScope, $controller) {
emailDaoMock = sinon.createStubInstance(Email);
outboxFolder = account.folders[1];
outboxBoMock = sinon.createStubInstance(Outbox);
outboxBoMock.startChecking.returns();
dialogStub = sinon.createStubInstance(Dialog);
notificationStub = sinon.createStubInstance(Notif);
accountMock = sinon.createStubInstance(Account);
accountMock.list.returns([account]);
angular.module('navigationtest', ['woServices', 'woEmail', 'woUtil']);
angular.mock.module('navigationtest');
angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {};
ctrl = $controller(NavigationCtrl, {
$scope: scope,
$routeParams: {}
$routeParams: {},
account: accountMock,
email: emailDaoMock,
outbox: outboxBoMock,
notification: notificationStub,
dialog: dialogStub
});
done();
});
});
afterEach(function() {
// restore the module
appController._emailDao = origEmailDao;
onConnectStub.restore();
});
afterEach(function() {});
describe('initial state', function() {
it('should be well defined', function() {

View File

@ -1,48 +1,39 @@
'use strict';
var mocks = angular.mock,
PrivateKeyUploadCtrl = require('../../src/js/controller/privatekey-upload'),
appController = require('../../src/js/app-controller'),
KeychainDAO = require('../../src/js/dao/keychain-dao'),
PGP = require('../../src/js/crypto/pgp');
var PrivateKeyUploadCtrl = require('../../../../src/js/controller/app/privatekey-upload'),
KeychainDAO = require('../../../../src/js/service/keychain'),
PGP = require('../../../../src/js/crypto/pgp'),
Dialog = require('../../../../src/js/util/dialog');
describe('Private Key Upload Controller unit test', function() {
var scope, location, ctrl,
origEmailDao, emailDaoMock,
origKeychain, keychainMock,
pgpStub,
keychainMock, pgpStub, dialogStub,
emailAddress = 'fred@foo.com';
beforeEach(function(done) {
// remember original module to restore later, then replace it
origEmailDao = appController._emailDao;
appController._emailDao = emailDaoMock = {
_account: {
emailAddress: emailAddress
}
};
origKeychain = appController._keychain;
appController._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
keychainMock._pgp = pgpStub = sinon.createStubInstance(PGP);
beforeEach(function() {
keychainMock = sinon.createStubInstance(KeychainDAO);
pgpStub = sinon.createStubInstance(PGP);
dialogStub = sinon.createStubInstance(Dialog);
angular.module('login-privatekey-download-test', []);
mocks.module('login-privatekey-download-test');
mocks.inject(function($controller, $rootScope) {
angular.module('login-privatekey-download-test', ['woServices']);
angular.mock.module('login-privatekey-download-test');
angular.mock.inject(function($controller, $rootScope) {
scope = $rootScope.$new();
scope.state = {};
ctrl = $controller(PrivateKeyUploadCtrl, {
$location: location,
$scope: scope
$scope: scope,
keychain: keychainMock,
pgp: pgpStub,
dialog: dialogStub,
auth: {
emailAddress: emailAddress
}
});
done();
});
});
afterEach(function() {
// restore the app controller module
appController._keychain = origKeychain;
appController._emailDao = origEmailDao;
});
afterEach(function() {});
describe('checkServerForKey', function() {
var keyParams = {
@ -50,17 +41,14 @@ describe('Private Key Upload Controller unit test', function() {
_id: 'keyId',
};
it('should fail', function(done) {
it('should fail', function() {
pgpStub.getKeyParams.returns(keyParams);
keychainMock.hasPrivateKey.yields(42);
scope.onError = function(err) {
expect(err).to.exist;
expect(keychainMock.hasPrivateKey.calledOnce).to.be.true;
done();
};
scope.checkServerForKey();
expect(dialogStub.error.calledOnce).to.be.true;
expect(keychainMock.hasPrivateKey.calledOnce).to.be.true;
});
it('should return true', function(done) {
@ -165,16 +153,13 @@ describe('Private Key Upload Controller unit test', function() {
});
describe('encryptAndUploadKey', function() {
it('should fail due to keychain.registerDevice', function(done) {
it('should fail due to keychain.registerDevice', function() {
keychainMock.registerDevice.yields(42);
scope.onError = function(err) {
expect(err).to.exist;
expect(keychainMock.registerDevice.calledOnce).to.be.true;
done();
};
scope.encryptAndUploadKey();
expect(dialogStub.error.calledOnce).to.be.true;
expect(keychainMock.registerDevice.calledOnce).to.be.true;
});
it('should work', function(done) {
@ -237,45 +222,36 @@ describe('Private Key Upload Controller unit test', function() {
expect(scope.step).to.equal(2);
});
it('should fail for 3 due to error in setDeviceName', function(done) {
it('should fail for 3 due to error in setDeviceName', function() {
scope.step = 3;
setDeviceNameStub.yields(42);
scope.onError = function(err) {
expect(err).to.exist;
expect(scope.step).to.equal(3);
done();
};
scope.goForward();
expect(dialogStub.error.calledOnce).to.be.true;
expect(scope.step).to.equal(3);
});
it('should fail for 3 due to error in encryptAndUploadKey', function(done) {
it('should fail for 3 due to error in encryptAndUploadKey', function() {
scope.step = 3;
setDeviceNameStub.yields();
encryptAndUploadKeyStub.yields(42);
scope.onError = function(err) {
expect(err).to.exist;
expect(scope.step).to.equal(4);
done();
};
scope.goForward();
expect(dialogStub.error.calledOnce).to.be.true;
expect(scope.step).to.equal(4);
});
it('should work for 3', function(done) {
it('should work for 3', function() {
scope.step = 3;
setDeviceNameStub.yields();
encryptAndUploadKeyStub.yields();
scope.onError = function(err) {
expect(err.title).to.equal('Success');
expect(scope.step).to.equal(4);
done();
};
scope.goForward();
expect(dialogStub.info.calledOnce).to.be.true;
expect(scope.step).to.equal(4);
});
});
});

View File

@ -1,57 +1,49 @@
'use strict';
var mocks = angular.mock,
KeychainDAO = require('../../src/js/dao/keychain-dao'),
InvitationDAO = require('../../src/js/dao/invitation-dao'),
PGP = require('../../src/js/crypto/pgp'),
ReadCtrl = require('../../src/js/controller/read'),
OutboxBO = require('../../src/js/bo/outbox'),
appController = require('../../src/js/app-controller');
var Keychain = require('../../../../src/js/service/keychain'),
InvitationDAO = require('../../../../src/js/service/invitation'),
Email = require('../../../../src/js/email/email'),
PGP = require('../../../../src/js/crypto/pgp'),
ReadCtrl = require('../../../../src/js/controller/app/read'),
Outbox = require('../../../../src/js/email/outbox'),
Dialog = require('../../../../src/js/util/dialog'),
Auth = require('../../../../src/js/service/auth'),
Download = require('../../../../src/js/util/download');
describe('Read Controller unit test', function() {
var scope, ctrl,
origKeychain, keychainMock,
origInvitation, invitationMock,
origCrypto, cryptoMock,
origOutbox, outboxMock,
origEmailDao;
var scope, ctrl, keychainMock, invitationMock, emailMock, pgpMock, outboxMock, dialogMock, authMock, downloadMock,
emailAddress = 'sender@example.com';
beforeEach(function() {
origKeychain = appController._keychain;
appController._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
keychainMock = sinon.createStubInstance(Keychain);
invitationMock = sinon.createStubInstance(InvitationDAO);
pgpMock = sinon.createStubInstance(PGP);
outboxMock = sinon.createStubInstance(Outbox);
emailMock = sinon.createStubInstance(Email);
dialogMock = sinon.createStubInstance(Dialog);
authMock = sinon.createStubInstance(Auth);
downloadMock = sinon.createStubInstance(Download);
origInvitation = appController._invitationDao;
appController._invitationDao = invitationMock = sinon.createStubInstance(InvitationDAO);
origCrypto = appController._pgp;
appController._pgp = cryptoMock = sinon.createStubInstance(PGP);
origOutbox = appController._outboxBo;
appController._outboxBo = outboxMock = sinon.createStubInstance(OutboxBO);
origEmailDao = appController._emailDao;
appController._emailDao = {
_account: 'sender@example.com'
};
angular.module('readtest', []);
mocks.module('readtest');
mocks.inject(function($rootScope, $controller) {
angular.module('readtest', ['woServices']);
angular.mock.module('readtest');
angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {};
ctrl = $controller(ReadCtrl, {
$scope: scope
$scope: scope,
email: emailMock,
invitation: invitationMock,
outbox: outboxMock,
pgp: pgpMock,
keychain: keychainMock,
download: downloadMock,
auth: authMock,
dialog: dialogMock
});
});
});
afterEach(function() {
appController._keychain = origKeychain;
appController._invitationDao = origInvitation;
appController._pgp = origCrypto;
appController._outboxBo = origOutbox;
appController._emailDao = origEmailDao;
});
afterEach(function() {});
describe('scope variables', function() {
it('should be set correctly', function() {
@ -78,23 +70,18 @@ describe('Read Controller unit test', function() {
expect(scope.keyId).to.equal('No key found.');
keychainMock.getReceiverPublicKey.yields(42);
scope.onError = function(err) {
expect(err).to.equal(42);
expect(scope.keyId).to.equal('Searching...');
};
scope.getKeyId(address);
expect(dialogMock.error.calledOnce).to.be.true;
expect(scope.keyId).to.equal('Searching...');
});
it('should allow invitation on empty key', function() {
keychainMock.getReceiverPublicKey.yields();
scope.onError = function(err) {
expect(err).not.exist;
expect(scope.keyId).to.equal('User has no key. Click to invite.');
};
scope.getKeyId(address);
expect(scope.keyId).to.equal('User has no key. Click to invite.');
});
it('should show searching on error', function() {
@ -102,14 +89,10 @@ describe('Read Controller unit test', function() {
publicKey: 'PUBLIC KEY'
});
cryptoMock.getFingerprint.returns('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
scope.onError = function(err) {
expect(err).to.not.exist;
expect(scope.keyId).to.equal('PGP key: XXXXXXXX');
};
pgpMock.getFingerprint.returns('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
scope.getKeyId(address);
expect(scope.keyId).to.equal('PGP key: XXXXXXXX');
});
});
@ -128,39 +111,33 @@ describe('Read Controller unit test', function() {
it('should show error on invitation dao invite error', function() {
invitationMock.invite.yields(42);
scope.onError = function(err) {
expect(err).to.equal(42);
};
scope.invite({
address: 'asdf@asdf.de'
});
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should show error on outbox put error', function() {
invitationMock.invite.yields();
outboxMock.put.yields(42);
scope.onError = function(err) {
expect(err).to.equal(42);
};
scope.invite({
address: 'asdf@asdf.de'
});
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should work', function() {
invitationMock.invite.yields();
outboxMock.put.yields();
scope.onError = function(err) {
expect(err).to.not.exist;
};
scope.invite({
address: 'asdf@asdf.de'
});
expect(dialogMock.error.calledOnce).to.be.true;
});
});

View File

@ -1,31 +1,31 @@
'use strict';
var mocks = angular.mock,
SetPassphraseCtrl = require('../../src/js/controller/set-passphrase'),
PGP = require('../../src/js/crypto/pgp'),
appController = require('../../src/js/app-controller'),
KeychainDAO = require('../../src/js/dao/keychain-dao');
var SetPassphraseCtrl = require('../../../../src/js/controller/app/set-passphrase'),
PGP = require('../../../../src/js/crypto/pgp'),
Keychain = require('../../../../src/js/service/keychain'),
Dialog = require('../../../../src/js/util/dialog');
describe('Set Passphrase Controller unit test', function() {
var scope, setPassphraseCtrl,
dummyFingerprint, expectedFingerprint,
dummyKeyId, expectedKeyId,
emailAddress, keySize, cryptoMock, keychainMock;
emailAddress, keySize, pgpStub, keychainStub, dialogStub;
beforeEach(function() {
appController._pgp = cryptoMock = sinon.createStubInstance(PGP);
appController._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
pgpStub = sinon.createStubInstance(PGP);
keychainStub = sinon.createStubInstance(Keychain);
dialogStub = sinon.createStubInstance(Dialog);
dummyFingerprint = '3A2D39B4E1404190B8B949DE7D7E99036E712926';
expectedFingerprint = '3A2D 39B4 E140 4190 B8B9 49DE 7D7E 9903 6E71 2926';
dummyKeyId = '9FEB47936E712926';
expectedKeyId = '6E712926';
cryptoMock.getFingerprint.returns(dummyFingerprint);
cryptoMock.getKeyId.returns(dummyKeyId);
pgpStub.getFingerprint.returns(dummyFingerprint);
pgpStub.getKeyId.returns(dummyKeyId);
emailAddress = 'fred@foo.com';
keySize = 1234;
cryptoMock.getKeyParams.returns({
pgpStub.getKeyParams.returns({
_id: dummyKeyId,
fingerprint: dummyFingerprint,
userId: emailAddress,
@ -33,13 +33,16 @@ describe('Set Passphrase Controller unit test', function() {
bitSize: keySize
});
angular.module('setpassphrasetest', []);
mocks.module('setpassphrasetest');
mocks.inject(function($rootScope, $controller) {
angular.module('setpassphrasetest', ['woServices', 'woUtil']);
angular.mock.module('setpassphrasetest');
angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {};
setPassphraseCtrl = $controller(SetPassphraseCtrl, {
$scope: scope
$scope: scope,
pgp: pgpStub,
keychain: keychainStub,
dialog: dialogStub
});
});
});
@ -47,33 +50,30 @@ describe('Set Passphrase Controller unit test', function() {
afterEach(function() {});
describe('setPassphrase', function() {
it('should work', function(done) {
it('should work', function() {
scope.oldPassphrase = 'old';
scope.newPassphrase = 'new';
keychainMock.lookupPrivateKey.withArgs(dummyKeyId).yields(null, {
keychainStub.lookupPrivateKey.withArgs(dummyKeyId).yields(null, {
encryptedKey: 'encrypted'
});
cryptoMock.changePassphrase.withArgs({
pgpStub.changePassphrase.withArgs({
privateKeyArmored: 'encrypted',
oldPassphrase: 'old',
newPassphrase: 'new'
}).yields(null, 'newArmoredKey');
keychainMock.saveLocalPrivateKey.withArgs({
keychainStub.saveLocalPrivateKey.withArgs({
_id: dummyKeyId,
userId: emailAddress,
userIds: [],
encryptedKey: 'newArmoredKey'
}).yields();
scope.onError = function(err) {
expect(err.title).to.equal('Success');
done();
};
scope.setPassphrase();
expect(dialogStub.info.calledOnce).to.be.true;
});
});

View File

@ -1,22 +1,21 @@
'use strict';
var mocks = angular.mock,
AddAccountCtrl = require('../../src/js/controller/add-account'),
Auth = require('../../src/js/bo/auth'),
appController = require('../../src/js/app-controller'),
cfg = require('../../src/js/app-config').config;
var AddAccountCtrl = require('../../../../src/js/controller/login/add-account'),
Auth = require('../../../../src/js/service/auth'),
Dialog = require('../../../../src/js/util/dialog'),
cfg = require('../../../../src/js/app-config').config;
describe('Add Account Controller unit test', function() {
var scope, location, mailConfigMock, ctrl, authStub, origAuth;
var scope, location, mailConfigMock, ctrl, authStub, dialogStub;
beforeEach(function() {
// remember original module to restore later, then replace it
origAuth = appController._auth;
appController._auth = authStub = sinon.createStubInstance(Auth);
authStub = sinon.createStubInstance(Auth);
dialogStub = sinon.createStubInstance(Dialog);
angular.module('addaccounttest', ['woServices']);
mocks.module('addaccounttest');
mocks.inject(function($controller, $rootScope, $location, mailConfig) {
angular.mock.module('addaccounttest');
angular.mock.inject(function($controller, $rootScope, $location, mailConfig) {
location = $location;
mailConfigMock = mailConfig;
scope = $rootScope.$new();
@ -31,15 +30,14 @@ describe('Add Account Controller unit test', function() {
$location: location,
$scope: scope,
$routeParams: {},
mailConfig: mailConfigMock
mailConfig: mailConfigMock,
auth: authStub,
dialog: dialogStub
});
});
});
afterEach(function() {
// restore the app controller module
appController._auth = origAuth;
location.path.restore();
location.search.restore();
if (scope.$apply.restore) {
@ -119,7 +117,7 @@ describe('Add Account Controller unit test', function() {
});
it('should use oauth', function() {
scope.onError = function(options) {
dialogStub.confirm = function(options) {
options.callback(true);
};
authStub.getOAuthToken.yields();
@ -131,7 +129,7 @@ describe('Add Account Controller unit test', function() {
});
it('should not use oauth', function() {
scope.onError = function(options) {
dialogStub.confirm = function(options) {
options.callback(false);
};
@ -141,19 +139,16 @@ describe('Add Account Controller unit test', function() {
expect(authStub.getOAuthToken.called).to.be.false;
});
it('should not forward to login when oauth fails', function(done) {
scope.onError = function(options) {
scope.onError = function(err) {
expect(err).to.exist;
expect(setCredentialsStub.called).to.be.false;
done();
};
it('should not forward to login when oauth fails', function() {
dialogStub.confirm = function(options) {
options.callback(true);
};
authStub.getOAuthToken.yields(new Error());
scope.oauthPossible();
expect(dialogStub.error.calledOnce).to.be.true;
expect(setCredentialsStub.called).to.be.false;
});
});

View File

@ -1,22 +1,20 @@
'use strict';
var mocks = angular.mock,
CreateAccountCtrl = require('../../src/js/controller/create-account'),
AdminDao = require('../../src/js/dao/admin-dao'),
appController = require('../../src/js/app-controller');
var CreateAccountCtrl = require('../../../../src/js/controller/login/create-account'),
AdminDao = require('../../../../src/js/service/admin'),
Auth = require('../../../../src/js/service/auth');
describe('Create Account Controller unit test', function() {
var scope, location, ctrl, authStub, origAuth, adminStub;
var scope, location, ctrl, authStub, adminStub;
beforeEach(function() {
// remember original module to restore later, then replace it
origAuth = appController._auth;
appController._auth = authStub = {};
appController._adminDao = adminStub = sinon.createStubInstance(AdminDao);
adminStub = sinon.createStubInstance(AdminDao);
authStub = sinon.createStubInstance(Auth);
angular.module('createaccounttest', []);
mocks.module('createaccounttest');
mocks.inject(function($controller, $rootScope, $location) {
angular.module('createaccounttest', ['woServices', 'woAppConfig']);
angular.mock.module('createaccounttest');
angular.mock.inject(function($controller, $rootScope, $location) {
location = $location;
scope = $rootScope.$new();
scope.state = {};
@ -30,15 +28,14 @@ describe('Create Account Controller unit test', function() {
ctrl = $controller(CreateAccountCtrl, {
$location: location,
$scope: scope,
$routeParams: {}
$routeParams: {},
auth: authStub,
admin: adminStub
});
});
});
afterEach(function() {
// restore the app controller module
appController._auth = origAuth;
location.path.restore();
location.search.restore();
if (scope.$apply.restore) {

View File

@ -1,236 +1,184 @@
'use strict';
var mocks = angular.mock,
LoginCtrl = require('../../src/js/controller/login'),
EmailDAO = require('../../src/js/dao/email-dao'),
Auth = require('../../src/js/bo/auth'),
appController = require('../../src/js/app-controller'),
KeychainDAO = require('../../src/js/dao/keychain-dao');
var LoginCtrl = require('../../../../src/js/controller/login/login'),
Email = require('../../../../src/js/email/email'),
Account = require('../../../../src/js/email/account'),
Dialog = require('../../../../src/js/util/dialog'),
UpdateHandler = require('../../../../src/js/util/update/update-handler'),
Auth = require('../../../../src/js/service/auth'),
Keychain = require('../../../../src/js/service/keychain');
describe('Login Controller unit test', function() {
var scope, location, ctrl,
origEmailDao, emailDaoMock,
origKeychain, keychainMock,
origAuth, authStub,
emailAddress = 'fred@foo.com',
startAppStub,
checkForUpdateStub,
initStub;
describe('initialization', function() {
var hasChrome, hasIdentity;
emailMock, keychainMock, authMock, accountMock, dialogMock, updateHandlerMock, pathStub,
emailAddress = 'fred@foo.com';
beforeEach(function() {
hasChrome = !!window.chrome;
hasIdentity = !!window.chrome.identity;
window.chrome = window.chrome || {};
window.chrome.identity = window.chrome.identity || {};
emailMock = sinon.createStubInstance(Email);
accountMock = sinon.createStubInstance(Account);
authMock = sinon.createStubInstance(Auth);
keychainMock = sinon.createStubInstance(Keychain);
dialogMock = sinon.createStubInstance(Dialog);
updateHandlerMock = sinon.createStubInstance(UpdateHandler);
// remember original module to restore later, then replace it
origEmailDao = appController._emailDao;
origKeychain = appController._keychain;
origAuth = appController._auth;
appController._emailDao = emailDaoMock = sinon.createStubInstance(EmailDAO);
appController._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
appController._auth = authStub = sinon.createStubInstance(Auth);
location = {
path: function() {}
};
pathStub = sinon.stub(location, 'path');
startAppStub = sinon.stub(appController, 'start');
checkForUpdateStub = sinon.stub(appController, 'checkForUpdate');
initStub = sinon.stub(appController, 'init');
authMock.emailAddress = emailAddress;
});
afterEach(function() {
// restore the browser
if (!hasIdentity) {
delete window.chrome.identity;
function createController() {
angular.module('login-test', ['woServices', 'woEmail', 'woUtil']);
angular.mock.module('login-test');
angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {};
scope.form = {};
ctrl = $controller(LoginCtrl, {
$scope: scope,
$location: location,
updateHandler: updateHandlerMock,
account: accountMock,
auth: authMock,
email: emailMock,
keychain: keychainMock,
dialog: dialogMock
});
});
}
if (!hasChrome) {
delete window.chrome;
}
afterEach(function() {});
// restore the app controller module
appController._emailDao = origEmailDao;
appController._keychain = origKeychain;
appController._auth = origAuth;
appController.start.restore && appController.start.restore();
appController.checkForUpdate.restore && appController.checkForUpdate.restore();
appController.init.restore && appController.init.restore();
location.path.restore && location.path.restore();
it('should fail for auth.getEmailAddress', function() {
authMock.getEmailAddress.yields(new Error());
startAppStub.restore();
checkForUpdateStub.restore();
initStub.restore();
createController();
expect(updateHandlerMock.checkForUpdate.calledOnce).to.be.true;
expect(authMock.init.calledOnce).to.be.true;
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should forward directly to desktop for empty passphrase', function(done) {
var testKeys = {
privateKey: 'a',
publicKey: 'b'
};
it('should redirect to /add-account', function() {
authMock.getEmailAddress.yields(null, {});
startAppStub.yields();
authStub.getEmailAddress.yields(null, {
emailAddress: emailAddress,
realname: 'asd'
});
authStub.storeCredentials.yields();
initStub.yields(null, testKeys);
createController();
emailDaoMock.unlock.withArgs({
keypair: testKeys,
passphrase: undefined
}).yields();
angular.module('logintest', []);
mocks.module('logintest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
sinon.stub(location, 'path', function(path) {
expect(path).to.equal('/desktop');
expect(startAppStub.calledOnce).to.be.true;
expect(checkForUpdateStub.calledOnce).to.be.true;
expect(authStub.getEmailAddress.calledOnce).to.be.true;
expect(authStub.storeCredentials.calledOnce).to.be.true;
done();
});
scope = $rootScope.$new();
scope.state = {};
ctrl = $controller(LoginCtrl, {
$location: location,
$scope: scope
});
});
expect(pathStub.withArgs('/add-account').calledOnce).to.be.true;
});
it('should forward to existing user login', function(done) {
var testKeys = {
privateKey: 'a',
publicKey: 'b'
};
it('should fail for auth.init', function() {
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
accountMock.init.yields(new Error());
startAppStub.yields();
authStub.getEmailAddress.yields(null, {
emailAddress: emailAddress,
realname: 'asd'
});
initStub.yields(null, testKeys);
createController();
emailDaoMock.unlock.withArgs({
keypair: testKeys,
passphrase: undefined
}).yields({});
angular.module('logintest', []);
mocks.module('logintest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
sinon.stub(location, 'path', function(path) {
expect(path).to.equal('/login-existing');
expect(startAppStub.calledOnce).to.be.true;
expect(checkForUpdateStub.calledOnce).to.be.true;
expect(authStub.getEmailAddress.calledOnce).to.be.true;
done();
});
scope = $rootScope.$new();
scope.state = {};
ctrl = $controller(LoginCtrl, {
$location: location,
$scope: scope
});
});
expect(accountMock.init.calledOnce).to.be.true;
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should forward to privatekey download login', function(done) {
startAppStub.yields();
authStub.getEmailAddress.yields(null, {
emailAddress: emailAddress,
realname: 'asd'
it('should redirect to /login-existing', function() {
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
initStub.yields(null, {
publicKey: 'b'
accountMock.init.yields(null, {
publicKey: 'publicKey',
privateKey: 'privateKey'
});
keychainMock.requestPrivateKeyDownload.yields(null, {});
emailMock.unlock.yields(new Error());
angular.module('logintest', []);
mocks.module('logintest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
sinon.stub(location, 'path', function(path) {
expect(path).to.equal('/login-privatekey-download');
expect(startAppStub.calledOnce).to.be.true;
expect(checkForUpdateStub.calledOnce).to.be.true;
expect(authStub.getEmailAddress.calledOnce).to.be.true;
expect(keychainMock.requestPrivateKeyDownload.calledOnce).to.be.true;
done();
});
scope = $rootScope.$new();
scope.state = {};
ctrl = $controller(LoginCtrl, {
$location: location,
$scope: scope
});
});
createController();
expect(pathStub.withArgs('/login-existing').calledOnce).to.be.true;
});
it('should forward to new device login', function(done) {
startAppStub.yields();
authStub.getEmailAddress.yields(null, {
emailAddress: emailAddress,
realname: 'asd'
it('should fail for auth.storeCredentials', function() {
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
initStub.yields(null, {
publicKey: 'b'
accountMock.init.yields(null, {
publicKey: 'publicKey',
privateKey: 'privateKey'
});
emailMock.unlock.yields();
authMock.storeCredentials.yields(new Error());
createController();
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should redirect to /desktop', function() {
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
accountMock.init.yields(null, {
publicKey: 'publicKey',
privateKey: 'privateKey'
});
emailMock.unlock.yields();
authMock.storeCredentials.yields();
createController();
expect(pathStub.withArgs('/desktop').calledOnce).to.be.true;
});
it('should fail for keychain.requestPrivateKeyDownload', function() {
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
accountMock.init.yields(null, {
publicKey: 'publicKey'
});
keychainMock.requestPrivateKeyDownload.yields(new Error());
createController();
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should redirect to /login-privatekey-download', function() {
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
accountMock.init.yields(null, {
publicKey: 'publicKey'
});
keychainMock.requestPrivateKeyDownload.yields(null, true);
createController();
expect(pathStub.withArgs('/login-privatekey-download').calledOnce).to.be.true;
});
it('should redirect to /login-new-device', function() {
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
accountMock.init.yields(null, {
publicKey: 'publicKey'
});
keychainMock.requestPrivateKeyDownload.yields();
angular.module('logintest', []);
mocks.module('logintest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
sinon.stub(location, 'path', function(path) {
expect(path).to.equal('/login-new-device');
expect(startAppStub.calledOnce).to.be.true;
expect(checkForUpdateStub.calledOnce).to.be.true;
expect(authStub.getEmailAddress.calledOnce).to.be.true;
expect(keychainMock.requestPrivateKeyDownload.calledOnce).to.be.true;
done();
});
scope = $rootScope.$new();
scope.state = {};
ctrl = $controller(LoginCtrl, {
$location: location,
$scope: scope
});
});
createController();
expect(pathStub.withArgs('/login-new-device').calledOnce).to.be.true;
});
it('should forward to initial login', function(done) {
startAppStub.yields();
authStub.getEmailAddress.yields(null, {
emailAddress: emailAddress,
realname: 'asd'
it('should redirect to /login-initial', function() {
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
initStub.yields();
accountMock.init.yields(null, {});
angular.module('logintest', []);
mocks.module('logintest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
sinon.stub(location, 'path', function(path) {
expect(path).to.equal('/login-initial');
expect(startAppStub.calledOnce).to.be.true;
expect(checkForUpdateStub.calledOnce).to.be.true;
expect(authStub.getEmailAddress.calledOnce).to.be.true;
done();
});
scope = $rootScope.$new();
scope.state = {};
ctrl = $controller(LoginCtrl, {
$location: location,
$scope: scope
});
});
});
createController();
expect(pathStub.withArgs('/login-initial').calledOnce).to.be.true;
});
});

View File

@ -1,53 +1,41 @@
'use strict';
var Auth = require('../../src/js/bo/auth'),
mocks = angular.mock,
LoginExistingCtrl = require('../../src/js/controller/login-existing'),
EmailDAO = require('../../src/js/dao/email-dao'),
KeychainDAO = require('../../src/js/dao/keychain-dao'),
appController = require('../../src/js/app-controller');
var Auth = require('../../../../src/js/service/auth'),
LoginExistingCtrl = require('../../../../src/js/controller/login/login-existing'),
EmailDAO = require('../../../../src/js/email/email'),
KeychainDAO = require('../../../../src/js/service/keychain');
describe('Login (existing user) Controller unit test', function() {
var scope, location, ctrl, origEmailDao, emailDaoMock,
origAuth, authMock,
var scope, location, ctrl, emailDaoMock, authMock,
emailAddress = 'fred@foo.com',
passphrase = 'asd',
keychainMock;
beforeEach(function() {
// remember original module to restore later
origEmailDao = appController._emailDao;
origAuth = appController._auth;
appController._emailDao = emailDaoMock = sinon.createStubInstance(EmailDAO);
appController._auth = authMock = sinon.createStubInstance(Auth);
emailDaoMock = sinon.createStubInstance(EmailDAO);
authMock = sinon.createStubInstance(Auth);
keychainMock = sinon.createStubInstance(KeychainDAO);
emailDaoMock._keychain = keychainMock;
emailDaoMock._account = {
emailAddress: emailAddress,
};
authMock.emailAddress = emailAddress;
angular.module('loginexistingtest', []);
mocks.module('loginexistingtest');
mocks.inject(function($rootScope, $controller, $location) {
angular.module('loginexistingtest', ['woServices']);
angular.mock.module('loginexistingtest');
angular.mock.inject(function($rootScope, $controller, $location) {
location = $location;
scope = $rootScope.$new();
scope.state = {};
scope.form = {};
ctrl = $controller(LoginExistingCtrl, {
$scope: scope,
$routeParams: {}
$routeParams: {},
email: emailDaoMock,
auth: authMock,
keychain: keychainMock
});
});
});
afterEach(function() {
// restore the module
appController._emailDao = origEmailDao;
appController._auth = origAuth;
});
afterEach(function() {});
describe('initial state', function() {
it('should be well defined', function() {

View File

@ -1,39 +1,26 @@
'use strict';
var Auth = require('../../src/js/bo/auth'),
mocks = angular.mock,
LoginInitialCtrl = require('../../src/js/controller/login-initial'),
PGP = require('../../src/js/crypto/pgp'),
EmailDAO = require('../../src/js/dao/email-dao'),
appController = require('../../src/js/app-controller');
var Auth = require('../../../../src/js/service/auth'),
LoginInitialCtrl = require('../../../../src/js/controller/login/login-initial'),
Email = require('../../../../src/js/email/email');
describe('Login (initial user) Controller unit test', function() {
var scope, ctrl, location, origEmailDao, emailDaoMock,
origAuth, authMock, newsletterStub,
var scope, ctrl, location, emailMock, authMock, newsletterStub,
emailAddress = 'fred@foo.com',
keyId, expectedKeyId,
cryptoMock;
keyId, expectedKeyId;
beforeEach(function() {
// remember original module to restore later
origEmailDao = appController._emailDao;
origAuth = appController._auth;
appController._emailDao = emailDaoMock = sinon.createStubInstance(EmailDAO);
appController._auth = authMock = sinon.createStubInstance(Auth);
emailMock = sinon.createStubInstance(Email);
authMock = sinon.createStubInstance(Auth);
keyId = '9FEB47936E712926';
expectedKeyId = '6E712926';
cryptoMock = sinon.createStubInstance(PGP);
emailDaoMock._crypto = cryptoMock;
emailDaoMock._account = {
emailAddress: emailAddress,
};
authMock.emailAddress = emailAddress;
angular.module('logininitialtest', ['woServices']);
mocks.module('logininitialtest');
mocks.inject(function($rootScope, $controller, $location, newsletter) {
angular.mock.module('logininitialtest');
angular.mock.inject(function($rootScope, $controller, $location, newsletter) {
scope = $rootScope.$new();
location = $location;
newsletterStub = sinon.stub(newsletter, 'signup');
@ -43,16 +30,14 @@ describe('Login (initial user) Controller unit test', function() {
ctrl = $controller(LoginInitialCtrl, {
$scope: scope,
$routeParams: {},
newsletter: newsletter
newsletter: newsletter,
email: emailMock,
auth: authMock
});
});
});
afterEach(function() {
// restore the module
appController._emailDao = origEmailDao;
appController._auth = origAuth;
});
afterEach(function() {});
describe('initial state', function() {
it('should be well defined', function() {
@ -92,7 +77,7 @@ describe('Login (initial user) Controller unit test', function() {
it('should fail due to error in emailDao.unlock', function() {
scope.agree = true;
emailDaoMock.unlock.withArgs({
emailMock.unlock.withArgs({
passphrase: undefined
}).yields(new Error('asdf'));
authMock.storeCredentials.yields();
@ -107,7 +92,7 @@ describe('Login (initial user) Controller unit test', function() {
it('should unlock crypto', function() {
scope.agree = true;
emailDaoMock.unlock.withArgs({
emailMock.unlock.withArgs({
passphrase: undefined
}).yields();
authMock.storeCredentials.yields();
@ -118,7 +103,7 @@ describe('Login (initial user) Controller unit test', function() {
expect(scope.state.ui).to.equal(2);
expect(newsletterStub.called).to.be.true;
expect(location.$$path).to.equal('/desktop');
expect(emailDaoMock.unlock.calledOnce).to.be.true;
expect(emailMock.unlock.calledOnce).to.be.true;
});
});
});

View File

@ -1,38 +1,32 @@
'use strict';
var mocks = angular.mock,
PGP = require('../../src/js/crypto/pgp'),
LoginNewDeviceCtrl = require('../../src/js/controller/login-new-device'),
KeychainDAO = require('../../src/js/dao/keychain-dao'),
EmailDAO = require('../../src/js/dao/email-dao'),
appController = require('../../src/js/app-controller');
var PGP = require('../../../../src/js/crypto/pgp'),
LoginNewDeviceCtrl = require('../../../../src/js/controller/login/login-new-device'),
KeychainDAO = require('../../../../src/js/service/keychain'),
EmailDAO = require('../../../../src/js/email/email'),
Auth = require('../../../../src/js/service/auth');
describe('Login (new device) Controller unit test', function() {
var scope, ctrl, origEmailDao, emailDaoMock, pgpMock,
var scope, ctrl, emailMock, pgpMock, authMock,
emailAddress = 'fred@foo.com',
passphrase = 'asd',
keyId,
keychainMock;
beforeEach(function() {
// remember original module to restore later
origEmailDao = appController._emailDao;
emailDaoMock = sinon.createStubInstance(EmailDAO);
appController._emailDao = emailDaoMock;
emailMock = sinon.createStubInstance(EmailDAO);
authMock = sinon.createStubInstance(Auth);
keyId = '9FEB47936E712926';
emailDaoMock._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
appController._pgp = pgpMock = sinon.createStubInstance(PGP);
keychainMock = sinon.createStubInstance(KeychainDAO);
pgpMock = sinon.createStubInstance(PGP);
pgpMock.extractPublicKey.returns('publicKeyArmored');
emailDaoMock._account = {
emailAddress: emailAddress,
};
authMock.emailAddress = emailAddress;
angular.module('loginnewdevicetest', []);
mocks.module('loginnewdevicetest');
mocks.inject(function($rootScope, $controller) {
angular.module('loginnewdevicetest', ['woServices']);
angular.mock.module('loginnewdevicetest');
angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {
ui: {}
@ -40,15 +34,16 @@ describe('Login (new device) Controller unit test', function() {
scope.form = {};
ctrl = $controller(LoginNewDeviceCtrl, {
$scope: scope,
$routeParams: {}
$routeParams: {},
email: emailMock,
auth: authMock,
pgp: pgpMock,
keychain: keychainMock
});
});
});
afterEach(function() {
// restore the module
appController._emailDao = origEmailDao;
});
afterEach(function() {});
describe('initial state', function() {
it('should be well defined', function() {
@ -73,12 +68,12 @@ describe('Login (new device) Controller unit test', function() {
_id: keyId,
publicKey: 'a'
});
emailDaoMock.unlock.withArgs(sinon.match.any, passphrase).yields();
emailMock.unlock.withArgs(sinon.match.any, passphrase).yields();
keychainMock.putUserKeyPair.yields();
scope.confirmPassphrase();
expect(emailDaoMock.unlock.calledOnce).to.be.true;
expect(emailMock.unlock.calledOnce).to.be.true;
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
});
@ -95,12 +90,12 @@ describe('Login (new device) Controller unit test', function() {
});
keychainMock.getUserKeyPair.withArgs(emailAddress).yields();
emailDaoMock.unlock.withArgs(sinon.match.any, passphrase).yields();
emailMock.unlock.withArgs(sinon.match.any, passphrase).yields();
keychainMock.putUserKeyPair.yields();
scope.confirmPassphrase();
expect(emailDaoMock.unlock.calledOnce).to.be.true;
expect(emailMock.unlock.calledOnce).to.be.true;
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
});
@ -119,7 +114,7 @@ describe('Login (new device) Controller unit test', function() {
_id: keyId,
publicKey: 'a'
});
emailDaoMock.unlock.yields();
emailMock.unlock.yields();
keychainMock.putUserKeyPair.yields({
errMsg: 'yo mamma.'
});
@ -127,7 +122,7 @@ describe('Login (new device) Controller unit test', function() {
scope.confirmPassphrase();
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
expect(emailDaoMock.unlock.calledOnce).to.be.true;
expect(emailMock.unlock.calledOnce).to.be.true;
expect(keychainMock.putUserKeyPair.calledOnce).to.be.true;
expect(scope.errMsg).to.equal('yo mamma.');
});
@ -147,7 +142,7 @@ describe('Login (new device) Controller unit test', function() {
_id: keyId,
publicKey: 'a'
});
emailDaoMock.unlock.yields({
emailMock.unlock.yields({
errMsg: 'yo mamma.'
});
@ -155,7 +150,7 @@ describe('Login (new device) Controller unit test', function() {
expect(scope.incorrect).to.be.true;
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
expect(emailDaoMock.unlock.calledOnce).to.be.true;
expect(emailMock.unlock.calledOnce).to.be.true;
expect(scope.errMsg).to.equal('yo mamma.');
});

View File

@ -1,55 +1,43 @@
'use strict';
var mocks = angular.mock,
Auth = require('../../src/js/bo/auth'),
LoginPrivateKeyDownloadCtrl = require('../../src/js/controller/login-privatekey-download'),
EmailDAO = require('../../src/js/dao/email-dao'),
appController = require('../../src/js/app-controller'),
KeychainDAO = require('../../src/js/dao/keychain-dao');
var Auth = require('../../../../src/js/service/auth'),
LoginPrivateKeyDownloadCtrl = require('../../../../src/js/controller/login/login-privatekey-download'),
Email = require('../../../../src/js/email/email'),
Keychain = require('../../../../src/js/service/keychain');
describe('Login Private Key Download Controller unit test', function() {
var scope, location, ctrl,
origEmailDao, emailDaoMock,
origAuth, authMock,
origKeychain, keychainMock,
emailDaoMock, authMock, keychainMock,
emailAddress = 'fred@foo.com';
beforeEach(function(done) {
// remember original module to restore later, then replace it
origEmailDao = appController._emailDao;
origKeychain = appController._keychain;
origAuth = appController._auth;
emailDaoMock = sinon.createStubInstance(Email);
keychainMock = sinon.createStubInstance(Keychain);
authMock = sinon.createStubInstance(Auth);
appController._emailDao = emailDaoMock = sinon.createStubInstance(EmailDAO);
appController._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
appController._auth = authMock = sinon.createStubInstance(Auth);
authMock.emailAddress = emailAddress;
emailDaoMock._account = {
emailAddress: emailAddress
};
angular.module('login-privatekey-download-test', []);
mocks.module('login-privatekey-download-test');
mocks.inject(function($controller, $rootScope) {
angular.module('login-privatekey-download-test', ['woServices']);
angular.mock.module('login-privatekey-download-test');
angular.mock.inject(function($controller, $rootScope, $location) {
scope = $rootScope.$new();
scope.state = {};
scope.tokenForm = {};
scope.codeForm = {};
location = $location;
ctrl = $controller(LoginPrivateKeyDownloadCtrl, {
$location: location,
$scope: scope,
$routeParams: {}
$routeParams: {},
auth: authMock,
email: emailDaoMock,
keychain: keychainMock
});
done();
});
});
afterEach(function() {
// restore the app controller module
appController._emailDao = origEmailDao;
appController._keychain = origKeychain;
appController._auth = origAuth;
});
afterEach(function() {});
describe('initialization', function() {
it('should work', function() {
@ -208,20 +196,9 @@ describe('Login Private Key Download Controller unit test', function() {
});
describe('goTo', function() {
it('should work', function(done) {
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
it('should work', function() {
sinon.stub(location, 'path', function(path) {
expect(path).to.equal('/desktop');
done();
});
scope = $rootScope.$new();
scope.state = {};
ctrl = $controller(LoginPrivateKeyDownloadCtrl, {
$location: location,
$scope: scope,
$routeParams: {}
});
});
scope.goTo('/desktop');

View File

@ -1,32 +1,28 @@
'use strict';
var mocks = angular.mock,
Auth = require('../../src/js/bo/auth'),
ConnectionDoctor = require('../../src/js/util/connection-doctor'),
SetCredentialsCtrl = require('../../src/js/controller/login-set-credentials'),
appController = require('../../src/js/app-controller');
var Auth = require('../../../../src/js/service/auth'),
ConnectionDoctor = require('../../../../src/js/util/connection-doctor'),
SetCredentialsCtrl = require('../../../../src/js/controller/login/login-set-credentials');
describe('Login (Set Credentials) Controller unit test', function() {
// Angular parameters
var scope, location, provider;
// Stubs
var auth, origAuth, doctor, origDoctor;
var auth, doctor;
// SUT
var setCredentialsCtrl;
beforeEach(function() {
// remeber pre-test state to restore later
origAuth = appController._auth;
origDoctor = appController._doctor;
auth = appController._auth = sinon.createStubInstance(Auth);
doctor = appController._doctor = sinon.createStubInstance(ConnectionDoctor);
auth = sinon.createStubInstance(Auth);
doctor = sinon.createStubInstance(ConnectionDoctor);
// setup the controller
angular.module('setcredentialstest', []);
mocks.module('setcredentialstest');
mocks.inject(function($rootScope, $controller, $location) {
angular.mock.module('setcredentialstest');
angular.mock.inject(function($rootScope, $controller, $location) {
scope = $rootScope.$new();
location = $location;
location.search({
@ -43,16 +39,14 @@ describe('Login (Set Credentials) Controller unit test', function() {
setCredentialsCtrl = $controller(SetCredentialsCtrl, {
$scope: scope,
$routeParams: {}
$routeParams: {},
auth: auth,
connectionDoctor: doctor
});
});
});
afterEach(function() {
// restore pre-test state
appController._auth = origAuth;
appController._doctor = origDoctor;
});
afterEach(function() {});
describe('set credentials', function() {
it('should work', function() {

View File

@ -1,23 +1,19 @@
'use strict';
var mocks = angular.mock,
ValidatePhoneCtrl = require('../../src/js/controller/validate-phone'),
Auth = require('../../src/js/bo/auth'),
AdminDao = require('../../src/js/dao/admin-dao'),
appController = require('../../src/js/app-controller');
var ValidatePhoneCtrl = require('../../../../src/js/controller/login/validate-phone'),
Auth = require('../../../../src/js/service/auth'),
AdminDao = require('../../../../src/js/service/admin');
describe('Validate Phone Controller unit test', function() {
var scope, location, mailConfigMock, ctrl, authStub, origAuth, adminStub;
var scope, location, mailConfigMock, ctrl, authStub, adminStub;
beforeEach(function() {
// remember original module to restore later, then replace it
origAuth = appController._auth;
appController._auth = authStub = sinon.createStubInstance(Auth);
appController._adminDao = adminStub = sinon.createStubInstance(AdminDao);
authStub = sinon.createStubInstance(Auth);
adminStub = sinon.createStubInstance(AdminDao);
angular.module('validatephonetest', ['woServices']);
mocks.module('validatephonetest');
mocks.inject(function($controller, $rootScope, $location, mailConfig) {
angular.mock.module('validatephonetest');
angular.mock.inject(function($controller, $rootScope, $location, mailConfig) {
location = $location;
mailConfigMock = mailConfig;
scope = $rootScope.$new();
@ -38,15 +34,14 @@ describe('Validate Phone Controller unit test', function() {
$location: location,
$scope: scope,
$routeParams: {},
mailConfig: mailConfigMock
mailConfig: mailConfigMock,
auth: authStub,
admin: adminStub
});
});
});
afterEach(function() {
// restore the app controller module
appController._auth = origAuth;
location.path.restore();
location.search.restore();
if (scope.$apply.restore) {