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';
6 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
7 import { ToastrModule } from 'ngx-toastr';
8 import { of } from 'rxjs';
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 { BootstrapImportModalComponent } from './bootstrap-import-modal.component';
20 describe('BootstrapImportModalComponent', () => {
21 let component: BootstrapImportModalComponent;
22 let fixture: ComponentFixture<BootstrapImportModalComponent>;
23 let notificationService: NotificationService;
24 let rbdMirroringService: RbdMirroringService;
25 let formHelper: FormHelper;
28 declarations: [BootstrapImportModalComponent],
30 HttpClientTestingModule,
34 ToastrModule.forRoot()
36 providers: [NgbActiveModal, i18nProviders]
40 fixture = TestBed.createComponent(BootstrapImportModalComponent);
41 component = fixture.componentInstance;
42 component.siteName = 'site-A';
44 notificationService = TestBed.inject(NotificationService);
45 spyOn(notificationService, 'show').and.stub();
47 rbdMirroringService = TestBed.inject(RbdMirroringService);
49 formHelper = new FormHelper(component.importBootstrapForm);
51 spyOn(rbdMirroringService, 'getSiteName').and.callFake(() => of({ site_name: 'site-A' }));
52 spyOn(rbdMirroringService, 'subscribeSummary').and.callFake((call) =>
56 { name: 'pool1', mirror_mode: 'disabled' },
57 { name: 'pool2', mirror_mode: 'disabled' },
58 { name: 'pool3', mirror_mode: 'disabled' }
65 it('should import', () => {
66 expect(component).toBeTruthy();
69 describe('import token', () => {
71 spyOn(rbdMirroringService, 'refresh').and.stub();
72 spyOn(component.activeModal, 'close').and.callThrough();
73 fixture.detectChanges();
77 expect(rbdMirroringService.getSiteName).toHaveBeenCalledTimes(1);
78 expect(rbdMirroringService.subscribeSummary).toHaveBeenCalledTimes(1);
79 expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
82 it('should generate a bootstrap token', () => {
83 spyOn(rbdMirroringService, 'setSiteName').and.callFake(() => of({ site_name: 'new-site-A' }));
84 spyOn(rbdMirroringService, 'updatePool').and.callFake(() => of({}));
85 spyOn(rbdMirroringService, 'importBootstrapToken').and.callFake(() => of({ token: 'token' }));
87 component.importBootstrapForm.patchValue({
88 siteName: 'new-site-A',
89 pools: { pool1: true, pool3: true },
93 expect(rbdMirroringService.setSiteName).toHaveBeenCalledWith('new-site-A');
94 expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool1', {
97 expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool3', {
100 expect(rbdMirroringService.importBootstrapToken).toHaveBeenCalledWith(
105 expect(rbdMirroringService.importBootstrapToken).toHaveBeenCalledWith(
113 describe('form validation', () => {
115 fixture.detectChanges();
118 it('should require a site name', () => {
119 formHelper.expectErrorChange('siteName', '', 'required');
122 it('should require at least one pool', () => {
123 formHelper.expectError(component.importBootstrapForm.get('pools'), 'requirePool');
126 it('should require a token', () => {
127 formHelper.expectErrorChange('token', '', 'required');
130 it('should verify token is base64-encoded JSON', () => {
131 formHelper.expectErrorChange('token', 'VEVTVA==', 'invalidToken');
132 formHelper.expectErrorChange('token', 'e2RmYXNqZGZrbH0=', 'invalidToken');