]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-form/role-form.component.spec.ts
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / core / auth / role-form / role-form.component.spec.ts
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { Component } from '@angular/core';
3 import { ComponentFixture, TestBed } from '@angular/core/testing';
4 import { ReactiveFormsModule } from '@angular/forms';
5 import { Router, Routes } from '@angular/router';
6 import { RouterTestingModule } from '@angular/router/testing';
7
8 import { ToastrModule } from 'ngx-toastr';
9 import { of } from 'rxjs';
10
11 import { configureTestBed, FormHelper, i18nProviders } from '../../../../testing/unit-test-helper';
12 import { RoleService } from '../../../shared/api/role.service';
13 import { ScopeService } from '../../../shared/api/scope.service';
14 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
15 import { NotificationService } from '../../../shared/services/notification.service';
16 import { SharedModule } from '../../../shared/shared.module';
17 import { RoleFormComponent } from './role-form.component';
18 import { RoleFormModel } from './role-form.model';
19
20 describe('RoleFormComponent', () => {
21 let component: RoleFormComponent;
22 let form: CdFormGroup;
23 let fixture: ComponentFixture<RoleFormComponent>;
24 let httpTesting: HttpTestingController;
25 let roleService: RoleService;
26 let router: Router;
27 const setUrl = (url) => Object.defineProperty(router, 'url', { value: url });
28
29 @Component({ selector: 'cd-fake', template: '' })
30 class FakeComponent {}
31
32 const routes: Routes = [{ path: 'roles', component: FakeComponent }];
33
34 configureTestBed(
35 {
36 imports: [
37 RouterTestingModule.withRoutes(routes),
38 HttpClientTestingModule,
39 ReactiveFormsModule,
40 ToastrModule.forRoot(),
41 SharedModule
42 ],
43 declarations: [RoleFormComponent, FakeComponent],
44 providers: i18nProviders
45 },
46 true
47 );
48
49 beforeEach(() => {
50 fixture = TestBed.createComponent(RoleFormComponent);
51 component = fixture.componentInstance;
52 form = component.roleForm;
53 httpTesting = TestBed.get(HttpTestingController);
54 roleService = TestBed.get(RoleService);
55 router = TestBed.get(Router);
56 spyOn(router, 'navigate');
57 fixture.detectChanges();
58 const notify = TestBed.get(NotificationService);
59 spyOn(notify, 'show');
60 });
61
62 it('should create', () => {
63 expect(component).toBeTruthy();
64 expect(form).toBeTruthy();
65 });
66
67 describe('create mode', () => {
68 let formHelper: FormHelper;
69
70 beforeEach(() => {
71 setUrl('/user-management/roles/add');
72 component.ngOnInit();
73 formHelper = new FormHelper(form);
74 });
75
76 it('should not disable fields', () => {
77 ['name', 'description', 'scopes_permissions'].forEach((key) =>
78 expect(form.get(key).disabled).toBeFalsy()
79 );
80 });
81
82 it('should validate name required', () => {
83 formHelper.expectErrorChange('name', '', 'required');
84 });
85
86 it('should set mode', () => {
87 expect(component.mode).toBeUndefined();
88 });
89
90 it('should submit', () => {
91 const role: RoleFormModel = {
92 name: 'role1',
93 description: 'Role 1',
94 scopes_permissions: { osd: ['read'] }
95 };
96 formHelper.setMultipleValues(role);
97 component.submit();
98 const roleReq = httpTesting.expectOne('api/role');
99 expect(roleReq.request.method).toBe('POST');
100 expect(roleReq.request.body).toEqual(role);
101 roleReq.flush({});
102 expect(router.navigate).toHaveBeenCalledWith(['/user-management/roles']);
103 });
104
105 it('should check all perms for a scope', () => {
106 formHelper.setValue('scopes_permissions', { cephfs: ['read'] });
107 component.onClickCellCheckbox('grafana', 'scope');
108 const scopes_permissions = form.getValue('scopes_permissions');
109 expect(Object.keys(scopes_permissions)).toContain('grafana');
110 expect(scopes_permissions['grafana']).toEqual(['create', 'delete', 'read', 'update']);
111 });
112
113 it('should uncheck all perms for a scope', () => {
114 formHelper.setValue('scopes_permissions', { cephfs: ['read', 'create', 'update', 'delete'] });
115 component.onClickCellCheckbox('cephfs', 'scope');
116 const scopes_permissions = form.getValue('scopes_permissions');
117 expect(Object.keys(scopes_permissions)).not.toContain('cephfs');
118 });
119
120 it('should uncheck all scopes and perms', () => {
121 component.scopes = ['cephfs', 'grafana'];
122 formHelper.setValue('scopes_permissions', {
123 cephfs: ['read', 'delete'],
124 grafana: ['update']
125 });
126 component.onClickHeaderCheckbox('scope', ({
127 target: { checked: false }
128 } as unknown) as Event);
129 const scopes_permissions = form.getValue('scopes_permissions');
130 expect(scopes_permissions).toEqual({});
131 });
132
133 it('should check all scopes and perms', () => {
134 component.scopes = ['cephfs', 'grafana'];
135 formHelper.setValue('scopes_permissions', {
136 cephfs: ['create', 'update'],
137 grafana: ['delete']
138 });
139 component.onClickHeaderCheckbox('scope', ({ target: { checked: true } } as unknown) as Event);
140 const scopes_permissions = form.getValue('scopes_permissions');
141 const keys = Object.keys(scopes_permissions);
142 expect(keys).toEqual(['cephfs', 'grafana']);
143 keys.forEach((key) => {
144 expect(scopes_permissions[key].sort()).toEqual(['create', 'delete', 'read', 'update']);
145 });
146 });
147
148 it('should check if column is checked', () => {
149 component.scopes_permissions = [
150 { scope: 'a', read: true, create: true, update: true, delete: true },
151 { scope: 'b', read: false, create: true, update: false, delete: true }
152 ];
153 expect(component.isRowChecked('a')).toBeTruthy();
154 expect(component.isRowChecked('b')).toBeFalsy();
155 expect(component.isRowChecked('c')).toBeFalsy();
156 });
157
158 it('should check if header is checked', () => {
159 component.scopes_permissions = [
160 { scope: 'a', read: true, create: true, update: false, delete: true },
161 { scope: 'b', read: false, create: true, update: false, delete: true }
162 ];
163 expect(component.isHeaderChecked('read')).toBeFalsy();
164 expect(component.isHeaderChecked('create')).toBeTruthy();
165 expect(component.isHeaderChecked('update')).toBeFalsy();
166 });
167 });
168
169 describe('edit mode', () => {
170 const role: RoleFormModel = {
171 name: 'role1',
172 description: 'Role 1',
173 scopes_permissions: { osd: ['read', 'create'] }
174 };
175 const scopes = ['osd', 'user'];
176 beforeEach(() => {
177 spyOn(roleService, 'get').and.callFake(() => of(role));
178 spyOn(TestBed.get(ScopeService), 'list').and.callFake(() => of(scopes));
179 setUrl('/user-management/roles/edit/role1');
180 component.ngOnInit();
181 const reqScopes = httpTesting.expectOne('ui-api/scope');
182 expect(reqScopes.request.method).toBe('GET');
183 });
184
185 afterEach(() => {
186 httpTesting.verify();
187 });
188
189 it('should disable fields if editing', () => {
190 expect(form.get('name').disabled).toBeTruthy();
191 ['description', 'scopes_permissions'].forEach((key) =>
192 expect(form.get(key).disabled).toBeFalsy()
193 );
194 });
195
196 it('should set control values', () => {
197 ['name', 'description', 'scopes_permissions'].forEach((key) =>
198 expect(form.getValue(key)).toBe(role[key])
199 );
200 });
201
202 it('should set mode', () => {
203 expect(component.mode).toBe('editing');
204 });
205
206 it('should submit', () => {
207 component.onClickCellCheckbox('osd', 'update');
208 component.onClickCellCheckbox('osd', 'create');
209 component.onClickCellCheckbox('user', 'read');
210 component.submit();
211 const roleReq = httpTesting.expectOne(`api/role/${role.name}`);
212 expect(roleReq.request.method).toBe('PUT');
213 expect(roleReq.request.body).toEqual({
214 name: 'role1',
215 description: 'Role 1',
216 scopes_permissions: { osd: ['read', 'update'], user: ['read'] }
217 });
218 roleReq.flush({});
219 expect(router.navigate).toHaveBeenCalledWith(['/user-management/roles']);
220 });
221 });
222 });