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