1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { FormControl, ReactiveFormsModule } from '@angular/forms';
4 import { Router } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import * as _ from 'lodash';
8 import { ToastrModule } from 'ngx-toastr';
9 import { of as observableOf } from 'rxjs';
11 import { configureTestBed, FormHelper, i18nProviders } from '../../../../testing/unit-test-helper';
12 import { RgwBucketService } from '../../../shared/api/rgw-bucket.service';
13 import { RgwSiteService } from '../../../shared/api/rgw-site.service';
14 import { NotificationType } from '../../../shared/enum/notification-type.enum';
15 import { NotificationService } from '../../../shared/services/notification.service';
16 import { SharedModule } from '../../../shared/shared.module';
17 import { RgwBucketMfaDelete } from '../models/rgw-bucket-mfa-delete';
18 import { RgwBucketVersioning } from '../models/rgw-bucket-versioning';
19 import { RgwBucketFormComponent } from './rgw-bucket-form.component';
21 describe('RgwBucketFormComponent', () => {
22 let component: RgwBucketFormComponent;
23 let fixture: ComponentFixture<RgwBucketFormComponent>;
24 let rgwBucketService: RgwBucketService;
25 let getPlacementTargetsSpy: jasmine.Spy;
26 let rgwBucketServiceGetSpy: jasmine.Spy;
27 let formHelper: FormHelper;
30 declarations: [RgwBucketFormComponent],
32 HttpClientTestingModule,
36 ToastrModule.forRoot()
38 providers: [i18nProviders]
42 fixture = TestBed.createComponent(RgwBucketFormComponent);
43 component = fixture.componentInstance;
44 rgwBucketService = TestBed.get(RgwBucketService);
45 rgwBucketServiceGetSpy = spyOn(rgwBucketService, 'get');
46 getPlacementTargetsSpy = spyOn(TestBed.get(RgwSiteService), 'getPlacementTargets');
47 formHelper = new FormHelper(component.bucketForm);
50 it('should create', () => {
51 expect(component).toBeTruthy();
54 describe('bucketNameValidator', () => {
55 const testValidator = (name: string, valid: boolean) => {
56 const validatorFn = component.bucketNameValidator();
57 const ctrl = new FormControl(name);
59 const validatorPromise = validatorFn(ctrl);
60 expect(validatorPromise instanceof Promise).toBeTruthy();
61 if (validatorPromise instanceof Promise) {
62 validatorPromise.then((resp) => {
64 expect(resp).toBe(null);
66 expect(resp instanceof Object).toBeTruthy();
67 expect(resp.bucketNameInvalid).toBeTruthy();
73 it('should validate empty name', () => {
74 testValidator('', true);
77 it('bucket names cannot be formatted as IP address', () => {
78 testValidator('172.10.4.51', false);
81 it('bucket name must be >= 3 characters long (1/2)', () => {
82 testValidator('ab', false);
85 it('bucket name must be >= 3 characters long (2/2)', () => {
86 testValidator('abc', true);
89 it('bucket name must be <= than 63 characters long (1/2)', () => {
90 testValidator(_.repeat('a', 64), false);
93 it('bucket name must be <= than 63 characters long (2/2)', () => {
94 testValidator(_.repeat('a', 63), true);
97 it('bucket names must not contain uppercase characters or underscores (1/2)', () => {
98 testValidator('iAmInvalid', false);
101 it('bucket names must not contain uppercase characters or underscores (2/2)', () => {
102 testValidator('i_am_invalid', false);
105 it('bucket names with invalid labels (1/3)', () => {
106 testValidator('abc.1def.Ghi2', false);
109 it('bucket names with invalid labels (2/3)', () => {
110 testValidator('abc.1-xy', false);
113 it('bucket names with invalid labels (3/3)', () => {
114 testValidator('abc.*def', false);
117 it('bucket names must be a series of one or more labels and can contain lowercase letters, numbers, and hyphens (1/3)', () => {
118 testValidator('xyz.abc', true);
121 it('bucket names must be a series of one or more labels and can contain lowercase letters, numbers, and hyphens (2/3)', () => {
122 testValidator('abc.1-def', true);
125 it('bucket names must be a series of one or more labels and can contain lowercase letters, numbers, and hyphens (3/3)', () => {
126 testValidator('abc.ghi2', true);
129 it('bucket names must be unique', () => {
130 spyOn(rgwBucketService, 'enumerate').and.returnValue(observableOf(['abcd']));
131 const validatorFn = component.bucketNameValidator();
132 const ctrl = new FormControl('abcd');
134 const validatorPromise = validatorFn(ctrl);
135 expect(validatorPromise instanceof Promise).toBeTruthy();
136 if (validatorPromise instanceof Promise) {
137 validatorPromise.then((resp) => {
138 expect(resp instanceof Object).toBeTruthy();
139 expect(resp.bucketNameExists).toBeTruthy();
144 it('should get zonegroup and placement targets', () => {
145 const payload: Record<string, any> = {
146 zonegroup: 'default',
149 name: 'default-placement',
150 data_pool: 'default.rgw.buckets.data'
153 name: 'placement-target2',
154 data_pool: 'placement-target2.rgw.buckets.data'
158 getPlacementTargetsSpy.and.returnValue(observableOf(payload));
159 fixture.detectChanges();
161 expect(component.zonegroup).toBe(payload.zonegroup);
162 const placementTargets = [];
163 for (const placementTarget of payload['placement_targets']) {
166 ] = `${placementTarget['name']} (pool: ${placementTarget['data_pool']})`;
167 placementTargets.push(placementTarget);
169 expect(component.placementTargets).toEqual(placementTargets);
173 describe('submit form', () => {
174 let notificationService: NotificationService;
177 spyOn(TestBed.get(Router), 'navigate').and.stub();
178 notificationService = TestBed.get(NotificationService);
179 spyOn(notificationService, 'show');
182 it('should validate name', () => {
183 component.editing = false;
184 component.createForm();
185 const control = component.bucketForm.get('bid');
186 expect(_.isFunction(control.asyncValidator)).toBeTruthy();
189 it('should not validate name', () => {
190 component.editing = true;
191 component.createForm();
192 const control = component.bucketForm.get('bid');
193 expect(control.asyncValidator).toBeNull();
196 it('tests create success notification', () => {
197 spyOn(rgwBucketService, 'create').and.returnValue(observableOf([]));
198 component.editing = false;
199 component.bucketForm.markAsDirty();
201 expect(notificationService.show).toHaveBeenCalledWith(
202 NotificationType.success,
203 'Created Object Gateway bucket ""'
207 it('tests update success notification', () => {
208 spyOn(rgwBucketService, 'update').and.returnValue(observableOf([]));
209 component.editing = true;
210 component.bucketForm.markAsDirty();
212 expect(notificationService.show).toHaveBeenCalledWith(
213 NotificationType.success,
214 'Updated Object Gateway bucket "".'
219 describe('mfa credentials', () => {
220 const checkMfaCredentialsVisibility = (
221 fakeResponse: object,
222 versioningChecked: boolean,
223 mfaDeleteChecked: boolean,
224 expectedVisibility: boolean
226 component['route'].params = observableOf({ bid: 'bid' });
227 component.editing = true;
228 rgwBucketServiceGetSpy.and.returnValue(observableOf(fakeResponse));
229 component.ngOnInit();
230 component.isVersioningEnabled = versioningChecked;
231 component.isMfaDeleteEnabled = mfaDeleteChecked;
232 fixture.detectChanges();
234 const mfaTokenSerial = fixture.debugElement.nativeElement.querySelector('#mfa-token-serial');
235 const mfaTokenPin = fixture.debugElement.nativeElement.querySelector('#mfa-token-pin');
236 if (expectedVisibility) {
237 expect(mfaTokenSerial).toBeTruthy();
238 expect(mfaTokenPin).toBeTruthy();
240 expect(mfaTokenSerial).toBeFalsy();
241 expect(mfaTokenPin).toBeFalsy();
245 it('inputs should be visible when required', () => {
246 checkMfaCredentialsVisibility(
248 versioning: RgwBucketVersioning.SUSPENDED,
249 mfa_delete: RgwBucketMfaDelete.DISABLED
255 checkMfaCredentialsVisibility(
257 versioning: RgwBucketVersioning.SUSPENDED,
258 mfa_delete: RgwBucketMfaDelete.DISABLED
264 checkMfaCredentialsVisibility(
266 versioning: RgwBucketVersioning.ENABLED,
267 mfa_delete: RgwBucketMfaDelete.DISABLED
273 checkMfaCredentialsVisibility(
275 versioning: RgwBucketVersioning.ENABLED,
276 mfa_delete: RgwBucketMfaDelete.ENABLED
282 checkMfaCredentialsVisibility(
284 versioning: RgwBucketVersioning.SUSPENDED,
285 mfa_delete: RgwBucketMfaDelete.DISABLED
291 checkMfaCredentialsVisibility(
293 versioning: RgwBucketVersioning.SUSPENDED,
294 mfa_delete: RgwBucketMfaDelete.ENABLED
300 checkMfaCredentialsVisibility(
302 versioning: RgwBucketVersioning.SUSPENDED,
303 mfa_delete: RgwBucketMfaDelete.ENABLED
309 checkMfaCredentialsVisibility(
311 versioning: RgwBucketVersioning.ENABLED,
312 mfa_delete: RgwBucketMfaDelete.ENABLED
321 describe('object locking', () => {
322 const setDaysAndYears = (fn: (name: string) => void) => {
323 ['lock_retention_period_days', 'lock_retention_period_years'].forEach(fn);
326 const expectPatternLockError = (value: string) => {
327 formHelper.setValue('lock_enabled', true, true);
328 setDaysAndYears((name: string) => {
329 formHelper.setValue(name, value);
330 formHelper.expectError(name, 'pattern');
334 const expectValidLockInputs = (enabled: boolean, mode: string, days: string, years: string) => {
335 formHelper.setValue('lock_enabled', enabled);
336 formHelper.setValue('lock_mode', mode);
337 formHelper.setValue('lock_retention_period_days', days);
338 formHelper.setValue('lock_retention_period_years', years);
342 'lock_retention_period_days',
343 'lock_retention_period_years'
344 ].forEach((name) => {
345 const control = component.bucketForm.get(name);
346 expect(control.valid).toBeTruthy();
347 expect(control.errors).toBeNull();
351 it('should check lock enabled checkbox [mode=create]', () => {
352 component.createForm();
353 const control = component.bucketForm.get('lock_enabled');
354 expect(control.disabled).toBeFalsy();
357 it('should check lock enabled checkbox [mode=edit]', () => {
358 component.editing = true;
359 component.createForm();
360 const control = component.bucketForm.get('lock_enabled');
361 expect(control.disabled).toBeTruthy();
364 it('should have the "eitherDaysOrYears" error', () => {
365 formHelper.setValue('lock_enabled', true);
366 setDaysAndYears((name: string) => {
367 const control = component.bucketForm.get(name);
368 control.updateValueAndValidity();
369 expect(control.value).toBe(0);
370 expect(control.invalid).toBeTruthy();
371 formHelper.expectError(control, 'eitherDaysOrYears');
375 it('should have the "pattern" error [1]', () => {
376 expectPatternLockError('-1');
379 it('should have the "pattern" error [2]', () => {
380 expectPatternLockError('1.2');
383 it('should have valid values [1]', () => {
384 expectValidLockInputs(true, 'Governance', '0', '1');
387 it('should have valid values [2]', () => {
388 expectValidLockInputs(false, 'Compliance', '100', '0');