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 { ToastModule } from 'ng2-toastr';
8 import { BsModalService } from 'ngx-bootstrap/modal';
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,
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/7)', () => {
91 const resp = component.quotaMaxSizeValidator(new FormControl(''));
92 expect(resp).toBe(null);
95 it('should validate max size (2/7)', () => {
96 const resp = component.quotaMaxSizeValidator(new FormControl('xxxx'));
97 expect(resp.quotaMaxSize).toBeTruthy();
100 it('should validate max size (3/7)', () => {
101 const resp = component.quotaMaxSizeValidator(new FormControl('1023'));
102 expect(resp.quotaMaxSize).toBeTruthy();
105 it('should validate max size (4/7)', () => {
106 const resp = component.quotaMaxSizeValidator(new FormControl('1024'));
107 expect(resp).toBe(null);
110 it('should validate max size (5/7)', () => {
111 const resp = component.quotaMaxSizeValidator(new FormControl('1M'));
112 expect(resp).toBe(null);
115 it('should validate max size (6/7)', () => {
116 const resp = component.quotaMaxSizeValidator(new FormControl('1024 gib'));
117 expect(resp).toBe(null);
120 it('should validate max size (7/7)', () => {
121 const resp = component.quotaMaxSizeValidator(new FormControl('10 X'));
122 expect(resp.quotaMaxSize).toBeTruthy();
126 describe('username validation', () => {
128 spyOn(rgwUserService, 'enumerate').and.returnValue(observableOf(['abc', 'xyz']));
131 it('should validate that username is required', () => {
132 formHelper.expectErrorChange('uid', '', 'required', true);
135 it('should validate that username is valid', fakeAsync(() => {
136 formHelper.setValue('uid', 'ab', true);
138 formHelper.expectValid('uid');
141 it('should validate that username is invalid', fakeAsync(() => {
142 formHelper.setValue('uid', 'abc', true);
144 formHelper.expectError('uid', 'notUnique');
148 describe('submit form', () => {
149 let notificationService: NotificationService;
152 spyOn(TestBed.get(Router), 'navigate').and.stub();
153 notificationService = TestBed.get(NotificationService);
154 spyOn(notificationService, 'show');
157 it('should be able to clear the mail field on update', () => {
158 spyOn(rgwUserService, 'update');
159 component.editing = true;
160 formHelper.setValue('email', '', true);
161 component.onSubmit();
162 expect(rgwUserService.update).toHaveBeenCalledWith(null, {
170 it('tests create success notification', () => {
171 spyOn(rgwUserService, 'create').and.returnValue(observableOf([]));
172 component.editing = false;
173 formHelper.setValue('suspended', true, true);
174 component.onSubmit();
175 expect(notificationService.show).toHaveBeenCalledWith(
176 NotificationType.success,
177 'Created Object Gateway user ""'
181 it('tests update success notification', () => {
182 spyOn(rgwUserService, 'update').and.returnValue(observableOf([]));
183 component.editing = true;
184 formHelper.setValue('suspended', true, true);
185 component.onSubmit();
186 expect(notificationService.show).toHaveBeenCalledWith(
187 NotificationType.success,
188 'Updated Object Gateway user ""'