kibana/x-pack/plugins/security/server/spaces/setup_spaces_client.test.ts
Larry Gregory 064fd8ef84
[7.x] Removing circular dependency between spaces and security (#81891) (#83841)
* Removing circular dependency between spaces and security

* Apply suggestions from code review

Co-authored-by: Constance <constancecchen@users.noreply.github.com>
Co-authored-by: Aleh Zasypkin <aleh.zasypkin@gmail.com>

* Tests refactor

- Reorganize top level describes into 3 space-based blocks into based on spaces:
  - space disabled
  - spaces plugin unavailable
  - space enabled (most previous tests go under this new block) with new beforeEach

- wrote new tests for uncovered lines 58, 66-69

* Review1: address PR feedback

* changing fake requests for alerts/actions

* Fixing tests

* fixing more tests

* Additional testing and refactoring

* Apply suggestions from code review

Co-authored-by: Aleh Zasypkin <aleh.zasypkin@gmail.com>

* Review 2: Address feedback

* Make ESLint happy again

Co-authored-by: Constance <constancecchen@users.noreply.github.com>
Co-authored-by: Aleh Zasypkin <aleh.zasypkin@gmail.com>
Co-authored-by: Constance Chen <constance.chen.3@gmail.com>

Co-authored-by: Constance <constancecchen@users.noreply.github.com>
Co-authored-by: Aleh Zasypkin <aleh.zasypkin@gmail.com>
Co-authored-by: Constance Chen <constance.chen.3@gmail.com>
2020-11-19 15:56:05 -05:00

80 lines
3.2 KiB
TypeScript

/*
* 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 { coreMock, httpServerMock } from '../../../../../src/core/server/mocks';
import { spacesMock } from '../../../spaces/server/mocks';
import { auditServiceMock } from '../audit/index.mock';
import { authorizationMock } from '../authorization/index.mock';
import { setupSpacesClient } from './setup_spaces_client';
describe('setupSpacesClient', () => {
it('does not setup the spaces client when spaces is disabled', () => {
const authz = authorizationMock.create();
const audit = auditServiceMock.create();
setupSpacesClient({ authz, audit });
expect(audit.getLogger).not.toHaveBeenCalled();
});
it('configures the repository factory, wrapper, and audit logger', () => {
const authz = authorizationMock.create();
const audit = auditServiceMock.create();
const spaces = spacesMock.createSetup();
setupSpacesClient({ authz, audit, spaces });
expect(spaces.spacesClient.registerClientWrapper).toHaveBeenCalledTimes(1);
expect(spaces.spacesClient.setClientRepositoryFactory).toHaveBeenCalledTimes(1);
expect(audit.getLogger).toHaveBeenCalledTimes(1);
});
it('creates a factory that creates an internal repository when RBAC is used for the request', () => {
const authz = authorizationMock.create();
const audit = auditServiceMock.create();
const spaces = spacesMock.createSetup();
const { savedObjects } = coreMock.createStart();
setupSpacesClient({ authz, audit, spaces });
expect(spaces.spacesClient.setClientRepositoryFactory).toHaveBeenCalledTimes(1);
const [repositoryFactory] = spaces.spacesClient.setClientRepositoryFactory.mock.calls[0];
const request = httpServerMock.createKibanaRequest();
authz.mode.useRbacForRequest.mockReturnValueOnce(true);
repositoryFactory(request, savedObjects);
expect(savedObjects.createInternalRepository).toHaveBeenCalledTimes(1);
expect(savedObjects.createInternalRepository).toHaveBeenCalledWith(['space']);
expect(savedObjects.createScopedRepository).not.toHaveBeenCalled();
});
it('creates a factory that creates a scoped repository when RBAC is NOT used for the request', () => {
const authz = authorizationMock.create();
const audit = auditServiceMock.create();
const spaces = spacesMock.createSetup();
const { savedObjects } = coreMock.createStart();
setupSpacesClient({ authz, audit, spaces });
expect(spaces.spacesClient.setClientRepositoryFactory).toHaveBeenCalledTimes(1);
const [repositoryFactory] = spaces.spacesClient.setClientRepositoryFactory.mock.calls[0];
const request = httpServerMock.createKibanaRequest();
authz.mode.useRbacForRequest.mockReturnValueOnce(false);
repositoryFactory(request, savedObjects);
expect(savedObjects.createInternalRepository).not.toHaveBeenCalled();
expect(savedObjects.createScopedRepository).toHaveBeenCalledTimes(1);
expect(savedObjects.createScopedRepository).toHaveBeenCalledWith(request, ['space']);
});
});