kibana/x-pack/plugins/spaces/server/plugin.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

121 lines
4.5 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 { CoreSetup } from 'src/core/server';
import { coreMock } from 'src/core/server/mocks';
import { featuresPluginMock } from '../../features/server/mocks';
import { licensingMock } from '../../licensing/server/mocks';
import { Plugin, PluginsStart } from './plugin';
import { usageCollectionPluginMock } from '../../../../src/plugins/usage_collection/server/mocks';
describe('Spaces Plugin', () => {
describe('#setup', () => {
it('can setup with all optional plugins disabled, exposing the expected contract', () => {
const initializerContext = coreMock.createPluginInitializerContext({});
const core = coreMock.createSetup() as CoreSetup<PluginsStart>;
const features = featuresPluginMock.createSetup();
const licensing = licensingMock.createSetup();
const plugin = new Plugin(initializerContext);
const spacesSetup = plugin.setup(core, { features, licensing });
expect(spacesSetup).toMatchInlineSnapshot(`
Object {
"spacesClient": Object {
"registerClientWrapper": [Function],
"setClientRepositoryFactory": [Function],
},
"spacesService": Object {
"getSpaceId": [Function],
"namespaceToSpaceId": [Function],
"spaceIdToNamespace": [Function],
},
}
`);
});
it('registers the capabilities provider and switcher', () => {
const initializerContext = coreMock.createPluginInitializerContext({});
const core = coreMock.createSetup() as CoreSetup<PluginsStart>;
const features = featuresPluginMock.createSetup();
const licensing = licensingMock.createSetup();
const plugin = new Plugin(initializerContext);
plugin.setup(core, { features, licensing });
expect(core.capabilities.registerProvider).toHaveBeenCalledTimes(1);
expect(core.capabilities.registerSwitcher).toHaveBeenCalledTimes(1);
});
it('registers the usage collector', () => {
const initializerContext = coreMock.createPluginInitializerContext({});
const core = coreMock.createSetup() as CoreSetup<PluginsStart>;
const features = featuresPluginMock.createSetup();
const licensing = licensingMock.createSetup();
const usageCollection = usageCollectionPluginMock.createSetupContract();
const plugin = new Plugin(initializerContext);
plugin.setup(core, { features, licensing, usageCollection });
expect(usageCollection.getCollectorByType('spaces')).toBeDefined();
});
it('registers the "space" saved object type and client wrapper', () => {
const initializerContext = coreMock.createPluginInitializerContext({});
const core = coreMock.createSetup() as CoreSetup<PluginsStart>;
const features = featuresPluginMock.createSetup();
const licensing = licensingMock.createSetup();
const plugin = new Plugin(initializerContext);
plugin.setup(core, { features, licensing });
expect(core.savedObjects.registerType).toHaveBeenCalledWith({
name: 'space',
namespaceType: 'agnostic',
hidden: true,
mappings: expect.any(Object),
migrations: expect.any(Object),
});
expect(core.savedObjects.addClientWrapper).toHaveBeenCalledWith(
Number.MIN_SAFE_INTEGER,
'spaces',
expect.any(Function)
);
});
});
describe('#start', () => {
it('can start with all optional plugins disabled, exposing the expected contract', () => {
const initializerContext = coreMock.createPluginInitializerContext({});
const coreSetup = coreMock.createSetup() as CoreSetup<PluginsStart>;
const features = featuresPluginMock.createSetup();
const licensing = licensingMock.createSetup();
const plugin = new Plugin(initializerContext);
plugin.setup(coreSetup, { features, licensing });
const coreStart = coreMock.createStart();
const spacesStart = plugin.start(coreStart);
expect(spacesStart).toMatchInlineSnapshot(`
Object {
"spacesService": Object {
"createSpacesClient": [Function],
"getActiveSpace": [Function],
"getSpaceId": [Function],
"isInDefaultSpace": [Function],
"namespaceToSpaceId": [Function],
"spaceIdToNamespace": [Function],
},
}
`);
});
});
});