4beb4819ef8a01cfad547a035b33eaa28b1daca2
[portal.git] / ecomp-portal-FE-common / client / app / views / admins / add-admin-dialogs / new-admin.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/8/15.
40  */
41 'use strict';
42
43 describe('Controller: NewAdminCtrl ', () => {
44     beforeEach(module('ecompApp'));
45
46     //destroy $http default cache before starting to prevent the error 'default cache already exists'
47     beforeEach(inject((_CacheFactory_)=> {
48         _CacheFactory_.destroyAll();
49     }));
50
51
52     let newCtrl, $controller, $q, $rootScope, $log;
53
54     beforeEach(inject((_$controller_, _$q_, _$rootScope_, _$log_)=> {
55         [$controller, $q, $rootScope, $log] = [_$controller_, _$q_, _$rootScope_, _$log_];
56     }));
57
58     let deferredAdminAppsRoles, deferredUpdateRolesRes;
59     let adminsServiceMock;
60     beforeEach(()=> {
61         [deferredAdminAppsRoles, deferredUpdateRolesRes] = [$q.defer(), $q.defer()];
62
63         adminsServiceMock = jasmine.createSpyObj('adminsServiceMock', ['getAdminAppsRoles', 'updateAdminAppsRoles']);
64
65         adminsServiceMock.getAdminAppsRoles.and.returnValue(deferredAdminAppsRoles.promise);
66         adminsServiceMock.updateAdminAppsRoles.and.returnValue(deferredUpdateRolesRes.promise);
67
68         newCtrl = $controller('NewAdminModalCtrl', {
69             $log: $log,
70             adminsService: adminsServiceMock,
71             $scope: $rootScope
72         });
73     });
74
75     it('should init default values when loading the controller', ()=> {
76         expect(newCtrl.dialogState).toBe(1);
77         expect(newCtrl.selectedUser).toBe(null);
78     });
79
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);
83
84         newCtrl.selectedUser = {orgUserId: 'orgUserId'};
85
86         newCtrl.getAdminAppsRoles();
87         $rootScope.$apply();
88
89         expect(adminsServiceMock.getAdminAppsRoles).toHaveBeenCalledWith(newCtrl.selectedUser.orgUserId);
90         expect(newCtrl.adminAppsRoles).toEqual(userApps.appsRoles);
91         expect(newCtrl.dialogState).toBe(2);
92     });
93
94     it('should  log the error when adminsService.getAdminAppsRoles fails', ()=> {
95         spyOn($log, 'error');
96         deferredAdminAppsRoles.reject('some error');
97
98         newCtrl.searchUsersInProgress = false;
99         newCtrl.selectedUser = {orgUserId: 'orgUserId'};
100
101         newCtrl.getAdminAppsRoles();
102         $rootScope.$apply();
103
104         expect($log.error).toHaveBeenCalled();
105     });
106     it('should  log the error when trying to getAdminAppsRoles without selecting user ', ()=> {
107         spyOn($log, 'error');
108
109         newCtrl.searchUsersInProgress = false;
110         newCtrl.selectedUser = null;
111
112         newCtrl.getAdminAppsRoles();
113         $rootScope.$apply();
114
115         expect($log.error).toHaveBeenCalled();
116     });
117
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}];
120         //simulate UI change
121         $rootScope.$apply('newAdmin.selectedNewApp = null');
122         $rootScope.$apply('newAdmin.selectedNewApp = {id: 1, isAdmin: true}');
123
124         expect(newCtrl.adminAppsRoles[0].isAdmin).toBe(true);
125         expect(newCtrl.selectedNewApp).toBe(null);
126     });
127
128     it('should close the modal when updating apps roles succeeded', ()=> {
129         $rootScope.closeThisDialog = () => {};
130         spyOn($rootScope,'closeThisDialog');
131
132         newCtrl.selectedUser = {orgUserId: 'orgUserId'};
133         newCtrl.adminAppsRoles = [{id: 1}];
134
135         deferredUpdateRolesRes.resolve();
136         newCtrl.updateAdminAppsRoles();
137         $rootScope.$apply();
138
139         expect(adminsServiceMock.updateAdminAppsRoles).toHaveBeenCalledWith({orgUserId: newCtrl.selectedUser.orgUserId, appsRoles: newCtrl.adminAppsRoles});
140         expect($rootScope.closeThisDialog).toHaveBeenCalled();
141     });
142     it('should log the error when updating apps roles fails', ()=> {
143         newCtrl.selectedUser = {orgUserId: 'orgUserId'};
144         newCtrl.adminAppsRoles = [{id: 1}];
145
146         spyOn($log,'error');
147         deferredUpdateRolesRes.reject();
148         newCtrl.updateAdminAppsRoles();
149         $rootScope.$apply();
150         expect($log.error).toHaveBeenCalled();
151     });
152 });