backend-common: fix GithubUrlReader tests

This commit is contained in:
Patrik Oldsberg
2020-10-02 16:27:41 +02:00
parent 1e97419d30
commit 07d19aad96
2 changed files with 25 additions and 44 deletions
@@ -15,7 +15,6 @@
*/
import { ConfigReader } from '@backstage/config';
import { getVoidLogger } from '../logging';
import {
getApiRequestOptions,
getApiUrl,
@@ -29,7 +28,7 @@ import {
describe('GithubUrlReader', () => {
describe('getApiRequestOptions', () => {
it('sets the correct API version', () => {
const config: ProviderConfig = { target: '', apiBaseUrl: '' };
const config: ProviderConfig = { host: '', apiBaseUrl: '' };
expect((getApiRequestOptions(config).headers as any).Accept).toEqual(
'application/vnd.github.v3.raw',
);
@@ -37,12 +36,12 @@ describe('GithubUrlReader', () => {
it('inserts a token when needed', () => {
const withToken: ProviderConfig = {
target: '',
host: '',
apiBaseUrl: '',
token: 'A',
};
const withoutToken: ProviderConfig = {
target: '',
host: '',
apiBaseUrl: '',
};
expect(
@@ -57,12 +56,12 @@ describe('GithubUrlReader', () => {
describe('getRawRequestOptions', () => {
it('inserts a token when needed', () => {
const withToken: ProviderConfig = {
target: '',
host: '',
rawBaseUrl: '',
token: 'A',
};
const withoutToken: ProviderConfig = {
target: '',
host: '',
rawBaseUrl: '',
};
expect(
@@ -76,13 +75,13 @@ describe('GithubUrlReader', () => {
describe('getApiUrl', () => {
it('rejects targets that do not look like URLs', () => {
const config: ProviderConfig = { target: '', apiBaseUrl: '' };
const config: ProviderConfig = { host: '', apiBaseUrl: '' };
expect(() => getApiUrl('a/b', config)).toThrow(/Incorrect URL: a\/b/);
});
it('happy path for github', () => {
const config: ProviderConfig = {
target: 'https://github.com',
host: 'github.com',
apiBaseUrl: 'https://api.github.com',
};
expect(
@@ -109,7 +108,7 @@ describe('GithubUrlReader', () => {
it('happy path for ghe', () => {
const config: ProviderConfig = {
target: 'https://ghe.mycompany.net',
host: 'ghe.mycompany.net',
apiBaseUrl: 'https://ghe.mycompany.net/api/v3',
};
expect(
@@ -127,13 +126,13 @@ describe('GithubUrlReader', () => {
describe('getRawUrl', () => {
it('rejects targets that do not look like URLs', () => {
const config: ProviderConfig = { target: '', apiBaseUrl: '' };
const config: ProviderConfig = { host: '', apiBaseUrl: '' };
expect(() => getRawUrl('a/b', config)).toThrow(/Incorrect URL: a\/b/);
});
it('happy path for github', () => {
const config: ProviderConfig = {
target: 'https://github.com',
host: 'github.com',
rawBaseUrl: 'https://raw.githubusercontent.com',
};
expect(
@@ -150,7 +149,7 @@ describe('GithubUrlReader', () => {
it('happy path for ghe', () => {
const config: ProviderConfig = {
target: 'https://ghe.mycompany.net',
host: 'ghe.mycompany.net',
rawBaseUrl: 'https://ghe.mycompany.net/raw',
};
expect(
@@ -166,23 +165,23 @@ describe('GithubUrlReader', () => {
describe('readConfig', () => {
function config(
providers: { target: string; apiBaseUrl?: string; token?: string }[],
providers: { host: string; apiBaseUrl?: string; token?: string }[],
) {
return ConfigReader.fromConfigs([
{
context: '',
data: {
catalog: { processors: { github: { providers } } },
integrations: { github: providers },
},
},
]);
}
it('adds a default GitHub entry when missing', () => {
const output = readConfig(config([]), getVoidLogger());
const output = readConfig(config([]));
expect(output).toEqual([
{
target: 'https://github.com',
host: 'github.com',
apiBaseUrl: 'https://api.github.com',
rawBaseUrl: 'https://raw.githubusercontent.com',
},
@@ -190,13 +189,10 @@ describe('GithubUrlReader', () => {
});
it('injects the correct GitHub API base URL when missing', () => {
const output = readConfig(
config([{ target: 'https://github.com' }]),
getVoidLogger(),
);
const output = readConfig(config([{ host: 'github.com' }]));
expect(output).toEqual([
{
target: 'https://github.com',
host: 'github.com',
apiBaseUrl: 'https://api.github.com',
rawBaseUrl: 'https://raw.githubusercontent.com',
},
@@ -204,34 +200,19 @@ describe('GithubUrlReader', () => {
});
it('rejects custom targets with no base URLs', () => {
expect(() =>
readConfig(
config([{ target: 'https://ghe.company.com' }]),
getVoidLogger(),
),
).toThrow(
'Provider at https://ghe.company.com must configure an explicit apiBaseUrl or rawBaseUrl',
expect(() => readConfig(config([{ host: 'ghe.company.com' }]))).toThrow(
"GitHub integration for 'ghe.company.com' must configure an explicit apiBaseUrl and rawBaseUrl",
);
});
it('rejects funky configs', () => {
expect(() => readConfig(config([{ host: 7 } as any]))).toThrow(/host/);
expect(() => readConfig(config([{ token: 7 } as any]))).toThrow(/token/);
expect(() =>
readConfig(config([{ target: 7 } as any]), getVoidLogger()),
).toThrow(/target/);
expect(() =>
readConfig(config([{ noTarget: '7' } as any]), getVoidLogger()),
).toThrow(/target/);
expect(() =>
readConfig(
config([{ target: 'https://github.com', apiBaseUrl: 7 } as any]),
getVoidLogger(),
),
readConfig(config([{ host: 'github.com', apiBaseUrl: 7 } as any])),
).toThrow(/apiBaseUrl/);
expect(() =>
readConfig(
config([{ target: 'https://github.com', token: 7 } as any]),
getVoidLogger(),
),
readConfig(config([{ host: 'github.com', token: 7 } as any])),
).toThrow(/token/);
});
});
@@ -239,7 +220,7 @@ describe('GithubUrlReader', () => {
describe('implementation', () => {
it('rejects unknown targets', async () => {
const processor = new GithubUrlReader({
target: 'https://github.com',
host: 'github.com',
apiBaseUrl: 'https://api.github.com',
});
await expect(
@@ -163,7 +163,7 @@ export function readConfig(config: Config): ProviderConfig[] {
if (!apiBaseUrl && !rawBaseUrl) {
throw new Error(
`GitHub integration for ${host} must configure an explicit apiBaseUrl and rawBaseUrl`,
`GitHub integration for '${host}' must configure an explicit apiBaseUrl and rawBaseUrl`,
);
}