-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Loosen apiClient ESLint rule on Security
#252212
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
Changes from 3 commits
269e67d
49c3f13
329f8b9
9d2c805
6d6f29b
a5ddb0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,10 @@ const { RuleTester } = require('eslint'); | |
| const rule = require('./scout_require_api_client_in_api_test'); | ||
| const dedent = require('dedent'); | ||
|
|
||
| const ERROR_MSG = | ||
| const DEFAULT_ERROR_MSG = | ||
| 'The `apiClient` fixture should be used in `apiTest` to call an endpoint and later verify response code and body.'; | ||
| const ALT_ERROR_MSG = | ||
| 'One of `apiClient` or `esArchiver` fixtures should be used in `apiTest` to interact with an endpoint and later verify the response.'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const ruleTester = new RuleTester({ | ||
| parser: require.resolve('@typescript-eslint/parser'), | ||
| parserOptions: { | ||
|
|
@@ -161,7 +163,7 @@ ruleTester.run('@kbn/eslint/scout_require_api_client_in_api_test', rule, { | |
| console.log(other); | ||
| }); | ||
| `, | ||
| errors: [{ message: ERROR_MSG }], | ||
| errors: [{ message: DEFAULT_ERROR_MSG }], | ||
| }, | ||
| // Nested apiTest inside apiTest.describe missing apiClient | ||
| { | ||
|
|
@@ -170,7 +172,7 @@ ruleTester.run('@kbn/eslint/scout_require_api_client_in_api_test', rule, { | |
| apiTest('inner missing', () => {}); | ||
| }); | ||
| `, | ||
| errors: [{ message: ERROR_MSG }], | ||
| errors: [{ message: DEFAULT_ERROR_MSG }], | ||
| }, | ||
| // Nested blocks missing apiClient | ||
| { | ||
|
|
@@ -181,7 +183,7 @@ ruleTester.run('@kbn/eslint/scout_require_api_client_in_api_test', rule, { | |
| } | ||
| }); | ||
| `, | ||
| errors: [{ message: ERROR_MSG }], | ||
| errors: [{ message: DEFAULT_ERROR_MSG }], | ||
| }, | ||
| // Nested helper function missing apiClient | ||
| { | ||
|
|
@@ -193,7 +195,7 @@ ruleTester.run('@kbn/eslint/scout_require_api_client_in_api_test', rule, { | |
| helper(apiClient); | ||
| }); | ||
| `, | ||
| errors: [{ message: ERROR_MSG }], | ||
| errors: [{ message: DEFAULT_ERROR_MSG }], | ||
| }, | ||
| // Arrow function inside test | ||
| { | ||
|
|
@@ -202,7 +204,7 @@ ruleTester.run('@kbn/eslint/scout_require_api_client_in_api_test', rule, { | |
| const callApi = () => apiClient.get('/bar'); | ||
| }); | ||
| `, | ||
| errors: [{ message: ERROR_MSG }], | ||
| errors: [{ message: DEFAULT_ERROR_MSG }], | ||
| }, | ||
| // External helper without apiClient usage | ||
| { | ||
|
|
@@ -214,7 +216,7 @@ ruleTester.run('@kbn/eslint/scout_require_api_client_in_api_test', rule, { | |
| externalHelper(); | ||
| }); | ||
| `, | ||
| errors: [{ message: ERROR_MSG }], | ||
| errors: [{ message: DEFAULT_ERROR_MSG }], | ||
| }, | ||
| // Passing apiClient to a variable but never using it | ||
| { | ||
|
|
@@ -224,7 +226,50 @@ ruleTester.run('@kbn/eslint/scout_require_api_client_in_api_test', rule, { | |
| fn(apiClient); | ||
| }); | ||
| `, | ||
| errors: [{ message: ERROR_MSG }], | ||
| errors: [{ message: DEFAULT_ERROR_MSG }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // --- Tests with alternativeFixtures option --- | ||
| const altOptions = [{ alternativeFixtures: ['esArchiver'] }]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| ruleTester.run( | ||
| '@kbn/eslint/scout_require_api_client_in_api_test (with alternativeFixtures)', | ||
| rule, | ||
| { | ||
| valid: [ | ||
| // apiClient still accepted when alternativeFixtures is configured | ||
| { | ||
| code: dedent` | ||
| apiTest('uses apiClient', async ({ apiClient }) => { | ||
| await apiClient.get('/foo'); | ||
| }); | ||
| `, | ||
| options: altOptions, | ||
| }, | ||
| // Alternative fixture used instead of apiClient | ||
| { | ||
| code: dedent` | ||
| apiTest('uses esArchiver', async ({ esArchiver }) => { | ||
| await esArchiver.load('path/to/archive'); | ||
| }); | ||
|
Comment on lines
+253
to
+256
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| `, | ||
| options: altOptions, | ||
| }, | ||
| ], | ||
|
|
||
| invalid: [ | ||
| // Neither apiClient nor alternative fixture used | ||
| { | ||
| code: dedent` | ||
| apiTest('uses neither', async () => { | ||
| console.log('no fixture'); | ||
| }); | ||
| `, | ||
| options: altOptions, | ||
| errors: [{ message: ALT_ERROR_MSG }], | ||
| }, | ||
| ], | ||
| } | ||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I say "Security Solution" but
x-pack/platform/**/plugins/**/security/**/test/{scout,scout_*}/**/*.tsis not security solution - it's just a platform plugin, correct?If yes, do you have any better name suggestions for this section?
cc @dmlemeshko
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's allow it globally, it will be annoying to update
.eslintrc.jsevery time a new team decided to use onlyesClientfor api tests.As Sophie pointed out, it is totally fine to use
apiClientoresClientfor API tests when Teams think this what they want to do.