Skip to content

Commit

Permalink
add api test
Browse files Browse the repository at this point in the history
  • Loading branch information
peluja1012 committed Jul 15, 2020
1 parent d4211ca commit 88ec097
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ describe('create_endpoint_list_schema', () => {
expect(message.schema).toEqual(payload);
});

test('it should NOT allow missing fields', () => {
const payload = getExceptionListSchemaMock();
delete payload.list_id;
const decoded = createEndpointListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);

expect(getPaths(left(message.errors)).length).toEqual(1);
expect(message.schema).toEqual({});
});

test('it should not allow an extra key to be sent in', () => {
const payload: CreateEndpointListSchema & {
extraKey?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import * as t from 'io-ts';

import { exceptionListSchema } from './exception_list_schema';

export const createEndpointListSchema = t.union([exceptionListSchema, t.type({})]);
export const createEndpointListSchema = t.union([exceptionListSchema, t.exact(t.type({}))]);

export type CreateEndpointListSchema = t.TypeOf<typeof createEndpointListSchema>;
36 changes: 36 additions & 0 deletions x-pack/plugins/lists/public/exceptions/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '../../common/schemas';

import {
addEndpointExceptionList,
addExceptionList,
addExceptionListItem,
deleteExceptionListById,
Expand Down Expand Up @@ -738,4 +739,39 @@ describe('Exceptions Lists API', () => {
).rejects.toEqual('Invalid value "undefined" supplied to "id"');
});
});

describe('#addEndpointExceptionList', () => {
beforeEach(() => {
fetchMock.mockClear();
fetchMock.mockResolvedValue(getExceptionListSchemaMock());
});

test('it invokes "addEndpointExceptionList" with expected url and body values', async () => {
await addEndpointExceptionList({
http: mockKibanaHttpService(),
signal: abortCtrl.signal,
});
expect(fetchMock).toHaveBeenCalledWith('/api/endpoint_list', {
method: 'POST',
signal: abortCtrl.signal,
});
});

test('it returns expected exception list on success', async () => {
const exceptionResponse = await addEndpointExceptionList({
http: mockKibanaHttpService(),
signal: abortCtrl.signal,
});
expect(exceptionResponse).toEqual(getExceptionListSchemaMock());
});

test('it returns an empty object when list already exists', async () => {
fetchMock.mockResolvedValue({});
const exceptionResponse = await addEndpointExceptionList({
http: mockKibanaHttpService(),
signal: abortCtrl.signal,
});
expect(exceptionResponse).toEqual({});
});
});
});

0 comments on commit 88ec097

Please sign in to comment.