-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
get.test.ts
109 lines (87 loc) · 3.87 KB
/
get.test.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
/*
* 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 * as Rx from 'rxjs';
import * as kbnTestServer from '../../../../../../../src/test_utils/kbn_server';
import { createSpaces, createLegacyAPI, createMockSavedObjectsRepository } from '../__fixtures__';
import { initGetSpacesApi } from './get';
import { CoreSetup } from 'src/core/server';
import { loggingServiceMock, elasticsearchServiceMock } from 'src/core/server/mocks';
import { SpacesService } from '../../../spaces_service';
import { createOptionalPlugin } from '../../../../../../legacy/server/lib/optional_plugin';
import { SpacesAuditLogger } from '../../../lib/audit_logger';
import { SpacesClient } from '../../../lib/spaces_client';
jest.setTimeout(30000);
describe('GET spaces', () => {
const spacesSavedObjects = createSpaces();
const spaces = spacesSavedObjects.map(s => ({ id: s.id, ...s.attributes }));
let root: ReturnType<typeof kbnTestServer.createRoot>;
beforeAll(async () => {
root = kbnTestServer.createRoot();
const { http } = await root.setup();
const router = http.createRouter('/');
const log = loggingServiceMock.create().get('spaces');
const legacyAPI = createLegacyAPI({ spaces });
const savedObjectsRepositoryMock = createMockSavedObjectsRepository(spacesSavedObjects);
const service = new SpacesService(log, () => legacyAPI);
const spacesService = await service.setup({
http: (http as unknown) as CoreSetup['http'],
elasticsearch: elasticsearchServiceMock.createSetupContract(),
getSecurity: () =>
createOptionalPlugin({ get: () => null }, 'xpack.security', {}, 'security'),
getSpacesAuditLogger: () => ({} as SpacesAuditLogger),
config$: Rx.of({ maxSpaces: 1000 }),
});
spacesService.scopedClient = jest.fn((req: any) => {
return Promise.resolve(
new SpacesClient(
null as any,
() => null,
null,
savedObjectsRepositoryMock,
{ maxSpaces: 1000 },
savedObjectsRepositoryMock,
req
)
);
});
initGetSpacesApi({
externalRouter: router,
getSavedObjects: () => legacyAPI.savedObjects,
log: loggingServiceMock.create().get('spaces'),
spacesService,
});
await root.start();
});
afterAll(async () => await root.shutdown());
test(`'GET spaces' returns all available spaces`, async () => {
const response = await kbnTestServer.request.get(root, '/api/spaces/space');
expect(response.status).toEqual(200);
expect(response.body).toEqual(spaces);
});
test(`'GET spaces' returns all available spaces with the 'any' purpose`, async () => {
const response = await kbnTestServer.request.get(root, '/api/spaces/space?purpose=any');
expect(response.status).toEqual(200);
expect(response.body).toEqual(spaces);
});
test(`'GET spaces' returns all available spaces with the 'copySavedObjectsIntoSpace' purpose`, async () => {
const response = await kbnTestServer.request.get(
root,
'/api/spaces/space?purpose=copySavedObjectsIntoSpace'
);
expect(response.status).toEqual(200);
expect(response.body).toEqual(spaces);
});
test.todo(`returns result of routePreCheckLicense`);
test(`'GET spaces/{id}' returns the space with that id`, async () => {
const response = await kbnTestServer.request.get(root, '/api/spaces/space/default');
expect(response.status).toEqual(200);
expect(response.body).toEqual(spaces.find(s => s.id === 'default'));
});
test(`'GET spaces/{id}' returns 404 when retrieving a non-existent space`, async () => {
const response = await kbnTestServer.request.get(root, '/api/spaces/space/not-a-space');
expect(response.status).toEqual(404);
});
});