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

HttpService: duplicate some APIs from setup to start and clean mocks. #69021

Merged
merged 10 commits into from
Jun 18, 2020
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [CoreStart](./kibana-plugin-core-server.corestart.md) &gt; [http](./kibana-plugin-core-server.corestart.http.md)

## CoreStart.http property

[HttpServiceStart](./kibana-plugin-core-server.httpservicestart.md)

<b>Signature:</b>

```typescript
http: HttpServiceStart;
```
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface CoreStart
| --- | --- | --- |
| [capabilities](./kibana-plugin-core-server.corestart.capabilities.md) | <code>CapabilitiesStart</code> | [CapabilitiesStart](./kibana-plugin-core-server.capabilitiesstart.md) |
| [elasticsearch](./kibana-plugin-core-server.corestart.elasticsearch.md) | <code>ElasticsearchServiceStart</code> | [ElasticsearchServiceStart](./kibana-plugin-core-server.elasticsearchservicestart.md) |
| [http](./kibana-plugin-core-server.corestart.http.md) | <code>HttpServiceStart</code> | [HttpServiceStart](./kibana-plugin-core-server.httpservicestart.md) |
| [savedObjects](./kibana-plugin-core-server.corestart.savedobjects.md) | <code>SavedObjectsServiceStart</code> | [SavedObjectsServiceStart](./kibana-plugin-core-server.savedobjectsservicestart.md) |
| [uiSettings](./kibana-plugin-core-server.corestart.uisettings.md) | <code>UiSettingsServiceStart</code> | [UiSettingsServiceStart](./kibana-plugin-core-server.uisettingsservicestart.md) |

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [HttpAuth](./kibana-plugin-core-server.httpauth.md) &gt; [get](./kibana-plugin-core-server.httpauth.get.md)

## HttpAuth.get property

Gets authentication state for a request. Returned by `auth` interceptor. [GetAuthState](./kibana-plugin-core-server.getauthstate.md)

<b>Signature:</b>

```typescript
get: GetAuthState;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [HttpAuth](./kibana-plugin-core-server.httpauth.md) &gt; [isAuthenticated](./kibana-plugin-core-server.httpauth.isauthenticated.md)

## HttpAuth.isAuthenticated property

Returns authentication status for a request. [IsAuthenticated](./kibana-plugin-core-server.isauthenticated.md)

<b>Signature:</b>

```typescript
isAuthenticated: IsAuthenticated;
```
20 changes: 20 additions & 0 deletions docs/development/core/server/kibana-plugin-core-server.httpauth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [HttpAuth](./kibana-plugin-core-server.httpauth.md)

## HttpAuth interface


<b>Signature:</b>

```typescript
export interface HttpAuth
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [get](./kibana-plugin-core-server.httpauth.get.md) | <code>GetAuthState</code> | Gets authentication state for a request. Returned by <code>auth</code> interceptor. [GetAuthState](./kibana-plugin-core-server.getauthstate.md) |
| [isAuthenticated](./kibana-plugin-core-server.httpauth.isauthenticated.md) | <code>IsAuthenticated</code> | Returns authentication status for a request. [IsAuthenticated](./kibana-plugin-core-server.isauthenticated.md) |

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

## HttpServiceSetup.auth property

Auth status. See [HttpAuth](./kibana-plugin-core-server.httpauth.md)

<b>Signature:</b>

