]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-form/rgw-user-form.component.spec.ts
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / rgw / rgw-user-form / rgw-user-form.component.spec.ts
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';
6
7 import { BsModalService } from 'ngx-bootstrap/modal';
8 import { ToastrModule } from 'ngx-toastr';
9 import { of as observableOf } from 'rxjs';
10
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';
18
19 describe('RgwUserFormComponent', () => {
20 let component: RgwUserFormComponent;
21 let fixture: ComponentFixture<RgwUserFormComponent>;
22 let rgwUserService: RgwUserService;
23 let formHelper: FormHelper;
24
25 configureTestBed({
26 declarations: [RgwUserFormComponent],
27 imports: [
28 HttpClientTestingModule,
29 ReactiveFormsModule,
30 RouterTestingModule,
31 SharedModule,
32 ToastrModule.forRoot()
33 ],
34 providers: [BsModalService, i18nProviders]
35 });
36
37 beforeEach(() => {
38 fixture = TestBed.createComponent(RgwUserFormComponent);
39 component = fixture.componentInstance;
40 fixture.detectChanges();
41 rgwUserService = TestBed.get(RgwUserService);
42 formHelper = new FormHelper(component.userForm);
43 });
44
45 it('should create', () => {
46 expect(component).toBeTruthy();
47 });
48
49 describe('s3 key management', () => {
50 beforeEach(() => {
51 spyOn(rgwUserService, 'addS3Key').and.stub();
52 });
53
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();
58 });
59
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', {
67 subuser: 'subuser2',
68 generate_key: 'false',
69 access_key: undefined,
70 secret_key: undefined
71 });
72 });
73
74 it('should set key w/o subuser', () => {
75 const key = new RgwUserS3Key();
76 key.user = 'test1';
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', {
81 subuser: '',
82 generate_key: 'false',
83 access_key: undefined,
84 secret_key: undefined
85 });
86 });
87 });
88
89 describe('quotaMaxSizeValidator', () => {
90 it('should validate max size (1)', () => {
91 const resp = component.quotaMaxSizeValidator(new FormControl(''));
92 expect(resp).toBe(null);
93 });
94
95 it('should validate max size (2)', () => {
96 const resp = component.quotaMaxSizeValidator(new FormControl('xxxx'));
97 expect(resp.quotaMaxSize).toBeTruthy();
98 });
99
100 it('should validate max size (3)', () => {
101 const resp = component.quotaMaxSizeValidator(new FormControl('1023'));
102 expect(resp.quotaMaxSize).toBeTruthy();
103 });
104
105 it('should validate max size (4)', () => {
106 const resp = component.quotaMaxSizeValidator(new FormControl('1024'));
107 expect(resp).toBe(null);
108 });
109
110 it('should validate max size (5)', () => {
111 const resp = component.quotaMaxSizeValidator(new FormControl('1M'));
112 expect(resp).toBe(null);
113 });
114
115 it('should validate max size (6)', () => {
116 const resp = component.quotaMaxSizeValidator(new FormControl('1024 gib'));
117 expect(resp).toBe(null);
118 });
119
120 it('should validate max size (7)', () => {
121 const resp = component.quotaMaxSizeValidator(new FormControl('10 X'));
122 expect(resp.quotaMaxSize).toBeTruthy();
123 });
124
125 it('should validate max size (8)', () => {
126 const resp = component.quotaMaxSizeValidator(new FormControl('1.085 GiB'));
127 expect(resp).toBe(null);
128 });
129
130 it('should validate max size (9)', () => {
131 const resp = component.quotaMaxSizeValidator(new FormControl('1,085 GiB'));
132 expect(resp.quotaMaxSize).toBeTruthy();
133 });
134 });
135
136 describe('username validation', () => {
137 beforeEach(() => {
138 spyOn(rgwUserService, 'enumerate').and.returnValue(observableOf(['abc', 'xyz']));
139 });
140
141 it('should validate that username is required', () => {
142 formHelper.expectErrorChange('uid', '', 'required', true);
143 });
144
145 it('should validate that username is valid', fakeAsync(() => {
146 formHelper.setValue('uid', 'ab', true);
147 tick(500);
148 formHelper.expectValid('uid');
149 }));
150
151 it('should validate that username is invalid', fakeAsync(() => {
152 formHelper.setValue('uid', 'abc', true);
153 tick(500);
154 formHelper.expectError('uid', 'notUnique');
155 }));
156 });
157
158 describe('submit form', () => {
159 let notificationService: NotificationService;
160
161 beforeEach(() => {
162 spyOn(TestBed.get(Router), 'navigate').and.stub();
163 notificationService = TestBed.get(NotificationService);
164 spyOn(notificationService, 'show');
165 });
166
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, {
173 display_name: null,
174 email: '',
175 max_buckets: 1000,
176 suspended: false
177 });
178 });
179
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 ""'
188 );
189 });
190
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 ""'
199 );
200 });
201 });
202 });