Skip to content

Commit

Permalink
added api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Oct 29, 2024
1 parent 79b5711 commit 2197a99
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import { schema } from '@kbn/config-schema';
import { DeleteMonitorAPI } from '../services/delete_monitor_api';
import { SYNTHETICS_API_URLS } from '../../../../common/constants';
import { DeleteParamsResponse } from '../../../../common/runtime_types';
import { SyntheticsRestApiRouteFactory } from '../../types';

export const deleteSyntheticsMonitorBulkRoute: SyntheticsRestApiRouteFactory<
DeleteParamsResponse[],
Array<{ id: string; deleted: boolean }>,
Record<string, string>,
Record<string, string>,
{ ids: string[] }
Expand All @@ -29,15 +28,13 @@ export const deleteSyntheticsMonitorBulkRoute: SyntheticsRestApiRouteFactory<
}),
},
},
handler: async (routeContext): Promise<any> => {
handler: async (routeContext) => {
const { request } = routeContext;

const { ids: idsToDelete } = request.body || {};

const result: DeleteParamsResponse[] = [];
const deleteMonitorAPI = new DeleteMonitorAPI(routeContext);

const { errors } = await deleteMonitorAPI.execute({
const { errors, result } = await deleteMonitorAPI.execute({
monitorIds: idsToDelete,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class DeleteMonitorAPI {
});
});

return { errors };
return { errors, result: this.result };
} catch (e) {
server.logger.error(`Unable to delete Synthetics monitor with error ${e.message}`);
server.logger.error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export const deleteSyntheticsParamsRoute: SyntheticsRestApiRouteFactory<
},
handler: async ({ savedObjectsClient, request, response }) => {
const { ids } = request.body;
const { id: queryId } = request;
const { id: paramId } = request.params ?? {};

if (ids && queryId) {
if (ids && paramId) {
return response.badRequest({
body: 'Both query and body parameters cannot be provided',
body: `Both param id ${queryId} and body parameters cannot be provided`,
});
}

Expand Down
38 changes: 38 additions & 0 deletions x-pack/test/api_integration/apis/synthetics/add_edit_params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,5 +353,43 @@ export default function ({ getService }: FtrProviderContext) {
expect(param.key).to.not.empty();
});
});

it('should handle bulk deleting params', async () => {
const params = [
{ key: 'param1', value: 'value1' },
{ key: 'param2', value: 'value2' },
{ key: 'param3', value: 'value3' },
];

for (const param of params) {
await supertestAPI
.post(SYNTHETICS_API_URLS.PARAMS)
.set('kbn-xsrf', 'true')
.send(param)
.expect(200);
}

const getResponse = await supertestAPI
.get(SYNTHETICS_API_URLS.PARAMS)
.set('kbn-xsrf', 'true')
.expect(200);

expect(getResponse.body.length).to.eql(3);

const ids = getResponse.body.map((param: any) => param.id);

await supertestAPI
.post(SYNTHETICS_API_URLS.PARAMS + '/_bulk_delete')
.set('kbn-xsrf', 'true')
.send({ ids })
.expect(200);

const getResponseAfterDelete = await supertestAPI
.get(SYNTHETICS_API_URLS.PARAMS)
.set('kbn-xsrf', 'true')
.expect(200);

expect(getResponseAfterDelete.body.length).to.eql(0);
});
});
}
27 changes: 27 additions & 0 deletions x-pack/test/api_integration/apis/synthetics/delete_monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,33 @@ export default function ({ getService }: FtrProviderContext) {
await supertest.get(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + '/' + monitorId).expect(404);
});

it('deletes multiple monitors by bulk delete', async () => {
const { id: monitorId } = await saveMonitor(httpMonitorJson as MonitorFields);
const { id: monitorId2 } = await saveMonitor({
...httpMonitorJson,
name: 'another -2',
} as MonitorFields);

const deleteResponse = await monitorTestService.deleteMonitorBulk(
[monitorId2, monitorId],
200
);

expect(
deleteResponse.body.result.sort((a: { id: string }, b: { id: string }) =>
a.id > b.id ? 1 : -1
)
).eql(
[
{ id: monitorId2, deleted: true },
{ id: monitorId, deleted: true },
].sort((a, b) => (a.id > b.id ? 1 : -1))
);

// Hit get endpoint and expect 404 as well
await supertest.get(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + '/' + monitorId).expect(404);
});

it('returns 404 if monitor id is not found', async () => {
const invalidMonitorId = 'invalid-id';
const expected404Message = `Monitor id ${invalidMonitorId} not found!`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,17 @@ export class SyntheticsMonitorTestService {
expect(deleteResponse.status).to.eql(statusCode);
return deleteResponse;
}

async deleteMonitorBulk(monitorIds: string[], statusCode = 200, spaceId?: string) {
const deleteResponse = await this.supertest
.post(
spaceId
? `/s/${spaceId}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}/_bulk_delete`
: SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + '/_bulk_delete'
)
.send({ ids: monitorIds })
.set('kbn-xsrf', 'true');
expect(deleteResponse.status).to.eql(statusCode);
return deleteResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,9 @@ export default function ({ getService }: FtrProviderContext) {
const deleteResponse = await supertestAPI
.delete(SYNTHETICS_API_URLS.PARAMS)
.set('kbn-xsrf', 'true')
.send({ ids })
.expect(200);
.send({ ids });

expect(deleteResponse.status).eql(200, JSON.stringify(deleteResponse.body));

expect(deleteResponse.body).to.have.length(2);

Expand Down

0 comments on commit 2197a99

Please sign in to comment.