]> 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
85632651ff4d59d8230791328addde42cc0ccb97
[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 { TooltipModule } from 'ngx-bootstrap/tooltip';
9 import { ToastrModule } from 'ngx-toastr';
10 import { of as observableOf } from 'rxjs';
11
12 import { configureTestBed, FormHelper, i18nProviders } from '../../../../testing/unit-test-helper';
13 import { RgwUserService } from '../../../shared/api/rgw-user.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 { RgwUserCapabilities } from '../models/rgw-user-capabilities';
18 import { RgwUserCapability } from '../models/rgw-user-capability';
19 import { RgwUserS3Key } from '../models/rgw-user-s3-key';
20 import { RgwUserFormComponent } from './rgw-user-form.component';
21
22 describe('RgwUserFormComponent', () => {
23 let component: RgwUserFormComponent;
24 let fixture: ComponentFixture<RgwUserFormComponent>;
25 let rgwUserService: RgwUserService;
26 let formHelper: FormHelper;
27
28 configureTestBed({
29 declarations: [RgwUserFormComponent],
30 imports: [
31 HttpClientTestingModule,
32 ReactiveFormsModule,
33 RouterTestingModule,
34 SharedModule,
35 ToastrModule.forRoot(),
36 TooltipModule.forRoot()
37 ],
38 providers: [BsModalService, i18nProviders]
39 });
40
41 beforeEach(() => {
42 fixture = TestBed.createComponent(RgwUserFormComponent);
43 component = fixture.componentInstance;
44 fixture.detectChanges();
45 rgwUserService = TestBed.get(RgwUserService);
46 formHelper = new FormHelper(component.userForm);
47 });
48
49 it('should create', () => {
50 expect(component).toBeTruthy();
51 });
52
53 describe('s3 key management', () => {
54 beforeEach(() => {
55 spyOn(rgwUserService, 'addS3Key').and.stub();
56 });
57
58 it('should not update key', () => {
59 component.setS3Key(new RgwUserS3Key(), 3);
60 expect(component.s3Keys.length).toBe(0);
61 expect(rgwUserService.addS3Key).not.toHaveBeenCalled();
62 });
63
64 it('should set user defined key', () => {
65 const key = new RgwUserS3Key();
66 key.user = 'test1:subuser2';
67 key.access_key = 'my-access-key';
68 key.secret_key = 'my-secret-key';
69 component.setS3Key(key);
70 expect(component.s3Keys.length).toBe(1);
71 expect(component.s3Keys[0].user).toBe('test1:subuser2');
72 expect(rgwUserService.addS3Key).toHaveBeenCalledWith('test1', {
73 subuser: 'subuser2',
74 generate_key: 'false',
75 access_key: 'my-access-key',
76 secret_key: 'my-secret-key'
77 });
78 });
79
80 it('should set params for auto-generating key', () => {
81 const key = new RgwUserS3Key();
82 key.user = 'test1:subuser2';
83 key.generate_key = true;
84 key.access_key = 'my-access-key';
85 key.secret_key = 'my-secret-key';
86 component.setS3Key(key);
87 expect(component.s3Keys.length).toBe(1);
88 expect(component.s3Keys[0].user).toBe('test1:subuser2');
89 expect(rgwUserService.addS3Key).toHaveBeenCalledWith('test1', {
90 subuser: 'subuser2',
91 generate_key: 'true'
92 });
93 });
94
95 it('should set key w/o subuser', () => {
96 const key = new RgwUserS3Key();
97 key.user = 'test1';
98 component.setS3Key(key);
99 expect(component.s3Keys.length).toBe(1);
100 expect(component.s3Keys[0].user).toBe('test1');
101 expect(rgwUserService.addS3Key).toHaveBeenCalledWith('test1', {
102 subuser: '',
103 generate_key: 'false',
104 access_key: undefined,
105 secret_key: undefined
106 });
107 });
108 });
109
110 describe('quotaMaxSizeValidator', () => {
111 it('should validate max size (1)', () => {
112 const resp = component.quotaMaxSizeValidator(new FormControl(''));
113 expect(resp).toBe(null);
114 });
115
116 it('should validate max size (2)', () => {
117 const resp = component.quotaMaxSizeValidator(new FormControl('xxxx'));
118 expect(resp.quotaMaxSize).toBeTruthy();
119 });
120
121 it('should validate max size (3)', () => {
122 const resp = component.quotaMaxSizeValidator(new FormControl('1023'));
123 expect(resp.quotaMaxSize).toBeTruthy();
124 });
125
126 it('should validate max size (4)', () => {
127 const resp = component.quotaMaxSizeValidator(new FormControl('1024'));
128 expect(resp).toBe(null);
129 });
130
131 it('should validate max size (5)', () => {
132 const resp = component.quotaMaxSizeValidator(new FormControl('1M'));
133 expect(resp).toBe(null);
134 });
135
136 it('should validate max size (6)', () => {
137 const resp = component.quotaMaxSizeValidator(new FormControl('1024 gib'));
138 expect(resp).toBe(null);
139 });
140
141 it('should validate max size (7)', () => {
142 const resp = component.quotaMaxSizeValidator(new FormControl('10 X'));
143 expect(resp.quotaMaxSize).toBeTruthy();
144 });
145
146 it('should validate max size (8)', () => {
147 const resp = component.quotaMaxSizeValidator(new FormControl('1.085 GiB'));
148 expect(resp).toBe(null);
149 });
150
151 it('should validate max size (9)', () => {
152 const resp = component.quotaMaxSizeValidator(new FormControl('1,085 GiB'));
153 expect(resp.quotaMaxSize).toBeTruthy();
154 });
155 });
156
157 describe('username validation', () => {
158 beforeEach(() => {
159 spyOn(rgwUserService, 'enumerate').and.returnValue(observableOf(['abc', 'xyz']));
160 });
161
162 it('should validate that username is required', () => {
163 formHelper.expectErrorChange('uid', '', 'required', true);
164 });
165
166 it('should validate that username is valid', fakeAsync(() => {
167 formHelper.setValue('uid', 'ab', true);
168 tick(500);
169 formHelper.expectValid('uid');
170 }));
171
172 it('should validate that username is invalid', fakeAsync(() => {
173 formHelper.setValue('uid', 'abc', true);
174 tick(500);
175 formHelper.expectError('uid', 'notUnique');
176 }));
177 });
178
179 describe('max buckets', () => {
180 it('disable creation (create)', () => {
181 spyOn(rgwUserService, 'create');
182 formHelper.setValue('max_buckets_mode', -1, true);
183 component.onSubmit();
184 expect(rgwUserService.create).toHaveBeenCalledWith({
185 access_key: '',
186 display_name: null,
187 email: '',
188 generate_key: true,
189 max_buckets: -1,
190 secret_key: '',
191 suspended: false,
192 uid: null
193 });
194 });
195
196 it('disable creation (edit)', () => {
197 spyOn(rgwUserService, 'update');
198 component.editing = true;
199 formHelper.setValue('max_buckets_mode', -1, true);
200 component.onSubmit();
201 expect(rgwUserService.update).toHaveBeenCalledWith(null, {
202 display_name: null,
203 email: null,
204 max_buckets: -1,
205 suspended: false
206 });
207 });
208
209 it('unlimited buckets (create)', () => {
210 spyOn(rgwUserService, 'create');
211 formHelper.setValue('max_buckets_mode', 0, true);
212 component.onSubmit();
213 expect(rgwUserService.create).toHaveBeenCalledWith({
214 access_key: '',
215 display_name: null,
216 email: '',
217 generate_key: true,
218 max_buckets: 0,
219 secret_key: '',
220 suspended: false,
221 uid: null
222 });
223 });
224
225 it('unlimited buckets (edit)', () => {
226 spyOn(rgwUserService, 'update');
227 component.editing = true;
228 formHelper.setValue('max_buckets_mode', 0, true);
229 component.onSubmit();
230 expect(rgwUserService.update).toHaveBeenCalledWith(null, {
231 display_name: null,
232 email: null,
233 max_buckets: 0,
234 suspended: false
235 });
236 });
237
238 it('custom (create)', () => {
239 spyOn(rgwUserService, 'create');
240 formHelper.setValue('max_buckets_mode', 1, true);
241 formHelper.setValue('max_buckets', 100, true);
242 component.onSubmit();
243 expect(rgwUserService.create).toHaveBeenCalledWith({
244 access_key: '',
245 display_name: null,
246 email: '',
247 generate_key: true,
248 max_buckets: 100,
249 secret_key: '',
250 suspended: false,
251 uid: null
252 });
253 });
254
255 it('custom (edit)', () => {
256 spyOn(rgwUserService, 'update');
257 component.editing = true;
258 formHelper.setValue('max_buckets_mode', 1, true);
259 formHelper.setValue('max_buckets', 100, true);
260 component.onSubmit();
261 expect(rgwUserService.update).toHaveBeenCalledWith(null, {
262 display_name: null,
263 email: null,
264 max_buckets: 100,
265 suspended: false
266 });
267 });
268 });
269
270 describe('submit form', () => {
271 let notificationService: NotificationService;
272
273 beforeEach(() => {
274 spyOn(TestBed.get(Router), 'navigate').and.stub();
275 notificationService = TestBed.get(NotificationService);
276 spyOn(notificationService, 'show');
277 });
278
279 it('should be able to clear the mail field on update', () => {
280 spyOn(rgwUserService, 'update');
281 component.editing = true;
282 formHelper.setValue('email', '', true);
283 component.onSubmit();
284 expect(rgwUserService.update).toHaveBeenCalledWith(null, {
285 display_name: null,
286 email: '',
287 max_buckets: 1000,
288 suspended: false
289 });
290 });
291
292 it('tests create success notification', () => {
293 spyOn(rgwUserService, 'create').and.returnValue(observableOf([]));
294 component.editing = false;
295 formHelper.setValue('suspended', true, true);
296 component.onSubmit();
297 expect(notificationService.show).toHaveBeenCalledWith(
298 NotificationType.success,
299 'Created Object Gateway user ""'
300 );
301 });
302
303 it('tests update success notification', () => {
304 spyOn(rgwUserService, 'update').and.returnValue(observableOf([]));
305 component.editing = true;
306 formHelper.setValue('suspended', true, true);
307 component.onSubmit();
308 expect(notificationService.show).toHaveBeenCalledWith(
309 NotificationType.success,
310 'Updated Object Gateway user ""'
311 );
312 });
313 });
314
315 describe('RgwUserCapabilities', () => {
316 it('capability button disabled when all capabilities are added', () => {
317 component.editing = true;
318 for (const capabilityType of RgwUserCapabilities.getAll()) {
319 const capability = new RgwUserCapability();
320 capability.type = capabilityType;
321 capability.perm = 'read';
322 component.setCapability(capability);
323 }
324
325 fixture.detectChanges();
326
327 expect(component.hasAllCapabilities()).toBeTruthy();
328 const capabilityButton = fixture.debugElement.nativeElement.querySelector('.tc_addCapButton');
329 expect(capabilityButton.disabled).toBeTruthy();
330 });
331
332 it('capability button not disabled when not all capabilities are added', () => {
333 component.editing = true;
334
335 fixture.detectChanges();
336
337 const capabilityButton = fixture.debugElement.nativeElement.querySelector('.tc_addCapButton');
338 expect(capabilityButton.disabled).toBeFalsy();
339 });
340 });
341 });