nexus site path corrected
[portal.git] / ecomp-portal-FE / 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 'use strict';
22
23 describe('Controller: NewUserModalCtrl ', () => {
24     beforeEach(module('testUtils'));
25     beforeEach(module('ecompApp'));
26
27         let promisesTestUtils;
28         beforeEach(inject((_CacheFactory_, _promisesTestUtils_)=> {
29             _CacheFactory_.destroyAll();
30             promisesTestUtils = _promisesTestUtils_;
31         }));
32
33         let newUser, $controller, $q, $rootScope, $log, $scope;
34
35         let applicationsServiceMock, usersServiceMock, confirmBoxServiceMock;
36         let deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate;
37
38         beforeEach(inject((_$controller_, _$q_, _$rootScope_, _$log_)=> {
39             $rootScope = _$rootScope_;
40             $q = _$q_;
41             $controller = _$controller_;
42             $log = _$log_;
43         }));
44
45     beforeEach(()=> {
46             [deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate] = [$q.defer(),$q.defer(), $q.defer(), $q.defer()];
47
48             /*applicationsServiceMock = {
49                 getAdminApps: () => {
50                     var promise = () => {return deferredAdminApps.promise};
51                     var cancel = jasmine.createSpy();
52                     return {
53                         promise: promise,
54                         cancel: cancel
55                     }
56                 }
57             };*/
58
59             confirmBoxServiceMock = {
60                 deleteItem: () => {
61                     var promise = () => {return deferredAdminApps.promise};
62                     var cancel = jasmine.createSpy();
63                     return {
64                         promise: promise,
65                         cancel: cancel
66                     }
67                 }
68             };
69
70             applicationsServiceMock = jasmine.createSpyObj('applicationsServiceMock', ['getAdminAppsSimpler']);
71             applicationsServiceMock.getAdminAppsSimpler.and.returnValue(deferredAdminApps.promise);
72
73             usersServiceMock = jasmine.createSpyObj('usersServiceMock', ['getAccountUsers','getUserAppRoles','updateUserAppsRoles']);
74
75             usersServiceMock.getAccountUsers.and.returnValue(deferredUsersAccounts.promise);
76             usersServiceMock.getUserAppRoles.and.returnValue(deferredUsersAppRoles.promise);
77             usersServiceMock.updateUserAppsRoles.and.returnValue(deferredUsersAppRoleUpdate.promise);
78
79             $scope = $rootScope.$new();
80             newUser = $controller('NewUserModalCtrl', {
81                 $scope: $scope,
82                 $log: $log,
83                 usersService: usersServiceMock,
84                 applicationsService: applicationsServiceMock,
85                 confirmBoxService: confirmBoxServiceMock
86             });
87         });
88
89         it('should open modal window without user when no user is selected', ()=> {
90             expect(newUser.selectedUser).toBe(null);
91         });
92
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'};
96
97            deferredUsersAppRoles.resolve(roles);
98            deferredAdminApps.resolve(roles.apps);
99
100             $scope.ngDialogData = {
101                 selectedUser: someUser,
102                 dialogState: 2
103             };
104
105             newUser = $controller('NewUserModalCtrl', {
106                 $scope: $scope,
107                 $log: $log,
108                 usersService: usersServiceMock,
109                 applicationsService: applicationsServiceMock,
110                 confirmBoxService: confirmBoxServiceMock
111             });
112
113             newUser.getUserAppsRoles();
114             $scope.$apply();
115
116             expect(newUser.selectedUser).toBe(someUser);
117             expect(newUser.adminApps).toEqual(roles.apps);
118         });
119
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'};
123
124             deferredUsersAppRoles.resolve(roles);
125
126             $scope.ngDialogData = {
127                 selectedUser: someUser,
128                 dialogState: 2
129             };
130
131             newUser = $controller('NewUserModalCtrl', {
132                 $scope: $scope,
133                 $log: $log,
134                 usersService: usersServiceMock,
135                 applicationsService: applicationsServiceMock,
136                 confirmBoxService: confirmBoxServiceMock
137             });
138
139             $scope.$apply();
140
141         });
142
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'};
146
147             deferredUsersAppRoles.resolve(roles);
148
149             $scope.ngDialogData = {
150                 selectedUser: someUser,
151                 dialogState: 2
152             };
153
154             newUser = $controller('NewUserModalCtrl', {
155                 $scope: $scope,
156                 $log: $log,
157                 usersService: usersServiceMock,
158                 applicationsService: applicationsServiceMock,
159                 confirmBoxService: confirmBoxServiceMock
160             });
161
162             $scope.$apply();
163
164         });
165
166
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'};
170
171             promisesTestUtils.resolvePromise(confirmBoxServiceMock, 'deleteItem', true);
172
173             deferredUsersAppRoles.resolve(roles);
174
175             $scope.ngDialogData = {
176                 selectedUser: someUser,
177                 dialogState: 2
178             };
179
180             newUser = $controller('NewUserModalCtrl', {
181                 $scope: $scope,
182                 $log: $log,
183                 usersService: usersServiceMock,
184                 applicationsService: applicationsServiceMock,
185                 confirmBoxService: confirmBoxServiceMock
186             });
187
188             $scope.$apply();
189             newUser.deleteApp(roles.apps[0]);
190             $scope.$apply();
191
192         });
193
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);
200
201             $scope.ngDialogData = {
202                 selectedUser: someUser,
203                 dialogState: 2
204             };
205
206             newUser = $controller('NewUserModalCtrl', {
207                 $scope: $scope,
208                 $log: $log,
209                 usersService: usersServiceMock,
210                 applicationsService: applicationsServiceMock,
211                 confirmBoxService: confirmBoxServiceMock
212             });
213             $scope.closeThisDialog = function(){};
214             spyOn($scope, 'closeThisDialog');
215
216             newUser.getUserAppsRoles();
217             $scope.$apply();
218             newUser.updateUserAppsRoles();
219             $scope.$apply();
220             expect($scope.closeThisDialog).toHaveBeenCalledWith(true);
221         });
222     });