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/outbox-bo-test.js',
'test/unit/email/email-dao-test.js', 'test/unit/email/email-dao-test.js',
'test/unit/email/account-test.js', 'test/unit/email/account-test.js',
/*'test/unit/dialog-ctrl-test.js', 'test/unit/controller/app/dialog-ctrl-test.js',
'test/unit/add-account-ctrl-test.js', 'test/unit/controller/login/add-account-ctrl-test.js',
'test/unit/create-account-ctrl-test.js', 'test/unit/controller/login/create-account-ctrl-test.js',
'test/unit/validate-phone-ctrl-test.js', 'test/unit/controller/login/validate-phone-ctrl-test.js',
'test/unit/account-ctrl-test.js', 'test/unit/controller/login/login-existing-ctrl-test.js',
'test/unit/set-passphrase-ctrl-test.js', 'test/unit/controller/login/login-initial-ctrl-test.js',
'test/unit/contacts-ctrl-test.js', 'test/unit/controller/login/login-new-device-ctrl-test.js',
'test/unit/login-existing-ctrl-test.js', 'test/unit/controller/login/login-privatekey-download-ctrl-test.js',
'test/unit/login-initial-ctrl-test.js', 'test/unit/controller/login/login-set-credentials-ctrl-test.js',
'test/unit/login-new-device-ctrl-test.js', 'test/unit/controller/login/login-ctrl-test.js',
'test/unit/login-privatekey-download-ctrl-test.js', 'test/unit/controller/app/privatekey-upload-ctrl-test.js',
'test/unit/login-set-credentials-ctrl-test.js', 'test/unit/controller/app/account-ctrl-test.js',
'test/unit/privatekey-upload-ctrl-test.js', 'test/unit/controller/app/set-passphrase-ctrl-test.js',
'test/unit/login-ctrl-test.js', 'test/unit/controller/app/contacts-ctrl-test.js',
'test/unit/read-ctrl-test.js', 'test/unit/controller/app/read-ctrl-test.js',
'test/unit/navigation-ctrl-test.js', 'test/unit/controller/app/navigation-ctrl-test.js',
'test/unit/mail-list-ctrl-test.js', /*'test/unit/mail-list-ctrl-test.js',
'test/unit/write-ctrl-test.js', 'test/unit/write-ctrl-test.js',
'test/unit/action-bar-ctrl-test.js',*/ 'test/unit/action-bar-ctrl-test.js',*/
] ]

View File

