]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-discovery-modal/iscsi-target-discovery-modal.component.spec.ts
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / block / iscsi-target-discovery-modal / iscsi-target-discovery-modal.component.spec.ts
CommitLineData
11fdf7f2
TL
1import {
2 HttpClientTestingModule,
3 HttpTestingController,
4 TestRequest
5} from '@angular/common/http/testing';
6import { ComponentFixture, TestBed } from '@angular/core/testing';
7import { ReactiveFormsModule } from '@angular/forms';
81eedcae 8import { By } from '@angular/platform-browser';
11fdf7f2
TL
9import { RouterTestingModule } from '@angular/router/testing';
10
f67539c2 11import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
494da23a 12import { ToastrModule } from 'ngx-toastr';
11fdf7f2 13
f67539c2
TL
14import { Permission } from '~/app/shared/models/permissions';
15import { SharedModule } from '~/app/shared/shared.module';
16import { configureTestBed, FormHelper, IscsiHelper } from '~/testing/unit-test-helper';
11fdf7f2
TL
17import { IscsiTargetDiscoveryModalComponent } from './iscsi-target-discovery-modal.component';
18
19describe('IscsiTargetDiscoveryModalComponent', () => {
20 let component: IscsiTargetDiscoveryModalComponent;
21 let fixture: ComponentFixture<IscsiTargetDiscoveryModalComponent>;
22 let httpTesting: HttpTestingController;
23 let req: TestRequest;
24
9f95a23c
TL
25 const elem = (css: string) => fixture.debugElement.query(By.css(css));
26 const elemDisabled = (css: string) => elem(css).nativeElement.disabled;
81eedcae 27
11fdf7f2
TL
28 configureTestBed({
29 declarations: [IscsiTargetDiscoveryModalComponent],
30 imports: [
31 HttpClientTestingModule,
32 ReactiveFormsModule,
33 SharedModule,
494da23a 34 ToastrModule.forRoot(),
11fdf7f2
TL
35 RouterTestingModule
36 ],
f67539c2 37 providers: [NgbActiveModal]
11fdf7f2
TL
38 });
39
40 beforeEach(() => {
41 fixture = TestBed.createComponent(IscsiTargetDiscoveryModalComponent);
42 component = fixture.componentInstance;
f67539c2 43 httpTesting = TestBed.inject(HttpTestingController);
11fdf7f2
TL
44 });
45
81eedcae
TL
46 describe('with update permissions', () => {
47 beforeEach(() => {
48 component.permission = new Permission(['update']);
49 fixture.detectChanges();
50 req = httpTesting.expectOne('api/iscsi/discoveryauth');
51 });
11fdf7f2 52
81eedcae
TL
53 it('should create', () => {
54 expect(component).toBeTruthy();
11fdf7f2 55 });
11fdf7f2 56
81eedcae
TL
57 it('should create form', () => {
58 expect(component.discoveryForm.value).toEqual({
59 user: '',
60 password: '',
61 mutual_user: '',
62 mutual_password: ''
63 });
11fdf7f2 64 });
81eedcae
TL
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 });
11fdf7f2 79 });
11fdf7f2 80
81eedcae
TL
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 });
11fdf7f2 98 });
11fdf7f2 99
81eedcae
TL
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();
11fdf7f2
TL
106 });
107 });
81eedcae
TL
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 });
1911f103
TL
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 });
11fdf7f2 133});