-
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.
[File upload] Migrate routing to NP & add route validation (#52313)
* Cursory validation * Handle empty data arrays and settings conditionally * Set validation defaults. Move logic to routes folder and separate for testing * Move plugin init back into routes folder. Syntax updates * Migrate router to NP * Use new np router and fetch. Add placeholder schema validation * Override default 'maxBytes' * Body with first-level schema keys in place * Add conditional validation of mappings, data and settings. Clean up old joi code * Ensure query is handled correctly on both sides. Iron out decision logic on server-side * Move conditional validation to first step in payload handling * Update http_service to work with latest NP changes on master * Some reorg. Only update telemetry if no errors * Clean up * Test file upload validation logic * Linting * Review feedback. Remove unneeded apiBasePath var * Pass entire req object with through to ES, not just the validated fields
- Loading branch information
Aaron Caldwell
authored
Feb 6, 2020
1 parent
a08116f
commit fabb28d
Showing
7 changed files
with
212 additions
and
66 deletions.
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
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
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
76 changes: 76 additions & 0 deletions
76
x-pack/legacy/plugins/file_upload/server/routes/file_upload.test.js
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,76 @@ | ||
/* | ||
* 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 { querySchema, bodySchema, idConditionalValidation } from './file_upload'; | ||
|
||
const queryWithId = { | ||
id: '123', | ||
}; | ||
|
||
const bodyWithoutQueryId = { | ||
index: 'islandofone', | ||
data: [], | ||
settings: { number_of_shards: 1 }, | ||
mappings: { coordinates: { type: 'geo_point' } }, | ||
ingestPipeline: {}, | ||
fileType: 'json', | ||
app: 'Maps', | ||
}; | ||
|
||
const bodyWithQueryId = { | ||
index: 'islandofone2', | ||
data: [{ coordinates: [], name: 'islandofone2' }], | ||
settings: {}, | ||
mappings: {}, | ||
ingestPipeline: {}, | ||
fileType: 'json', | ||
}; | ||
|
||
describe('route validation', () => { | ||
it(`validates query with id`, async () => { | ||
const validationResult = querySchema.validate(queryWithId); | ||
expect(validationResult.id).toBe(queryWithId.id); | ||
}); | ||
|
||
it(`validates query without id`, async () => { | ||
const validationResult = querySchema.validate({}); | ||
expect(validationResult.id).toBeNull(); | ||
}); | ||
|
||
it(`throws when query contains content other than an id`, async () => { | ||
expect(() => querySchema.validate({ notAnId: 123 })).toThrowError( | ||
`[notAnId]: definition for this key is missing` | ||
); | ||
}); | ||
|
||
it(`validates body with valid fields`, async () => { | ||
const validationResult = bodySchema.validate(bodyWithoutQueryId); | ||
expect(validationResult).toEqual(bodyWithoutQueryId); | ||
}); | ||
|
||
it(`throws if an expected field is missing`, async () => { | ||
/* eslint-disable no-unused-vars */ | ||
const { index, ...bodyWithoutIndexField } = bodyWithoutQueryId; | ||
expect(() => bodySchema.validate(bodyWithoutIndexField)).toThrowError( | ||
`[index]: expected value of type [string] but got [undefined]` | ||
); | ||
}); | ||
|
||
it(`validates conditional fields when id has been provided in query`, async () => { | ||
const validationResult = idConditionalValidation(bodyWithQueryId, true); | ||
expect(validationResult).toEqual(bodyWithQueryId); | ||
}); | ||
|
||
it(`validates conditional fields when no id has been provided in query`, async () => { | ||
const validationResultWhenIdPresent = idConditionalValidation(bodyWithoutQueryId, false); | ||
expect(validationResultWhenIdPresent).toEqual(bodyWithoutQueryId); | ||
// Conditions for no id are more strict since this query sets up the index, | ||
// expect it to throw if expected fields aren't present | ||
expect(() => idConditionalValidation(bodyWithoutQueryId, true)).toThrowError( | ||
`[data]: array size is [0], but cannot be smaller than [1]` | ||
); | ||
}); | ||
}); |