]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-form/role-form.component.spec.ts
update ceph source to reef 18.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / core / auth / role-form / role-form.component.spec.ts
CommitLineData
11fdf7f2
TL
1import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2import { Component } from '@angular/core';
3import { ComponentFixture, TestBed } from '@angular/core/testing';
4import { ReactiveFormsModule } from '@angular/forms';
5import { Router, Routes } from '@angular/router';
6import { RouterTestingModule } from '@angular/router/testing';
7
494da23a 8import { ToastrModule } from 'ngx-toastr';
11fdf7f2
TL
9import { of } from 'rxjs';
10
f67539c2
TL
11import { RoleService } from '~/app/shared/api/role.service';
12import { ScopeService } from '~/app/shared/api/scope.service';
13import { LoadingPanelComponent } from '~/app/shared/components/loading-panel/loading-panel.component';
14import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
15import { NotificationService } from '~/app/shared/services/notification.service';
16import { SharedModule } from '~/app/shared/shared.module';
17import { configureTestBed, FormHelper } from '~/testing/unit-test-helper';
11fdf7f2
TL
18import { RoleFormComponent } from './role-form.component';
19import { RoleFormModel } from './role-form.model';
20
21describe('RoleFormComponent', () => {
22 let component: RoleFormComponent;
23 let form: CdFormGroup;
24 let fixture: ComponentFixture<RoleFormComponent>;
25 let httpTesting: HttpTestingController;
26 let roleService: RoleService;
27 let router: Router;
9f95a23c 28 const setUrl = (url: string) => Object.defineProperty(router, 'url', { value: url });
11fdf7f2
TL
29
30 @Component({ selector: 'cd-fake', template: '' })
31 class FakeComponent {}
32
33 const routes: Routes = [{ path: 'roles', component: FakeComponent }];
34
f67539c2
TL
35 configureTestBed(
36 {
37 imports: [
38 RouterTestingModule.withRoutes(routes),
39 HttpClientTestingModule,
40 ReactiveFormsModule,
41 ToastrModule.forRoot(),
42 SharedModule
43 ],
44 declarations: [RoleFormComponent, FakeComponent]
45 },
46 [LoadingPanelComponent]
47 );
11fdf7f2
TL
48
49 beforeEach(() => {
50 fixture = TestBed.createComponent(RoleFormComponent);
51 component = fixture.componentInstance;
52 form = component.roleForm;
f67539c2
TL
53 httpTesting = TestBed.inject(HttpTestingController);
54 roleService = TestBed.inject(RoleService);
55 router = TestBed.inject(Router);
11fdf7f2
TL
56 spyOn(router, 'navigate');
57 fixture.detectChanges();
f67539c2 58 const notify = TestBed.inject(NotificationService);
11fdf7f2
TL
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 });
11fdf7f2
TL
104 });
105
106 describe('edit mode', () => {
aee94f69
TL
107 let formHelper: FormHelper;
108
11fdf7f2
TL
109 const role: RoleFormModel = {
110 name: 'role1',
111 description: 'Role 1',
112 scopes_permissions: { osd: ['read', 'create'] }
113 };
114 const scopes = ['osd', 'user'];
115 beforeEach(() => {
aee94f69 116 formHelper = new FormHelper(form);
11fdf7f2 117 spyOn(roleService, 'get').and.callFake(() => of(role));
f67539c2 118 spyOn(TestBed.inject(ScopeService), 'list').and.callFake(() => of(scopes));
11fdf7f2
TL
119 setUrl('/user-management/roles/edit/role1');
120 component.ngOnInit();
121 const reqScopes = httpTesting.expectOne('ui-api/scope');
122 expect(reqScopes.request.method).toBe('GET');
123 });
124
125 afterEach(() => {
126 httpTesting.verify();
127 });
128
129 it('should disable fields if editing', () => {
130 expect(form.get('name').disabled).toBeTruthy();
131 ['description', 'scopes_permissions'].forEach((key) =>
132 expect(form.get(key).disabled).toBeFalsy()
133 );
134 });
135
136 it('should set control values', () => {
137 ['name', 'description', 'scopes_permissions'].forEach((key) =>
138 expect(form.getValue(key)).toBe(role[key])
139 );
140 });
141
142 it('should set mode', () => {
143 expect(component.mode).toBe('editing');
144 });
145
146 it('should submit', () => {
aee94f69
TL
147 formHelper.setValue('scopes_permissions', {
148 osd: ['read', 'update'],
149 user: ['read']
150 });
11fdf7f2
TL
151 component.submit();
152 const roleReq = httpTesting.expectOne(`api/role/${role.name}`);
153 expect(roleReq.request.method).toBe('PUT');
154 expect(roleReq.request.body).toEqual({
155 name: 'role1',
156 description: 'Role 1',
157 scopes_permissions: { osd: ['read', 'update'], user: ['read'] }
158 });
159 roleReq.flush({});
160 expect(router.navigate).toHaveBeenCalledWith(['/user-management/roles']);
161 });
162 });
163});