2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
23 describe('Controller: NewUserModalCtrl ', () => {
24 beforeEach(module('testUtils'));
25 beforeEach(module('ecompApp'));
27 let promisesTestUtils;
28 beforeEach(inject((_CacheFactory_, _promisesTestUtils_)=> {
29 _CacheFactory_.destroyAll();
30 promisesTestUtils = _promisesTestUtils_;
33 let newUser, $controller, $q, $rootScope, $log, $scope;
35 let applicationsServiceMock, usersServiceMock, confirmBoxServiceMock;
36 let deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate;
38 beforeEach(inject((_$controller_, _$q_, _$rootScope_, _$log_)=> {
39 $rootScope = _$rootScope_;
41 $controller = _$controller_;
46 [deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate] = [$q.defer(),$q.defer(), $q.defer(), $q.defer()];
48 /*applicationsServiceMock = {
50 var promise = () => {return deferredAdminApps.promise};
51 var cancel = jasmine.createSpy();
59 confirmBoxServiceMock = {
61 var promise = () => {return deferredAdminApps.promise};
62 var cancel = jasmine.createSpy();
70 applicationsServiceMock = jasmine.createSpyObj('applicationsServiceMock', ['getAdminAppsSimpler']);
71 applicationsServiceMock.getAdminAppsSimpler.and.returnValue(deferredAdminApps.promise);
73 usersServiceMock = jasmine.createSpyObj('usersServiceMock', ['getAccountUsers','getUserAppRoles','updateUserAppsRoles']);
75 usersServiceMock.getAccountUsers.and.returnValue(deferredUsersAccounts.promise);
76 usersServiceMock.getUserAppRoles.and.returnValue(deferredUsersAppRoles.promise);
77 usersServiceMock.updateUserAppsRoles.and.returnValue(deferredUsersAppRoleUpdate.promise);
79 $scope = $rootScope.$new();
80 newUser = $controller('NewUserModalCtrl', {
83 usersService: usersServiceMock,
84 applicationsService: applicationsServiceMock,
85 confirmBoxService: confirmBoxServiceMock
89 it('should open modal window without user when no user is selected', ()=> {
90 expect(newUser.selectedUser).toBe(null);
93 it('should open modal window with selectedUser apps roles when user is selected', ()=> {
94 let roles = {apps: [{id: 1, appRoles: [{id: 3, isApplied: true}]}]};
95 let someUser = {userId: 'asdfjl'};
97 deferredUsersAppRoles.resolve(roles);
98 deferredAdminApps.resolve(roles.apps);
100 $scope.ngDialogData = {
101 selectedUser: someUser,
105 newUser = $controller('NewUserModalCtrl', {
108 usersService: usersServiceMock,
109 applicationsService: applicationsServiceMock,
110 confirmBoxService: confirmBoxServiceMock
113 newUser.getUserAppsRoles();
116 expect(newUser.selectedUser).toBe(someUser);
117 expect(newUser.adminApps).toEqual(roles.apps);
120 it('should push to apps order list only apps that has applied roles when initializing', () => {
121 let roles = {apps: [{appId: 13, appRoles: [{id: 3, isApplied: true}]},{appId: 20, appRoles: [{id: 3, isApplied: false}]}]};
122 let someUser = {userId: 'asdfjl'};
124 deferredUsersAppRoles.resolve(roles);
126 $scope.ngDialogData = {
127 selectedUser: someUser,
131 newUser = $controller('NewUserModalCtrl', {
134 usersService: usersServiceMock,
135 applicationsService: applicationsServiceMock,
136 confirmBoxService: confirmBoxServiceMock
143 it('should push app to apps order list when applying at least one role to user from app', () => {
144 let roles = {apps: [{appId: 13, appRoles: [{id: 3, isApplied: true}]},{appId: 20, appRoles: [{id: 3, isApplied: false}]}]};
145 let someUser = {userId: 'asdfjl'};
147 deferredUsersAppRoles.resolve(roles);
149 $scope.ngDialogData = {
150 selectedUser: someUser,
154 newUser = $controller('NewUserModalCtrl', {
157 usersService: usersServiceMock,
158 applicationsService: applicationsServiceMock,
159 confirmBoxService: confirmBoxServiceMock
167 it('should remove app from list when removing all user roles in it', () => {
168 let roles = {apps: [{appName: 'aaa', appId: 13, appRoles: [{id: 3, isApplied: true}]},{appName: 'vvv', appId: 20, appRoles: [{id: 3, isApplied: true}]}]};
169 let someUser = {userId: 'asdfjl'};
171 promisesTestUtils.resolvePromise(confirmBoxServiceMock, 'deleteItem', true);
173 deferredUsersAppRoles.resolve(roles);
175 $scope.ngDialogData = {
176 selectedUser: someUser,
180 newUser = $controller('NewUserModalCtrl', {
183 usersService: usersServiceMock,
184 applicationsService: applicationsServiceMock,
185 confirmBoxService: confirmBoxServiceMock
189 newUser.deleteApp(roles.apps[0]);
194 it('should close the modal when update changes succeeded', () => {
195 let roles = {apps: [{appName: 'aaa', appId: 13, appRoles: [{id: 3, isApplied: true}]},{appName: 'vvv', appId: 20, appRoles: [{id: 3, isApplied: true}]}]};
196 let someUser = {userId: 'asdfjl'};
197 deferredUsersAppRoles.resolve(roles);
198 deferredUsersAppRoleUpdate.resolve();
199 deferredAdminApps.resolve(roles.apps);
201 $scope.ngDialogData = {
202 selectedUser: someUser,
206 newUser = $controller('NewUserModalCtrl', {
209 usersService: usersServiceMock,
210 applicationsService: applicationsServiceMock,
211 confirmBoxService: confirmBoxServiceMock
213 $scope.closeThisDialog = function(){};
214 spyOn($scope, 'closeThisDialog');
216 newUser.getUserAppsRoles();
218 newUser.updateUserAppsRoles();
220 expect($scope.closeThisDialog).toHaveBeenCalledWith(true);