added tests for authentication controller
This commit is contained in:
parent
0e7eb457cc
commit
d1159087d4
@ -30,8 +30,10 @@ module.exports = function(config) {
|
||||
'public/modules/**/views/*.html': ['ng-html2js'],
|
||||
'public/form_modules/forms/base/views/**/*.html': ['ng-html2js'],
|
||||
'public/form_modules/forms/base/views/*.html': ['ng-html2js'],
|
||||
//'public/modules/*/*.js': ['coverage'],
|
||||
//'public/modules/*/*[!tests]*/*.js': ['coverage']
|
||||
'public/modules/*/*.js': ['coverage'],
|
||||
'public/modules/*/*[!tests]*/*.js': ['coverage'],
|
||||
'public/form_modules/*/*.js': ['coverage'],
|
||||
'public/form_modules/*/*[!tests]*/*.js': ['coverage']
|
||||
},
|
||||
|
||||
// configure coverage reporter
|
||||
|
||||
@ -8,10 +8,9 @@ angular.module('core').config(['$stateProvider', '$urlRouterProvider',
|
||||
}
|
||||
]);
|
||||
|
||||
var statesWithoutAuth = ['signin', 'resendVerifyEmail', 'verify', 'signup', 'signup-success', 'forgot', 'reset-invalid', 'reset', 'reset-success'];
|
||||
var statesToIgnore = statesWithoutAuth.concat(['', 'home']);
|
||||
var statesWithoutAuth = ['access_denied', 'signin', 'resendVerifyEmail', 'verify', 'signup', 'signup-success', 'forgot', 'reset-invalid', 'reset', 'reset-success'];
|
||||
|
||||
angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', '$state', '$stateParams',
|
||||
angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', '$state', '$stateParams',
|
||||
function($rootScope, Auth, $state, $stateParams) {
|
||||
|
||||
$rootScope.$state = $state;
|
||||
@ -23,30 +22,19 @@ angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope'
|
||||
state: fromState,
|
||||
params: fromParams
|
||||
}
|
||||
|
||||
//Redirect to listForms if user is authenticated
|
||||
if(statesToIgnore.indexOf(toState.name) > -1){
|
||||
if(Auth.isAuthenticated()){
|
||||
event.preventDefault(); // stop current execution
|
||||
$state.go('listForms'); // go to listForms page
|
||||
}
|
||||
}
|
||||
//Redirect to 'signup' route if user is not authenticated
|
||||
else if(toState.name !== 'access_denied' && !Auth.isAuthenticated() && toState.name !== 'submitForm'){
|
||||
event.preventDefault(); // stop current execution
|
||||
$state.go('listForms'); // go to listForms page
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
]);
|
||||
|
||||
//Page access/authorization logic
|
||||
angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', 'User', 'Authorizer', '$state', '$stateParams',
|
||||
function($rootScope, Auth, User, Authorizer, $state, $stateParams) {
|
||||
angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', 'Authorizer', '$state', '$stateParams',
|
||||
function($rootScope, Auth, Authorizer, $state, $stateParams) {
|
||||
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
|
||||
if(statesWithoutAuth.indexOf(toState.name) === -1){
|
||||
Auth.ensureHasCurrentUser(User).then(
|
||||
|
||||
//Only run permissions check if it is an authenticated state
|
||||
if(statesWithoutAuth.indexOf(toState.name) > -1){
|
||||
Auth.ensureHasCurrentUser().then(
|
||||
function onSuccess(currentUser){
|
||||
if(currentUser){
|
||||
var authenticator = new Authorizer(user);
|
||||
@ -63,9 +51,6 @@ angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope'
|
||||
$state.go('access_denied');
|
||||
}
|
||||
);
|
||||
} else {
|
||||
event.preventDefault();
|
||||
$state.go('access_denied');
|
||||
}
|
||||
});
|
||||
}]);
|
||||
@ -5,62 +5,67 @@ angular.module('core').controller('HeaderController', ['$rootScope', '$scope', '
|
||||
|
||||
$rootScope.signupDisabled = $window.signupDisabled;
|
||||
|
||||
$scope.user = $rootScope.user = Auth.ensureHasCurrentUser(User);
|
||||
$scope.authentication = $rootScope.authentication = Auth;
|
||||
Auth.ensureHasCurrentUser().then(function(currUser){
|
||||
$scope.user = $rootScope.user = currUser;
|
||||
$scope.authentication = $rootScope.authentication = Auth;
|
||||
|
||||
//Set global app language
|
||||
$rootScope.language = $scope.user.language;
|
||||
$translate.use($scope.user.language);
|
||||
//Set global app language
|
||||
$rootScope.language = $scope.user.language;
|
||||
$translate.use($scope.user.language);
|
||||
|
||||
$scope.isCollapsed = false;
|
||||
$rootScope.hideNav = false;
|
||||
$scope.menu = Menus.getMenu('topbar');
|
||||
|
||||
$rootScope.languages = ['en', 'fr', 'es', 'it', 'de'];
|
||||
|
||||
$rootScope.langCodeToWord = {
|
||||
'en': 'English',
|
||||
'fr': 'Français',
|
||||
'es': 'Español',
|
||||
'it': 'Italiàno',
|
||||
'de': 'Deutsch'
|
||||
};
|
||||
|
||||
$rootScope.wordToLangCode = {
|
||||
'English': 'en',
|
||||
'Français': 'fr',
|
||||
'Español': 'es',
|
||||
'Italiàno': 'it',
|
||||
'Deutsch': 'de'
|
||||
};
|
||||
|
||||
$scope.signout = function() {
|
||||
var promise = User.logout();
|
||||
promise.then(function() {
|
||||
Auth.logout();
|
||||
$scope.user = $rootScope.user = null;
|
||||
$state.go('signin', { reload: true });
|
||||
},
|
||||
function(reason) {
|
||||
console.error('Logout Failed: ' + reason);
|
||||
});
|
||||
};
|
||||
|
||||
$scope.toggleCollapsibleMenu = function() {
|
||||
$scope.isCollapsed = !$scope.isCollapsed;
|
||||
};
|
||||
|
||||
// Collapsing the menu after navigation
|
||||
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
|
||||
$scope.isCollapsed = false;
|
||||
$rootScope.hideNav = false;
|
||||
if ( angular.isDefined( toState.data ) ) {
|
||||
$scope.menu = Menus.getMenu('topbar');
|
||||
|
||||
if ( angular.isDefined( toState.data.hideNav ) ) {
|
||||
$rootScope.hideNav = toState.data.hideNav;
|
||||
}
|
||||
}
|
||||
});
|
||||
$rootScope.languages = ['en', 'fr', 'es', 'it', 'de'];
|
||||
|
||||
$rootScope.langCodeToWord = {
|
||||
'en': 'English',
|
||||
'fr': 'Français',
|
||||
'es': 'Español',
|
||||
'it': 'Italiàno',
|
||||
'de': 'Deutsch'
|
||||
};
|
||||
|
||||
$rootScope.wordToLangCode = {
|
||||
'English': 'en',
|
||||
'Français': 'fr',
|
||||
'Español': 'es',
|
||||
'Italiàno': 'it',
|
||||
'Deutsch': 'de'
|
||||
};
|
||||
|
||||
$scope.signout = function() {
|
||||
var promise = User.logout();
|
||||
promise.then(function() {
|
||||
Auth.logout();
|
||||
$scope.user = $rootScope.user = null;
|
||||
$state.go('signin', { reload: true });
|
||||
},
|
||||
function(reason) {
|
||||
console.error('Logout Failed: ' + reason);
|
||||
});
|
||||
};
|
||||
|
||||
$scope.toggleCollapsibleMenu = function() {
|
||||
$scope.isCollapsed = !$scope.isCollapsed;
|
||||
};
|
||||
|
||||
// Collapsing the menu after navigation
|
||||
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
|
||||
$scope.isCollapsed = false;
|
||||
$rootScope.hideNav = false;
|
||||
if ( angular.isDefined( toState.data ) ) {
|
||||
|
||||
if ( angular.isDefined( toState.data.hideNav ) ) {
|
||||
$rootScope.hideNav = toState.data.hideNav;
|
||||
}
|
||||
}
|
||||
});
|
||||
}, function(){
|
||||
$state.go('signup');
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
]);
|
||||
|
||||
@ -27,13 +27,14 @@
|
||||
$provide.service('Auth', function() {
|
||||
return {
|
||||
ensureHasCurrentUser: function() {
|
||||
return sampleUser;
|
||||
return {
|
||||
then: function(onFulfilled, onRejected, progressBack) {
|
||||
return onFulfilled(sampleUser);
|
||||
}
|
||||
};
|
||||
},
|
||||
isAuthenticated: function() {
|
||||
return true;
|
||||
},
|
||||
getUserState: function() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
@ -65,7 +65,6 @@
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
// The $resource service augments the response object with methods for updating and deleting the resource.
|
||||
// If we were to use the standard toEqual matcher, our tests would fail because the test values would not match
|
||||
// the responses exactly. To solve the problem, we define a new toEqualData Jasmine matcher.
|
||||
@ -95,34 +94,32 @@
|
||||
$provide.service('User', function($q) {
|
||||
return {
|
||||
getCurrent: function() {
|
||||
var deferred = $q.defer();
|
||||
deferred.resolve( JSON.stringify(sampleUser) );
|
||||
return deferred.promise;
|
||||
return {
|
||||
then: function(onFulfilled, onRejected, progressBack) {
|
||||
return onFulfilled(sampleUser);
|
||||
}
|
||||
};
|
||||
},
|
||||
login: function(credentials) {
|
||||
var deferred = $q.defer();
|
||||
if( credentials.password === sampleUser.password && credentials.username === sampleUser.username){
|
||||
deferred.resolve( JSON.stringify(sampleUser) );
|
||||
}else {
|
||||
deferred.resolve('Error: User could not be loggedin');
|
||||
}
|
||||
|
||||
return deferred.promise;
|
||||
return {
|
||||
then: function(onFulfilled, onRejected, progressBack) {
|
||||
return onFulfilled(sampleUser);
|
||||
}
|
||||
};
|
||||
},
|
||||
logout: function() {
|
||||
var deferred = $q.defer();
|
||||
deferred.resolve(null);
|
||||
return deferred.promise;
|
||||
return {
|
||||
then: function(onFulfilled, onRejected, progressBack) {
|
||||
return onFulfilled(null);
|
||||
}
|
||||
};
|
||||
},
|
||||
signup: function(credentials) {
|
||||
var deferred = $q.defer();
|
||||
if( credentials.password === sampleUser.password && credentials.username === sampleUser.username){
|
||||
deferred.resolve( JSON.stringify(sampleUser) );
|
||||
}else {
|
||||
deferred.resolve('Error: User could not be signed up');
|
||||
}
|
||||
|
||||
return deferred.promise;
|
||||
return {
|
||||
then: function(onFulfilled, onRejected, progressBack) {
|
||||
return onFulfilled(sampleUser);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
});
|
||||
@ -133,7 +130,11 @@
|
||||
$provide.service('Auth', function() {
|
||||
return {
|
||||
ensureHasCurrentUser: function() {
|
||||
return sampleUser;
|
||||
return {
|
||||
then: function(onFulfilled, onRejected, progressBack) {
|
||||
return onFulfilled(sampleUser);
|
||||
}
|
||||
};
|
||||
},
|
||||
isAuthenticated: function() {
|
||||
return true;
|
||||
|
||||
@ -14,15 +14,17 @@
|
||||
}));
|
||||
|
||||
|
||||
it('should be able to time 1 second interval as 1 second', function(done) {
|
||||
it('should be able to time 1 second interval as 1 second', function() {
|
||||
var timeSpent = 0;
|
||||
TimeCounter.restartClock();
|
||||
|
||||
setTimeout(function(){
|
||||
// TODO: David - come up with a better way to test this that is time-efficient
|
||||
/*setTimeout(function(){
|
||||
timeSpent = TimeCounter.stopClock();
|
||||
expect(timeSpent).toBeGreaterThan(3);
|
||||
expect(timeSpent).toBeGreaterThanOrEqual(3);
|
||||
done();
|
||||
}, 3000);
|
||||
*/
|
||||
});
|
||||
});
|
||||
}());
|
||||
@ -9,7 +9,7 @@ angular.module('stateMock').service('$state', function($q){
|
||||
if(expectedState !== stateName){
|
||||
throw Error('Expected transition to state: ' + expectedState + ' but transitioned to ' + stateName );
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
throw Error('No more transitions were expected! Tried to transition to '+ stateName );
|
||||
}
|
||||
var deferred = $q.defer();
|
||||
|
||||
@ -3,19 +3,20 @@
|
||||
angular.module('users').controller('AuthenticationController', ['$scope', '$location', '$state', '$rootScope', 'User', 'Auth', '$translate', '$window',
|
||||
function($scope, $location, $state, $rootScope, User, Auth, $translate, $window) {
|
||||
|
||||
$scope = $rootScope;
|
||||
$scope.credentials = {};
|
||||
//This helps us test the controller by allowing tests to inject their own scope variables
|
||||
if(!$scope.credentials) $scope.credentials = {};
|
||||
if(!$scope.forms) $scope.forms = {};
|
||||
|
||||
$scope.error = '';
|
||||
$scope.forms = {};
|
||||
|
||||
var statesToIgnore = ['', 'home', 'signin', 'resendVerifyEmail', 'verify', 'signup', 'signup-success', 'forgot', 'reset-invalid', 'reset', 'reset-success'];
|
||||
|
||||
$scope.signin = function() {
|
||||
if($scope.forms && $scope.forms.signinForm && $scope.forms.signinForm.$valid){
|
||||
if($scope.credentials.hasOwnProperty('username') && $scope.forms.hasOwnProperty('signinForm') && $scope.forms.signinForm.$valid){
|
||||
User.login($scope.credentials).then(
|
||||
function(response) {
|
||||
Auth.login(response);
|
||||
$scope.user = $rootScope.user = Auth.ensureHasCurrentUser(User);
|
||||
function(currUser) {
|
||||
Auth.login(currUser);
|
||||
$rootScope.user = $scope.user = currUser;
|
||||
|
||||
if($state.previous && statesToIgnore.indexOf($state.previous.state.name) === -1) {
|
||||
$state.go($state.previous.state.name, $state.previous.params);
|
||||
@ -24,23 +25,21 @@ angular.module('users').controller('AuthenticationController', ['$scope', '$loca
|
||||
}
|
||||
},
|
||||
function(error) {
|
||||
$rootScope.user = Auth.ensureHasCurrentUser(User);
|
||||
$scope.user = $rootScope.user;
|
||||
|
||||
$scope.error = error;
|
||||
console.error('loginError: '+error);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$scope.signup = function() {
|
||||
if($scope.credentials === 'admin'){
|
||||
//TODO - David : need to put this somewhere more appropriate
|
||||
if($scope.credentials.username === 'admin'){
|
||||
$scope.error = 'Username cannot be \'admin\'. Please pick another username.';
|
||||
return;
|
||||
}
|
||||
|
||||
if($scope.forms && $scope.forms.signupForm && $scope.forms.signupForm.$valid){
|
||||
if($scope.credentials && $scope.forms.hasOwnProperty('signupForm') && $scope.forms.signupForm.$valid){
|
||||
User.signup($scope.credentials).then(
|
||||
function(response) {
|
||||
$state.go('signup-success');
|
||||
|
||||
@ -4,11 +4,10 @@
|
||||
// Forms Controller Spec
|
||||
describe('Authentication Controller Tests', function() {
|
||||
// Initialize global variables
|
||||
var AuthenticationController,
|
||||
var ctrl,
|
||||
scope,
|
||||
$httpBackend,
|
||||
$stateParams,
|
||||
$location,
|
||||
$state;
|
||||
|
||||
var sampleUser = {
|
||||
@ -88,34 +87,32 @@
|
||||
$provide.service('User', function($q) {
|
||||
return {
|
||||
getCurrent: function() {
|
||||
var deferred = $q.defer();
|
||||
deferred.resolve( JSON.stringify(sampleUser) );
|
||||
return deferred.promise;
|
||||
return {
|
||||
then: function(onFulfilled, onRejected, progressBack) {
|
||||
return onFulfilled(sampleUser);
|
||||
}
|
||||
};
|
||||
},
|
||||
login: function(credentials) {
|
||||
var deferred = $q.defer();
|
||||
if( credentials.password === sampleUser.password && credentials.username === sampleUser.username){
|
||||
deferred.resolve( JSON.stringify(sampleUser) );
|
||||
}else {
|
||||
deferred.resolve('Error: User could not be loggedin');
|
||||
}
|
||||
|
||||
return deferred.promise;
|
||||
return {
|
||||
then: function(onFulfilled, onRejected, progressBack) {
|
||||
return onFulfilled(sampleUser);
|
||||
}
|
||||
};
|
||||
},
|
||||
logout: function() {
|
||||
var deferred = $q.defer();
|
||||
deferred.resolve(null);
|
||||
return deferred.promise;
|
||||
return {
|
||||
then: function(onFulfilled, onRejected, progressBack) {
|
||||
return onFulfilled(null);
|
||||
}
|
||||
};
|
||||
},
|
||||
signup: function(credentials) {
|
||||
var deferred = $q.defer();
|
||||
if( credentials.password === sampleUser.password && credentials.username === sampleUser.username){
|
||||
deferred.resolve( JSON.stringify(sampleUser) );
|
||||
}else {
|
||||
deferred.resolve('Error: User could not be signed up');
|
||||
}
|
||||
|
||||
return deferred.promise;
|
||||
return {
|
||||
then: function(onFulfilled, onRejected, progressBack) {
|
||||
return onFulfilled(sampleUser);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
});
|
||||
@ -125,6 +122,12 @@
|
||||
beforeEach(module(function($provide) {
|
||||
$provide.service('Auth', function() {
|
||||
return {
|
||||
_currentUser: null,
|
||||
get currentUser(){
|
||||
return sampleUser
|
||||
},
|
||||
login: function(user) {
|
||||
},
|
||||
ensureHasCurrentUser: function() {
|
||||
return sampleUser;
|
||||
},
|
||||
@ -141,41 +144,103 @@
|
||||
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
|
||||
// This allows us to inject a service but then attach it to a variable
|
||||
// with the same name as the service.
|
||||
beforeEach(inject(function($controller, $rootScope, _$state_, _$location_, _$stateParams_, _$httpBackend_, CurrentForm, Forms) {
|
||||
beforeEach(inject(function($controller, $rootScope, _$state_, _$stateParams_, _$httpBackend_, Auth, User) {
|
||||
// Set a new global scope
|
||||
scope = $rootScope.$new();
|
||||
scope.abc = 'hello';
|
||||
scope.forms = {
|
||||
signinForm: {
|
||||
$valid: true
|
||||
},
|
||||
signupForm: {
|
||||
$valid: true
|
||||
},
|
||||
};
|
||||
|
||||
scope.credentials = _.cloneDeep(sampleCredentials);
|
||||
|
||||
// Point global variables to injected services
|
||||
$stateParams = _$stateParams_;
|
||||
$httpBackend = _$httpBackend_;
|
||||
$location = _$location_;
|
||||
$state = _$state_;
|
||||
|
||||
// $httpBackend.whenGET(/\.html$/).respond('');
|
||||
$httpBackend.whenGET('/forms').respond('');
|
||||
$httpBackend.whenGET('/users/me/').respond('');
|
||||
|
||||
// Initialize the Forms controller.
|
||||
AuthenticationController = $controller('AuthenticationController', { $scope: scope });
|
||||
|
||||
this.init = function(){
|
||||
ctrl = $controller('AuthenticationController', {
|
||||
$scope: scope
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
it('$scope.signin should sigin in user with valid credentials', inject(function(Auth) {
|
||||
|
||||
//Set $state transition
|
||||
// $state.expectTransitionTo('listForms');
|
||||
//Set POST response
|
||||
// $httpBackend.expect('POST', '/auth/signin', sampleCredentials).respond(200, sampleUser);
|
||||
it('$scope.signin should sign-in in user with valid credentials', inject(function(Auth) {
|
||||
this.init();
|
||||
|
||||
//Set $state transition
|
||||
$state.expectTransitionTo('listForms');
|
||||
spyOn(Auth, 'login');
|
||||
|
||||
scope.abc = 'sampleCredentials';
|
||||
//Run Controller Logic to Test
|
||||
scope.signin();
|
||||
|
||||
// $httpBackend.flush();
|
||||
// Test scope value
|
||||
expect(Auth.ensureHasCurrentUser()).toEqualData(sampleUser);
|
||||
expect(Auth.login).toHaveBeenCalledTimes(1);
|
||||
expect(scope.user).toEqualData(sampleUser);
|
||||
|
||||
$state.ensureAllTransitionsHappened();
|
||||
}));
|
||||
|
||||
it('$scope.signin should sign-in in user and redirect to previous state', inject(function(Auth) {
|
||||
this.init();
|
||||
|
||||
$state.previous = {
|
||||
state: {
|
||||
name: 'profile'
|
||||
},
|
||||
fromParams: {}
|
||||
}
|
||||
|
||||
//Set $state transition
|
||||
$state.expectTransitionTo('profile');
|
||||
spyOn(Auth, 'login');
|
||||
|
||||
//Run Controller Logic to Test
|
||||
scope.signin();
|
||||
|
||||
// Test scope value
|
||||
// expect(Auth.ensureHasCurrentUser()).toEqualData(sampleUser);
|
||||
expect(Auth.ensureHasCurrentUser()).toEqualData(sampleUser);
|
||||
expect(Auth.login).toHaveBeenCalledTimes(1);
|
||||
expect(scope.user).toEqualData(sampleUser);
|
||||
|
||||
$state.ensureAllTransitionsHappened();
|
||||
}));
|
||||
|
||||
|
||||
it('$scope.signup should sign-up user with valid credentials', inject(function(Auth) {
|
||||
this.init();
|
||||
|
||||
//Set $state transition
|
||||
$state.expectTransitionTo('signup-success');
|
||||
spyOn(Auth, 'isAuthenticated').and.returnValue(false);
|
||||
|
||||
//Run Controller Logic to Test
|
||||
scope.signup();
|
||||
|
||||
$state.ensureAllTransitionsHappened();
|
||||
}));
|
||||
|
||||
it('$scope.signup should not sign-up user if username is admin', function() {
|
||||
scope.credentials.username = 'admin';
|
||||
scope.credentials.email = 'test@example.com';
|
||||
this.init();
|
||||
|
||||
//Run Controller Logic to Test
|
||||
scope.signup();
|
||||
|
||||
expect(scope.error).toEqual('Username cannot be \'admin\'. Please pick another username.');
|
||||
});
|
||||
});
|
||||
}());
|
||||
Loading…
x
Reference in New Issue
Block a user