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<NavigationComponent>;
+ let ifAlertmanagerConfiguredSpy: jasmine.Spy;
+ let ifPrometheusConfigured: jasmine.Spy;
configureTestBed({
imports: [AppModule],
});
beforeEach(() => {
+ ifAlertmanagerConfiguredSpy = spyOn(
+ TestBed.get(PrometheusService),
+ 'ifAlertmanagerConfigured'
+ ).and.stub();
+ ifPrometheusConfigured = spyOn(
+ TestBed.get(PrometheusService),
+ 'ifPrometheusConfigured'
+ ).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);
+ expect(ifPrometheusConfigured).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);
+ expect(ifPrometheusConfigured).toHaveBeenCalledTimes(1);
});
});
}
this.summaryData = data;
});
- this.prometheusService.ifAlertmanagerConfigured(() => {
- this.isAlertmanagerConfigured = true;
- });
- this.prometheusService.ifPrometheusConfigured(() => {
- this.isPrometheusConfigured = true;
- });
-
+ if (this.permissions.configOpt.read) {
+ this.prometheusService.ifAlertmanagerConfigured(() => {
+ this.isAlertmanagerConfigured = true;
+ });
+ this.prometheusService.ifPrometheusConfigured(() => {
+ this.isPrometheusConfigured = true;
+ });
+ }
this.authStorageService.isPwdDisplayed$.subscribe((isDisplayed) => {
this.isPwdDisplayed = isDisplayed;
});
import { SettingsService } from '../../api/settings.service';
import { NotificationType } from '../../enum/notification-type.enum';
import { ExecutingTask } from '../../models/executing-task';
+import { Permissions } from '../../models/permissions';
import { PipesModule } from '../../pipes/pipes.module';
import { AuthStorageService } from '../../services/auth-storage.service';
import { NotificationService } from '../../services/notification.service';
describe('prometheus alert handling', () => {
let prometheusAlertService: PrometheusAlertService;
let prometheusNotificationService: PrometheusNotificationService;
- let prometheusAccessAllowed: boolean;
+ let prometheusReadPermission: string;
+ let configOptReadPermission: string;
const expectPrometheusServicesToBeCalledTimes = (n: number) => {
expect(prometheusNotificationService.refresh).toHaveBeenCalledTimes(n);
};
beforeEach(() => {
- prometheusAccessAllowed = true;
- spyOn(TestBed.get(AuthStorageService), 'getPermissions').and.callFake(() => ({
- prometheus: { read: prometheusAccessAllowed }
- }));
+ prometheusReadPermission = 'read';
+ configOptReadPermission = 'read';
+ spyOn(TestBed.get(AuthStorageService), 'getPermissions').and.callFake(
+ () =>
+ new Permissions({
+ prometheus: [prometheusReadPermission],
+ 'config-opt': [configOptReadPermission]
+ })
+ );
spyOn(TestBed.get(PrometheusService), 'ifAlertmanagerConfigured').and.callFake((fn) => fn());
});
it('should not refresh prometheus services if not allowed', () => {
- prometheusAccessAllowed = false;
+ prometheusReadPermission = '';
+ configOptReadPermission = 'read';
+ fixture.detectChanges();
+
+ expectPrometheusServicesToBeCalledTimes(0);
+
+ prometheusReadPermission = 'read';
+ configOptReadPermission = '';
fixture.detectChanges();
expectPrometheusServicesToBeCalledTimes(0);
}
ngOnInit() {
- if (this.authStorageService.getPermissions().prometheus.read) {
+ const permissions = this.authStorageService.getPermissions();
+ if (permissions.prometheus.read && permissions.configOpt.read) {
this.triggerPrometheusAlerts();
this.ngZone.runOutsideAngular(() => {
this.interval = window.setInterval(() => {