-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3121e47
commit 9d22cbd
Showing
4 changed files
with
313 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
x-pack/plugins/enterprise_search/server/routes/app_search/search_settings.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { MockRouter, mockRequestHandler, mockDependencies } from '../../__mocks__'; | ||
|
||
import { registerSearchSettingsRoutes } from './search_settings'; | ||
|
||
describe('search settings routes', () => { | ||
const boosts = { foo: [{}] }; | ||
const resultFields = { foo: {} }; | ||
const searchFields = { foo: {} }; | ||
const searchSettings = { | ||
boosts, | ||
result_fields: resultFields, | ||
search_fields: searchFields, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('GET /api/app_search/engines/{name}/search_settings/details', () => { | ||
let mockRouter: MockRouter; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockRouter = new MockRouter({ | ||
method: 'get', | ||
path: '/api/app_search/engines/{engineName}/search_settings/details', | ||
}); | ||
|
||
registerSearchSettingsRoutes({ | ||
...mockDependencies, | ||
router: mockRouter.router, | ||
}); | ||
}); | ||
|
||
it('creates a request to enterprise search', () => { | ||
mockRouter.callRoute({ | ||
params: { engineName: 'some-engine' }, | ||
}); | ||
|
||
expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ | ||
path: '/as/engines/some-engine/search_settings/details', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('PUT /api/app_search/engines/{name}/search_settings', () => { | ||
let mockRouter: MockRouter; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockRouter = new MockRouter({ | ||
method: 'put', | ||
path: '/api/app_search/engines/{engineName}/search_settings', | ||
payload: 'body', | ||
}); | ||
|
||
registerSearchSettingsRoutes({ | ||
...mockDependencies, | ||
router: mockRouter.router, | ||
}); | ||
}); | ||
|
||
it('creates a request to enterprise search', () => { | ||
mockRouter.callRoute({ | ||
params: { engineName: 'some-engine' }, | ||
body: searchSettings, | ||
}); | ||
|
||
expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ | ||
path: '/as/engines/some-engine/search_settings', | ||
}); | ||
}); | ||
|
||
describe('validates', () => { | ||
it('correctly', () => { | ||
const request = { body: searchSettings }; | ||
mockRouter.shouldValidate(request); | ||
}); | ||
|
||
it('missing required fields', () => { | ||
const request = { body: {} }; | ||
mockRouter.shouldThrow(request); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('POST /api/app_search/engines/{name}/search_settings/reset', () => { | ||
let mockRouter: MockRouter; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockRouter = new MockRouter({ | ||
method: 'post', | ||
path: '/api/app_search/engines/{engineName}/search_settings/reset', | ||
}); | ||
|
||
registerSearchSettingsRoutes({ | ||
...mockDependencies, | ||
router: mockRouter.router, | ||
}); | ||
}); | ||
|
||
it('creates a request to enterprise search', () => { | ||
mockRouter.callRoute({ | ||
params: { engineName: 'some-engine' }, | ||
}); | ||
|
||
expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ | ||
path: '/as/engines/some-engine/search_settings/reset', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('POST /api/app_search/engines/{name}/search_settings_search', () => { | ||
let mockRouter: MockRouter; | ||
|
||
it('creates a request to enterprise search', () => { | ||
mockRouter = new MockRouter({ | ||
method: 'post', | ||
path: '/api/app_search/engines/{engineName}/search_settings_search', | ||
payload: 'body', | ||
}); | ||
|
||
registerSearchSettingsRoutes({ | ||
...mockDependencies, | ||
router: mockRouter.router, | ||
}); | ||
|
||
mockRouter.callRoute({ | ||
params: { engineName: 'some-engine' }, | ||
body: searchSettings, | ||
}); | ||
|
||
expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ | ||
path: '/as/engines/some-engine/search_settings_search', | ||
}); | ||
}); | ||
|
||
describe('validates body', () => { | ||
beforeEach(() => { | ||
mockRouter = new MockRouter({ | ||
method: 'post', | ||
path: '/api/app_search/engines/{engineName}/search_settings_search', | ||
payload: 'body', | ||
}); | ||
|
||
registerSearchSettingsRoutes({ | ||
...mockDependencies, | ||
router: mockRouter.router, | ||
}); | ||
}); | ||
|
||
it('correctly', () => { | ||
const request = { | ||
body: { | ||
boosts, | ||
search_fields: searchFields, | ||
}, | ||
}; | ||
mockRouter.shouldValidate(request); | ||
}); | ||
|
||
it('missing required fields', () => { | ||
const request = { body: {} }; | ||
mockRouter.shouldThrow(request); | ||
}); | ||
}); | ||
|
||
describe('validates query', () => { | ||
it('correctly', () => { | ||
mockRouter = new MockRouter({ | ||
method: 'post', | ||
path: '/api/app_search/engines/{engineName}/search_settings_search', | ||
payload: 'query', | ||
}); | ||
|
||
registerSearchSettingsRoutes({ | ||
...mockDependencies, | ||
router: mockRouter.router, | ||
}); | ||
|
||
const request = { | ||
query: { | ||
query: 'foo', | ||
}, | ||
}; | ||
mockRouter.shouldValidate(request); | ||
}); | ||
|
||
it('missing required fields', () => { | ||
const request = { query: {} }; | ||
mockRouter.shouldThrow(request); | ||
}); | ||
}); | ||
}); | ||
}); |
101 changes: 101 additions & 0 deletions
101
x-pack/plugins/enterprise_search/server/routes/app_search/search_settings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
|
||
import { RouteDependencies } from '../../plugin'; | ||
|
||
// We only do a very light type check here, and allow unknowns, because the request is validated | ||
// on the ent-search server, so it would be redundant to check it here as well. | ||
const boosts = schema.recordOf( | ||
schema.string(), | ||
schema.arrayOf(schema.object({}, { unknowns: 'allow' })) | ||
); | ||
const resultFields = schema.recordOf(schema.string(), schema.object({}, { unknowns: 'allow' })); | ||
const searchFields = schema.recordOf(schema.string(), schema.object({}, { unknowns: 'allow' })); | ||
|
||
const searchSettingsSchema = schema.object({ | ||
boosts, | ||
result_fields: resultFields, | ||
search_fields: searchFields, | ||
}); | ||
|
||
export function registerSearchSettingsRoutes({ | ||
router, | ||
enterpriseSearchRequestHandler, | ||
}: RouteDependencies) { | ||
router.get( | ||
{ | ||
path: '/api/app_search/engines/{engineName}/search_settings/details', | ||
validate: { | ||
params: schema.object({ | ||
engineName: schema.string(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response) => { | ||
return enterpriseSearchRequestHandler.createRequest({ | ||
path: `/as/engines/${request.params.engineName}/search_settings/details`, | ||
})(context, request, response); | ||
} | ||
); | ||
|
||
router.post( | ||
{ | ||
path: '/api/app_search/engines/{engineName}/search_settings/reset', | ||
validate: { | ||
params: schema.object({ | ||
engineName: schema.string(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response) => { | ||
return enterpriseSearchRequestHandler.createRequest({ | ||
path: `/as/engines/${request.params.engineName}/search_settings/reset`, | ||
})(context, request, response); | ||
} | ||
); | ||
|
||
router.put( | ||
{ | ||
path: '/api/app_search/engines/{engineName}/search_settings', | ||
validate: { | ||
params: schema.object({ | ||
engineName: schema.string(), | ||
}), | ||
body: searchSettingsSchema, | ||
}, | ||
}, | ||
async (context, request, response) => { | ||
return enterpriseSearchRequestHandler.createRequest({ | ||
path: `/as/engines/${request.params.engineName}/search_settings`, | ||
})(context, request, response); | ||
} | ||
); | ||
|
||
router.post( | ||
{ | ||
path: '/api/app_search/engines/{engineName}/search_settings_search', | ||
validate: { | ||
params: schema.object({ | ||
engineName: schema.string(), | ||
}), | ||
body: schema.object({ | ||
boosts, | ||
search_fields: searchFields, | ||
}), | ||
query: schema.object({ | ||
query: schema.string(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response) => { | ||
return enterpriseSearchRequestHandler.createRequest({ | ||
path: `/as/engines/${request.params.engineName}/search_settings_search`, | ||
})(context, request, response); | ||
} | ||
); | ||
} |