Skip to content

[Security] tests for API keys app#48560

Merged
alisonelizabeth merged 8 commits intoelastic:masterfrom
alisonelizabeth:api_keys_tests
Nov 20, 2019
Merged

[Security] tests for API keys app#48560
alisonelizabeth merged 8 commits intoelastic:masterfrom
alisonelizabeth:api_keys_tests

Conversation

@alisonelizabeth
Copy link
Contributor

@alisonelizabeth alisonelizabeth commented Oct 17, 2019

This PR adds tests for the new API keys app added via #45740.

Fixes #47869

@alisonelizabeth alisonelizabeth added chore Feature:Users/Roles/API Keys v8.0.0 Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more t// release_note:skip Skip the PR/issue when compiling release notes labels Oct 17, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/es-ui (Team:Elasticsearch UI)

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💔 Build Failed

@alisonelizabeth alisonelizabeth marked this pull request as ready for review October 21, 2019 19:02
@alisonelizabeth alisonelizabeth requested a review from a team as a code owner October 21, 2019 19:02
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@alisonelizabeth
Copy link
Contributor Author

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@alisonelizabeth
Copy link
Contributor Author

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@alisonelizabeth
Copy link
Contributor Author

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@cuff-links
Copy link
Contributor

Testing in progress.

@cuff-links cuff-links self-assigned this Oct 30, 2019
Copy link
Contributor

@cuff-links cuff-links left a comment

Choose a reason for hiding this comment

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

LGTM. I ran the tests. Has pretty good coverage.

@alisonelizabeth
Copy link
Contributor Author

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

import { initApiKeysApi } from '../api_keys';
import * as ClientShield from '../../../../../../../server/lib/get_client_shield';

describe('API Keys routes', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

We've generally tried to write all unit tests in jest as opposed to mocha and no longer rely on sinon. Can we change these to use jest without sinon?


afterEach(() => sandbox.restore());

describe('privileges', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: These tests are missing the assertions to ensure that callWithRequest is invoked with the proper parameters... it's possible to change

callWithRequest(
  request,
  'shield.hasPrivileges',
  {
    body: {
      cluster: [
        'manage_security',
        'manage_api_key',
      ],
    },
  }
)

to

callWithRequest(
  request,
  'shield.hasPrivileges',
  {
    body: {
      foo: true,
    },
  }
)

and still have these tests pass.


const { areApiKeysEnabled } = await privilegesRoute.handler(request);

expect(areApiKeysEnabled).to.be(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expect(areApiKeysEnabled).to.be(true);
expect(areApiKeysEnabled).to.be(false);

sinon.match.same(request),
'shield.getAPIKeys',
)
.resolves(apiKeysDisabledError);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.resolves(apiKeysDisabledError);
.rejects(apiKeysDisabledError);

expect(areApiKeysEnabled).to.be(true);
});

it('sets isAdmin to true if a user has the manage_security or manage_api_key privilege', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: This isn't really testing whether or not the user has the manage_security cluster privilege or the manage_api_key privilege, it's testing what happens when they have both. We're already testing this with "returns areApiKeysEnabled and isAdmin"

expect(errors).to.have.length(0);
});

it('returns a list of errors encountered when trying to invalidate each key', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we change this test to only throw an error when invalidating the first api key, and ensure the second api key is still invalidated?

});
});

it('only returns valid keys', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: This test is also missing the assertions to ensure that callWithRequest is invoked with the proper parameters...

.resolves(MOCK_API_KEYS_WITH_INVALIDATED_RESPONSE);

const { apiKeys } = await getRoute.handler(request);
expect(apiKeys).to.have.length(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expect(apiKeys).to.have.length(1);
expect(apiKeys).to.have.length(1);
expect(apiKeys[0]).to.equal(MOCK_API_KEYS_WITH_INVALIDATED_RESPONSE.api_keys[0]);


const { itemsInvalidated, errors } = await invalidateRoute.handler(request);

expect(itemsInvalidated).to.have.length(MOCK_INVALIDATE_API_KEYS.length);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expect(itemsInvalidated).to.have.length(MOCK_INVALIDATE_API_KEYS.length);
expect(itemsInvalidated).to.have.length(MOCK_INVALIDATE_API_KEYS.length);
expect(itemsInvalidated).to.equal(MOCK_INVALIDATE_API_KEYS);

it('renders a loading state when fetching API keys', async () => {
const wrapper = mountWithIntl(<ApiKeysGridPage />);

expect(wrapper.find(SectionLoading)).toHaveLength(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would using a data-test-subj here let us get away from having to use // eslint-disable-next-line @kbn/eslint/no-restricted-paths above?

@kobelb
Copy link
Contributor

kobelb commented Nov 1, 2019

Thanks so much for adding these @alisonelizabeth!

@alisonelizabeth
Copy link
Contributor Author

@elasticmachine merge upstream

@alisonelizabeth
Copy link
Contributor Author

@kobelb thanks for the review! I finally got around to addressing your feedback. I converted the API tests to jest and removed sinon. Do you think you could take another look?

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@alisonelizabeth
Copy link
Contributor Author

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Feature:Users/Roles/API Keys release_note:skip Skip the PR/issue when compiling release notes Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more t// v7.6.0 v8.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add tests for API Keys app

4 participants