2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
39 * Created by nnaffar on 12/20/15.
43 describe('Controller: NewUserModalCtrl ', () => {
44 beforeEach(module('testUtils'));
45 beforeEach(module('ecompApp'));
47 let promisesTestUtils;
48 //destroy $http default cache before starting to prevent the error 'default cache already exists'
49 //_promisesTestUtils_ comes from testUtils for promises resolve/reject
50 beforeEach(inject((_CacheFactory_, _promisesTestUtils_)=> {
51 _CacheFactory_.destroyAll();
52 promisesTestUtils = _promisesTestUtils_;
55 let newUser, $controller, $q, $rootScope, $log, $scope;
57 let applicationsServiceMock, usersServiceMock, confirmBoxServiceMock;
58 let deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate;
60 beforeEach(inject((_$controller_, _$q_, _$rootScope_, _$log_)=> {
61 $rootScope = _$rootScope_;
63 $controller = _$controller_;
68 [deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate] = [$q.defer(),$q.defer(), $q.defer(), $q.defer()];
70 /*applicationsServiceMock = {
72 var promise = () => {return deferredAdminApps.promise};
73 var cancel = jasmine.createSpy();
81 confirmBoxServiceMock = {
83 var promise = () => {return deferredAdminApps.promise};
84 var cancel = jasmine.createSpy();
92 applicationsServiceMock = jasmine.createSpyObj('applicationsServiceMock', ['getAdminAppsSimpler']);
93 applicationsServiceMock.getAdminAppsSimpler.and.returnValue(deferredAdminApps.promise);
95 usersServiceMock = jasmine.createSpyObj('usersServiceMock', ['getAccountUsers','getUserAppRoles','updateUserAppsRoles']);
97 //applicationsServiceMock.getAdminApps().promise().and.returnValue(deferredAdminApps.promise);
98 usersServiceMock.getAccountUsers.and.returnValue(deferredUsersAccounts.promise);
99 usersServiceMock.getUserAppRoles.and.returnValue(deferredUsersAppRoles.promise);
100 usersServiceMock.updateUserAppsRoles.and.returnValue(deferredUsersAppRoleUpdate.promise);
102 $scope = $rootScope.$new();
103 newUser = $controller('NewUserModalCtrl', {
106 usersService: usersServiceMock,
107 applicationsService: applicationsServiceMock,
108 confirmBoxService: confirmBoxServiceMock
110 //$scope.users = users;
114 scope = $rootScope.$new();
115 newUser = $controller('NewUserModalCtrl', {
118 usersService: usersService,
119 applicationsService: applicationsService,
120 confirmBoxService: confirmBoxService
125 it('should open modal window without user when no user is selected', ()=> {
126 expect(newUser.selectedUser).toBe(null);
129 it('should open modal window with selectedUser apps roles when user is selected', ()=> {
130 let roles = {apps: [{id: 1, appRoles: [{id: 3, isApplied: true}]}]};
131 let someUser = {orgUserId: 'asdfjl'};
133 deferredUsersAppRoles.resolve(roles);
134 deferredAdminApps.resolve(roles.apps);
136 $scope.ngDialogData = {
137 selectedUser: someUser,
141 //inject ngDialogData to the scope controller
142 newUser = $controller('NewUserModalCtrl', {
145 usersService: usersServiceMock,
146 applicationsService: applicationsServiceMock,
147 confirmBoxService: confirmBoxServiceMock
150 newUser.getUserAppsRoles();
153 expect(newUser.selectedUser).toBe(someUser);
154 expect(newUser.adminApps).toEqual(roles.apps);
157 it('should push to apps order list only apps that has applied roles when initializing', () => {
158 let roles = {apps: [{appId: 13, appRoles: [{id: 3, isApplied: true}]},{appId: 20, appRoles: [{id: 3, isApplied: false}]}]};
159 let someUser = {orgUserId: 'asdfjl'};
161 deferredUsersAppRoles.resolve(roles);
162 //deferredAdminApps.resolve(roles.apps);
164 $scope.ngDialogData = {
165 selectedUser: someUser,
169 //inject ngDialogData to the scope controller
170 newUser = $controller('NewUserModalCtrl', {
173 usersService: usersServiceMock,
174 applicationsService: applicationsServiceMock,
175 confirmBoxService: confirmBoxServiceMock
180 // expect(newUser.appsOrder).toEqual([13]);
183 it('should push app to apps order list when applying at least one role to user from app', () => {
184 let roles = {apps: [{appId: 13, appRoles: [{id: 3, isApplied: true}]},{appId: 20, appRoles: [{id: 3, isApplied: false}]}]};
185 let someUser = {orgUserId: 'asdfjl'};
187 // promisesTestUtils.resolvePromise(usersService, 'getUserAppsRoles', roles);
188 deferredUsersAppRoles.resolve(roles);
190 $scope.ngDialogData = {
191 selectedUser: someUser,
195 //inject ngDialogData to the scope controller
196 newUser = $controller('NewUserModalCtrl', {
199 usersService: usersServiceMock,
200 applicationsService: applicationsServiceMock,
201 confirmBoxService: confirmBoxServiceMock
205 //newUser.updateAppsOrder({appId: 39, appRoles: [{id: 13, isApplied: true}]});
208 // expect(newUser.appsOrder).toEqual([13, 39]);
212 it('should remove app from list when removing all user roles in it', () => {
213 let roles = {apps: [{appName: 'aaa', appId: 13, appRoles: [{id: 3, isApplied: true}]},{appName: 'vvv', appId: 20, appRoles: [{id: 3, isApplied: true}]}]};
214 let someUser = {orgUserId: 'asdfjl'};
216 // promisesTestUtils.resolvePromise(usersService, 'getUserAppsRoles', roles);
217 promisesTestUtils.resolvePromise(confirmBoxServiceMock, 'deleteItem', true);
219 deferredUsersAppRoles.resolve(roles);
221 $scope.ngDialogData = {
222 selectedUser: someUser,
226 //inject ngDialogData to the scope controller
227 newUser = $controller('NewUserModalCtrl', {
230 usersService: usersServiceMock,
231 applicationsService: applicationsServiceMock,
232 confirmBoxService: confirmBoxServiceMock
236 newUser.deleteApp(roles.apps[0]);
239 // expect(newUser.appsOrder).toEqual([20]);
242 it('should close the modal when update changes succeeded', () => {
243 let roles = {apps: [{appName: 'aaa', appId: 13, appRoles: [{id: 3, isApplied: true}]},{appName: 'vvv', appId: 20, appRoles: [{id: 3, isApplied: true}]}]};
244 let someUser = {orgUserId: 'asdfjl'};
245 //promisesTestUtils.resolvePromise(usersServiceMock, 'getUserAppsRoles', roles);
246 //promisesTestUtils.resolvePromise(usersServiceMock, 'updateUserAppsRoles');
247 deferredUsersAppRoles.resolve(roles);
248 deferredUsersAppRoleUpdate.resolve();
249 deferredAdminApps.resolve(roles.apps);
251 $scope.ngDialogData = {
252 selectedUser: someUser,
256 //inject ngDialogData to the scope controller
257 newUser = $controller('NewUserModalCtrl', {
260 usersService: usersServiceMock,
261 applicationsService: applicationsServiceMock,
262 confirmBoxService: confirmBoxServiceMock
264 $scope.closeThisDialog = function(){};
265 spyOn($scope, 'closeThisDialog');
267 newUser.getUserAppsRoles();
269 newUser.updateUserAppsRoles();
271 expect($scope.closeThisDialog).toHaveBeenCalledWith(true);