From 59afa791221507b39d26190aca21b45487baabe4 Mon Sep 17 00:00:00 2001 From: Aashish Sharma Date: Mon, 27 Mar 2023 19:06:21 +0530 Subject: [PATCH] mgr/dashboard: Add unit test for realm Fixes: https://tracker.ceph.com/issues/59171 Signed-off-by: Aashish Sharma (cherry picked from commit eb56f2680c2b6fe8c8d0a930fae8aae6fee21801) --- src/pybind/mgr/dashboard/controllers/rgw.py | 2 +- ...rgw-multisite-realm-form.component.spec.ts | 57 +++++++++++++++++++ src/pybind/mgr/dashboard/openapi.yaml | 1 + .../mgr/dashboard/requirements-test.txt | 2 +- .../mgr/dashboard/services/rgw_client.py | 2 +- 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/pybind/mgr/dashboard/controllers/rgw.py b/src/pybind/mgr/dashboard/controllers/rgw.py index ffb757e86d4..b5230a1fe39 100644 --- a/src/pybind/mgr/dashboard/controllers/rgw.py +++ b/src/pybind/mgr/dashboard/controllers/rgw.py @@ -748,7 +748,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 ec3c052e4d1..2c2ceedb0bc 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 1f79dd12670..f1a39c4df51 100644 --- a/src/pybind/mgr/dashboard/openapi.yaml +++ b/src/pybind/mgr/dashboard/openapi.yaml @@ -8552,6 +8552,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 fe9dccbc44b..4e925e8616f 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 829a0f5c1e3..836531b65f9 100644 --- a/src/pybind/mgr/dashboard/services/rgw_client.py +++ b/src/pybind/mgr/dashboard/services/rgw_client.py @@ -661,7 +661,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', -- 2.39.5