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