Skip to content
This repository has been archived by the owner on Jul 10, 2019. It is now read-only.

Commit

Permalink
Begin conrtoller unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tankred Hase committed Dec 18, 2014
1 parent eab0704 commit 6beddf4
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 231 deletions.
4 changes: 2 additions & 2 deletions src/js/controller/login/add-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var AddAccountCtrl = function($scope, $location, $routeParams, $timeout, $q, mai
callback: function(granted) {
if (granted) {
// query oauth token
getOAuthToken();
return getOAuthToken();
} else {
// use normal user/password login
$scope.setCredentials();
Expand All @@ -61,7 +61,7 @@ var AddAccountCtrl = function($scope, $location, $routeParams, $timeout, $q, mai

function getOAuthToken() {
// fetches the email address from the chrome identity api
auth.getOAuthToken().then(function() {
return auth.getOAuthToken().then(function() {
// continue to setting credentials
return $scope.setCredentials();

Expand Down
69 changes: 43 additions & 26 deletions src/js/controller/login/login.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
'use strict';

var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, auth, email, keychain, dialog) {
var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, auth, email, keychain, dialog, appConfig) {

// check for app update
updateHandler.checkForUpdate();
//
// Scope functions
//

// initialize the user account
auth.init().then(function() {
// get email address
return auth.getEmailAddress();
$scope.init = function() {
// initialize the user account
return auth.init().then(function() {
// get email address
return auth.getEmailAddress();

}).then(function(info) {
// check if account needs to be selected
if (!info.emailAddress) {
return $scope.goTo('/add-account');
}
}).then(function(info) {
// check if account needs to be selected
if (!info.emailAddress) {
return $scope.goTo('/add-account');
}

// initiate the account by initializing the email dao and user storage
return account.init({
emailAddress: info.emailAddress,
realname: info.realname
}).then(function(availableKeys) {
return redirect(availableKeys);
});
// initiate the account by initializing the email dao and user storage
return account.init({
emailAddress: info.emailAddress,
realname: info.realname
}).then(function(availableKeys) {
return redirect(availableKeys);
});

}).catch(dialog.error);
}).catch(dialog.error);
};

function redirect(availableKeys) {
if (availableKeys && availableKeys.publicKey && availableKeys.privateKey) {
// public and private key available, try empty passphrase
return email.unlock({
keypair: availableKeys,
passphrase: undefined
}).then(function() {
// no passphrase set... go to main screen
return auth.storeCredentials().then(function() {
return $scope.goTo('/account');
});

}).catch(function() {
// passphrase set... ask for passphrase
return $scope.goTo('/login-existing');

}).then(function() {
// no passphrase set... go to main screen
return auth.storeCredentials();

}).then(function() {
return $scope.goTo('/account');
});

} else if (availableKeys && availableKeys.publicKey && !availableKeys.privateKey) {
Expand Down Expand Up @@ -69,6 +73,19 @@ var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, au
$location.path(location);
});
};

//
// Start the app
//

// check for app update
updateHandler.checkForUpdate();

// init the app
if (!appConfig.preventAutoStart) {
$scope.init();
}

};

module.exports = LoginCtrl;
4 changes: 4 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ require('../src/js/email');
// Global mocks
//

window.qMock = function(res, rej) {
return new Promise(res, rej);
};

window.resolves = function(val) {
return new Promise(function(res) {
res(val);
Expand Down
33 changes: 19 additions & 14 deletions test/unit/controller/login/add-account-ctrl-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,17 @@ describe('Add Account Controller unit test', function() {
setCredentialsStub.restore();
});

it('should use oauth', function() {
it('should use oauth', function(done) {
dialogStub.confirm = function(options) {
options.callback(true);
options.callback(true).then(function() {
expect(setCredentialsStub.calledOnce).to.be.true;
expect(authStub.getOAuthToken.calledOnce).to.be.true;
done();
});
};
authStub.getOAuthToken.yields();
authStub.getOAuthToken.returns(resolves());

scope.oauthPossible();

expect(setCredentialsStub.calledOnce).to.be.true;
expect(authStub.getOAuthToken.calledOnce).to.be.true;
});

it('should not use oauth', function() {
Expand All @@ -139,25 +140,29 @@ describe('Add Account Controller unit test', function() {
expect(authStub.getOAuthToken.called).to.be.false;
});

it('should not forward to login when oauth fails', function() {
it('should not forward to login when oauth fails', function(done) {
dialogStub.error = function(err) {
expect(err).to.exist;
expect(setCredentialsStub.called).to.be.false;
done();
};

dialogStub.confirm = function(options) {
options.callback(true);
};
authStub.getOAuthToken.yields(new Error());
authStub.getOAuthToken.returns(rejects(new Error()));

scope.oauthPossible();

expect(dialogStub.error.calledOnce).to.be.true;
expect(setCredentialsStub.called).to.be.false;
});
});

describe('setCredentials', function() {
it('should work', function() {
scope.setCredentials();
it('should work', inject(function($timeout) {
scope.setCredentials().then();
$timeout.flush();

expect(location.path.calledWith('/login-set-credentials')).to.be.true;
});
}));
});

});
15 changes: 7 additions & 8 deletions test/unit/controller/login/create-account-ctrl-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('Create Account Controller unit test', function() {
$location: location,
$scope: scope,
$routeParams: {},
$q: window.qMock,
auth: authStub,
admin: adminStub
});
Expand All @@ -54,33 +55,31 @@ describe('Create Account Controller unit test', function() {
scope.form.$invalid = false;
scope.betaCode = 'asfd';
scope.phone = '12345';
adminStub.createUser.yieldsAsync(new Error('asdf'));
adminStub.createUser.returns(rejects(new Error('asdf')));

scope.$apply = function() {
scope.createWhiteoutAccount().then(function() {
expect(scope.busy).to.be.false;
expect(scope.errMsg).to.equal('asdf');
expect(adminStub.createUser.calledOnce).to.be.true;
done();
};
});

scope.createWhiteoutAccount();
expect(scope.busy).to.be.true;
});

it('should work', function(done) {
scope.form.$invalid = false;
scope.betaCode = 'asfd';
scope.phone = '12345';
adminStub.createUser.yieldsAsync();
adminStub.createUser.returns(resolves());

scope.$apply = function() {
scope.createWhiteoutAccount().then(function() {
expect(scope.busy).to.be.false;
expect(scope.errMsg).to.be.undefined;
expect(adminStub.createUser.calledOnce).to.be.true;
done();
};
});

scope.createWhiteoutAccount();
expect(scope.busy).to.be.true;
});
});
Expand Down
Loading

0 comments on commit 6beddf4

Please sign in to comment.