[Synthetics] Fixes all spaces params usage in monitors (#153826)

Co-authored-by: Dominique Clarke <dominique.clarke@elastic.co>
This commit is contained in:
Shahzad 2023-03-30 20:09:15 +02:00 committed by GitHub
parent c9dde2fbb3
commit 78450b0fae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 63 additions and 11 deletions

View file

@ -37,7 +37,7 @@ describe('StatusRuleExecutor', () => {
getSpaceId: jest.fn().mockReturnValue('test-space'),
},
},
encryptedSavedObjects: mockEncryptedSO,
encryptedSavedObjects: mockEncryptedSO(),
} as unknown as UptimeServerSetup;
const syntheticsService = new SyntheticsService(serverMock);

View file

@ -53,7 +53,7 @@ describe('syncEditedMonitor', () => {
buildPackagePolicyFromPackage: jest.fn().mockReturnValue({}),
},
},
encryptedSavedObjects: mockEncryptedSO,
encryptedSavedObjects: mockEncryptedSO(),
} as unknown as UptimeServerSetup;
const editedMonitor = {

View file

@ -37,7 +37,7 @@ describe('processMonitors', () => {
getSpaceId: jest.fn().mockReturnValue('test-space'),
},
},
encryptedSavedObjects: mockEncryptedSO,
encryptedSavedObjects: mockEncryptedSO(),
} as unknown as UptimeServerSetup;
const syntheticsService = new SyntheticsService(serverMock);

View file

@ -122,7 +122,7 @@ describe('ProjectMonitorFormatter', () => {
getSpaceId: jest.fn().mockReturnValue('test-space'),
},
},
encryptedSavedObjects: mockEncryptedSO,
encryptedSavedObjects: mockEncryptedSO(),
} as unknown as UptimeServerSetup;
const syntheticsService = new SyntheticsService(serverMock);

View file

@ -116,7 +116,7 @@ describe('ProjectMonitorFormatterLegacy', () => {
getSpaceId: jest.fn().mockReturnValue('test-space'),
},
},
encryptedSavedObjects: mockEncryptedSO,
encryptedSavedObjects: mockEncryptedSO(),
} as unknown as UptimeServerSetup;
const syntheticsService = new SyntheticsService(serverMock);

View file

@ -44,7 +44,7 @@ describe('SyntheticsMonitorClient', () => {
manifestUrl: 'http://localhost:8080/api/manifest',
},
},
encryptedSavedObjects: mockEncryptedSO,
encryptedSavedObjects: mockEncryptedSO(),
} as unknown as UptimeServerSetup;
const syntheticsService = new SyntheticsService(serverMock);
@ -181,6 +181,9 @@ describe('SyntheticsMonitorClient', () => {
{
monitor,
configId: id,
params: {
username: 'elastic',
},
},
]);
expect(syntheticsService.deleteConfigs).toHaveBeenCalledTimes(1);

View file

@ -38,7 +38,7 @@ describe('SyntheticsService', () => {
manifestUrl: 'http://localhost:8080/api/manifest',
},
},
encryptedSavedObjects: mockEncryptedSO,
encryptedSavedObjects: mockEncryptedSO(),
} as unknown as UptimeServerSetup;
const getMockedService = (locationsNum: number = 1) => {
@ -175,4 +175,49 @@ describe('SyntheticsService', () => {
);
});
});
describe('getSyntheticsParams', () => {
it('returns the params for all spaces', async () => {
const { service } = getMockedService();
(axios as jest.MockedFunction<typeof axios>).mockResolvedValue({} as AxiosResponse);
const params = await service.getSyntheticsParams();
expect(params).toEqual({
'*': {
username: 'elastic',
},
});
});
it('returns the params for specific space', async () => {
const { service } = getMockedService();
const params = await service.getSyntheticsParams({ spaceId: 'default' });
expect(params).toEqual({
'*': {
username: 'elastic',
},
default: {
username: 'elastic',
},
});
});
it('returns the space limited params', async () => {
const { service } = getMockedService();
serverMock.encryptedSavedObjects = mockEncryptedSO([
{ attributes: { key: 'username', value: 'elastic' }, namespaces: ['default'] },
]) as any;
const params = await service.getSyntheticsParams({ spaceId: 'default' });
expect(params).toEqual({
default: {
username: 'elastic',
},
});
});
});
});

View file

@ -528,7 +528,7 @@ export class SyntheticsService {
if (paramsBySpace[ALL_SPACES_ID]) {
Object.keys(paramsBySpace).forEach((space) => {
if (space !== ALL_SPACES_ID) {
paramsBySpace[space] = Object.assign(paramsBySpace[space], paramsBySpace['*']);
paramsBySpace[space] = Object.assign(paramsBySpace[ALL_SPACES_ID], paramsBySpace[space]);
}
});
if (spaceId) {

View file

@ -7,16 +7,20 @@
import { EncryptedSavedObjectsClient } from '@kbn/encrypted-saved-objects-plugin/server';
export const mockEncryptedSO = {
export const mockEncryptedSO = (
data: any = [{ attributes: { key: 'username', value: 'elastic' }, namespaces: ['*'] }]
) => ({
getClient: jest.fn().mockReturnValue({
getDecryptedAsInternalUser: jest.fn(),
createPointInTimeFinderDecryptedAsInternalUser: jest.fn().mockImplementation(() => ({
close: jest.fn(),
find: jest.fn().mockReturnValue({
async *[Symbol.asyncIterator]() {
yield { saved_objects: [{ attributes: { key: 'username', value: 'elastic' } }] };
yield {
saved_objects: data,
};
},
}),
})),
} as jest.Mocked<EncryptedSavedObjectsClient>),
};
});