Security/ Package Name changes
[portal.git] / ecomp-portal-FE-common / client / app / views / users / new-user-dialogs / new-user.controller.spec.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 /**
39  * Created by nnaffar on 12/20/15.
40  */
41 'use strict';
42
43 describe('Controller: NewUserModalCtrl ', () => {
44     beforeEach(module('testUtils'));
45     beforeEach(module('ecompApp'));
46
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_;
53         }));
54
55         let newUser, $controller, $q, $rootScope, $log, $scope;
56
57         let applicationsServiceMock, usersServiceMock, confirmBoxServiceMock;
58         let deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate;
59
60         beforeEach(inject((_$controller_, _$q_, _$rootScope_, _$log_)=> {
61             $rootScope = _$rootScope_;
62             $q = _$q_;
63             $controller = _$controller_;
64             $log = _$log_;
65         }));
66
67     beforeEach(()=> {
68             [deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate] = [$q.defer(),$q.defer(), $q.defer(), $q.defer()];
69
70             /*applicationsServiceMock = {
71                 getAdminApps: () => {
72                     var promise = () => {return deferredAdminApps.promise};
73                     var cancel = jasmine.createSpy();
74                     return {
75                         promise: promise,
76                         cancel: cancel
77                     }
78                 }
79             };*/
80
81             confirmBoxServiceMock = {
82                 deleteItem: () => {
83                     var promise = () => {return deferredAdminApps.promise};
84                     var cancel = jasmine.createSpy();
85                     return {
86                         promise: promise,
87                         cancel: cancel
88                     }
89                 }
90             };
91
92             applicationsServiceMock = jasmine.createSpyObj('applicationsServiceMock', ['getAdminAppsSimpler']);
93             applicationsServiceMock.getAdminAppsSimpler.and.returnValue(deferredAdminApps.promise);
94
95             usersServiceMock = jasmine.createSpyObj('usersServiceMock', ['getAccountUsers','getUserAppRoles','updateUserAppsRoles']);
96
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);
101
102             $scope = $rootScope.$new();
103             newUser = $controller('NewUserModalCtrl', {
104                 $scope: $scope,
105                 $log: $log,
106                 usersService: usersServiceMock,
107                 applicationsService: applicationsServiceMock,
108                 confirmBoxService: confirmBoxServiceMock
109             });
110             //$scope.users = users;
111         });
112
113         /*beforeEach(()=> {
114             scope = $rootScope.$new();
115             newUser = $controller('NewUserModalCtrl', {
116                 $scope: scope,
117                 $log: $log,
118                 usersService: usersService,
119                 applicationsService: applicationsService,
120                 confirmBoxService: confirmBoxService
121             });
122         });*/
123
124
125         it('should open modal window without user when no user is selected', ()=> {
126             expect(newUser.selectedUser).toBe(null);
127         });
128
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'};
132
133            deferredUsersAppRoles.resolve(roles);
134            deferredAdminApps.resolve(roles.apps);
135
136             $scope.ngDialogData = {
137                 selectedUser: someUser,
138                 dialogState: 2
139             };
140
141             //inject ngDialogData to the scope controller
142             newUser = $controller('NewUserModalCtrl', {
143                 $scope: $scope,
144                 $log: $log,
145                 usersService: usersServiceMock,
146                 applicationsService: applicationsServiceMock,
147                 confirmBoxService: confirmBoxServiceMock
148             });
149
150             newUser.getUserAppsRoles();
151             $scope.$apply();
152
153             expect(newUser.selectedUser).toBe(someUser);
154             expect(newUser.adminApps).toEqual(roles.apps);
155         });
156
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'};
160
161             deferredUsersAppRoles.resolve(roles);
162             //deferredAdminApps.resolve(roles.apps);
163
164             $scope.ngDialogData = {
165                 selectedUser: someUser,
166                 dialogState: 2
167             };
168
169             //inject ngDialogData to the scope controller
170             newUser = $controller('NewUserModalCtrl', {
171                 $scope: $scope,
172                 $log: $log,
173                 usersService: usersServiceMock,
174                 applicationsService: applicationsServiceMock,
175                 confirmBoxService: confirmBoxServiceMock
176             });
177
178             $scope.$apply();
179
180            // expect(newUser.appsOrder).toEqual([13]);
181         });
182
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'};
186
187             // promisesTestUtils.resolvePromise(usersService, 'getUserAppsRoles', roles);
188             deferredUsersAppRoles.resolve(roles);
189
190             $scope.ngDialogData = {
191                 selectedUser: someUser,
192                 dialogState: 2
193             };
194
195             //inject ngDialogData to the scope controller
196             newUser = $controller('NewUserModalCtrl', {
197                 $scope: $scope,
198                 $log: $log,
199                 usersService: usersServiceMock,
200                 applicationsService: applicationsServiceMock,
201                 confirmBoxService: confirmBoxServiceMock
202             });
203
204             //$scope.$apply();
205             //newUser.updateAppsOrder({appId: 39, appRoles: [{id: 13, isApplied: true}]});
206             $scope.$apply();
207
208           //  expect(newUser.appsOrder).toEqual([13, 39]);
209         });
210
211
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'};
215
216            // promisesTestUtils.resolvePromise(usersService, 'getUserAppsRoles', roles);
217             promisesTestUtils.resolvePromise(confirmBoxServiceMock, 'deleteItem', true);
218
219             deferredUsersAppRoles.resolve(roles);
220
221             $scope.ngDialogData = {
222                 selectedUser: someUser,
223                 dialogState: 2
224             };
225
226             //inject ngDialogData to the scope controller
227             newUser = $controller('NewUserModalCtrl', {
228                 $scope: $scope,
229                 $log: $log,
230                 usersService: usersServiceMock,
231                 applicationsService: applicationsServiceMock,
232                 confirmBoxService: confirmBoxServiceMock
233             });
234
235             $scope.$apply();
236             newUser.deleteApp(roles.apps[0]);
237             $scope.$apply();
238
239            // expect(newUser.appsOrder).toEqual([20]);
240         });
241
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);
250
251             $scope.ngDialogData = {
252                 selectedUser: someUser,
253                 dialogState: 2
254             };
255
256             //inject ngDialogData to the scope controller
257             newUser = $controller('NewUserModalCtrl', {
258                 $scope: $scope,
259                 $log: $log,
260                 usersService: usersServiceMock,
261                 applicationsService: applicationsServiceMock,
262                 confirmBoxService: confirmBoxServiceMock
263             });
264             $scope.closeThisDialog = function(){};
265             spyOn($scope, 'closeThisDialog');
266
267             newUser.getUserAppsRoles();
268             $scope.$apply();
269             newUser.updateUserAppsRoles();
270             $scope.$apply();
271             expect($scope.closeThisDialog).toHaveBeenCalledWith(true);
272         });
273     });