]> 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
import ceph pacific 16.2.5
[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';
21
22describe('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,
92f5a8d4 35 ToastrModule.forRoot(),
f67539c2
TL
36 NgbTooltipModule,
37 NgxPipeFunctionModule
38 ]
11fdf7f2
TL
39 });
40
41 beforeEach(() => {
42 fixture = TestBed.createComponent(RgwUserFormComponent);
43 component = fixture.componentInstance;
44 fixture.detectChanges();
f67539c2 45 rgwUserService = TestBed.inject(RgwUserService);
11fdf7f2
TL
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
92f5a8d4 64 it('should set user defined key', () => {
11fdf7f2
TL
65 const key = new RgwUserS3Key();
66 key.user = 'test1:subuser2';
92f5a8d4
TL
67 key.access_key = 'my-access-key';
68 key.secret_key = 'my-secret-key';
11fdf7f2
TL
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',
92f5a8d4
TL
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'
11fdf7f2
TL
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', () => {
494da23a 111 it('should validate max size (1)', () => {
11fdf7f2
TL
112 const resp = component.quotaMaxSizeValidator(new FormControl(''));
113 expect(resp).toBe(null);
114 });
115
494da23a 116 it('should validate max size (2)', () => {
11fdf7f2
TL
117 const resp = component.quotaMaxSizeValidator(new FormControl('xxxx'));
118 expect(resp.quotaMaxSize).toBeTruthy();
119 });
120
494da23a 121 it('should validate max size (3)', () => {
11fdf7f2
TL
122 const resp = component.quotaMaxSizeValidator(new FormControl('1023'));
123 expect(resp.quotaMaxSize).toBeTruthy();
124 });
125
494da23a 126 it('should validate max size (4)', () => {
11fdf7f2
TL
127 const resp = component.quotaMaxSizeValidator(new FormControl('1024'));
128 expect(resp).toBe(null);
129 });
130
494da23a 131 it('should validate max size (5)', () => {
11fdf7f2
TL
132 const resp = component.quotaMaxSizeValidator(new FormControl('1M'));
133 expect(resp).toBe(null);
134 });
135
494da23a 136 it('should validate max size (6)', () => {
11fdf7f2
TL
137 const resp = component.quotaMaxSizeValidator(new FormControl('1024 gib'));
138 expect(resp).toBe(null);
139 });
140
494da23a 141 it('should validate max size (7)', () => {
11fdf7f2
TL
142 const resp = component.quotaMaxSizeValidator(new FormControl('10 X'));
143 expect(resp.quotaMaxSize).toBeTruthy();
144 });
494da23a
TL
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 });
11fdf7f2
TL
155 });
156
157 describe('username validation', () => {
11fdf7f2 158 it('should validate that username is required', () => {
f67539c2 159 formHelper.expectErrorChange('user_id', '', 'required', true);
11fdf7f2
TL
160 });
161
162 it('should validate that username is valid', fakeAsync(() => {
cd265ab1 163 spyOn(rgwUserService, 'get').and.returnValue(throwError('foo'));
f67539c2 164 formHelper.setValue('user_id', 'ab', true);
b3b6e05e 165 tick();
f67539c2 166 formHelper.expectValid('user_id');
11fdf7f2
TL
167 }));
168
169 it('should validate that username is invalid', fakeAsync(() => {
cd265ab1 170 spyOn(rgwUserService, 'get').and.returnValue(observableOf({}));
f67539c2 171 formHelper.setValue('user_id', 'abc', true);
b3b6e05e 172 tick();
f67539c2 173 formHelper.expectError('user_id', 'notUnique');
11fdf7f2
TL
174 }));
175 });
176
9f95a23c
TL
177 describe('max buckets', () => {
178 it('disable creation (create)', () => {
179 spyOn(rgwUserService, 'create');
180 formHelper.setValue('max_buckets_mode', -1, true);
181 component.onSubmit();
182 expect(rgwUserService.create).toHaveBeenCalledWith({
183 access_key: '',
184 display_name: null,
185 email: '',
186 generate_key: true,
187 max_buckets: -1,
188 secret_key: '',
189 suspended: false,
190 uid: null
191 });
192 });
193
194 it('disable creation (edit)', () => {
195 spyOn(rgwUserService, 'update');
196 component.editing = true;
197 formHelper.setValue('max_buckets_mode', -1, true);
198 component.onSubmit();
199 expect(rgwUserService.update).toHaveBeenCalledWith(null, {
200 display_name: null,
201 email: null,
202 max_buckets: -1,
203 suspended: false
204 });
205 });
206
207 it('unlimited buckets (create)', () => {
208 spyOn(rgwUserService, 'create');
209 formHelper.setValue('max_buckets_mode', 0, true);
210 component.onSubmit();
211 expect(rgwUserService.create).toHaveBeenCalledWith({
212 access_key: '',
213 display_name: null,
214 email: '',
215 generate_key: true,
216 max_buckets: 0,
217 secret_key: '',
218 suspended: false,
219 uid: null
220 });
221 });
222
223 it('unlimited buckets (edit)', () => {
224 spyOn(rgwUserService, 'update');
225 component.editing = true;
226 formHelper.setValue('max_buckets_mode', 0, true);
227 component.onSubmit();
228 expect(rgwUserService.update).toHaveBeenCalledWith(null, {
229 display_name: null,
230 email: null,
231 max_buckets: 0,
232 suspended: false
233 });
234 });
235
236 it('custom (create)', () => {
237 spyOn(rgwUserService, 'create');
238 formHelper.setValue('max_buckets_mode', 1, true);
239 formHelper.setValue('max_buckets', 100, true);
240 component.onSubmit();
241 expect(rgwUserService.create).toHaveBeenCalledWith({
242 access_key: '',
243 display_name: null,
244 email: '',
245 generate_key: true,
246 max_buckets: 100,
247 secret_key: '',
248 suspended: false,
249 uid: null
250 });
251 });
252
253 it('custom (edit)', () => {
254 spyOn(rgwUserService, 'update');
255 component.editing = true;
256 formHelper.setValue('max_buckets_mode', 1, true);
257 formHelper.setValue('max_buckets', 100, true);
258 component.onSubmit();
259 expect(rgwUserService.update).toHaveBeenCalledWith(null, {
260 display_name: null,
261 email: null,
262 max_buckets: 100,
263 suspended: false
264 });
265 });
266 });
267
11fdf7f2
TL
268 describe('submit form', () => {
269 let notificationService: NotificationService;
270
271 beforeEach(() => {
f67539c2
TL
272 spyOn(TestBed.inject(Router), 'navigate').and.stub();
273 notificationService = TestBed.inject(NotificationService);
11fdf7f2
TL
274 spyOn(notificationService, 'show');
275 });
276
277 it('should be able to clear the mail field on update', () => {
278 spyOn(rgwUserService, 'update');
279 component.editing = true;
280 formHelper.setValue('email', '', true);
281 component.onSubmit();
282 expect(rgwUserService.update).toHaveBeenCalledWith(null, {
283 display_name: null,
284 email: '',
285 max_buckets: 1000,
286 suspended: false
287 });
288 });
289
290 it('tests create success notification', () => {
291 spyOn(rgwUserService, 'create').and.returnValue(observableOf([]));
292 component.editing = false;
293 formHelper.setValue('suspended', true, true);
294 component.onSubmit();
295 expect(notificationService.show).toHaveBeenCalledWith(
296 NotificationType.success,
f67539c2 297 `Created Object Gateway user 'null'`
11fdf7f2
TL
298 );
299 });
300
301 it('tests update success notification', () => {
302 spyOn(rgwUserService, 'update').and.returnValue(observableOf([]));
303 component.editing = true;
304 formHelper.setValue('suspended', true, true);
305 component.onSubmit();
306 expect(notificationService.show).toHaveBeenCalledWith(
307 NotificationType.success,
f67539c2 308 `Updated Object Gateway user 'null'`
11fdf7f2
TL
309 );
310 });
311 });
92f5a8d4
TL
312
313 describe('RgwUserCapabilities', () => {
314 it('capability button disabled when all capabilities are added', () => {
315 component.editing = true;
316 for (const capabilityType of RgwUserCapabilities.getAll()) {
317 const capability = new RgwUserCapability();
318 capability.type = capabilityType;
319 capability.perm = 'read';
320 component.setCapability(capability);
321 }
322
323 fixture.detectChanges();
324
f67539c2 325 expect(component.hasAllCapabilities(component.capabilities)).toBeTruthy();
92f5a8d4
TL
326 const capabilityButton = fixture.debugElement.nativeElement.querySelector('.tc_addCapButton');
327 expect(capabilityButton.disabled).toBeTruthy();
328 });
329
330 it('capability button not disabled when not all capabilities are added', () => {
331 component.editing = true;
332
333 fixture.detectChanges();
334
335 const capabilityButton = fixture.debugElement.nativeElement.querySelector('.tc_addCapButton');
336 expect(capabilityButton.disabled).toBeFalsy();
337 });
338 });
11fdf7f2 339});