Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[App Search] Credentials: Add final Logic and server routes #81519

Merged
merged 8 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,115 @@ describe('credentials routes', () => {
});
});

describe('POST /api/app_search/credentials', () => {
let mockRouter: MockRouter;

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'post', payload: 'body' });

registerCredentialsRoutes({
...mockDependencies,
router: mockRouter.router,
});
});

it('creates a request handler', () => {
expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({
path: '/as/credentials/collection',
});
});

describe('validates', () => {
describe('admin keys', () => {
it('correctly', () => {
const request = {
body: {
name: 'admin-key',
type: 'admin',
},
};
mockRouter.shouldValidate(request);
});

it('throws on unnecessary properties', () => {
const request = {
body: {
name: 'admin-key',
type: 'admin',
read: true,
access_all_engines: true,
},
};
mockRouter.shouldThrow(request);
});
});

describe('private keys', () => {
it('correctly', () => {
const request = {
body: {
name: 'private-key',
type: 'private',
read: true,
write: false,
access_all_engines: false,
engines: ['engine1', 'engine2'],
},
};
mockRouter.shouldValidate(request);
});

it('throws on missing keys', () => {
const request = {
body: {
name: 'private-key',
type: 'private',
},
};
mockRouter.shouldThrow(request);
});
});

describe('search keys', () => {
it('correctly', () => {
const request = {
body: {
name: 'search-key',
type: 'search',
access_all_engines: true,
},
};
mockRouter.shouldValidate(request);
});

it('throws on missing keys', () => {
const request = {
body: {
name: 'search-key',
type: 'search',
},
};
mockRouter.shouldThrow(request);
});

it('throws on extra keys', () => {
const request = {
body: {
name: 'search-key',
type: 'search',
read: true,
write: false,
access_all_engines: false,
engines: ['engine1', 'engine2'],
},
};
mockRouter.shouldThrow(request);
});
});
});
});

describe('GET /api/app_search/credentials/details', () => {
let mockRouter: MockRouter;

Expand All @@ -61,6 +170,123 @@ describe('credentials routes', () => {
});
});

describe('PUT /api/app_search/credentials/{name}', () => {
let mockRouter: MockRouter;

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'put', payload: 'body' });

registerCredentialsRoutes({
...mockDependencies,
router: mockRouter.router,
});
});

it('creates a request to enterprise search', () => {
const mockRequest = {
params: {
name: 'abc123',
},
};

mockRouter.callRoute(mockRequest);

expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({
path: '/as/credentials/abc123',
});
});

describe('validates', () => {
describe('admin keys', () => {
Comment on lines +200 to +201
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this is copied/pasted from the new POST route tests. In theory we could DRY it out (since both routes inherit the same exact schema), but I didn't want to over-complicate it for now.

it('correctly', () => {
const request = {
body: {
name: 'admin-key',
type: 'admin',
},
};
mockRouter.shouldValidate(request);
});

it('throws on unnecessary properties', () => {
const request = {
body: {
name: 'admin-key',
type: 'admin',
read: true,
access_all_engines: true,
},
};
mockRouter.shouldThrow(request);
});
});

describe('private keys', () => {
it('correctly', () => {
const request = {
body: {
name: 'private-key',
type: 'private',
read: true,
write: false,
access_all_engines: false,
engines: ['engine1', 'engine2'],
},
};
mockRouter.shouldValidate(request);
});

it('throws on missing keys', () => {
const request = {
body: {
name: 'private-key',
type: 'private',
},
};
mockRouter.shouldThrow(request);
});
});

describe('search keys', () => {
it('correctly', () => {
const request = {
body: {
name: 'search-key',
type: 'search',
access_all_engines: true,
},
};
mockRouter.shouldValidate(request);
});

it('throws on missing keys', () => {
const request = {
body: {
name: 'search-key',
type: 'search',
},
};
mockRouter.shouldThrow(request);
});

it('throws on extra keys', () => {
const request = {
body: {
name: 'search-key',
type: 'search',
read: true,
write: false,
access_all_engines: false,
engines: ['engine1', 'engine2'],
},
};
mockRouter.shouldThrow(request);
});
});
});
});

describe('DELETE /api/app_search/credentials/{name}', () => {
let mockRouter: MockRouter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,32 @@ import { schema } from '@kbn/config-schema';

import { IRouteDependencies } from '../../plugin';

const tokenSchema = schema.oneOf([
schema.object({
name: schema.string(),
type: schema.literal('admin'),
}),
schema.object({
name: schema.string(),
type: schema.literal('private'),
read: schema.boolean(),
write: schema.boolean(),
access_all_engines: schema.boolean(),
engines: schema.maybe(schema.arrayOf(schema.string())),
}),
schema.object({
name: schema.string(),
type: schema.literal('search'),
access_all_engines: schema.boolean(),
engines: schema.maybe(schema.arrayOf(schema.string())),
}),
]);

export function registerCredentialsRoutes({
router,
enterpriseSearchRequestHandler,
}: IRouteDependencies) {
// Credentials API
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
router.get(
{
path: '/api/app_search/credentials',
Expand All @@ -25,6 +47,19 @@ export function registerCredentialsRoutes({
path: '/as/credentials/collection',
})
);
router.post(
{
path: '/api/app_search/credentials',
validate: {
body: tokenSchema,
},
},
enterpriseSearchRequestHandler.createRequest({
path: '/as/credentials/collection',
})
);

// TODO: It would be great to remove this someday
router.get(
{
path: '/api/app_search/credentials/details',
Expand All @@ -34,6 +69,24 @@ export function registerCredentialsRoutes({
path: '/as/credentials/details',
})
);

// Single credential API
router.put(
{
path: '/api/app_search/credentials/{name}',
validate: {
params: schema.object({
name: schema.string(),
}),
body: tokenSchema,
},
},
async (context, request, response) => {
return enterpriseSearchRequestHandler.createRequest({
path: `/as/credentials/${request.params.name}`,
})(context, request, response);
}
);
router.delete(
{
path: '/api/app_search/credentials/{name}',
Expand Down