```typescript
auth: {
get: GetAuthState;
isAuthenticated: IsAuthenticated;
};
auth: HttpAuth;
```
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async (context, request, response) => {

| Property | Type | Description |
| --- | --- | --- |
| [auth](./kibana-plugin-core-server.httpservicesetup.auth.md) | <code>{</code><br/><code> get: GetAuthState;</code><br/><code> isAuthenticated: IsAuthenticated;</code><br/><code> }</code> | |
| [auth](./kibana-plugin-core-server.httpservicesetup.auth.md) | <code>HttpAuth</code> | Auth status. See [HttpAuth](./kibana-plugin-core-server.httpauth.md) |
| [basePath](./kibana-plugin-core-server.httpservicesetup.basepath.md) | <code>IBasePath</code> | Access or manipulate the Kibana base path See [IBasePath](./kibana-plugin-core-server.ibasepath.md)<!-- -->. |
| [createCookieSessionStorageFactory](./kibana-plugin-core-server.httpservicesetup.createcookiesessionstoragefactory.md) | <code>&lt;T&gt;(cookieOptions: SessionStorageCookieOptions&lt;T&gt;) =&gt; Promise&lt;SessionStorageFactory&lt;T&gt;&gt;</code> | Creates cookie based session storage factory [SessionStorageFactory](./kibana-plugin-core-server.sessionstoragefactory.md) |
| [createRouter](./kibana-plugin-core-server.httpservicesetup.createrouter.md) | <code>() =&gt; IRouter</code> | Provides ability to declare a handler function for a particular path and HTTP request method. |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [HttpServiceStart](./kibana-plugin-core-server.httpservicestart.md) &gt; [isListening](./kibana-plugin-core-server.httpservicestart.islistening.md)
[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [HttpServiceStart](./kibana-plugin-core-server.httpservicestart.md) &gt; [auth](./kibana-plugin-core-server.httpservicestart.auth.md)

## HttpServiceStart.isListening property
## HttpServiceStart.auth property

Indicates if http server is listening on a given port
Auth status. See [HttpAuth](./kibana-plugin-core-server.httpauth.md)

<b>Signature:</b>

```typescript
isListening: (port: number) => boolean;
auth: HttpAuth;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [HttpServiceStart](./kibana-plugin-core-server.httpservicestart.md) &gt; [basePath](./kibana-plugin-core-server.httpservicestart.basepath.md)

## HttpServiceStart.basePath property

Access or manipulate the Kibana base path See [IBasePath](./kibana-plugin-core-server.ibasepath.md)<!-- -->.

<b>Signature:</b>

```typescript
basePath: IBasePath;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [HttpServiceStart](./kibana-plugin-core-server.httpservicestart.md) &gt; [getServerInfo](./kibana-plugin-core-server.httpservicestart.getserverinfo.md)

## HttpServiceStart.getServerInfo property

Provides common [information](./kibana-plugin-core-server.httpserverinfo.md) about the running http server.

<b>Signature:</b>

```typescript
getServerInfo: () => HttpServerInfo;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [HttpServiceStart](./kibana-plugin-core-server.httpservicestart.md) &gt; [isTlsEnabled](./kibana-plugin-core-server.httpservicestart.istlsenabled.md)

## HttpServiceStart.isTlsEnabled property

Flag showing whether a server was configured to use TLS connection.

<b>Signature:</b>

```typescript
isTlsEnabled: boolean;
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ export interface HttpServiceStart

| Property | Type | Description |
| --- | --- | --- |
| [isListening](./kibana-plugin-core-server.httpservicestart.islistening.md) | <code>(port: number) =&gt; boolean</code> | Indicates if http server is listening on a given port |
| [auth](./kibana-plugin-core-server.httpservicestart.auth.md) | <code>HttpAuth</code> | Auth status. See [HttpAuth](./kibana-plugin-core-server.httpauth.md) |
| [basePath](./kibana-plugin-core-server.httpservicestart.basepath.md) | <code>IBasePath</code> | Access or manipulate the Kibana base path See [IBasePath](./kibana-plugin-core-server.ibasepath.md)<!-- -->. |
| [getServerInfo](./kibana-plugin-core-server.httpservicestart.getserverinfo.md) | <code>() =&gt; HttpServerInfo</code> | Provides common [information](./kibana-plugin-core-server.httpserverinfo.md) about the running http server. |
| [isTlsEnabled](./kibana-plugin-core-server.httpservicestart.istlsenabled.md) | <code>boolean</code> | Flag showing whether a server was configured to use TLS connection. |

1 change: 1 addition & 0 deletions docs/development/core/server/kibana-plugin-core-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [EnvironmentMode](./kibana-plugin-core-server.environmentmode.md) | |
| [ErrorHttpResponseOptions](./kibana-plugin-core-server.errorhttpresponseoptions.md) | HTTP response parameters |
| [FakeRequest](./kibana-plugin-core-server.fakerequest.md) | Fake request object created manually by Kibana plugins. |
| [HttpAuth](./kibana-plugin-core-server.httpauth.md) | |
| [HttpResources](./kibana-plugin-core-server.httpresources.md) | HttpResources service is responsible for serving static &amp; dynamic assets for Kibana application via HTTP. Provides API allowing plug-ins to respond with: - a pre-configured HTML page bootstrapping Kibana client app - custom HTML page - custom JS script file. |
| [HttpResourcesRenderOptions](./kibana-plugin-core-server.httpresourcesrenderoptions.md) | Allows to configure HTTP response parameters |
| [HttpResourcesServiceToolkit](./kibana-plugin-core-server.httpresourcesservicetoolkit.md) | Extended set of [KibanaResponseFactory](./kibana-plugin-core-server.kibanaresponsefactory.md) helpers used to respond with HTML or JS resource. |
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/capabilities/capabilities_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
* under the License.
*/

import { httpServiceMock, HttpServiceSetupMock } from '../http/http_service.mock';
import { httpServiceMock, InternalHttpServiceSetupMock } from '../http/http_service.mock';
import { mockRouter, RouterMock } from '../http/router/router.mock';
import { CapabilitiesService, CapabilitiesSetup } from './capabilities_service';
import { mockCoreContext } from '../core_context.mock';

describe('CapabilitiesService', () => {
let http: HttpServiceSetupMock;
let http: InternalHttpServiceSetupMock;
let service: CapabilitiesService;
let setup: CapabilitiesSetup;
let router: RouterMock;

beforeEach(() => {
http = httpServiceMock.createSetupContract();
http = httpServiceMock.createInternalSetupContract();
router = mockRouter.create();
http.createRouter.mockReturnValue(router);
service = new CapabilitiesService(mockCoreContext.create());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const delay = async (durationMs: number) =>
let elasticsearchService: ElasticsearchService;
const configService = configServiceMock.create();
const deps = {
http: httpServiceMock.createSetupContract(),
http: httpServiceMock.createInternalSetupContract(),
};
configService.atPath.mockReturnValue(
new BehaviorSubject({
Expand Down
100 changes: 87 additions & 13 deletions src/core/server/http/http_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@

import { Server } from 'hapi';
import { CspConfig } from '../csp';
import { mockRouter } from './router/router.mock';
import { mockRouter, RouterMock } from './router/router.mock';
import { configMock } from '../config/config.mock';
import { InternalHttpServiceSetup } from './types';
import {
InternalHttpServiceSetup,
HttpServiceSetup,
HttpServiceStart,
InternalHttpServiceStart,
} from './types';
import { HttpService } from './http_service';
import { AuthStatus } from './auth_state_storage';
import { OnPreAuthToolkit } from './lifecycle/on_pre_auth';
Expand All @@ -32,7 +37,23 @@ import { OnPreResponseToolkit } from './lifecycle/on_pre_response';

type BasePathMocked = jest.Mocked<InternalHttpServiceSetup['basePath']>;
type AuthMocked = jest.Mocked<InternalHttpServiceSetup['auth']>;
export type HttpServiceSetupMock = jest.Mocked<InternalHttpServiceSetup> & {

export type HttpServiceSetupMock = jest.Mocked<
Omit<HttpServiceSetup, 'basePath' | 'createRouter'>
> & {
basePath: BasePathMocked;
createRouter: jest.MockedFunction<() => RouterMock>;
};
export type InternalHttpServiceSetupMock = jest.Mocked<
Omit<InternalHttpServiceSetup, 'basePath' | 'createRouter'>
> & {
basePath: BasePathMocked;
createRouter: jest.MockedFunction<(path: string) => RouterMock>;
};
export type HttpServiceStartMock = jest.Mocked<HttpServiceStart> & {
basePath: BasePathMocked;
};
export type InternalHttpServiceStartMock = jest.Mocked<InternalHttpServiceStart> & {
Comment on lines +41 to +56
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These test types are more complex that I'd like them to be, however no combination of jest.Mocked and DeeplyMockedKey was reflecting the real mock type while respecting the 'concrete' one's signature.

I also modified the mocked createRouter signature to explicitly return a RouterMock (It was previously returning a plain IRouter, forcing consumers to forcecast to jest.Mocked in some tests). Downside is that createSetupContact().createRouter.mockReturnValue(instance of IRouter) will not be an invalid statement, but I didn't encounter any in the codebase, so I think it overall improves the testing experience.

basePath: BasePathMocked;
};

Expand All @@ -54,8 +75,8 @@ const createAuthMock = () => {
return mock;
};

const createSetupContractMock = () => {
const setupContract: HttpServiceSetupMock = {
const createInternalSetupContractMock = () => {
const mock: InternalHttpServiceSetupMock = {
// we can mock other hapi server methods when we need it
server: ({
name: 'http-server-test',
Expand All @@ -80,28 +101,78 @@ const createSetupContractMock = () => {
isTlsEnabled: false,
getServerInfo: jest.fn(),
};
setupContract.createCookieSessionStorageFactory.mockResolvedValue(
sessionStorageMock.createFactory()
);
setupContract.createRouter.mockImplementation(() => mockRouter.create());
setupContract.getAuthHeaders.mockReturnValue({ authorization: 'authorization-header' });
setupContract.getServerInfo.mockReturnValue({
mock.createCookieSessionStorageFactory.mockResolvedValue(sessionStorageMock.createFactory());
mock.createRouter.mockImplementation(() => mockRouter.create());
mock.getAuthHeaders.mockReturnValue({ authorization: 'authorization-header' });
mock.getServerInfo.mockReturnValue({
host: 'localhost',
name: 'kibana',
port: 80,
protocol: 'http',
});
return setupContract;
return mock;
};

const createSetupContractMock = () => {
const internalMock = createInternalSetupContractMock();

const mock: HttpServiceSetupMock = {
createCookieSessionStorageFactory: internalMock.createCookieSessionStorageFactory,
registerOnPreAuth: internalMock.registerOnPreAuth,
registerAuth: internalMock.registerAuth,
registerOnPostAuth: internalMock.registerOnPostAuth,
registerOnPreResponse: internalMock.registerOnPreResponse,
basePath: internalMock.basePath,
csp: CspConfig.DEFAULT,
isTlsEnabled: internalMock.isTlsEnabled,
createRouter: jest.fn(),
registerRouteHandlerContext: jest.fn(),
auth: {
get: internalMock.auth.get,
isAuthenticated: internalMock.auth.isAuthenticated,
},
getServerInfo: internalMock.getServerInfo,
};

mock.createRouter.mockImplementation(() => internalMock.createRouter(''));

return mock;
};

const createStartContractMock = () => {
const mock: HttpServiceStartMock = {
auth: createAuthMock(),
basePath: createBasePathMock(),
getServerInfo: jest.fn(),
isTlsEnabled: false,
};

return mock;
};

const createInternalStartContractMock = () => {
const mock: InternalHttpServiceStartMock = {
...createStartContractMock(),
isListening: jest.fn(),
};

mock.isListening.mockReturnValue(true);

return mock;
};

type HttpServiceContract = PublicMethodsOf<HttpService>;

const createHttpServiceMock = () => {
const mocked: jest.Mocked<HttpServiceContract> = {
setup: jest.fn(),
getStartContract: jest.fn(),
start: jest.fn(),
stop: jest.fn(),
};
mocked.setup.mockResolvedValue(createSetupContractMock());
mocked.setup.mockResolvedValue(createInternalSetupContractMock());
mocked.getStartContract.mockReturnValue(createInternalStartContractMock());
mocked.start.mockResolvedValue(createInternalStartContractMock());
return mocked;
};

Expand All @@ -128,7 +199,10 @@ export const httpServiceMock = {
create: createHttpServiceMock,
createBasePath: createBasePathMock,
createAuth: createAuthMock,
createInternalSetupContract: createInternalSetupContractMock,
createSetupContract: createSetupContractMock,
createInternalStartContract: createInternalStartContractMock,
createStartContract: createStartContractMock,
createOnPreAuthToolkit: createOnPreAuthToolkitMock,
createOnPostAuthToolkit: createOnPostAuthToolkitMock,
createOnPreResponseToolkit: createOnPreResponseToolkitMock,
Expand Down
Loading