]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-discovery-modal/iscsi-target-discovery-modal.component.spec.ts
update ceph source to reef 18.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / block / iscsi-target-discovery-modal / iscsi-target-discovery-modal.component.spec.ts
1 import {
2 HttpClientTestingModule,
3 HttpTestingController,
4 TestRequest
5 } from '@angular/common/http/testing';
6 import { ComponentFixture, TestBed } from '@angular/core/testing';
7 import { ReactiveFormsModule } from '@angular/forms';
8 import { By } from '@angular/platform-browser';
9 import { RouterTestingModule } from '@angular/router/testing';
10
11 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
12 import { ToastrModule } from 'ngx-toastr';
13
14 import { Permission } from '~/app/shared/models/permissions';
15 import { SharedModule } from '~/app/shared/shared.module';
16 import { configureTestBed, FormHelper, IscsiHelper } from '~/testing/unit-test-helper';
17 import { IscsiTargetDiscoveryModalComponent } from './iscsi-target-discovery-modal.component';
18
19 describe('IscsiTargetDiscoveryModalComponent', () => {
20 let component: IscsiTargetDiscoveryModalComponent;
21 let fixture: ComponentFixture<IscsiTargetDiscoveryModalComponent>;
22 let httpTesting: HttpTestingController;
23 let req: TestRequest;
24
25 const elem = (css: string) => fixture.debugElement.query(By.css(css));
26 const elemDisabled = (css: string) => elem(css).nativeElement.disabled;
27
28 configureTestBed({
29 declarations: [IscsiTargetDiscoveryModalComponent],
30 imports: [
31 HttpClientTestingModule,
32 ReactiveFormsModule,
33 SharedModule,
34 ToastrModule.forRoot(),
35 RouterTestingModule
36 ],
37 providers: [NgbActiveModal]
38 });
39
40 beforeEach(() => {
41 fixture = TestBed.createComponent(IscsiTargetDiscoveryModalComponent);
42 component = fixture.componentInstance;
43 httpTesting = TestBed.inject(HttpTestingController);
44 });
45
46 describe('with update permissions', () => {
47 beforeEach(() => {
48 component.permission = new Permission(['update']);
49 fixture.detectChanges();
50 req = httpTesting.expectOne('api/iscsi/discoveryauth');
51 });
52
53 it('should create', () => {
54 expect(component).toBeTruthy();
55 });
56
57 it('should create form', () => {
58 expect(component.discoveryForm.value).toEqual({
59 user: '',
60 password: '',
61 mutual_user: '',
62 mutual_password: ''
63 });
64 });
65
66 it('should patch form', () => {
67 req.flush({
68 user: 'foo',
69 password: 'bar',
70 mutual_user: 'mutual_foo',
71 mutual_password: 'mutual_bar'
72 });
73 expect(component.discoveryForm.value).toEqual({
74 user: 'foo',
75 password: 'bar',
76 mutual_user: 'mutual_foo',
77 mutual_password: 'mutual_bar'
78 });
79 });
80
81 it('should submit new values', () => {
82 component.discoveryForm.patchValue({
83 user: 'new_user',
84 password: 'new_pass',
85 mutual_user: 'mutual_new_user',
86 mutual_password: 'mutual_new_pass'
87 });
88 component.submitAction();
89
90 const submit_req = httpTesting.expectOne('api/iscsi/discoveryauth');
91 expect(submit_req.request.method).toBe('PUT');
92 expect(submit_req.request.body).toEqual({
93 user: 'new_user',
94 password: 'new_pass',
95 mutual_user: 'mutual_new_user',
96 mutual_password: 'mutual_new_pass'
97 });
98 });
99
100 it('should enable form if user has update permission', () => {
101 expect(elemDisabled('input#user')).toBeFalsy();
102 expect(elemDisabled('input#password')).toBeFalsy();
103 expect(elemDisabled('input#mutual_user')).toBeFalsy();
104 expect(elemDisabled('input#mutual_password')).toBeFalsy();
105 expect(elem('cd-submit-button')).toBeDefined();
106 });
107 });
108
109 it('should disabled form if user does not have update permission', () => {
110 component.permission = new Permission(['read', 'create', 'delete']);
111 fixture.detectChanges();
112 req = httpTesting.expectOne('api/iscsi/discoveryauth');
113
114 expect(elemDisabled('input#user')).toBeTruthy();
115 expect(elemDisabled('input#password')).toBeTruthy();
116 expect(elemDisabled('input#mutual_user')).toBeTruthy();
117 expect(elemDisabled('input#mutual_password')).toBeTruthy();
118 expect(elem('cd-submit-button')).toBeNull();
119 });
120
121 it('should validate authentication', () => {
122 component.permission = new Permission(['read', 'create', 'update', 'delete']);
123 fixture.detectChanges();
124 const control = component.discoveryForm;
125 const formHelper = new FormHelper(control);
126 formHelper.expectValid(control);
127
128 IscsiHelper.validateUser(formHelper, 'user');
129 IscsiHelper.validatePassword(formHelper, 'password');
130 IscsiHelper.validateUser(formHelper, 'mutual_user');
131 IscsiHelper.validatePassword(formHelper, 'mutual_password');
132 });
133 });