renovate/test/util/host-rules.spec.ts

152 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-09-12 10:48:31 +00:00
import { add, find, clear, hosts } from '../../lib/util/host-rules';
2020-02-06 12:15:54 +00:00
import { DATASOURCE_NUGET } from '../../lib/constants/data-binary-source';
import { PLATFORM_TYPE_AZURE } from '../../lib/constants/platforms';
2018-07-06 05:26:36 +00:00
describe('util/host-rules', () => {
2018-07-06 05:26:36 +00:00
beforeEach(() => {
clear();
});
describe('add()', () => {
it('throws if both domainName and hostName', () => {
expect(() =>
add({
2020-02-06 12:15:54 +00:00
hostType: PLATFORM_TYPE_AZURE,
domainName: 'github.com',
hostName: 'api.github.com',
})
).toThrow('hostRules cannot contain both a domainName and hostName');
2018-07-06 05:26:36 +00:00
});
it('throws if both domainName and baseUrl', () => {
expect(() =>
add({
2020-02-06 12:15:54 +00:00
hostType: PLATFORM_TYPE_AZURE,
domainName: 'github.com',
baseUrl: 'https://api.github.com',
})
).toThrow('hostRules cannot contain both a domainName and baseUrl');
2018-07-06 05:26:36 +00:00
});
it('throws if both hostName and baseUrl', () => {
2018-07-06 05:26:36 +00:00
expect(() =>
add({
2020-02-06 12:15:54 +00:00
hostType: PLATFORM_TYPE_AZURE,
hostName: 'api.github.com',
baseUrl: 'https://api.github.com',
})
).toThrow('hostRules cannot contain both a hostName and baseUrl');
2018-07-06 05:26:36 +00:00
});
it('supports baseUrl-only', () => {
add({
baseUrl: 'https://some.endpoint',
username: 'user1',
password: 'pass1',
});
expect(find({ url: 'https://some.endpoint/v3/' })).toMatchSnapshot();
});
2018-07-06 05:26:36 +00:00
});
describe('find()', () => {
it('warns and returns empty for bad search', () => {
2019-08-28 04:46:48 +00:00
expect(find({ abc: 'def' } as any)).toEqual({});
});
it('needs exact host matches', () => {
add({
2020-02-06 12:15:54 +00:00
hostType: DATASOURCE_NUGET,
hostName: 'nuget.org',
username: 'root',
password: 'p4$$w0rd',
token: undefined,
});
2020-02-06 12:15:54 +00:00
expect(find({ hostType: DATASOURCE_NUGET })).toMatchSnapshot();
expect(
find({ hostType: DATASOURCE_NUGET, url: 'https://nuget.org' })
).not.toEqual({});
expect(
find({ hostType: DATASOURCE_NUGET, url: 'https://not.nuget.org' })
).toEqual({});
expect(
find({ hostType: DATASOURCE_NUGET, url: 'https://not-nuget.org' })
).toEqual({});
});
it('matches on empty rules', () => {
add({
json: true,
});
expect(
2020-02-06 12:15:54 +00:00
find({ hostType: DATASOURCE_NUGET, url: 'https://api.github.com' })
).toEqual({ json: true });
});
it('matches on hostType', () => {
add({
2020-02-06 12:15:54 +00:00
hostType: DATASOURCE_NUGET,
token: 'abc',
});
expect(
2020-02-06 12:15:54 +00:00
find({ hostType: DATASOURCE_NUGET, url: 'https://nuget.local/api' })
).toMatchSnapshot();
});
it('matches on domainName', () => {
add({
domainName: 'github.com',
token: 'def',
});
expect(
2020-02-06 12:15:54 +00:00
find({ hostType: DATASOURCE_NUGET, url: 'https://api.github.com' })
.token
).toEqual('def');
});
it('matches on hostName', () => {
add({
hostName: 'nuget.local',
token: 'abc',
});
expect(
2020-02-06 12:15:54 +00:00
find({ hostType: DATASOURCE_NUGET, url: 'https://nuget.local/api' })
).toMatchSnapshot();
});
it('matches on hostType and endpoint', () => {
add({
2020-02-06 12:15:54 +00:00
hostType: DATASOURCE_NUGET,
baseUrl: 'https://nuget.local/api',
token: 'abc',
});
expect(
2020-02-06 12:15:54 +00:00
find({ hostType: DATASOURCE_NUGET, url: 'https://nuget.local/api' })
.token
).toEqual('abc');
});
it('matches on endpoint subresource', () => {
add({
2020-02-06 12:15:54 +00:00
hostType: DATASOURCE_NUGET,
baseUrl: 'https://nuget.local/api',
token: 'abc',
});
expect(
find({
2020-02-06 12:15:54 +00:00
hostType: DATASOURCE_NUGET,
url: 'https://nuget.local/api/sub-resource',
})
).toMatchSnapshot();
});
it('returns hosts', () => {
add({
2020-02-06 12:15:54 +00:00
hostType: DATASOURCE_NUGET,
token: 'aaaaaa',
});
add({
2020-02-06 12:15:54 +00:00
hostType: DATASOURCE_NUGET,
baseUrl: 'https://nuget.local/api',
token: 'abc',
});
add({
2020-02-06 12:15:54 +00:00
hostType: DATASOURCE_NUGET,
hostName: 'my.local.registry',
token: 'def',
});
const res = hosts({
2020-02-06 12:15:54 +00:00
hostType: DATASOURCE_NUGET,
});
expect(res).toMatchSnapshot();
expect(res).toHaveLength(2);
});
2018-07-06 05:26:36 +00:00
});
});