refactored user settings controller

This commit is contained in:
David Baldwynn 2017-11-19 13:34:39 -08:00
parent 35ea6276a7
commit cb3fe0932f
3 changed files with 129 additions and 35 deletions

View file

@ -5,40 +5,8 @@ angular.module('users').controller('SettingsController', ['$scope', '$rootScope'
$scope.user = currentUser;
// Check if there are additional accounts
$scope.hasConnectedAdditionalSocialAccounts = function(provider) {
for (var i in $scope.user.additionalProvidersData) {
return true;
}
return false;
};
$scope.cancel = function(){
$scope.user = Auth.currentUser;
};
// Check if provider is already in use with current user
$scope.isConnectedSocialAccount = function(provider) {
return $scope.user.provider === provider || ($scope.user.additionalProvidersData && $scope.user.additionalProvidersData[provider]);
};
// Remove a user social account
$scope.removeUserSocialAccount = function(provider) {
$scope.success = $scope.error = null;
$http.delete('/users/accounts', {
params: {
provider: provider
}
}).then(function(response) {
// If successful show success message and clear form
$scope.success = true;
$scope.error = null;
$scope.user = response;
}, function(response) {
$scope.success = null;
$scope.error = response.message;
});
$scope.user = currentUser;
};
// Update a user profile
@ -55,8 +23,6 @@ angular.module('users').controller('SettingsController', ['$scope', '$rootScope'
$scope.success = null;
$scope.error = response.data.message;
});
} else {
$scope.submitted = true;
}
};

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);
}));
});
}());