Azure: Fix default subscription setting (#90185)

* Ensure default subscription is set

* Include some info for when the default subscription is required

* Rename settings mock

- Add mockDatasourceSettings function

* Add MonitorConfig test

* Revert "Rename settings mock"

- Avoid issues when backporting

* Remove description

* Remove required field

* Remove unused import
This commit is contained in:
Andreas Christou 2024-07-10 17:26:49 +07:00 committed by GitHub
parent 9216a3df7d
commit bba26f8e13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 84 additions and 2 deletions

View File

@ -0,0 +1,40 @@
import { KeyValue } from '@grafana/data';
import { AzureDataSourceSettings } from '../types';
import { DeepPartial } from './utils';
export const createMockDatasourceSettings = (
overrides?: DeepPartial<AzureDataSourceSettings>,
secureJsonFieldsOverrides?: KeyValue<boolean>
): AzureDataSourceSettings => {
return {
id: 1,
uid: 'uid',
orgId: 1,
name: 'test-data-source',
typeLogoUrl: 'logo',
type: 'grafana-azure-monitor-datasource',
typeName: 'datasource',
access: '',
url: '',
user: '',
database: '',
basicAuth: false,
basicAuthUser: '',
isDefault: false,
jsonData: {
cloudName: 'azuremonitor',
azureAuthType: 'clientsecret',
tenantId: 'abc-123',
clientId: 'def-456',
subscriptionId: 'ghi-789',
...overrides?.jsonData,
},
secureJsonData: { ...overrides?.secureJsonData },
secureJsonFields: { ...secureJsonFieldsOverrides },
readOnly: false,
withCredentials: false,
};
};

View File

@ -0,0 +1,42 @@
import { render, screen, waitFor } from '@testing-library/react';
import { createMockDatasourceSettings } from '../../__mocks__/datasourceSettings';
import { MonitorConfig, Props } from './MonitorConfig';
const mockDatasourceSettings = createMockDatasourceSettings();
const defaultProps: Props = {
options: mockDatasourceSettings,
updateOptions: jest.fn(),
getSubscriptions: jest.fn().mockResolvedValue([]),
};
describe('MonitorConfig', () => {
it('should render component', () => {
render(<MonitorConfig {...defaultProps} />);
expect(screen.getByText('Azure Cloud')).toBeInTheDocument();
});
it('should render component and set the default auth type if unset', () => {
const mockDsSettingsWithoutAuth = createMockDatasourceSettings({
jsonData: { azureAuthType: undefined, clientId: undefined, tenantId: undefined },
});
render(<MonitorConfig {...defaultProps} options={mockDsSettingsWithoutAuth} />);
expect(defaultProps.updateOptions).toHaveBeenCalled();
expect(screen.getByText('Azure Cloud')).toBeInTheDocument();
});
expect(defaultProps.options.jsonData.azureAuthType).toBe('clientsecret');
it('should render component and set the default subscription if specified', async () => {
const mockDsSettingsWithAuth = createMockDatasourceSettings(undefined, { clientSecret: true });
const getSubscriptions = jest.fn().mockResolvedValue([{ label: 'Test Sub', value: 'ghi-789' }]);
render(<MonitorConfig {...defaultProps} options={mockDsSettingsWithAuth} getSubscriptions={getSubscriptions} />);
expect(screen.getByText('Azure Cloud')).toBeInTheDocument();
await waitFor(() => expect(screen.getByText('Test Sub')).toBeInTheDocument());
});
});

View File

@ -42,8 +42,8 @@ export const MonitorConfig = (props: Props) => {
// The auth type needs to be set on the first load of the data source // The auth type needs to be set on the first load of the data source
useEffectOnce(() => { useEffectOnce(() => {
if (!options.jsonData.authType) { if (!options.jsonData.authType || !credentials.authType) {
onCredentialsChange(credentials); onCredentialsChange(credentials, options.jsonData.subscriptionId);
} }
}); });