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 * ================================================================================
21 * Created by nnaffar on 12/20/15.
25 describe('Controller: NewUserModalCtrl ', () => {
26 beforeEach(module('testUtils'));
27 beforeEach(module('ecompApp'));
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_;
37 let newUser, $controller, $q, $rootScope, $log, $scope;
39 let applicationsServiceMock, usersServiceMock, confirmBoxServiceMock;
40 let deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate;
42 beforeEach(inject((_$controller_, _$q_, _$rootScope_, _$log_)=> {
43 $rootScope = _$rootScope_;
45 $controller = _$controller_;
50 [deferredAdminApps, deferredUsersAccounts, deferredUsersAppRoles, deferredUsersAppRoleUpdate] = [$q.defer(),$q.defer(), $q.defer(), $q.defer()];
52 /*applicationsServiceMock = {
54 var promise = () => {return deferredAdminApps.promise};
55 var cancel = jasmine.createSpy();
63 confirmBoxServiceMock = {
65 var promise = () => {return deferredAdminApps.promise};
66 var cancel = jasmine.createSpy();
74 applicationsServiceMock = jasmine.createSpyObj('applicationsServiceMock', ['getAdminAppsSimpler']);
75 applicationsServiceMock.getAdminAppsSimpler.and.returnValue(deferredAdminApps.promise);
77 usersServiceMock = jasmine.createSpyObj('usersServiceMock', ['getAccountUsers','getUserAppRoles','updateUserAppsRoles']);
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);
84 $scope = $rootScope.$new();
85 newUser = $controller('NewUserModalCtrl', {
88 usersService: usersServiceMock,
89 applicationsService: applicationsServiceMock,
90 confirmBoxService: confirmBoxServiceMock
92 //$scope.users = users;
96 scope = $rootScope.$new();
97 newUser = $controller('NewUserModalCtrl', {
100 usersService: usersService,
101 applicationsService: applicationsService,
102 confirmBoxService: confirmBoxService
107 it('should open modal window without user when no user is selected', ()=> {
108 expect(newUser.selectedUser).toBe(null);
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'};
115 deferredUsersAppRoles.resolve(roles);
116 deferredAdminApps.resolve(roles.apps);
118 $scope.ngDialogData = {
119 selectedUser: someUser,
123 //inject ngDialogData to the scope controller
124 newUser = $controller('NewUserModalCtrl', {
127 usersService: usersServiceMock,
128 applicationsService: applicationsServiceMock,
129 confirmBoxService: confirmBoxServiceMock
132 newUser.getUserAppsRoles();
135 expect(newUser.selectedUser).toBe(someUser);
136 expect(newUser.adminApps).toEqual(roles.apps);
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'};
143 deferredUsersAppRoles.resolve(roles);
144 //deferredAdminApps.resolve(roles.apps);
146 $scope.ngDialogData = {
147 selectedUser: someUser,
151 //inject ngDialogData to the scope controller
152 newUser = $controller('NewUserModalCtrl', {
155 usersService: usersServiceMock,
156 applicationsService: applicationsServiceMock,
157 confirmBoxService: confirmBoxServiceMock
162 // expect(newUser.appsOrder).toEqual([13]);
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'};
169 // promisesTestUtils.resolvePromise(usersService, 'getUserAppsRoles', roles);
170 deferredUsersAppRoles.resolve(roles);
172 $scope.ngDialogData = {
173 selectedUser: someUser,
177 //inject ngDialogData to the scope controller
178 newUser = $controller('NewUserModalCtrl', {
181 usersService: usersServiceMock,
182 applicationsService: applicationsServiceMock,
183 confirmBoxService: confirmBoxServiceMock
187 //newUser.updateAppsOrder({appId: 39, appRoles: [{id: 13, isApplied: true}]});
190 // expect(newUser.appsOrder).toEqual([13, 39]);
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'};
198 // promisesTestUtils.resolvePromise(usersService, 'getUserAppsRoles', roles);
199 promisesTestUtils.resolvePromise(confirmBoxServiceMock, 'deleteItem', true);
201 deferredUsersAppRoles.resolve(roles);
203 $scope.ngDialogData = {
204 selectedUser: someUser,
208 //inject ngDialogData to the scope controller
209 newUser = $controller('NewUserModalCtrl', {
212 usersService: usersServiceMock,
213 applicationsService: applicationsServiceMock,
214 confirmBoxService: confirmBoxServiceMock
218 newUser.deleteApp(roles.apps[0]);
221 // expect(newUser.appsOrder).toEqual([20]);
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);
233 $scope.ngDialogData = {
234 selectedUser: someUser,
238 //inject ngDialogData to the scope controller
239 newUser = $controller('NewUserModalCtrl', {
242 usersService: usersServiceMock,
243 applicationsService: applicationsServiceMock,
244 confirmBoxService: confirmBoxServiceMock
246 $scope.closeThisDialog = function(){};
247 spyOn($scope, 'closeThisDialog');
249 newUser.getUserAppsRoles();
251 newUser.updateUserAppsRoles();
253 expect($scope.closeThisDialog).toHaveBeenCalledWith(true);