added tests for password controller

This commit is contained in:
David Baldwynn 2017-11-19 13:29:12 -08:00
parent d1159087d4
commit 35ea6276a7
2 changed files with 132 additions and 4 deletions

View File

@ -1,10 +1,10 @@
'use strict';
angular.module('users').controller('PasswordController', ['$scope', '$stateParams', '$state', 'User', '$translate', '$window',
function($scope, $stateParams, $state, User, $translate, $window) {
angular.module('users').controller('PasswordController', ['$scope', '$stateParams', '$state', 'User', '$window',
function($scope, $stateParams, $state, User, $window) {
$scope.error = '';
$scope.forms = {};
if(!$scope.forms) $scope.forms = {};
// Submit forgotten password account id
$scope.askForPasswordReset = function() {
@ -24,7 +24,7 @@ angular.module('users').controller('PasswordController', ['$scope', '$stateParam
// Change user password
$scope.resetUserPassword = function() {
if(!$scope.forms.resetPasswordForm.$invalid){
if($scope.forms.hasOwnProperty('resetPasswordForm') && $scope.forms.resetPasswordForm.$valid){
$scope.success = $scope.error = null;
User.resetPassword($scope.passwordDetails, $stateParams.token).then(
function(response){

View File

@ -0,0 +1,128 @@
'use strict';
(function() {
// Forms Controller Spec
describe('Password Controller Tests', function() {
// Initialize global variables
var ctrl,
scope,
$httpBackend,
$state;
var sampleUser = {
firstName: 'Full',
lastName: 'Name',
email: 'test@test.com',
username: 'test@test.com',
password: 'password',
provider: 'local',
roles: ['user'],
_id: 'ed873933b1f1dea0ce12fab9'
};
var sampleForm = {
title: 'Form Title',
admin: 'ed873933b1f1dea0ce12fab9',
language: 'english',
form_fields: [
{fieldType:'textfield', title:'First Name', fieldValue: '', deletePreserved: false},
{fieldType:'checkbox', title:'nascar', fieldValue: '', deletePreserved: false},
{fieldType:'checkbox', title:'hockey', fieldValue: '', deletePreserved: false}
],
_id: '525a8422f6d0f87f0e407a33'
};
var sampleCredentials = {
username: sampleUser.username,
password: sampleUser.password,
};
// Load the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));
beforeEach(module('module-templates'));
beforeEach(module('stateMock'));
var thenFunction = function(onFulfilled, onRejected, progressBack){
onFulfilled(sampleForm)
};
// 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_, _$httpBackend_, Auth, User) {
// Set a new global scope
scope = $rootScope.$new();
scope.credentials = _.cloneDeep(sampleCredentials);
scope.passwordDetails = {
newPassword: 'aoeeaoaeo',
verifyPassword: 'aoeeaoaeo'
}
// Point global variables to injected services
$httpBackend = _$httpBackend_;
$state = _$state_;
$httpBackend.whenGET('/forms').respond('');
$httpBackend.whenGET('/users/me/').respond('');
// Initialize the Forms controller.
this.init = function(){
ctrl = $controller('PasswordController', {
$scope: scope
});
}
}));
it('$scope.resetUserPassword should call User.resetPassword if form is valid', inject(function(User) {
scope.forms = {
resetPasswordForm: {
$valid: true
}
};
this.init();
//Set $state transition
$state.expectTransitionTo('reset-success');
spyOn(User, 'resetPassword').and.returnValue({ then: thenFunction });
//Run Controller Logic to Test
scope.resetUserPassword();
// Test scope value
expect(User.resetPassword).toHaveBeenCalledTimes(1);
$state.ensureAllTransitionsHappened();
}));
it('$scope.resetUserPassword should not call User.resetPassword if form is invalid', inject(function(User) {
scope.forms = {
resetPasswordForm: {
$valid: false
}
};
this.init();
//Set $state transition
spyOn(User, 'resetPassword').and.returnValue({ then: thenFunction });
//Run Controller Logic to Test
scope.resetUserPassword();
// Test scope value
expect(User.resetPassword).toHaveBeenCalledTimes(0);
}));
it('$scope.askForPasswordReset should call User.askForPasswordReset', inject(function(User) {
this.init();
spyOn(User, 'askForPasswordReset').and.returnValue({ then: thenFunction });
//Run Controller Logic to Test
scope.askForPasswordReset();
// Test scope value
expect(User.askForPasswordReset).toHaveBeenCalledTimes(1);
}));
});
}());