From: Aashish Sharma Date: Mon, 27 Mar 2023 13:36:21 +0000 (+0530) Subject: mgr/dashboard: Add unit test for realm X-Git-Tag: v19.0.0~1503^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=eb56f2680c2b6fe8c8d0a930fae8aae6fee21801;p=ceph.git mgr/dashboard: Add unit test for realm Fixes: https://tracker.ceph.com/issues/59171 Signed-off-by: Aashish Sharma --- diff --git a/src/pybind/mgr/dashboard/controllers/rgw.py b/src/pybind/mgr/dashboard/controllers/rgw.py index a7c9cb9ac8ef1..eb71504dbf4c7 100644 --- a/src/pybind/mgr/dashboard/controllers/rgw.py +++ b/src/pybind/mgr/dashboard/controllers/rgw.py @@ -704,7 +704,7 @@ class RgwRealm(RESTController): @allow_empty_body # pylint: disable=W0613 - def set(self, realm_name: str, new_realm_name: str, default: str = None, daemon_name=None): + def set(self, realm_name: str, new_realm_name: str, default: str = '', daemon_name=None): try: instance = RgwClient.admin_instance(daemon_name=daemon_name) result = instance.edit_realm(realm_name, new_realm_name, default) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-multisite-realm-form/rgw-multisite-realm-form.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-multisite-realm-form/rgw-multisite-realm-form.component.spec.ts index ec3c052e4d177..2c2ceedb0bc4e 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-multisite-realm-form/rgw-multisite-realm-form.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-multisite-realm-form/rgw-multisite-realm-form.component.spec.ts @@ -1,9 +1,15 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ReactiveFormsModule } from '@angular/forms'; +import { Router } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; +import _ from 'lodash'; +import { of as observableOf } from 'rxjs'; import { ToastrModule } from 'ngx-toastr'; +import { RgwRealmService } from '~/app/shared/api/rgw-realm.service'; +import { NotificationType } from '~/app/shared/enum/notification-type.enum'; +import { NotificationService } from '~/app/shared/services/notification.service'; import { SharedModule } from '~/app/shared/shared.module'; import { RgwMultisiteRealmFormComponent } from './rgw-multisite-realm-form.component'; @@ -11,6 +17,7 @@ import { RgwMultisiteRealmFormComponent } from './rgw-multisite-realm-form.compo describe('RgwMultisiteRealmFormComponent', () => { let component: RgwMultisiteRealmFormComponent; let fixture: ComponentFixture; + let rgwRealmService: RgwRealmService; beforeEach(async () => { await TestBed.configureTestingModule({ @@ -35,4 +42,54 @@ describe('RgwMultisiteRealmFormComponent', () => { it('should create', () => { expect(component).toBeTruthy(); }); + + describe('submit form', () => { + let notificationService: NotificationService; + + beforeEach(() => { + spyOn(TestBed.inject(Router), 'navigate').and.stub(); + notificationService = TestBed.inject(NotificationService); + spyOn(notificationService, 'show'); + rgwRealmService = TestBed.inject(RgwRealmService); + }); + + it('should validate name', () => { + component.action = 'create'; + component.createForm(); + const control = component.multisiteRealmForm.get('realmName'); + expect(_.isFunction(control.validator)).toBeTruthy(); + }); + + it('should not validate name', () => { + component.action = 'edit'; + component.createForm(); + const control = component.multisiteRealmForm.get('realmName'); + expect(control.asyncValidator).toBeNull(); + }); + + it('tests create success notification', () => { + spyOn(rgwRealmService, 'create').and.returnValue(observableOf([])); + component.action = 'create'; + component.multisiteRealmForm.markAsDirty(); + component.submit(); + expect(notificationService.show).toHaveBeenCalledWith( + NotificationType.success, + "Realm: 'null' created successfully" + ); + }); + + it('tests update success notification', () => { + spyOn(rgwRealmService, 'update').and.returnValue(observableOf([])); + component.action = 'edit'; + component.info = { + data: { name: 'null' } + }; + component.multisiteRealmForm.markAsDirty(); + component.submit(); + expect(notificationService.show).toHaveBeenCalledWith( + NotificationType.success, + "Realm: 'null' updated successfully" + ); + }); + }); }); diff --git a/src/pybind/mgr/dashboard/openapi.yaml b/src/pybind/mgr/dashboard/openapi.yaml index 00ad59bb1e694..9b0cd30ce242d 100644 --- a/src/pybind/mgr/dashboard/openapi.yaml +++ b/src/pybind/mgr/dashboard/openapi.yaml @@ -8317,6 +8317,7 @@ paths: daemon_name: type: string default: + default: '' type: string new_realm_name: type: string diff --git a/src/pybind/mgr/dashboard/requirements-test.txt b/src/pybind/mgr/dashboard/requirements-test.txt index fe9dccbc44b5d..4e925e8616f1a 100644 --- a/src/pybind/mgr/dashboard/requirements-test.txt +++ b/src/pybind/mgr/dashboard/requirements-test.txt @@ -1,4 +1,4 @@ pytest-cov pytest-instafail pyfakefs==4.5.0 -jsonschema==4.16.0 \ No newline at end of file +jsonschema==4.16.0 diff --git a/src/pybind/mgr/dashboard/services/rgw_client.py b/src/pybind/mgr/dashboard/services/rgw_client.py index 6b07358638723..214c435da2c85 100644 --- a/src/pybind/mgr/dashboard/services/rgw_client.py +++ b/src/pybind/mgr/dashboard/services/rgw_client.py @@ -660,7 +660,7 @@ class RgwClient(RestClient): except SubprocessError as error: raise DashboardException(error, http_status_code=500, component='rgw') - def edit_realm(self, realm_name: str, new_realm_name: str, default: str = None): + def edit_realm(self, realm_name: str, new_realm_name: str, default: str = ''): rgw_realm_edit_cmd = [] if new_realm_name != realm_name: rgw_realm_edit_cmd = ['realm', 'rename', '--rgw-realm',