forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fleet_start_services.tsx
97 lines (84 loc) · 2.92 KB
/
fleet_start_services.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import { I18nProvider } from '@kbn/i18n-react';
import type { MockedKeys } from '@kbn/utility-types/jest';
import { coreMock } from '../../../../../src/core/public/mocks';
import type { IStorage } from '../../../../../src/plugins/kibana_utils/public';
import { Storage } from '../../../../../src/plugins/kibana_utils/public';
import { setHttpClient } from '../hooks/use_request';
import type { FleetAuthz } from '../../common';
import { createStartDepsMock, createSetupDepsMock } from './plugin_dependencies';
import type { MockedFleetStartServices } from './types';
// Taken from core. See: src/plugins/kibana_utils/public/storage/storage.test.ts
const createMockStore = (): MockedKeys<IStorage> => {
let store: Record<string, any> = {};
return {
getItem: jest.fn().mockImplementation((key) => store[key]),
setItem: jest.fn().mockImplementation((key, value) => (store[key] = value)),
removeItem: jest.fn().mockImplementation((key: string) => delete store[key]),
clear: jest.fn().mockImplementation(() => (store = {})),
};
};
const fleetAuthzMock: FleetAuthz = {
fleet: {
all: true,
setup: true,
readEnrollmentTokens: true,
readAgentPolicies: true,
},
integrations: {
readPackageInfo: true,
readInstalledPackages: true,
installPackages: true,
upgradePackages: true,
uploadPackages: true,
removePackages: true,
readPackageSettings: true,
writePackageSettings: true,
readIntegrationPolicies: true,
writeIntegrationPolicies: true,
},
};
const configureStartServices = (services: MockedFleetStartServices): void => {
// Store the http for use by useRequest
setHttpClient(services.http);
// Set Fleet and Integrations capabilities
services.application.capabilities = {
...services.application.capabilities,
// Fleet
fleetv2: {
read: true,
all: true,
},
// Integration
fleet: {
read: true,
all: true,
},
};
// Setup the `i18n.Context` component
services.i18n.Context.mockImplementation(({ children }: { children: React.ReactNode }) => (
<I18nProvider>{children}</I18nProvider>
));
};
export const createStartServices = (basePath: string = '/mock'): MockedFleetStartServices => {
const { cloud: cloudStart, ...startDeps } = createStartDepsMock();
const { cloud: cloudSetup } = createSetupDepsMock();
const startServices: MockedFleetStartServices = {
...coreMock.createStart({ basePath }),
...startDeps,
cloud: {
...cloudStart,
...cloudSetup,
},
storage: new Storage(createMockStore()) as jest.Mocked<Storage>,
authz: fleetAuthzMock,
};
configureStartServices(startServices);
return startServices;
};