Skip to content

Commit

Permalink
[ML] Update test descriptions for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Jul 21, 2020
1 parent c222100 commit 7fc0597
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 144 deletions.
97 changes: 47 additions & 50 deletions x-pack/test/api_integration/apis/ml/calendars/delete_calendars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export default ({ getService }: FtrProviderContext) => {
const ml = getService('ml');

describe('delete_calendars', function () {
const calendarId = `test_get_cal`;
const testCalendar = {
calendar_id: calendarId,
job_ids: ['test_job_1', 'test_job_2'],
description: `Test calendar`,
};
const testEvents = [
{ description: 'event 1', start_time: 1513641600000, end_time: 1513728000000 },
{ description: 'event 2', start_time: 1513814400000, end_time: 1513900800000 },
Expand All @@ -26,65 +32,56 @@ export default ({ getService }: FtrProviderContext) => {
await ml.testResources.setKibanaTimeZoneToUTC();
});

describe('get multiple calendars', function () {
const calendarId = `test_get_cal`;
const testCalendar = {
calendar_id: calendarId,
job_ids: ['test_job_1', 'test_job_2'],
description: `Test calendar`,
};

beforeEach(async () => {
await ml.api.createCalendar(calendarId, testCalendar);
await ml.api.createCalendarEvents(calendarId, testEvents);
});
beforeEach(async () => {
await ml.api.createCalendar(calendarId, testCalendar);
await ml.api.createCalendarEvents(calendarId, testEvents);
});

afterEach(async () => {
await ml.api.deleteCalendar(calendarId);
});
afterEach(async () => {
await ml.api.deleteCalendar(calendarId);
});

it('should delete calendar by id', async () => {
const { body } = await supertest
.delete(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.expect(200);
it('should delete calendar by id', async () => {
const { body } = await supertest
.delete(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.expect(200);

expect(body.acknowledged).to.eql(true);
await ml.api.waitForCalendarNotToExist(calendarId);
});
expect(body.acknowledged).to.eql(true);
await ml.api.waitForCalendarNotToExist(calendarId);
});

it('should not delete calendar for user without required permission', async () => {
const { body } = await supertest
.delete(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.expect(404);
it('should not delete calendar for user without required permission', async () => {
const { body } = await supertest
.delete(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.expect(404);

expect(body.error).to.eql('Not Found');
await ml.api.waitForCalendarToExist(calendarId);
});
expect(body.error).to.eql('Not Found');
await ml.api.waitForCalendarToExist(calendarId);
});

it('should not delete calendar for unauthorized user', async () => {
const { body } = await supertest
.delete(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
.set(COMMON_REQUEST_HEADERS)
.expect(404);
it('should not delete calendar for unauthorized user', async () => {
const { body } = await supertest
.delete(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
.set(COMMON_REQUEST_HEADERS)
.expect(404);

expect(body.error).to.eql('Not Found');
await ml.api.waitForCalendarToExist(calendarId);
});
expect(body.error).to.eql('Not Found');
await ml.api.waitForCalendarToExist(calendarId);
});

it('should return 404 if invalid calendarId', async () => {
const { body } = await supertest
.delete(`/api/ml/calendars/calendar_id_dne`)
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.expect(404);
it('should return 404 if invalid calendarId', async () => {
const { body } = await supertest
.delete(`/api/ml/calendars/calendar_id_dne`)
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.expect(404);

expect(body.error).to.eql('Not Found');
});
expect(body.error).to.eql('Not Found');
});
});
};
156 changes: 77 additions & 79 deletions x-pack/test/api_integration/apis/ml/calendars/update_calendars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,85 +20,83 @@ export default ({ getService }: FtrProviderContext) => {
await ml.testResources.setKibanaTimeZoneToUTC();
});

describe('update calendar by id', function () {
const calendarId = `test_update_cal`;
const originalCalendar = {
calendar_id: calendarId,
job_ids: ['test_job_1'],
description: `Test calendar`,
};
const originalEvents = [
{ description: 'event 1', start_time: 1513641600000, end_time: 1513728000000 },
];

const updateCalendarRequest = {
calendarId,
job_ids: ['test_updated_job_1', 'test_updated_job_2'],
description: 'Updated calendar #1',
events: [
{ description: 'updated event 2', start_time: 1513814400000, end_time: 1513900800000 },
{ description: 'updated event 3', start_time: 1514160000000, end_time: 1514246400000 },
],
};

beforeEach(async () => {
await ml.api.createCalendar(calendarId, originalCalendar);
await ml.api.createCalendarEvents(calendarId, originalEvents);
});

afterEach(async () => {
await ml.api.deleteCalendar(calendarId);
});

it('should update calendar with new settings', async () => {
await supertest
.put(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.send(updateCalendarRequest)
.expect(200);

await ml.api.waitForCalendarToExist(calendarId);

const getCalendarResult = await ml.api.getCalendar(calendarId);
const getEventsResult = await ml.api.getCalendarEvents(calendarId);

const updatedCalendar = getCalendarResult.body.calendars[0];
const updatedEvents = getEventsResult.body.events;

// TODO: need to implement update description in the API
// expect(updatedCalendar.description).to.eql(updateCalendarRequest.description);
expect(updatedCalendar.calendar_id).to.eql(updateCalendarRequest.calendarId);
expect(updatedCalendar.job_ids).to.have.length(updateCalendarRequest.job_ids.length);
expect(updatedEvents).to.have.length(updateCalendarRequest.events.length);
});

it('should not allow to update calendar for user without required permission ', async () => {
await supertest
.put(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.send(updateCalendarRequest)
.expect(404);
});

it('should not allow to update calendar for unauthorized user', async () => {
await supertest
.put(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
.set(COMMON_REQUEST_HEADERS)
.send(updateCalendarRequest)
.expect(404);
});

it('should return error if invalid calendarId ', async () => {
await supertest
.put(`/api/ml/calendars/calendar_id_dne`)
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.send(updateCalendarRequest)
.expect(404);
});
const calendarId = `test_update_cal`;
const originalCalendar = {
calendar_id: calendarId,
job_ids: ['test_job_1'],
description: `Test calendar`,
};
const originalEvents = [
{ description: 'event 1', start_time: 1513641600000, end_time: 1513728000000 },
];

const updateCalendarRequest = {
calendarId,
job_ids: ['test_updated_job_1', 'test_updated_job_2'],
description: 'Updated calendar #1',
events: [
{ description: 'updated event 2', start_time: 1513814400000, end_time: 1513900800000 },
{ description: 'updated event 3', start_time: 1514160000000, end_time: 1514246400000 },
],
};

beforeEach(async () => {
await ml.api.createCalendar(calendarId, originalCalendar);
await ml.api.createCalendarEvents(calendarId, originalEvents);
});

afterEach(async () => {
await ml.api.deleteCalendar(calendarId);
});

it('should update calendar by id with new settings', async () => {
await supertest
.put(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.send(updateCalendarRequest)
.expect(200);

await ml.api.waitForCalendarToExist(calendarId);

const getCalendarResult = await ml.api.getCalendar(calendarId);
const getEventsResult = await ml.api.getCalendarEvents(calendarId);

const updatedCalendar = getCalendarResult.body.calendars[0];
const updatedEvents = getEventsResult.body.events;

// TODO: need to implement update description in the API
// expect(updatedCalendar.description).to.eql(updateCalendarRequest.description);
expect(updatedCalendar.calendar_id).to.eql(updateCalendarRequest.calendarId);
expect(updatedCalendar.job_ids).to.have.length(updateCalendarRequest.job_ids.length);
expect(updatedEvents).to.have.length(updateCalendarRequest.events.length);
});

it('should not allow to update calendar for user without required permission ', async () => {
await supertest
.put(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.send(updateCalendarRequest)
.expect(404);
});

it('should not allow to update calendar for unauthorized user', async () => {
await supertest
.put(`/api/ml/calendars/${calendarId}`)
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
.set(COMMON_REQUEST_HEADERS)
.send(updateCalendarRequest)
.expect(404);
});

it('should return error if invalid calendarId ', async () => {
await supertest
.put(`/api/ml/calendars/calendar_id_dne`)
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.send(updateCalendarRequest)
.expect(404);
});
});
};
4 changes: 2 additions & 2 deletions x-pack/test/api_integration/apis/ml/filters/delete_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default ({ getService }: FtrProviderContext) => {
await ml.api.waitForFilterToNotExist(filterId);
});

it(`should not allow user to delete filter by id if no permission`, async () => {
it(`should not delete filter for user without required permission`, async () => {
const { filterId } = validFilters[1];
const { body } = await supertest
.delete(`/api/ml/filters/${filterId}`)
Expand All @@ -71,7 +71,7 @@ export default ({ getService }: FtrProviderContext) => {
await ml.api.waitForFilterToExist(filterId);
});

it(`should not allow user to delete filter by id if unauthorized`, async () => {
it(`should not delete filter for unauthorized user`, async () => {
const { filterId } = validFilters[1];
const { body } = await supertest
.delete(`/api/ml/filters/${filterId}`)
Expand Down
19 changes: 9 additions & 10 deletions x-pack/test/api_integration/apis/ml/filters/get_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ export default ({ getService }: FtrProviderContext) => {
expect(body).to.have.length(validFilters.length);
});

// TODO: check if this block should return 200
// it(`should allow to retrieve for user with no permission`, async () => {
// const { body } = await supertest
// .get(`/api/ml/filters`)
// .auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
// .set(COMMON_REQUEST_HEADERS)
// .expect(200);
//
// expect(body).to.have.length(validFilters.length);
// });
it(`should not allow to retrieve for user without required permission`, async () => {
const { body } = await supertest
.get(`/api/ml/filters`)
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.expect(404);
expect(body.error).to.eql('Not Found');
expect(body.message).to.eql('Not Found');
});

it(`should not allow to retrieve filters for unauthorized user`, async () => {
const { body } = await supertest
Expand Down
6 changes: 3 additions & 3 deletions x-pack/test/api_integration/apis/ml/filters/update_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default ({ getService }: FtrProviderContext) => {
expect(body.items).to.eql(updateFilterRequest.addItems);
});

it(`should not allow user to delete filter by id if no permission`, async () => {
it(`should not allow to update filter for user without required permission`, async () => {
const { filterId } = validFilters[1];
const { body } = await supertest
.put(`/api/ml/filters/${filterId}`)
Expand All @@ -77,7 +77,7 @@ export default ({ getService }: FtrProviderContext) => {
expect(body.error).to.eql('Not Found');
});

it(`should not allow user to delete filter by id if unauthorized`, async () => {
it(`should not allow to update filter for unauthorized user`, async () => {
const { filterId, requestBody: oldFilterRequest } = validFilters[1];
const { body } = await supertest
.put(`/api/ml/filters/${filterId}`)
Expand All @@ -97,7 +97,7 @@ export default ({ getService }: FtrProviderContext) => {
expect(updatedFilter.items).to.eql(oldFilterRequest.items);
});

it(`should not allow user to delete filter if invalid filterId`, async () => {
it(`should return appropriate error if invalid filterId`, async () => {
const { body } = await supertest
.put(`/api/ml/filters/filter_id_dne`)
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
Expand Down

0 comments on commit 7fc0597

Please sign in to comment.