Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Oct 19, 2023
1 parent 45c43e7 commit d9f2c2a
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { AuthStateStorage } from './auth_state_storage';
import { AuthHeadersStorage } from './auth_headers_storage';
import { BasePath } from './base_path_service';
import { getEcsResponseLog } from './logging';
import { StaticAssets } from './static_assets';
import { StaticAssets, type IStaticAssets } from './static_assets';

/**
* Adds ELU timings for the executed function to the current's context transaction
Expand Down Expand Up @@ -136,7 +136,7 @@ export interface HttpServerSetup {
* @note Static assets may be served over CDN
*/
registerStaticDir: (path: string, dirPath: string) => void;
staticAssets: StaticAssets;
staticAssets: IStaticAssets;
basePath: HttpServiceSetup['basePath'];
csp: HttpServiceSetup['csp'];
createCookieSessionStorageFactory: HttpServiceSetup['createCookieSessionStorageFactory'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import type { KibanaRequest } from '@kbn/core-http-server/src/router';
import type { BasePath } from './base_path_service';
import { CdnConfig } from './cdn';

export class StaticAssets {
export interface IStaticAssets {
getHrefBase(request?: KibanaRequest): string;
}

export class StaticAssets implements IStaticAssets {
constructor(private readonly basePath: BasePath, private readonly cdnConfig: CdnConfig) {}
/**
* Returns a href (hypertext reference) intended to be used as the base for constructing
Expand Down
28 changes: 23 additions & 5 deletions packages/core/http/core-http-server-mocks/src/http_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,28 @@ import type {
import { sessionStorageMock } from './cookie_session_storage.mocks';

type BasePathMocked = jest.Mocked<InternalHttpServiceSetup['basePath']>;
type StaticAssetsMocked = jest.Mocked<InternalHttpServiceSetup['staticAssets']>;
type AuthMocked = jest.Mocked<InternalHttpServiceSetup['auth']>;

export type HttpServicePrebootMock = jest.Mocked<HttpServicePreboot>;
export type InternalHttpServicePrebootMock = jest.Mocked<
Omit<InternalHttpServicePreboot, 'basePath'>
> & { basePath: BasePathMocked };
Omit<InternalHttpServicePreboot, 'basePath' | 'staticAssets'>
> & { basePath: BasePathMocked; staticAssets: StaticAssetsMocked };
export type HttpServiceSetupMock<
ContextType extends RequestHandlerContextBase = RequestHandlerContextBase
> = jest.Mocked<Omit<HttpServiceSetup<ContextType>, 'basePath' | 'createRouter'>> & {
basePath: BasePathMocked;
createRouter: jest.MockedFunction<() => RouterMock>;
};
export type InternalHttpServiceSetupMock = jest.Mocked<
Omit<InternalHttpServiceSetup, 'basePath' | 'createRouter' | 'authRequestHeaders' | 'auth'>
Omit<
InternalHttpServiceSetup,
'basePath' | 'staticAssets' | 'createRouter' | 'authRequestHeaders' | 'auth'
>
> & {
auth: AuthMocked;
basePath: BasePathMocked;
staticAssets: StaticAssetsMocked;
createRouter: jest.MockedFunction<(path: string) => RouterMock>;
authRequestHeaders: jest.Mocked<IAuthHeadersStorage>;
};
Expand All @@ -73,6 +78,13 @@ const createBasePathMock = (
remove: jest.fn(),
});

const createStaticAssetsMock = (
basePath: BasePathMocked,
cdnUrl: undefined | string = undefined
): StaticAssetsMocked => ({
getHrefBase: jest.fn(() => cdnUrl ?? basePath.serverBasePath),
});

const createAuthMock = () => {
const mock: AuthMocked = {
get: jest.fn(),
Expand All @@ -91,12 +103,17 @@ const createAuthHeaderStorageMock = () => {
return mock;
};

const createInternalPrebootContractMock = () => {
interface CreateMockArgs {
cdnUrl?: string;
}
const createInternalPrebootContractMock = (args: CreateMockArgs = {}) => {
const basePath = createBasePathMock();
const mock: InternalHttpServicePrebootMock = {
registerRoutes: jest.fn(),
registerRouteHandlerContext: jest.fn(),
registerStaticDir: jest.fn(),
basePath: createBasePathMock(),
basePath,
staticAssets: createStaticAssetsMock(basePath, args.cdnUrl),
csp: CspConfig.DEFAULT,
externalUrl: ExternalUrlConfig.DEFAULT,
auth: createAuthMock(),
Expand Down Expand Up @@ -149,6 +166,7 @@ const createInternalSetupContractMock = () => {
registerStaticDir: jest.fn(),
basePath: createBasePathMock(),
csp: CspConfig.DEFAULT,
staticAssets: { getHrefBase: jest.fn(() => mock.basePath.serverBasePath) },
externalUrl: ExternalUrlConfig.DEFAULT,
auth: createAuthMock(),
authRequestHeaders: createAuthHeaderStorageMock(),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ describe('bootstrapRenderer', () => {
expect(getPluginsBundlePathsMock).toHaveBeenCalledWith({
isAnonymousPage,
uiPlugins,
regularBundlePath: '/base-path/42/bundles',
bundlesHref: '/base-path/42/bundles',
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,25 @@ function renderTestCases(
expect(getStylesheetPathsMock).toHaveBeenCalledWith({
darkMode: true,
themeVersion: 'v8',
basePath: '/mock-server-basepath',
baseHref: '/mock-server-basepath',
buildNum: expect.any(Number),
});
});

it('renders "core" CDN url injected', async () => {
const userSettings = { 'theme:darkMode': { userValue: true } };
uiSettings.client.getUserProvided.mockResolvedValue(userSettings);
(mockRenderingPrebootDeps.http.staticAssets.getHrefBase as jest.Mock).mockImplementation(
() => 'http://foo.bar:1773'
);
const [render] = await getRender();
const content = await render(createKibanaRequest(), uiSettings, {
isAnonymousPage: false,
});
const dom = load(content);
const data = JSON.parse(dom('kbn-injected-metadata').attr('data') ?? '""');
expect(data).toMatchSnapshot(INJECTED_METADATA);
});
});
}

Expand Down Expand Up @@ -233,7 +248,7 @@ function renderDarkModeTestCases(
expect(getStylesheetPathsMock).toHaveBeenCalledWith({
darkMode: true,
themeVersion: 'v8',
basePath: '/mock-server-basepath',
baseHref: '/mock-server-basepath',
buildNum: expect.any(Number),
});
});
Expand All @@ -259,7 +274,7 @@ function renderDarkModeTestCases(
expect(getStylesheetPathsMock).toHaveBeenCalledWith({
darkMode: false,
themeVersion: 'v8',
basePath: '/mock-server-basepath',
baseHref: '/mock-server-basepath',
buildNum: expect.any(Number),
});
});
Expand All @@ -283,7 +298,7 @@ function renderDarkModeTestCases(
expect(getStylesheetPathsMock).toHaveBeenCalledWith({
darkMode: false,
themeVersion: 'v8',
basePath: '/mock-server-basepath',
baseHref: '/mock-server-basepath',
buildNum: expect.any(Number),
});
});
Expand All @@ -307,7 +322,7 @@ function renderDarkModeTestCases(
expect(getStylesheetPathsMock).toHaveBeenCalledWith({
darkMode: true,
themeVersion: 'v8',
basePath: '/mock-server-basepath',
baseHref: '/mock-server-basepath',
buildNum: expect.any(Number),
});
});
Expand All @@ -331,7 +346,7 @@ function renderDarkModeTestCases(
expect(getStylesheetPathsMock).toHaveBeenCalledWith({
darkMode: false,
themeVersion: 'v8',
basePath: '/mock-server-basepath',
baseHref: '/mock-server-basepath',
buildNum: expect.any(Number),
});
});
Expand All @@ -355,7 +370,7 @@ function renderDarkModeTestCases(
expect(getStylesheetPathsMock).toHaveBeenCalledWith({
darkMode: false,
themeVersion: 'v8',
basePath: '/mock-server-basepath',
baseHref: '/mock-server-basepath',
buildNum: expect.any(Number),
});
});
Expand All @@ -379,7 +394,7 @@ function renderDarkModeTestCases(
expect(getStylesheetPathsMock).toHaveBeenCalledWith({
darkMode: true,
themeVersion: 'v8',
basePath: '/mock-server-basepath',
baseHref: '/mock-server-basepath',
buildNum: expect.any(Number),
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class RenderingService {
const metadata: RenderingMetadata = {
strictCsp: http.csp.strict,
uiPublicUrl: `${staticAssetsHrefBase}/ui`,
bootstrapScriptUrl: `${serverBasePath}/${bootstrapScript}`,
bootstrapScriptUrl: `${basePath}/${bootstrapScript}`,
i18n: i18n.translate,
locale: i18n.getLocale(),
darkMode,
Expand All @@ -214,7 +214,7 @@ export class RenderingService {
anonymousStatusPage: status?.isStatusPageAnonymous() ?? false,
i18n: {
// TODO: Make this load as part of static assets!
translationsUrl: `${serverBasePath}/translations/${i18n.getLocale()}.json`,
translationsUrl: `${basePath}/translations/${i18n.getLocale()}.json`,
},
theme: {
darkMode,
Expand Down

0 comments on commit d9f2c2a

Please sign in to comment.