1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } 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 { BsModalService } from 'ngx-bootstrap/modal';
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 { RgwUserService } from '../../../shared/api/rgw-user.service';
13 import { NotificationType } from '../../../shared/enum/notification-type.enum';
14 import { NotificationService } from '../../../shared/services/notification.service';
15 import { SharedModule } from '../../../shared/shared.module';
16 import { RgwUserS3Key } from '../models/rgw-user-s3-key';
17 import { RgwUserFormComponent } from './rgw-user-form.component';
19 describe('RgwUserFormComponent', () => {
20 let component: RgwUserFormComponent;
21 let fixture: ComponentFixture<RgwUserFormComponent>;
22 let rgwUserService: RgwUserService;
23 let formHelper: FormHelper;
26 declarations: [RgwUserFormComponent],
28 HttpClientTestingModule,
32 ToastrModule.forRoot()
34 providers: [BsModalService, i18nProviders]
38 fixture = TestBed.createComponent(RgwUserFormComponent);
39 component = fixture.componentInstance;
40 fixture.detectChanges();
41 rgwUserService = TestBed.get(RgwUserService);
42 formHelper = new FormHelper(component.userForm);
45 it('should create', () => {
46 expect(component).toBeTruthy();
49 describe('s3 key management', () => {
51 spyOn(rgwUserService, 'addS3Key').and.stub();
54 it('should not update key', () => {
55 component.setS3Key(new RgwUserS3Key(), 3);
56 expect(component.s3Keys.length).toBe(0);
57 expect(rgwUserService.addS3Key).not.toHaveBeenCalled();
60 it('should set key', () => {
61 const key = new RgwUserS3Key();
62 key.user = 'test1:subuser2';
63 component.setS3Key(key);
64 expect(component.s3Keys.length).toBe(1);
65 expect(component.s3Keys[0].user).toBe('test1:subuser2');
66 expect(rgwUserService.addS3Key).toHaveBeenCalledWith('test1', {
68 generate_key: 'false',
69 access_key: undefined,
74 it('should set key w/o subuser', () => {
75 const key = new RgwUserS3Key();
77 component.setS3Key(key);
78 expect(component.s3Keys.length).toBe(1);
79 expect(component.s3Keys[0].user).toBe('test1');
80 expect(rgwUserService.addS3Key).toHaveBeenCalledWith('test1', {
82 generate_key: 'false',
83 access_key: undefined,
89 describe('quotaMaxSizeValidator', () => {
90 it('should validate max size (1)', () => {
91 const resp = component.quotaMaxSizeValidator(new FormControl(''));
92 expect(resp).toBe(null);
95 it('should validate max size (2)', () => {
96 const resp = component.quotaMaxSizeValidator(new FormControl('xxxx'));
97 expect(resp.quotaMaxSize).toBeTruthy();
100 it('should validate max size (3)', () => {
101 const resp = component.quotaMaxSizeValidator(new FormControl('1023'));
102 expect(resp.quotaMaxSize).toBeTruthy();
105 it('should validate max size (4)', () => {
106 const resp = component.quotaMaxSizeValidator(new FormControl('1024'));
107 expect(resp).toBe(null);
110 it('should validate max size (5)', () => {
111 const resp = component.quotaMaxSizeValidator(new FormControl('1M'));
112 expect(resp).toBe(null);
115 it('should validate max size (6)', () => {
116 const resp = component.quotaMaxSizeValidator(new FormControl('1024 gib'));
117 expect(resp).toBe(null);
120 it('should validate max size (7)', () => {
121 const resp = component.quotaMaxSizeValidator(new FormControl('10 X'));
122 expect(resp.quotaMaxSize).toBeTruthy();
125 it('should validate max size (8)', () => {
126 const resp = component.quotaMaxSizeValidator(new FormControl('1.085 GiB'));
127 expect(resp).toBe(null);
130 it('should validate max size (9)', () => {
131 const resp = component.quotaMaxSizeValidator(new FormControl('1,085 GiB'));
132 expect(resp.quotaMaxSize).toBeTruthy();
136 describe('username validation', () => {
138 spyOn(rgwUserService, 'enumerate').and.returnValue(observableOf(['abc', 'xyz']));
141 it('should validate that username is required', () => {
142 formHelper.expectErrorChange('uid', '', 'required', true);
145 it('should validate that username is valid', fakeAsync(() => {
146 formHelper.setValue('uid', 'ab', true);
148 formHelper.expectValid('uid');
151 it('should validate that username is invalid', fakeAsync(() => {
152 formHelper.setValue('uid', 'abc', true);
154 formHelper.expectError('uid', 'notUnique');
158 describe('submit form', () => {
159 let notificationService: NotificationService;
162 spyOn(TestBed.get(Router), 'navigate').and.stub();
163 notificationService = TestBed.get(NotificationService);
164 spyOn(notificationService, 'show');
167 it('should be able to clear the mail field on update', () => {
168 spyOn(rgwUserService, 'update');
169 component.editing = true;
170 formHelper.setValue('email', '', true);
171 component.onSubmit();
172 expect(rgwUserService.update).toHaveBeenCalledWith(null, {
180 it('tests create success notification', () => {
181 spyOn(rgwUserService, 'create').and.returnValue(observableOf([]));
182 component.editing = false;
183 formHelper.setValue('suspended', true, true);
184 component.onSubmit();
185 expect(notificationService.show).toHaveBeenCalledWith(
186 NotificationType.success,
187 'Created Object Gateway user ""'
191 it('tests update success notification', () => {
192 spyOn(rgwUserService, 'update').and.returnValue(observableOf([]));
193 component.editing = true;
194 formHelper.setValue('suspended', true, true);
195 component.onSubmit();
196 expect(notificationService.show).toHaveBeenCalledWith(
197 NotificationType.success,
198 'Updated Object Gateway user ""'