mirror of https://github.com/grafana/grafana.git
Alerting: Fix subpath handling in the alerting package (#109448)
Take subpath into account when building contact points APIs URL
This commit is contained in:
parent
1625bc1e4e
commit
a0cf529465
|
@ -0,0 +1,67 @@
|
|||
import { config } from '@grafana/runtime';
|
||||
|
||||
import { getAPIBaseURL, getAPINamespace, getAPIReducerPath } from './util';
|
||||
|
||||
describe('API utilities', () => {
|
||||
const originalAppSubUrl = config.appSubUrl;
|
||||
const originalNamespace = config.namespace;
|
||||
|
||||
afterEach(() => {
|
||||
// Restore original config after each test
|
||||
config.appSubUrl = originalAppSubUrl;
|
||||
config.namespace = originalNamespace;
|
||||
});
|
||||
|
||||
describe('getAPIBaseURL', () => {
|
||||
const group = 'notifications.alerting.grafana.app';
|
||||
const version = 'v0alpha1';
|
||||
|
||||
it('should generate correct API base URL without subpath', () => {
|
||||
config.appSubUrl = '';
|
||||
config.namespace = 'default';
|
||||
|
||||
const result = getAPIBaseURL(group, version);
|
||||
|
||||
expect(result).toBe('/apis/notifications.alerting.grafana.app/v0alpha1/namespaces/default');
|
||||
});
|
||||
|
||||
it('should generate correct API base URL with subpath', () => {
|
||||
config.appSubUrl = '/grafana';
|
||||
config.namespace = 'default';
|
||||
|
||||
const result = getAPIBaseURL(group, version);
|
||||
|
||||
expect(result).toBe('/grafana/apis/notifications.alerting.grafana.app/v0alpha1/namespaces/default');
|
||||
});
|
||||
|
||||
it('should handle different namespace', () => {
|
||||
config.appSubUrl = '/grafana';
|
||||
config.namespace = 'custom-namespace';
|
||||
|
||||
const result = getAPIBaseURL(group, version);
|
||||
|
||||
expect(result).toBe('/grafana/apis/notifications.alerting.grafana.app/v0alpha1/namespaces/custom-namespace');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAPINamespace', () => {
|
||||
it('should return configured namespace', () => {
|
||||
config.namespace = 'test-namespace';
|
||||
|
||||
const result = getAPINamespace();
|
||||
|
||||
expect(result).toBe('test-namespace');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAPIReducerPath', () => {
|
||||
it('should generate correct reducer path', () => {
|
||||
const group = 'notifications.alerting.grafana.app';
|
||||
const version = 'v0alpha1';
|
||||
|
||||
const result = getAPIReducerPath(group, version);
|
||||
|
||||
expect(result).toBe('notifications.alerting.grafana.app/v0alpha1');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -6,8 +6,10 @@ import { config } from '@grafana/runtime';
|
|||
|
||||
export const getAPINamespace = () => config.namespace;
|
||||
|
||||
export const getAPIBaseURL = (group: string, version: string) =>
|
||||
`/apis/${group}/${version}/namespaces/${getAPINamespace()}` as const;
|
||||
export const getAPIBaseURL = (group: string, version: string) => {
|
||||
const subPath = config.appSubUrl || '';
|
||||
return `${subPath}/apis/${group}/${version}/namespaces/${getAPINamespace()}` as const;
|
||||
};
|
||||
|
||||
// By including the version in the reducer path we can prevent cache bugs when different versions of the API are used for the same entities
|
||||
export const getAPIReducerPath = (group: string, version: string) => `${group}/${version}` as const;
|
||||
|
|
Loading…
Reference in New Issue