summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/bootstrap-import-modal/bootstrap-import-modal.component.spec.ts
blob: 93c1405df9a243d40e32b132c360863b142ecf6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { ToastrModule } from 'ngx-toastr';
import { of } from 'rxjs';

import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
import { NotificationService } from '~/app/shared/services/notification.service';
import { SharedModule } from '~/app/shared/shared.module';
import { configureTestBed, FormHelper } from '~/testing/unit-test-helper';
import { BootstrapImportModalComponent } from './bootstrap-import-modal.component';

describe('BootstrapImportModalComponent', () => {
  let component: BootstrapImportModalComponent;
  let fixture: ComponentFixture<BootstrapImportModalComponent>;
  let notificationService: NotificationService;
  let rbdMirroringService: RbdMirroringService;
  let formHelper: FormHelper;

  configureTestBed({
    declarations: [BootstrapImportModalComponent],
    imports: [
      HttpClientTestingModule,
      ReactiveFormsModule,
      RouterTestingModule,
      SharedModule,
      ToastrModule.forRoot()
    ],
    providers: [NgbActiveModal]
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(BootstrapImportModalComponent);
    component = fixture.componentInstance;
    component.siteName = 'site-A';

    notificationService = TestBed.inject(NotificationService);
    spyOn(notificationService, 'show').and.stub();

    rbdMirroringService = TestBed.inject(RbdMirroringService);

    formHelper = new FormHelper(component.importBootstrapForm);

    spyOn(rbdMirroringService, 'getSiteName').and.callFake(() => of({ site_name: 'site-A' }));
    spyOn(rbdMirroringService, 'subscribeSummary').and.callFake((call) =>
      of({
        content_data: {
          pools: [
            { name: 'pool1', mirror_mode: 'disabled' },
            { name: 'pool2', mirror_mode: 'disabled' },
            { name: 'pool3', mirror_mode: 'disabled' }
          ]
        }
      }).subscribe(call)
    );
  });

  it('should import', () => {
    expect(component).toBeTruthy();
  });

  describe('import token', () => {
    beforeEach(() => {
      spyOn(rbdMirroringService, 'refresh').and.stub();
      spyOn(component.activeModal, 'close').and.callThrough();
      fixture.detectChanges();
    });

    afterEach(() => {
      expect(rbdMirroringService.getSiteName).toHaveBeenCalledTimes(1);
      expect(rbdMirroringService.subscribeSummary).toHaveBeenCalledTimes(1);
      expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
    });

    it('should generate a bootstrap token', () => {
      spyOn(rbdMirroringService, 'setSiteName').and.callFake(() => of({ site_name: 'new-site-A' }));
      spyOn(rbdMirroringService, 'updatePool').and.callFake(() => of({}));
      spyOn(rbdMirroringService, 'importBootstrapToken').and.callFake(() => of({ token: 'token' }));

      component.importBootstrapForm.patchValue({
        siteName: 'new-site-A',
        pools: { pool1: true, pool3: true },
        token: 'e30='
      });
      component.import();
      expect(rbdMirroringService.setSiteName).toHaveBeenCalledWith('new-site-A');
      expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool1', {
        mirror_mode: 'image'
      });
      expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool3', {
        mirror_mode: 'image'
      });
      expect(rbdMirroringService.importBootstrapToken).toHaveBeenCalledWith(
        'pool1',
        'rx-tx',
        'e30='
      );
      expect(rbdMirroringService.importBootstrapToken).toHaveBeenCalledWith(
        'pool3',
        'rx-tx',
        'e30='
      );
    });
  });

  describe('form validation', () => {
    beforeEach(() => {
      fixture.detectChanges();
    });

    it('should require a site name', () => {
      formHelper.expectErrorChange('siteName', '', 'required');
    });

    it('should require at least one pool', () => {
      formHelper.expectError(component.importBootstrapForm.get('pools'), 'requirePool');
    });

    it('should require a token', () => {
      formHelper.expectErrorChange('token', '', 'required');
    });

    it('should verify token is base64-encoded JSON', () => {
      formHelper.expectErrorChange('token', 'VEVTVA==', 'invalidToken');
      formHelper.expectErrorChange('token', 'e2RmYXNqZGZrbH0=', 'invalidToken');
    });
  });
});