@ -7,7 +7,7 @@
</header> </header>
<div class="lightbox__content"> <div class="lightbox__content">
<p class="typo-paragraph">{{message}} <a ng-show="faqLink" href="{{faqLink}}" target="_blank">Learn more</a></p> <p class="typo-paragraph">{{message}}<a ng-show="faqLink" href="{{faqLink}}" target="_blank">Learn more</a></p>
</div> </div>
<footer class="lightbox__controls"> <footer class="lightbox__controls">

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,39 +1,26 @@
'use strict'; 'use strict';
var Auth = require('../../src/js/bo/auth'), var Auth = require('../../../../src/js/service/auth'),
mocks = angular.mock, LoginInitialCtrl = require('../../../../src/js/controller/login/login-initial'),
LoginInitialCtrl = require('../../src/js/controller/login-initial'), Email = require('../../../../src/js/email/email');
PGP = require('../../src/js/crypto/pgp'),
EmailDAO = require('../../src/js/dao/email-dao'),
appController = require('../../src/js/app-controller');
describe('Login (initial user) Controller unit test', function() { describe('Login (initial user) Controller unit test', function() {
var scope, ctrl, location, origEmailDao, emailDaoMock, var scope, ctrl, location, emailMock, authMock, newsletterStub,
origAuth, authMock, newsletterStub,
emailAddress = 'fred@foo.com', emailAddress = 'fred@foo.com',
keyId, expectedKeyId, keyId, expectedKeyId;
cryptoMock;
beforeEach(function() { beforeEach(function() {
// remember original module to restore later emailMock = sinon.createStubInstance(Email);
origEmailDao = appController._emailDao; authMock = sinon.createStubInstance(Auth);
origAuth = appController._auth;
appController._emailDao = emailDaoMock = sinon.createStubInstance(EmailDAO);
appController._auth = authMock = sinon.createStubInstance(Auth);
keyId = '9FEB47936E712926'; keyId = '9FEB47936E712926';
expectedKeyId = '6E712926'; expectedKeyId = '6E712926';
cryptoMock = sinon.createStubInstance(PGP);
emailDaoMock._crypto = cryptoMock;
emailDaoMock._account = { authMock.emailAddress = emailAddress;
emailAddress: emailAddress,
};
angular.module('logininitialtest', ['woServices']); angular.module('logininitialtest', ['woServices']);
mocks.module('logininitialtest'); angular.mock.module('logininitialtest');
mocks.inject(function($rootScope, $controller, $location, newsletter) { angular.mock.inject(function($rootScope, $controller, $location, newsletter) {
scope = $rootScope.$new(); scope = $rootScope.$new();
location = $location; location = $location;
newsletterStub = sinon.stub(newsletter, 'signup'); newsletterStub = sinon.stub(newsletter, 'signup');
@ -43,16 +30,14 @@ describe('Login (initial user) Controller unit test', function() {
ctrl = $controller(LoginInitialCtrl, { ctrl = $controller(LoginInitialCtrl, {
$scope: scope, $scope: scope,
$routeParams: {}, $routeParams: {},
newsletter: newsletter newsletter: newsletter,
email: emailMock,
auth: authMock
}); });
}); });
}); });
afterEach(function() { afterEach(function() {});
// restore the module
appController._emailDao = origEmailDao;
appController._auth = origAuth;
});
describe('initial state', function() { describe('initial state', function() {
it('should be well defined', 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() { it('should fail due to error in emailDao.unlock', function() {
scope.agree = true; scope.agree = true;
emailDaoMock.unlock.withArgs({ emailMock.unlock.withArgs({
passphrase: undefined passphrase: undefined
}).yields(new Error('asdf')); }).yields(new Error('asdf'));
authMock.storeCredentials.yields(); authMock.storeCredentials.yields();
@ -107,7 +92,7 @@ describe('Login (initial user) Controller unit test', function() {
it('should unlock crypto', function() { it('should unlock crypto', function() {
scope.agree = true; scope.agree = true;
emailDaoMock.unlock.withArgs({ emailMock.unlock.withArgs({
passphrase: undefined passphrase: undefined
}).yields(); }).yields();
authMock.storeCredentials.yields(); authMock.storeCredentials.yields();
@ -118,7 +103,7 @@ describe('Login (initial user) Controller unit test', function() {
expect(scope.state.ui).to.equal(2); expect(scope.state.ui).to.equal(2);
expect(newsletterStub.called).to.be.true; expect(newsletterStub.called).to.be.true;
expect(location.$$path).to.equal('/desktop'); 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'; 'use strict';
var mocks = angular.mock, var PGP = require('../../../../src/js/crypto/pgp'),
PGP = require('../../src/js/crypto/pgp'), LoginNewDeviceCtrl = require('../../../../src/js/controller/login/login-new-device'),
LoginNewDeviceCtrl = require('../../src/js/controller/login-new-device'), KeychainDAO = require('../../../../src/js/service/keychain'),
KeychainDAO = require('../../src/js/dao/keychain-dao'), EmailDAO = require('../../../../src/js/email/email'),
EmailDAO = require('../../src/js/dao/email-dao'), Auth = require('../../../../src/js/service/auth');
appController = require('../../src/js/app-controller');
describe('Login (new device) Controller unit test', function() { describe('Login (new device) Controller unit test', function() {
var scope, ctrl, origEmailDao, emailDaoMock, pgpMock, var scope, ctrl, emailMock, pgpMock, authMock,
emailAddress = 'fred@foo.com', emailAddress = 'fred@foo.com',
passphrase = 'asd', passphrase = 'asd',
keyId, keyId,
keychainMock; keychainMock;
beforeEach(function() { beforeEach(function() {
// remember original module to restore later emailMock = sinon.createStubInstance(EmailDAO);
origEmailDao = appController._emailDao; authMock = sinon.createStubInstance(Auth);
emailDaoMock = sinon.createStubInstance(EmailDAO);
appController._emailDao = emailDaoMock;
keyId = '9FEB47936E712926'; keyId = '9FEB47936E712926';
emailDaoMock._keychain = keychainMock = sinon.createStubInstance(KeychainDAO); keychainMock = sinon.createStubInstance(KeychainDAO);
appController._pgp = pgpMock = sinon.createStubInstance(PGP); pgpMock = sinon.createStubInstance(PGP);
pgpMock.extractPublicKey.returns('publicKeyArmored'); pgpMock.extractPublicKey.returns('publicKeyArmored');
emailDaoMock._account = { authMock.emailAddress = emailAddress;
emailAddress: emailAddress,
};
angular.module('loginnewdevicetest', []); angular.module('loginnewdevicetest', ['woServices']);
mocks.module('loginnewdevicetest'); angular.mock.module('loginnewdevicetest');
mocks.inject(function($rootScope, $controller) { angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new(); scope = $rootScope.$new();
scope.state = { scope.state = {
ui: {} ui: {}
@ -40,15 +34,16 @@ describe('Login (new device) Controller unit test', function() {
scope.form = {}; scope.form = {};
ctrl = $controller(LoginNewDeviceCtrl, { ctrl = $controller(LoginNewDeviceCtrl, {
$scope: scope, $scope: scope,
$routeParams: {} $routeParams: {},
email: emailMock,
auth: authMock,
pgp: pgpMock,
keychain: keychainMock
}); });
}); });
}); });
afterEach(function() { afterEach(function() {});
// restore the module
appController._emailDao = origEmailDao;
});
describe('initial state', function() { describe('initial state', function() {
it('should be well defined', function() { it('should be well defined', function() {
@ -73,12 +68,12 @@ describe('Login (new device) Controller unit test', function() {
_id: keyId, _id: keyId,
publicKey: 'a' publicKey: 'a'
}); });
emailDaoMock.unlock.withArgs(sinon.match.any, passphrase).yields(); emailMock.unlock.withArgs(sinon.match.any, passphrase).yields();
keychainMock.putUserKeyPair.yields(); keychainMock.putUserKeyPair.yields();
scope.confirmPassphrase(); scope.confirmPassphrase();
expect(emailDaoMock.unlock.calledOnce).to.be.true; expect(emailMock.unlock.calledOnce).to.be.true;
expect(keychainMock.getUserKeyPair.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(); keychainMock.getUserKeyPair.withArgs(emailAddress).yields();
emailDaoMock.unlock.withArgs(sinon.match.any, passphrase).yields(); emailMock.unlock.withArgs(sinon.match.any, passphrase).yields();
keychainMock.putUserKeyPair.yields(); keychainMock.putUserKeyPair.yields();
scope.confirmPassphrase(); scope.confirmPassphrase();
expect(emailDaoMock.unlock.calledOnce).to.be.true; expect(emailMock.unlock.calledOnce).to.be.true;
expect(keychainMock.getUserKeyPair.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, _id: keyId,
publicKey: 'a' publicKey: 'a'
}); });
emailDaoMock.unlock.yields(); emailMock.unlock.yields();
keychainMock.putUserKeyPair.yields({ keychainMock.putUserKeyPair.yields({
errMsg: 'yo mamma.' errMsg: 'yo mamma.'
}); });
@ -127,7 +122,7 @@ describe('Login (new device) Controller unit test', function() {
scope.confirmPassphrase(); scope.confirmPassphrase();
expect(keychainMock.getUserKeyPair.calledOnce).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(keychainMock.putUserKeyPair.calledOnce).to.be.true; expect(keychainMock.putUserKeyPair.calledOnce).to.be.true;
expect(scope.errMsg).to.equal('yo mamma.'); expect(scope.errMsg).to.equal('yo mamma.');
}); });
@ -147,7 +142,7 @@ describe('Login (new device) Controller unit test', function() {
_id: keyId, _id: keyId,
publicKey: 'a' publicKey: 'a'
}); });
emailDaoMock.unlock.yields({ emailMock.unlock.yields({
errMsg: 'yo mamma.' errMsg: 'yo mamma.'
}); });
@ -155,7 +150,7 @@ describe('Login (new device) Controller unit test', function() {
expect(scope.incorrect).to.be.true; expect(scope.incorrect).to.be.true;
expect(keychainMock.getUserKeyPair.calledOnce).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.'); expect(scope.errMsg).to.equal('yo mamma.');
}); });

View File

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

View File

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

View File

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