]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
f6236e707f070cfe41243c3c556ed84887458690
[ceph.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 { BsModalRef } from 'ngx-bootstrap/modal';
12 import { ToastrModule } from 'ngx-toastr';
13
14 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
15 import { Permission } from '../../../shared/models/permissions';
16 import { SharedModule } from '../../../shared/shared.module';
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) => fixture.debugElement.query(By.css(css));
26   const elemDisabled = (css) => 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: [i18nProviders, BsModalRef]
38   });
39
40   beforeEach(() => {
41     fixture = TestBed.createComponent(IscsiTargetDiscoveryModalComponent);
42     component = fixture.componentInstance;
43     httpTesting = TestBed.get(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 });