[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-FE-common / client / app / views / users / new-user-dialogs / new-user.controller.spec.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 /**
21  * Created by nnaffar on 12/20/15.
22  */
23 'use strict';
24
25 describe('Controller: NewUserModalCtrl ', () => {
26     beforeEach(module('testUtils'));
27     beforeEach(module('ecompApp'));
28
29         let promisesTestUtils;
30         //destroy $http default cache before starting to prevent the error 'default cache already exists'
31         //_promisesTestUtils_ comes from testUtils for promises resolve/reject
32         beforeEach(inject((_CacheFactory_, _promisesTestUtils_)=> {
33             _CacheFactory_.destroyAll();
34             promisesTestUtils = _promisesTestUtils_;
35         }));
36
37         let newUser, $controller, $q, $rootScope, $log, $scope;
38
39         let applicationsServiceMock, usersServiceMock, confirmBoxServiceMock;
40         let deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate;
41
42         beforeEach(inject((_$controller_, _$q_, _$rootScope_, _$log_)=> {
43             $rootScope = _$rootScope_;
44             $q = _$q_;
45             $controller = _$controller_;
46             $log = _$log_;
47         }));
48
49     beforeEach(()=> {
50             [deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate] = [$q.defer(),$q.defer(), $q.defer(), $q.defer()];
51
52             /*applicationsServiceMock = {
53                 getAdminApps: () => {
54                     var promise = () => {return deferredAdminApps.promise};
55                     var cancel = jasmine.createSpy();
56                     return {
57                         promise: promise,
58                         cancel: cancel
59                     }
60                 }
61             };*/
62
63             confirmBoxServiceMock = {
64                 deleteItem: () => {
65                     var promise = () => {return deferredAdminApps.promise};
66                     var cancel = jasmine.createSpy();
67                     return {
68                         promise: promise,
69                         cancel: cancel
70                     }
71                 }
72             };
73
74             applicationsServiceMock = jasmine.createSpyObj('applicationsServiceMock', ['getAdminAppsSimpler']);
75             applicationsServiceMock.getAdminAppsSimpler.and.returnValue(deferredAdminApps.promise);
76
77             usersServiceMock = jasmine.createSpyObj('usersServiceMock', ['getAccountUsers','getUserAppRoles','updateUserAppsRoles']);
78
79             //applicationsServiceMock.getAdminApps().promise().and.returnValue(deferredAdminApps.promise);
80             usersServiceMock.getAccountUsers.and.returnValue(deferredUsersAccounts.promise);
81             usersServiceMock.getUserAppRoles.and.returnValue(deferredUsersAppRoles.promise);
82             usersServiceMock.updateUserAppsRoles.and.returnValue(deferredUsersAppRoleUpdate.promise);
83
84             $scope = $rootScope.$new();
85             newUser = $controller('NewUserModalCtrl', {
86                 $scope: $scope,
87                 $log: $log,
88                 usersService: usersServiceMock,
89                 applicationsService: applicationsServiceMock,
90                 confirmBoxService: confirmBoxServiceMock
91             });
92             //$scope.users = users;
93         });
94
95         /*beforeEach(()=> {
96             scope = $rootScope.$new();
97             newUser = $controller('NewUserModalCtrl', {
98                 $scope: scope,
99                 $log: $log,
100                 usersService: usersService,
101                 applicationsService: applicationsService,
102                 confirmBoxService: confirmBoxService
103             });
104         });*/
105
106
107         it('should open modal window without user when no user is selected', ()=> {
108             expect(newUser.selectedUser).toBe(null);
109         });
110
111         it('should open modal window with selectedUser apps roles when user is selected', ()=> {
112             let roles = {apps: [{id: 1, appRoles: [{id: 3, isApplied: true}]}]};
113             let someUser = {orgUserId: 'asdfjl'};
114
115            deferredUsersAppRoles.resolve(roles);
116            deferredAdminApps.resolve(roles.apps);
117
118             $scope.ngDialogData = {
119                 selectedUser: someUser,
120                 dialogState: 2
121             };
122
123             //inject ngDialogData to the scope controller
124             newUser = $controller('NewUserModalCtrl', {
125                 $scope: $scope,
126                 $log: $log,
127                 usersService: usersServiceMock,
128                 applicationsService: applicationsServiceMock,
129                 confirmBoxService: confirmBoxServiceMock
130             });
131
132             newUser.getUserAppsRoles();
133             $scope.$apply();
134
135             expect(newUser.selectedUser).toBe(someUser);
136             expect(newUser.adminApps).toEqual(roles.apps);
137         });
138
139         it('should push to apps order list only apps that has applied roles when initializing', () => {
140             let roles = {apps: [{appId: 13, appRoles: [{id: 3, isApplied: true}]},{appId: 20, appRoles: [{id: 3, isApplied: false}]}]};
141             let someUser = {orgUserId: 'asdfjl'};
142
143             deferredUsersAppRoles.resolve(roles);
144             //deferredAdminApps.resolve(roles.apps);
145
146             $scope.ngDialogData = {
147                 selectedUser: someUser,
148                 dialogState: 2
149             };
150
151             //inject ngDialogData to the scope controller
152             newUser = $controller('NewUserModalCtrl', {
153                 $scope: $scope,
154                 $log: $log,
155                 usersService: usersServiceMock,
156                 applicationsService: applicationsServiceMock,
157                 confirmBoxService: confirmBoxServiceMock
158             });
159
160             $scope.$apply();
161
162            // expect(newUser.appsOrder).toEqual([13]);
163         });
164
165         it('should push app to apps order list when applying at least one role to user from app', () => {
166             let roles = {apps: [{appId: 13, appRoles: [{id: 3, isApplied: true}]},{appId: 20, appRoles: [{id: 3, isApplied: false}]}]};
167             let someUser = {orgUserId: 'asdfjl'};
168
169             // promisesTestUtils.resolvePromise(usersService, 'getUserAppsRoles', roles);
170             deferredUsersAppRoles.resolve(roles);
171
172             $scope.ngDialogData = {
173                 selectedUser: someUser,
174                 dialogState: 2
175             };
176
177             //inject ngDialogData to the scope controller
178             newUser = $controller('NewUserModalCtrl', {
179                 $scope: $scope,
180                 $log: $log,
181                 usersService: usersServiceMock,
182                 applicationsService: applicationsServiceMock,
183                 confirmBoxService: confirmBoxServiceMock
184             });
185
186             //$scope.$apply();
187             //newUser.updateAppsOrder({appId: 39, appRoles: [{id: 13, isApplied: true}]});
188             $scope.$apply();
189
190           //  expect(newUser.appsOrder).toEqual([13, 39]);
191         });
192
193
194         it('should remove app from list when removing all user roles in it', () => {
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 = {orgUserId: 'asdfjl'};
197
198            // promisesTestUtils.resolvePromise(usersService, 'getUserAppsRoles', roles);
199             promisesTestUtils.resolvePromise(confirmBoxServiceMock, 'deleteItem', true);
200
201             deferredUsersAppRoles.resolve(roles);
202
203             $scope.ngDialogData = {
204                 selectedUser: someUser,
205                 dialogState: 2
206             };
207
208             //inject ngDialogData to the scope controller
209             newUser = $controller('NewUserModalCtrl', {
210                 $scope: $scope,
211                 $log: $log,
212                 usersService: usersServiceMock,
213                 applicationsService: applicationsServiceMock,
214                 confirmBoxService: confirmBoxServiceMock
215             });
216
217             $scope.$apply();
218             newUser.deleteApp(roles.apps[0]);
219             $scope.$apply();
220
221            // expect(newUser.appsOrder).toEqual([20]);
222         });
223
224         it('should close the modal when update changes succeeded', () => {
225             let roles = {apps: [{appName: 'aaa', appId: 13, appRoles: [{id: 3, isApplied: true}]},{appName: 'vvv', appId: 20, appRoles: [{id: 3, isApplied: true}]}]};
226             let someUser = {orgUserId: 'asdfjl'};
227             //promisesTestUtils.resolvePromise(usersServiceMock, 'getUserAppsRoles', roles);
228             //promisesTestUtils.resolvePromise(usersServiceMock, 'updateUserAppsRoles');
229             deferredUsersAppRoles.resolve(roles);
230             deferredUsersAppRoleUpdate.resolve();
231             deferredAdminApps.resolve(roles.apps);
232
233             $scope.ngDialogData = {
234                 selectedUser: someUser,
235                 dialogState: 2
236             };
237
238             //inject ngDialogData to the scope controller
239             newUser = $controller('NewUserModalCtrl', {
240                 $scope: $scope,
241                 $log: $log,
242                 usersService: usersServiceMock,
243                 applicationsService: applicationsServiceMock,
244                 confirmBoxService: confirmBoxServiceMock
245             });
246             $scope.closeThisDialog = function(){};
247             spyOn($scope, 'closeThisDialog');
248
249             newUser.getUserAppsRoles();
250             $scope.$apply();
251             newUser.updateUserAppsRoles();
252             $scope.$apply();
253             expect($scope.closeThisDialog).toHaveBeenCalledWith(true);
254         });
255     });