From b2296bf89e719dc88b3e1aa7a918d0dccc99bb03 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alfonso=20Mart=C3=ADnez?= Date: Tue, 21 Jan 2020 13:49:02 +0100 Subject: [PATCH] mgr/dashboard: check if user has config-opt permissions before retrieving settings. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Fixes: https://tracker.ceph.com/issues/43595 Signed-off-by: Alfonso Martínez (cherry picked from commit ea1c8c7ef7f494208aec1c59d12fec2da759784a) Conflicts: src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.ts src/pybind/mgr/dashboard/frontend/src/app/shared/components/notifications-sidebar/notifications-sidebar.component.spec.ts src/pybind/mgr/dashboard/frontend/src/app/shared/components/notifications-sidebar/notifications-sidebar.component.ts --- .../navigation/navigation.component.spec.ts | 24 ++++++++++++++++++- .../navigation/navigation.component.ts | 4 +++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.spec.ts index 29dc31546895b..8e1662f41925c 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.spec.ts @@ -2,11 +2,15 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper'; import { AppModule } from '../../../app.module'; +import { PrometheusService } from '../../../shared/api/prometheus.service'; +import { Permissions } from '../../../shared/models/permissions'; +import { AuthStorageService } from '../../../shared/services/auth-storage.service'; import { NavigationComponent } from './navigation.component'; describe('NavigationComponent', () => { let component: NavigationComponent; let fixture: ComponentFixture; + let ifAlertmanagerConfiguredSpy: jasmine.Spy; configureTestBed({ imports: [AppModule], @@ -14,12 +18,30 @@ describe('NavigationComponent', () => { }); beforeEach(() => { + ifAlertmanagerConfiguredSpy = spyOn( + TestBed.get(PrometheusService), + 'ifAlertmanagerConfigured' + ).and.stub(); fixture = TestBed.createComponent(NavigationComponent); component = fixture.componentInstance; fixture.detectChanges(); }); - it('should create', () => { + it('should create and PrometheusService methods should not have been called', () => { expect(component).toBeTruthy(); + expect(ifAlertmanagerConfiguredSpy).toHaveBeenCalledTimes(0); + }); + + it('PrometheusService methods should have been called', () => { + const authStorageServiceSpy = spyOn( + TestBed.get(AuthStorageService), + 'getPermissions' + ).and.returnValue(new Permissions({ 'config-opt': ['read'] })); + TestBed.overrideProvider(AuthStorageService, { useValue: authStorageServiceSpy }); + fixture = TestBed.createComponent(NavigationComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + + expect(ifAlertmanagerConfiguredSpy).toHaveBeenCalledTimes(1); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.ts index 5f8d1f5a7e96a..3e0cd6f1d13fd 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.ts @@ -39,7 +39,9 @@ export class NavigationComponent implements OnInit { } this.summaryData = data; }); - this.prometheusService.ifAlertmanagerConfigured(() => (this.prometheusConfigured = true)); + if (this.permissions.configOpt.read) { + this.prometheusService.ifAlertmanagerConfigured(() => (this.prometheusConfigured = true)); + } } blockHealthColor() { -- 2.39.5