Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClusterClient, KibanaRequest, SessionFactory and Lifecycle toolkit mocks #40352

Merged
merged 5 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/core/server/elasticsearch/elasticsearch_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,30 @@

import { BehaviorSubject } from 'rxjs';
import { ClusterClient } from './cluster_client';
import { ScopedClusterClient } from './scoped_cluster_client';
import { ElasticsearchConfig } from './elasticsearch_config';
import { ElasticsearchService, ElasticsearchServiceSetup } from './elasticsearch_service';

const createScopedClusterClientMock = (): jest.Mocked<PublicMethodsOf<ScopedClusterClient>> => ({
callAsInternalUser: jest.fn(),
callAsCurrentUser: jest.fn(),
});

const createClusterClientMock = (): jest.Mocked<PublicMethodsOf<ClusterClient>> => ({
callAsInternalUser: jest.fn(),
asScoped: jest.fn().mockImplementation(createScopedClusterClientMock),
close: jest.fn(),
});

const createSetupContractMock = () => {
const setupContract: ElasticsearchServiceSetup = {
legacy: {
config$: new BehaviorSubject({} as ElasticsearchConfig),
},

createClient: jest.fn(),
adminClient$: new BehaviorSubject({} as ClusterClient),
dataClient$: new BehaviorSubject({} as ClusterClient),
createClient: jest.fn().mockImplementation(createClusterClientMock),
adminClient$: new BehaviorSubject((createClusterClientMock() as unknown) as ClusterClient),
dataClient$: new BehaviorSubject((createClusterClientMock() as unknown) as ClusterClient),
};
return setupContract;
};
Expand All @@ -50,4 +62,6 @@ const createMock = () => {
export const elasticsearchServiceMock = {
create: createMock,
createSetupContract: createSetupContractMock,
createClusterClient: createClusterClientMock,
createScopedClusterClient: createScopedClusterClientMock,
};
46 changes: 46 additions & 0 deletions src/core/server/http/cookie_session_storage.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { SessionStorageFactory, SessionStorage } from './session_storage';

const creatSessionStorageMock = <T>(): jest.Mocked<SessionStorage<T>> => ({
mshustov marked this conversation as resolved.
Show resolved Hide resolved
get: jest.fn().mockResolvedValue({}),
set: jest.fn(),
clear: jest.fn(),
});

type ReturnMocked<T> = {
[K in keyof T]: T[K] extends (...args: any[]) => infer U
? (...args: any[]) => jest.Mocked<U>
: T[K];
};

type DeepMocked<T> = jest.Mocked<ReturnMocked<T>>;

const creatSessionStorageFactoryMock = <T>() => {
mshustov marked this conversation as resolved.
Show resolved Hide resolved
const mocked: DeepMocked<SessionStorageFactory<T>> = {
asScoped: jest.fn(),
};
mocked.asScoped.mockImplementation(creatSessionStorageMock);
return mocked;
};

export const sessionStorageMock = {
create: creatSessionStorageMock,
createFactory: creatSessionStorageFactoryMock,
};
51 changes: 49 additions & 2 deletions src/core/server/http/http_server.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,55 @@
*/
import { Request, ResponseToolkit } from 'hapi';
import { merge } from 'lodash';
import url from 'url';
import querystring from 'querystring';

import { KibanaRequest } from './router';
import { schema } from '@kbn/config-schema';

import { KibanaRequest, RouteMethod } from './router';

interface RequestFixtureOptions {
headers?: Record<string, string>;
params?: Record<string, unknown>;
body?: Record<string, unknown>;
query?: Record<string, unknown>;
path?: string;
method?: RouteMethod;
}

function createKibanaRequestMock({
path = '/path',
headers = { accept: 'something/html' },
params = {},
body = {},
query,
Copy link
Contributor Author

@mshustov mshustov Jul 8, 2019

Choose a reason for hiding this comment

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

query, params and body are passed already parsed in KibanaRequest and not to overcomplicate setup I added them as plain objects

mshustov marked this conversation as resolved.
Show resolved Hide resolved
method = 'get',
}: RequestFixtureOptions = {}) {
return KibanaRequest.from(
{
headers,
params,
query: query || {},
payload: body,
path,
method,
url: {
path,
query: query ? querystring.stringify(query) : query,
search: query ? `?${querystring.stringify(query)}` : query,
},
route: { settings: {} },
raw: {
req: {},
},
} as any,
{
params: schema.object({}, { allowUnknowns: true }),
body: schema.object({}, { allowUnknowns: true }),
query: schema.object({}, { allowUnknowns: true }),
}
);
}

type DeepPartial<T> = T extends any[]
? DeepPartialArray<T[number]>
Expand Down Expand Up @@ -54,7 +101,7 @@ function createRawResponseToolkitMock(customization: DeepPartial<ResponseToolkit
}

export const httpServerMock = {
createKibanaRequest: () => KibanaRequest.from(createRawRequestMock()),
createKibanaRequest: createKibanaRequestMock,
createRawRequest: createRawRequestMock,
createRawResponseToolkit: createRawResponseToolkitMock,
};
54 changes: 38 additions & 16 deletions src/core/server/http/http_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,22 @@ import { Server } from 'hapi';
import { HttpService } from './http_service';
import { HttpServerSetup } from './http_server';
import { HttpServiceSetup } from './http_service';
import { OnPreAuthToolkit } from './lifecycle/on_pre_auth';
import { AuthToolkit } from './lifecycle/auth';
import { OnPostAuthToolkit } from './lifecycle/on_post_auth';
import { sessionStorageMock } from './cookie_session_storage.mocks';

type ServiceSetupMockType = jest.Mocked<HttpServiceSetup> & {
basePath: jest.Mocked<HttpServiceSetup['basePath']>;
};

const createBasePathMock = (): jest.Mocked<HttpServiceSetup['basePath']> => ({
get: jest.fn(),
set: jest.fn(),
prepend: jest.fn(),
remove: jest.fn(),
});

const createSetupContractMock = () => {
const setupContract: ServiceSetupMockType = {
// we can mock some hapi server method when we need it
Expand All @@ -33,12 +45,7 @@ const createSetupContractMock = () => {
registerAuth: jest.fn(),
registerOnPostAuth: jest.fn(),
registerRouter: jest.fn(),
basePath: {
get: jest.fn(),
set: jest.fn(),
prepend: jest.fn(),
remove: jest.fn(),
},
basePath: createBasePathMock(),
auth: {
get: jest.fn(),
isAuthenticated: jest.fn(),
Expand All @@ -47,17 +54,12 @@ const createSetupContractMock = () => {
createNewServer: jest.fn(),
};
setupContract.createNewServer.mockResolvedValue({} as HttpServerSetup);
setupContract.registerAuth.mockResolvedValue({
sessionStorageFactory: sessionStorageMock.createFactory(),
});
return setupContract;
};

const createStartContractMock = () => {
const startContract = {
isListening: jest.fn(),
};
startContract.isListening.mockReturnValue(true);
return startContract;
};

type HttpServiceContract = PublicMethodsOf<HttpService>;
const createHttpServiceMock = () => {
const mocked: jest.Mocked<HttpServiceContract> = {
Expand All @@ -66,12 +68,32 @@ const createHttpServiceMock = () => {
stop: jest.fn(),
};
mocked.setup.mockResolvedValue(createSetupContractMock());
mocked.start.mockResolvedValue(createStartContractMock());
return mocked;
};

const createOnPreAuthToolkitMock = (): jest.Mocked<OnPreAuthToolkit> => ({
next: jest.fn(),
redirected: jest.fn(),
rejected: jest.fn(),
});

const createAuthToolkitMock = (): jest.Mocked<AuthToolkit> => ({
authenticated: jest.fn(),
redirected: jest.fn(),
rejected: jest.fn(),
});

const createOnPostAuthToolkitMock = (): jest.Mocked<OnPostAuthToolkit> => ({
next: jest.fn(),
redirected: jest.fn(),
rejected: jest.fn(),
});

export const httpServiceMock = {
create: createHttpServiceMock,
createBasePath: createBasePathMock,
createSetupContract: createSetupContractMock,
createStartContract: createStartContractMock,
createOnPreAuthToolkit: createOnPreAuthToolkitMock,
createAuthToolkit: createAuthToolkitMock,
createOnPostAuthToolkit: createOnPostAuthToolkitMock,
};
18 changes: 10 additions & 8 deletions src/core/server/logging/logging_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// Test helpers to simplify mocking logs and collecting all their outputs
import { Logger } from './logger';
import { LoggingService } from './logging_service';
import { LoggerFactory } from './logger_factory';

type LoggingServiceContract = PublicMethodsOf<LoggingService>;
type MockedLogger = jest.Mocked<Logger>;
Expand Down Expand Up @@ -50,8 +51,8 @@ const createLoggingServiceMock = () => {
return mocked;
};

const collectLoggingServiceMock = (mocked: ReturnType<typeof createLoggingServiceMock>) => {
const mockLog = mocked.get() as MockedLogger;
const collectLoggingServiceMock = (loggerFactory: LoggerFactory) => {
const mockLog = loggerFactory.get() as MockedLogger;
return {
debug: mockLog.debug.mock.calls,
error: mockLog.error.mock.calls,
Expand All @@ -63,13 +64,14 @@ const collectLoggingServiceMock = (mocked: ReturnType<typeof createLoggingServic
};
};

const clearLoggingServiceMock = (mocked: ReturnType<typeof createLoggingServiceMock>) => {
const mockLog = mocked.get() as MockedLogger;
mocked.get.mockClear();
mocked.asLoggerFactory.mockClear();
mocked.upgrade.mockClear();
mocked.stop.mockClear();
const clearLoggingServiceMock = (loggerFactory: LoggerFactory) => {
const mockedLoggerFactory = (loggerFactory as unknown) as jest.Mocked<LoggingServiceContract>;
mockedLoggerFactory.get.mockClear();
mockedLoggerFactory.asLoggerFactory.mockClear();
mockedLoggerFactory.upgrade.mockClear();
mockedLoggerFactory.stop.mockClear();

const mockLog = loggerFactory.get() as MockedLogger;
mockLog.debug.mockClear();
mockLog.info.mockClear();
mockLog.warn.mockClear();
Expand Down
4 changes: 3 additions & 1 deletion src/core/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { loggingServiceMock } from './logging/logging_service.mock';
import { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.mock';
import { httpServiceMock } from './http/http_service.mock';

export { httpServerMock } from './http/http_server.mocks';
export { sessionStorageMock } from './http/cookie_session_storage.mocks';
export { configServiceMock } from './config/config_service.mock';
export { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.mock';
export { httpServiceMock } from './http/http_service.mock';
Expand All @@ -38,7 +40,7 @@ export function pluginInitializerContextConfigMock<T>(config: T) {
}

function pluginInitializerContextMock<T>(config: T) {
const mock: jest.Mocked<PluginInitializerContext<T>> = {
const mock: PluginInitializerContext<T> = {
logger: loggingServiceMock.create(),
env: {
mode: {
Expand Down