From 3c2e8e4f9a7645c20b5766b2c052063ce38c5650 Mon Sep 17 00:00:00 2001 From: Nizamudeen A Date: Sun, 17 Jan 2021 12:26:15 +0530 Subject: [PATCH] mgr/dashboard: Fix for incorrect validation in rgw user form The rgw users create form doesnt validate the username correctly if the username is a tenated one. For eg. Consider there is a user called tenate$sample. Now I am trying to create another user and I entered tenate$sample as username. But it doesn't async validate the username as existing. Instead it just shows the green tick and once the submit button is clicked it'll show the user as existing. Fixes: https://tracker.ceph.com/issues/48907 Signed-off-by: Nizamudeen A --- .../rgw-user-form.component.spec.ts | 8 +++----- .../app/shared/api/rgw-user.service.spec.ts | 20 +++++++++---------- .../src/app/shared/api/rgw-user.service.ts | 13 +++++++----- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-form/rgw-user-form.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-form/rgw-user-form.component.spec.ts index 091e7d124531c..61e40c757edd7 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-form/rgw-user-form.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-form/rgw-user-form.component.spec.ts @@ -7,7 +7,7 @@ import { RouterTestingModule } from '@angular/router/testing'; import { NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap'; import { NgxPipeFunctionModule } from 'ngx-pipe-function'; import { ToastrModule } from 'ngx-toastr'; -import { of as observableOf } from 'rxjs'; +import { of as observableOf, throwError } from 'rxjs'; import { RgwUserService } from '~/app/shared/api/rgw-user.service'; import { NotificationType } from '~/app/shared/enum/notification-type.enum'; @@ -155,21 +155,19 @@ describe('RgwUserFormComponent', () => { }); describe('username validation', () => { - beforeEach(() => { - spyOn(rgwUserService, 'enumerate').and.returnValue(observableOf(['abc', 'xyz'])); - }); - it('should validate that username is required', () => { formHelper.expectErrorChange('uid', '', 'required', true); }); it('should validate that username is valid', fakeAsync(() => { + spyOn(rgwUserService, 'get').and.returnValue(throwError('foo')); formHelper.setValue('uid', 'ab', true); tick(500); formHelper.expectValid('uid'); })); it('should validate that username is invalid', fakeAsync(() => { + spyOn(rgwUserService, 'get').and.returnValue(observableOf({})); formHelper.setValue('uid', 'abc', true); tick(500); formHelper.expectError('uid', 'notUnique'); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.spec.ts index 35963981590a5..c2954eac585a2 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.spec.ts @@ -1,7 +1,7 @@ import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; -import { of as observableOf } from 'rxjs'; +import { of as observableOf, throwError } from 'rxjs'; import { configureTestBed } from '~/testing/unit-test-helper'; import { RgwUserService } from './rgw-user.service'; @@ -138,21 +138,19 @@ describe('RgwUserService', () => { expect(req.request.method).toBe('DELETE'); }); - it('should call exists with an existent uid', () => { - spyOn(service, 'enumerate').and.returnValue(observableOf(['foo', 'bar'])); - let result; + it('should call exists with an existent uid', (done) => { + spyOn(service, 'get').and.returnValue(observableOf({})); service.exists('foo').subscribe((res) => { - result = res; + expect(res).toBe(true); + done(); }); - expect(result).toBe(true); }); - it('should call exists with a non existent uid', () => { - spyOn(service, 'enumerate').and.returnValue(observableOf(['foo', 'bar'])); - let result; + it('should call exists with a non existent uid', (done) => { + spyOn(service, 'get').and.returnValue(throwError('bar')); service.exists('baz').subscribe((res) => { - result = res; + expect(res).toBe(false); + done(); }); - expect(result).toBe(false); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.ts index e4b9b597adee3..f322a04fffd22 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.ts @@ -3,7 +3,7 @@ import { Injectable } from '@angular/core'; import _ from 'lodash'; import { forkJoin as observableForkJoin, Observable, of as observableOf } from 'rxjs'; -import { mergeMap } from 'rxjs/operators'; +import { catchError, mapTo, mergeMap } from 'rxjs/operators'; import { cdEncode } from '../decorators/cd-encode'; @@ -131,10 +131,13 @@ export class RgwUserService { * @return {Observable} */ exists(uid: string): Observable { - return this.enumerate().pipe( - mergeMap((resp: string[]) => { - const index = _.indexOf(resp, uid); - return observableOf(-1 !== index); + return this.get(uid).pipe( + mapTo(true), + catchError((error: Event) => { + if (_.isFunction(error.preventDefault)) { + error.preventDefault(); + } + return observableOf(false); }) ); } -- 2.39.5