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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 * Created by nnaffar on 12/8/15.
43 describe('Controller: NewAdminCtrl ', () => {
44 beforeEach(module('ecompApp'));
46 //destroy $http default cache before starting to prevent the error 'default cache already exists'
47 beforeEach(inject((_CacheFactory_)=> {
48 _CacheFactory_.destroyAll();
52 let newCtrl, $controller, $q, $rootScope, $log;
54 beforeEach(inject((_$controller_, _$q_, _$rootScope_, _$log_)=> {
55 [$controller, $q, $rootScope, $log] = [_$controller_, _$q_, _$rootScope_, _$log_];
58 let deferredAdminAppsRoles, deferredUpdateRolesRes;
59 let adminsServiceMock;
61 [deferredAdminAppsRoles, deferredUpdateRolesRes] = [$q.defer(), $q.defer()];
63 adminsServiceMock = jasmine.createSpyObj('adminsServiceMock', ['getAdminAppsRoles', 'updateAdminAppsRoles']);
65 adminsServiceMock.getAdminAppsRoles.and.returnValue(deferredAdminAppsRoles.promise);
66 adminsServiceMock.updateAdminAppsRoles.and.returnValue(deferredUpdateRolesRes.promise);
68 newCtrl = $controller('NewAdminModalCtrl', {
70 adminsService: adminsServiceMock,
75 it('should init default values when loading the controller', ()=> {
76 expect(newCtrl.dialogState).toBe(1);
77 expect(newCtrl.selectedUser).toBe(null);
80 it('should populate admin apps roles and move to the next screen when adminsService.getAdminAppsRoles succeeded', ()=> {
81 let userApps = {appsRoles: [{id: 1, isAdmin: false}, {id: 2, isAdmin: true}]};
82 deferredAdminAppsRoles.resolve(userApps);
84 newCtrl.selectedUser = {orgUserId: 'orgUserId'};
86 newCtrl.getAdminAppsRoles();
89 expect(adminsServiceMock.getAdminAppsRoles).toHaveBeenCalledWith(newCtrl.selectedUser.orgUserId);
90 expect(newCtrl.adminAppsRoles).toEqual(userApps.appsRoles);
91 expect(newCtrl.dialogState).toBe(2);
94 it('should log the error when adminsService.getAdminAppsRoles fails', ()=> {
96 deferredAdminAppsRoles.reject('some error');
98 newCtrl.searchUsersInProgress = false;
99 newCtrl.selectedUser = {orgUserId: 'orgUserId'};
101 newCtrl.getAdminAppsRoles();
104 expect($log.error).toHaveBeenCalled();
106 it('should log the error when trying to getAdminAppsRoles without selecting user ', ()=> {
107 spyOn($log, 'error');
109 newCtrl.searchUsersInProgress = false;
110 newCtrl.selectedUser = null;
112 newCtrl.getAdminAppsRoles();
115 expect($log.error).toHaveBeenCalled();
118 it('should set isAdmin as true when adding app via the dropdown menu', ()=> {
119 newCtrl.adminAppsRoles = [{id: 1, isAdmin: false},{id: 2, isAdmin: true}];
121 $rootScope.$apply('newAdmin.selectedNewApp = null');
122 $rootScope.$apply('newAdmin.selectedNewApp = {id: 1, isAdmin: true}');
124 expect(newCtrl.adminAppsRoles[0].isAdmin).toBe(true);
125 expect(newCtrl.selectedNewApp).toBe(null);
128 it('should close the modal when updating apps roles succeeded', ()=> {
129 $rootScope.closeThisDialog = () => {};
130 spyOn($rootScope,'closeThisDialog');
132 newCtrl.selectedUser = {orgUserId: 'orgUserId'};
133 newCtrl.adminAppsRoles = [{id: 1}];
135 deferredUpdateRolesRes.resolve();
136 newCtrl.updateAdminAppsRoles();
139 expect(adminsServiceMock.updateAdminAppsRoles).toHaveBeenCalledWith({orgUserId: newCtrl.selectedUser.orgUserId, appsRoles: newCtrl.adminAppsRoles});
140 expect($rootScope.closeThisDialog).toHaveBeenCalled();
142 it('should log the error when updating apps roles fails', ()=> {
143 newCtrl.selectedUser = {orgUserId: 'orgUserId'};
144 newCtrl.adminAppsRoles = [{id: 1}];
147 deferredUpdateRolesRes.reject();
148 newCtrl.updateAdminAppsRoles();
150 expect($log.error).toHaveBeenCalled();