[Uptime] fix sending flag to indicate edits when updating service monitors (#133608)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Lucas F. da Costa 2022-06-13 10:43:00 +01:00 committed by GitHub
parent 23f091b183
commit 8e81a6e569
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 7 deletions

View file

@ -0,0 +1,81 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { loggerMock } from '@kbn/logging-mocks';
import { syncEditedMonitor } from './edit_monitor';
import { SavedObjectsUpdateResponse, SavedObject } from '@kbn/core/server';
import { EncryptedSyntheticsMonitor, SyntheticsMonitor } from '../../../common/runtime_types';
import { UptimeServerSetup } from '../../legacy_uptime/lib/adapters';
import { SyntheticsService } from '../../synthetics_service/synthetics_service';
jest.mock('../telemetry/monitor_upgrade_sender', () => ({
sendTelemetryEvents: jest.fn(),
formatTelemetryUpdateEvent: jest.fn(),
}));
describe('syncEditedMonitor', () => {
const logger = loggerMock.create();
const serverMock: UptimeServerSetup = {
uptimeEsClient: { search: jest.fn() },
kibanaVersion: null,
authSavedObjectsClient: { bulkUpdate: jest.fn() },
logger,
} as unknown as UptimeServerSetup;
const syntheticsService = new SyntheticsService(logger, serverMock, {
username: 'dev',
password: '12345',
});
const fakePush = jest.fn();
jest.spyOn(syntheticsService, 'pushConfigs').mockImplementationOnce(fakePush);
serverMock.syntheticsService = syntheticsService;
const editedMonitor = {
type: 'http',
enabled: true,
schedule: {
number: '3',
unit: 'm',
},
name: 'my mon',
locations: [],
urls: 'http://google.com',
max_redirects: '0',
password: '',
proxy_url: '',
id: '7af7e2f0-d5dc-11ec-87ac-bdfdb894c53d',
fields: { config_id: '7af7e2f0-d5dc-11ec-87ac-bdfdb894c53d' },
fields_under_root: true,
} as unknown as SyntheticsMonitor;
const previousMonitor = { id: 'saved-obj-id' } as SavedObject<EncryptedSyntheticsMonitor>;
const editedMonitorSavedObject = {
id: 'saved-obj-id',
} as SavedObjectsUpdateResponse<EncryptedSyntheticsMonitor>;
it('includes the isEdit flag', () => {
syncEditedMonitor({
editedMonitor,
editedMonitorSavedObject,
previousMonitor,
server: serverMock,
});
expect(fakePush).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
id: 'saved-obj-id',
}),
]),
true
);
});
});

View file

@ -127,13 +127,16 @@ export const syncEditedMonitor = async ({
previousMonitor: SavedObject<EncryptedSyntheticsMonitor>;
server: UptimeServerSetup;
}) => {
const errors = await server.syntheticsService.pushConfigs([
formatHeartbeatRequest({
monitor: editedMonitor,
monitorId: editedMonitorSavedObject.id,
customHeartbeatId: (editedMonitor as MonitorFields)[ConfigKey.CUSTOM_HEARTBEAT_ID],
}),
]);
const errors = await server.syntheticsService.pushConfigs(
[
formatHeartbeatRequest({
monitor: editedMonitor,
monitorId: editedMonitorSavedObject.id,
customHeartbeatId: (editedMonitor as MonitorFields)[ConfigKey.CUSTOM_HEARTBEAT_ID],
}),
],
true
);
sendTelemetryEvents(
server.logger,