From cb3fe0932fea6d472b76eaeff8001209480dc5f9 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Sun, 19 Nov 2017 13:34:39 -0800 Subject: [PATCH] refactored user settings controller --- .../controllers/settings.client.controller.js | 36 +---- ....js => password.client.controller.test.js} | 0 .../settings.client.controller.test.js | 128 ++++++++++++++++++ 3 files changed, 129 insertions(+), 35 deletions(-) rename public/modules/users/tests/unit/controllers/{password.controller.test.js => password.client.controller.test.js} (100%) create mode 100644 public/modules/users/tests/unit/controllers/settings.client.controller.test.js diff --git a/public/modules/users/controllers/settings.client.controller.js b/public/modules/users/controllers/settings.client.controller.js index 140d78bc..9f617116 100755 --- a/public/modules/users/controllers/settings.client.controller.js +++ b/public/modules/users/controllers/settings.client.controller.js @@ -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; } }; diff --git a/public/modules/users/tests/unit/controllers/password.controller.test.js b/public/modules/users/tests/unit/controllers/password.client.controller.test.js similarity index 100% rename from public/modules/users/tests/unit/controllers/password.controller.test.js rename to public/modules/users/tests/unit/controllers/password.client.controller.test.js diff --git a/public/modules/users/tests/unit/controllers/settings.client.controller.test.js b/public/modules/users/tests/unit/controllers/settings.client.controller.test.js new file mode 100644 index 00000000..9059b0b6 --- /dev/null +++ b/public/modules/users/tests/unit/controllers/settings.client.controller.test.js @@ -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); + })); + }); +}()); \ No newline at end of file