]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
7cd8e5fb3a8d29dbf25cf316686ca4bf7f1345c1
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
7 import { ToastrModule } from 'ngx-toastr';
8 import { of } from 'rxjs';
9
10 import {
11   configureTestBed,
12   FormHelper,
13   i18nProviders
14 } from '../../../../../testing/unit-test-helper';
15 import { RbdMirroringService } from '../../../../shared/api/rbd-mirroring.service';
16 import { NotificationService } from '../../../../shared/services/notification.service';
17 import { SharedModule } from '../../../../shared/shared.module';
18 import { PoolEditPeerModalComponent } from './pool-edit-peer-modal.component';
19 import { PoolEditPeerResponseModel } from './pool-edit-peer-response.model';
20
21 describe('PoolEditPeerModalComponent', () => {
22   let component: PoolEditPeerModalComponent;
23   let fixture: ComponentFixture<PoolEditPeerModalComponent>;
24   let notificationService: NotificationService;
25   let rbdMirroringService: RbdMirroringService;
26   let formHelper: FormHelper;
27
28   configureTestBed({
29     declarations: [PoolEditPeerModalComponent],
30     imports: [
31       HttpClientTestingModule,
32       ReactiveFormsModule,
33       RouterTestingModule,
34       SharedModule,
35       ToastrModule.forRoot()
36     ],
37     providers: [BsModalRef, BsModalService, i18nProviders]
38   });
39
40   beforeEach(() => {
41     fixture = TestBed.createComponent(PoolEditPeerModalComponent);
42     component = fixture.componentInstance;
43     component.mode = 'add';
44     component.poolName = 'somePool';
45
46     notificationService = TestBed.inject(NotificationService);
47     spyOn(notificationService, 'show').and.stub();
48
49     rbdMirroringService = TestBed.inject(RbdMirroringService);
50
51     formHelper = new FormHelper(component.editPeerForm);
52   });
53
54   it('should create', () => {
55     expect(component).toBeTruthy();
56   });
57
58   describe('add pool peer', () => {
59     beforeEach(() => {
60       component.mode = 'add';
61       component.peerUUID = undefined;
62       spyOn(rbdMirroringService, 'refresh').and.stub();
63       spyOn(component.modalRef, 'hide').and.callThrough();
64       fixture.detectChanges();
65     });
66
67     afterEach(() => {
68       expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
69       expect(component.modalRef.hide).toHaveBeenCalledTimes(1);
70     });
71
72     it('should call addPeer', () => {
73       spyOn(rbdMirroringService, 'addPeer').and.callFake(() => of(''));
74
75       component.editPeerForm.patchValue({
76         clusterName: 'cluster',
77         clientID: 'id',
78         monAddr: 'mon_host',
79         key: 'dGVzdA=='
80       });
81
82       component.update();
83       expect(rbdMirroringService.addPeer).toHaveBeenCalledWith('somePool', {
84         cluster_name: 'cluster',
85         client_id: 'id',
86         mon_host: 'mon_host',
87         key: 'dGVzdA=='
88       });
89     });
90   });
91
92   describe('edit pool peer', () => {
93     beforeEach(() => {
94       component.mode = 'edit';
95       component.peerUUID = 'somePeer';
96
97       const response = new PoolEditPeerResponseModel();
98       response.uuid = 'somePeer';
99       response.cluster_name = 'cluster';
100       response.client_id = 'id';
101       response.mon_host = '1.2.3.4:1234';
102       response.key = 'dGVzdA==';
103
104       spyOn(rbdMirroringService, 'getPeer').and.callFake(() => of(response));
105       spyOn(rbdMirroringService, 'refresh').and.stub();
106       spyOn(component.modalRef, 'hide').and.callThrough();
107       fixture.detectChanges();
108     });
109
110     afterEach(() => {
111       expect(rbdMirroringService.getPeer).toHaveBeenCalledWith('somePool', 'somePeer');
112       expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
113       expect(component.modalRef.hide).toHaveBeenCalledTimes(1);
114     });
115
116     it('should call updatePeer', () => {
117       spyOn(rbdMirroringService, 'updatePeer').and.callFake(() => of(''));
118
119       component.update();
120       expect(rbdMirroringService.updatePeer).toHaveBeenCalledWith('somePool', 'somePeer', {
121         cluster_name: 'cluster',
122         client_id: 'id',
123         mon_host: '1.2.3.4:1234',
124         key: 'dGVzdA=='
125       });
126     });
127   });
128
129   describe('form validation', () => {
130     beforeEach(() => {
131       fixture.detectChanges();
132     });
133
134     it('should validate cluster name', () => {
135       formHelper.expectErrorChange('clusterName', '', 'required');
136       formHelper.expectErrorChange('clusterName', ' ', 'invalidClusterName');
137     });
138
139     it('should validate client ID', () => {
140       formHelper.expectErrorChange('clientID', '', 'required');
141       formHelper.expectErrorChange('clientID', 'client.id', 'invalidClientID');
142     });
143
144     it('should validate monitor address', () => {
145       formHelper.expectErrorChange('monAddr', '@', 'invalidMonAddr');
146     });
147
148     it('should validate key', () => {
149       formHelper.expectErrorChange('key', '(', 'invalidKey');
150     });
151   });
152 });