tests fixups

Signed-off-by: Emma Indal <emma.indahl@gmail.com>
This commit is contained in:
Emma Indal
2022-03-14 15:27:51 +01:00
parent b83063afe0
commit b44692890b
13 changed files with 44 additions and 212 deletions
@@ -7,4 +7,3 @@ backend:
techdocs:
builder: 'external'
requestUrl: http://localhost:3000/api
+3 -12
View File
@@ -64,17 +64,11 @@ class TechDocsDevStorageApi implements TechDocsStorageApi {
}
async getApiOrigin() {
return (
this.configApi.getOptionalString('techdocs.requestUrl') ??
(await this.discoveryApi.getBaseUrl('techdocs'))
);
return await this.discoveryApi.getBaseUrl('techdocs');
}
async getStorageUrl() {
return (
this.configApi.getOptionalString('techdocs.storageUrl') ??
`${await this.discoveryApi.getBaseUrl('techdocs')}/static/docs`
);
return `${await this.discoveryApi.getBaseUrl('techdocs')}/static/docs`;
}
async getBuilder() {
@@ -134,10 +128,7 @@ class TechDocsDevApi implements TechDocsApi {
}
async getApiOrigin() {
return (
this.configApi.getOptionalString('techdocs.requestUrl') ??
(await this.discoveryApi.getBaseUrl('techdocs'))
);
return await this.discoveryApi.getBaseUrl('techdocs');
}
async getEntityMetadata(_entityId: any) {
@@ -35,8 +35,8 @@ import { Content } from '@backstage/core-components';
import {
Reader,
TechDocsPage,
TechDocsPageHeader,
TechDocsReaderPage,
TechDocsReaderPageHeader,
} from '@backstage/plugin-techdocs';
const useStyles = makeStyles((theme: Theme) => ({
@@ -146,19 +146,19 @@ const DefaultTechDocsPage = () => {
};
return (
<TechDocsPage>
<TechDocsReaderPage>
{({ entityRef, onReady }) => (
<>
<TechDocsPageHeader
<TechDocsReaderPageHeader
entityRef={entityRef}
techDocsMetadata={techDocsMetadata}
>
<TechDocsThemeToggle />
</TechDocsPageHeader>
</TechDocsReaderPageHeader>
<TechDocsPageContent entityRef={entityRef} onReady={onReady} />
</>
)}
</TechDocsPage>
</TechDocsReaderPage>
);
};
@@ -22,7 +22,6 @@ const PRODUCTION_CONFIG = {
},
techdocs: {
builder: 'external',
requestUrl: 'http://localhost:3000/api',
},
};
@@ -32,7 +31,6 @@ const DEVELOPMENT_CONFIG = {
},
techdocs: {
builder: 'external',
requestUrl: 'http://localhost:7007/api',
},
};
@@ -21,7 +21,6 @@ import {
PluginEndpointDiscovery,
} from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
import { NotModifiedError } from '@backstage/errors';
import {
GeneratorBuilder,
PreparerBuilder,
@@ -31,12 +30,7 @@ import express, { Response } from 'express';
import request from 'supertest';
import { DocsSynchronizer, DocsSynchronizerSyncOpts } from './DocsSynchronizer';
import { CachedEntityLoader } from './CachedEntityLoader';
import {
createEventStream,
createHttpResponse,
createRouter,
RouterOptions,
} from './router';
import { createEventStream, createRouter, RouterOptions } from './router';
import { TechDocsCache } from '../cache';
import { DocsBuildStrategy } from './DocsBuildStrategy';
@@ -160,120 +154,6 @@ describe('createRouter', () => {
});
describe('GET /sync/:namespace/:kind/:name', () => {
describe('accept application/json', () => {
it('should return not found if entity is not found', async () => {
const app = await createApp(outOfTheBoxOptions);
MockCachedEntityLoader.prototype.load.mockResolvedValue(undefined);
const response = await request(app)
.get('/sync/default/Component/test')
.send();
expect(response.status).toBe(404);
});
it('should return not found if entity has no uid', async () => {
const app = await createApp(outOfTheBoxOptions);
MockCachedEntityLoader.prototype.load.mockResolvedValue(
entityWithoutMetadata,
);
const response = await request(app)
.get('/sync/default/Component/test')
.send();
expect(response.status).toBe(404);
});
it('should not check for an update when shouldBuild returns false', async () => {
const app = await createApp(outOfTheBoxOptions);
docsBuildStrategy.shouldBuild.mockResolvedValue(false);
MockCachedEntityLoader.prototype.load.mockResolvedValue(entity);
MockDocsSynchronizer.prototype.doCacheSync.mockImplementation(
async ({ responseHandler }) =>
responseHandler.finish({ updated: false }),
);
const response = await request(app)
.get('/sync/default/Component/test')
.send();
expect(response.status).toBe(304);
});
it('should error if build is required and is missing preparer', async () => {
const app = await createApp(recommendedOptions);
docsBuildStrategy.shouldBuild.mockResolvedValue(true);
MockCachedEntityLoader.prototype.load.mockResolvedValue(entity);
const response = await request(app)
.get('/sync/default/Component/test')
.send();
expect(response.status).toBe(500);
expect(response.text).toMatch(
/Invalid configuration\. docsBuildStrategy\.shouldBuild returned 'true', but no 'preparer' was provided to the router initialization./,
);
expect(MockDocsSynchronizer.prototype.doSync).toBeCalledTimes(0);
});
it('should execute synchronization', async () => {
const app = await createApp(outOfTheBoxOptions);
docsBuildStrategy.shouldBuild.mockResolvedValue(true);
MockCachedEntityLoader.prototype.load.mockResolvedValue(entity);
MockDocsSynchronizer.prototype.doSync.mockImplementation(
async ({ responseHandler }) =>
responseHandler.finish({ updated: true }),
);
await request(app).get('/sync/default/Component/test').send();
expect(MockDocsSynchronizer.prototype.doSync).toBeCalledTimes(1);
expect(MockDocsSynchronizer.prototype.doSync).toBeCalledWith({
responseHandler: {
log: expect.any(Function),
error: expect.any(Function),
finish: expect.any(Function),
},
entity,
generators,
preparers,
});
});
it('should return on updated', async () => {
const app = await createApp(outOfTheBoxOptions);
docsBuildStrategy.shouldBuild.mockResolvedValue(true);
MockCachedEntityLoader.prototype.load.mockResolvedValue(entity);
MockDocsSynchronizer.prototype.doSync.mockImplementation(
async ({ responseHandler }) => {
const { log, finish } = responseHandler;
log('Some log');
finish({ updated: true });
},
);
const response = await request(app)
.get('/sync/default/Component/test')
.send();
expect(response.status).toBe(201);
expect(response.get('content-type')).toMatch(/application\/json/);
expect(response.text).toEqual(
'{"message":"Docs updated or did not need updating"}',
);
});
});
describe('accept text/event-stream', () => {
it('should return not found if entity is not found', async () => {
const app = await createApp(outOfTheBoxOptions);
@@ -559,48 +439,3 @@ data: {"updated":true}
expect(res.end).toBeCalledTimes(1);
});
});
describe('createHttpResponse', () => {
const res: jest.Mocked<Response> = {
status: jest.fn(),
json: jest.fn(),
} as any;
let handlers: DocsSynchronizerSyncOpts;
beforeEach(() => {
res.status.mockImplementation(() => res);
handlers = createHttpResponse(res);
});
afterEach(() => {
jest.resetAllMocks();
});
it('should return CREATED if updated', async () => {
handlers.finish({ updated: true });
expect(res.status).toBeCalledTimes(1);
expect(res.status).toBeCalledWith(201);
expect(res.json).toBeCalledTimes(1);
expect(res.json).toBeCalledWith({
message: 'Docs updated or did not need updating',
});
});
it('should return NOT_MODIFIED if not updated', async () => {
expect(() => handlers.finish({ updated: false })).toThrowError(
NotModifiedError,
);
});
it('should throw custom error', async () => {
expect(() => handlers.error(new Error('Some Error'))).toThrowError(
/Some Error/,
);
});
it('should ignore logs', async () => {
expect(() => handlers.log('Some Message')).not.toThrow();
});
});
@@ -60,11 +60,10 @@ export async function startStandaloneServer(
logger.debug('Creating application...');
const preparers = new Preparers();
const directoryPreparer = new DirectoryPreparer(
config,
const directoryPreparer = DirectoryPreparer.fromConfig(config, {
logger,
mockUrlReader,
);
reader: mockUrlReader,
});
preparers.register('dir', directoryPreparer);
const dockerClient = new Docker();
@@ -54,7 +54,6 @@ const createPublisherFromConfig = ({
} = {}) => {
const mockConfig = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'awsS3',
awsS3: {
@@ -51,7 +51,6 @@ const createPublisherFromConfig = ({
} = {}) => {
const config = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'azureBlobStorage',
azureBlobStorage: {
@@ -52,7 +52,6 @@ const createPublisherFromConfig = ({
} = {}) => {
const config = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'googleGcs',
googleGcs: {
@@ -84,7 +84,6 @@ beforeEach(() => {
mockFs.restore();
const mockConfig = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'openStackSwift',
openStackSwift: {
@@ -114,7 +113,6 @@ describe('OpenStackSwiftPublish', () => {
it('should reject incorrect config', async () => {
const mockConfig = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'openStackSwift',
openStackSwift: {
@@ -37,11 +37,7 @@ describe('Publisher', () => {
});
it('should create local publisher by default', async () => {
const mockConfig = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
},
});
const mockConfig = new ConfigReader({});
const publisher = await Publisher.fromConfig(mockConfig, {
logger,
@@ -53,7 +49,6 @@ describe('Publisher', () => {
it('should create local publisher from config', async () => {
const mockConfig = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'local',
},
@@ -70,7 +65,6 @@ describe('Publisher', () => {
it('should create google gcs publisher from config', async () => {
const mockConfig = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'googleGcs',
googleGcs: {
@@ -91,7 +85,6 @@ describe('Publisher', () => {
it('should create AWS S3 publisher from config', async () => {
const mockConfig = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'awsS3',
awsS3: {
@@ -115,7 +108,6 @@ describe('Publisher', () => {
it('should create Azure Blob Storage publisher from config', async () => {
const mockConfig = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'azureBlobStorage',
azureBlobStorage: {
@@ -143,7 +135,6 @@ describe('Publisher', () => {
const mockConfig = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'azureBlobStorage',
azureBlobStorage: {
@@ -166,7 +157,6 @@ describe('Publisher', () => {
it('should create Open Stack Swift publisher from config', async () => {
const mockConfig = new ConfigReader({
techdocs: {
requestUrl: 'http://localhost:7007',
publisher: {
type: 'openStackSwift',
openStackSwift: {
+1 -3
View File
@@ -35,9 +35,7 @@ const mockEntity = {
describe('TechDocsStorageClient', () => {
const mockBaseUrl = 'http://backstage:9191/api/techdocs';
const configApi = new MockConfigApi({
techdocs: { requestUrl: 'http://backstage:9191/api/techdocs' },
});
const configApi = new MockConfigApi({});
const discoveryApi = UrlPatternDiscovery.compile(mockBaseUrl);
const identityApi: jest.Mocked<IdentityApi> = {
getCredentials: jest.fn(),
@@ -18,11 +18,13 @@ import React, { useCallback, useState } from 'react';
import { useOutlet } from 'react-router';
import { useParams } from 'react-router-dom';
import useAsync from 'react-use/lib/useAsync';
import { Reader } from './Reader';
import { TechDocsReaderPageHeader } from './TechDocsReaderPageHeader';
import { techdocsApiRef } from '../../api';
import { TechDocsEntityMetadata, TechDocsMetadata } from '../../types';
import { CompoundEntityRef } from '@backstage/catalog-model';
import { useApi, useApp } from '@backstage/core-plugin-api';
import { Page } from '@backstage/core-components';
import { Page, Content } from '@backstage/core-components';
/**
* Helper function that gives the children of {@link TechDocsReaderPage} access to techdocs and entity metadata
@@ -78,7 +80,32 @@ export const TechDocsReaderPage = (props: TechDocsReaderPageProps) => {
if (entityMetadataError) return <NotFoundErrorPage />;
if (!children) return outlet;
if (!children)
return (
outlet || (
<Page themeId="documentation">
<TechDocsReaderPageHeader
techDocsMetadata={techdocsMetadataValue}
entityMetadata={entityMetadataValue}
entityRef={{
kind,
namespace,
name,
}}
/>
<Content data-testid="techdocs-content">
<Reader
onReady={onReady}
entityRef={{
kind,
namespace,
name,
}}
/>
</Content>
</Page>
)
);
return (
<Page themeId="documentation">