2020-03-05 20:57:24 +00:00
|
|
|
import { PLATFORM_TYPE_AZURE } from '../constants/platforms';
|
2020-05-01 16:03:48 +00:00
|
|
|
import * as datasourceNuget from '../datasource/nuget';
|
|
|
|
import { add, clear, find, findAll, hosts } from './host-rules';
|
2018-07-06 05:26:36 +00:00
|
|
|
|
2018-09-12 10:16:17 +00:00
|
|
|
describe('util/host-rules', () => {
|
2018-07-06 05:26:36 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
clear();
|
|
|
|
});
|
2019-05-24 15:40:39 +00:00
|
|
|
describe('add()', () => {
|
|
|
|
it('throws if both domainName and hostName', () => {
|
|
|
|
expect(() =>
|
|
|
|
add({
|
2020-02-06 12:15:54 +00:00
|
|
|
hostType: PLATFORM_TYPE_AZURE,
|
2019-05-24 15:40:39 +00:00
|
|
|
domainName: 'github.com',
|
|
|
|
hostName: 'api.github.com',
|
|
|
|
})
|
|
|
|
).toThrow('hostRules cannot contain both a domainName and hostName');
|
2018-07-06 05:26:36 +00:00
|
|
|
});
|
2019-05-24 15:40:39 +00:00
|
|
|
it('throws if both domainName and baseUrl', () => {
|
|
|
|
expect(() =>
|
|
|
|
add({
|
2020-02-06 12:15:54 +00:00
|
|
|
hostType: PLATFORM_TYPE_AZURE,
|
2019-05-24 15:40:39 +00:00
|
|
|
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
|
|
|
});
|
2019-05-24 15:40:39 +00:00
|
|
|
it('throws if both hostName and baseUrl', () => {
|
2018-07-06 05:26:36 +00:00
|
|
|
expect(() =>
|
2019-05-24 15:40:39 +00:00
|
|
|
add({
|
2020-02-06 12:15:54 +00:00
|
|
|
hostType: PLATFORM_TYPE_AZURE,
|
2019-05-24 15:40:39 +00:00
|
|
|
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
|
|
|
});
|
2019-05-24 15:40:39 +00:00
|
|
|
it('supports baseUrl-only', () => {
|
|
|
|
add({
|
|
|
|
baseUrl: 'https://some.endpoint',
|
2018-10-15 11:37:08 +00:00
|
|
|
username: 'user1',
|
|
|
|
password: 'pass1',
|
|
|
|
});
|
2019-05-24 15:40:39 +00:00
|
|
|
expect(find({ url: 'https://some.endpoint/v3/' })).toMatchSnapshot();
|
2018-10-15 11:37:08 +00:00
|
|
|
});
|
2018-07-06 05:26:36 +00:00
|
|
|
});
|
|
|
|
describe('find()', () => {
|
2019-05-25 05:49:26 +00:00
|
|
|
it('warns and returns empty for bad search', () => {
|
2019-08-28 04:46:48 +00:00
|
|
|
expect(find({ abc: 'def' } as any)).toEqual({});
|
2019-05-24 15:40:39 +00:00
|
|
|
});
|
2019-02-22 09:34:01 +00:00
|
|
|
it('needs exact host matches', () => {
|
2019-05-24 15:40:39 +00:00
|
|
|
add({
|
2020-03-01 07:01:12 +00:00
|
|
|
hostType: datasourceNuget.id,
|
2019-05-24 15:40:39 +00:00
|
|
|
hostName: 'nuget.org',
|
2019-02-22 09:34:01 +00:00
|
|
|
username: 'root',
|
|
|
|
password: 'p4$$w0rd',
|
2019-05-24 15:40:39 +00:00
|
|
|
token: undefined,
|
2019-02-22 09:34:01 +00:00
|
|
|
});
|
2020-03-01 07:01:12 +00:00
|
|
|
expect(find({ hostType: datasourceNuget.id })).toMatchSnapshot();
|
2020-02-06 12:15:54 +00:00
|
|
|
expect(
|
2020-03-01 07:01:12 +00:00
|
|
|
find({ hostType: datasourceNuget.id, url: 'https://nuget.org' })
|
2020-02-06 12:15:54 +00:00
|
|
|
).not.toEqual({});
|
|
|
|
expect(
|
2020-03-01 07:01:12 +00:00
|
|
|
find({ hostType: datasourceNuget.id, url: 'https://not.nuget.org' })
|
2020-02-06 12:15:54 +00:00
|
|
|
).toEqual({});
|
|
|
|
expect(
|
2020-03-01 07:01:12 +00:00
|
|
|
find({ hostType: datasourceNuget.id, url: 'https://not-nuget.org' })
|
2020-02-06 12:15:54 +00:00
|
|
|
).toEqual({});
|
2019-05-24 15:40:39 +00:00
|
|
|
});
|
|
|
|
it('matches on empty rules', () => {
|
|
|
|
add({
|
|
|
|
json: true,
|
|
|
|
});
|
2019-02-22 09:34:01 +00:00
|
|
|
expect(
|
2020-03-01 07:01:12 +00:00
|
|
|
find({ hostType: datasourceNuget.id, url: 'https://api.github.com' })
|
2019-05-24 15:40:39 +00:00
|
|
|
).toEqual({ json: true });
|
|
|
|
});
|
|
|
|
it('matches on hostType', () => {
|
|
|
|
add({
|
2020-03-01 07:01:12 +00:00
|
|
|
hostType: datasourceNuget.id,
|
2019-05-24 15:40:39 +00:00
|
|
|
token: 'abc',
|
|
|
|
});
|
|
|
|
expect(
|
2020-03-01 07:01:12 +00:00
|
|
|
find({ hostType: datasourceNuget.id, url: 'https://nuget.local/api' })
|
2019-02-22 09:34:01 +00:00
|
|
|
).toMatchSnapshot();
|
2019-05-24 15:40:39 +00:00
|
|
|
});
|
|
|
|
it('matches on domainName', () => {
|
|
|
|
add({
|
|
|
|
domainName: 'github.com',
|
|
|
|
token: 'def',
|
|
|
|
});
|
2019-02-22 09:34:01 +00:00
|
|
|
expect(
|
2020-03-01 07:01:12 +00:00
|
|
|
find({ hostType: datasourceNuget.id, url: 'https://api.github.com' })
|
2020-02-06 12:15:54 +00:00
|
|
|
.token
|
2019-05-24 15:40:39 +00:00
|
|
|
).toEqual('def');
|
|
|
|
});
|
|
|
|
it('matches on hostName', () => {
|
|
|
|
add({
|
|
|
|
hostName: 'nuget.local',
|
|
|
|
token: 'abc',
|
|
|
|
});
|
|
|
|
expect(
|
2020-03-01 07:01:12 +00:00
|
|
|
find({ hostType: datasourceNuget.id, url: 'https://nuget.local/api' })
|
2019-02-22 09:34:01 +00:00
|
|
|
).toMatchSnapshot();
|
|
|
|
});
|
2019-05-24 15:40:39 +00:00
|
|
|
it('matches on hostType and endpoint', () => {
|
|
|
|
add({
|
2020-03-01 07:01:12 +00:00
|
|
|
hostType: datasourceNuget.id,
|
2019-05-24 15:40:39 +00:00
|
|
|
baseUrl: 'https://nuget.local/api',
|
2019-05-20 04:15:06 +00:00
|
|
|
token: 'abc',
|
2019-02-22 09:34:01 +00:00
|
|
|
});
|
|
|
|
expect(
|
2020-03-01 07:01:12 +00:00
|
|
|
find({ hostType: datasourceNuget.id, url: 'https://nuget.local/api' })
|
2020-02-06 12:15:54 +00:00
|
|
|
.token
|
2019-05-24 15:40:39 +00:00
|
|
|
).toEqual('abc');
|
2019-02-22 09:34:01 +00:00
|
|
|
});
|
|
|
|
it('matches on endpoint subresource', () => {
|
2019-05-24 15:40:39 +00:00
|
|
|
add({
|
2020-03-01 07:01:12 +00:00
|
|
|
hostType: datasourceNuget.id,
|
2019-05-24 15:40:39 +00:00
|
|
|
baseUrl: 'https://nuget.local/api',
|
2019-05-20 04:15:06 +00:00
|
|
|
token: 'abc',
|
2019-02-22 09:34:01 +00:00
|
|
|
});
|
|
|
|
expect(
|
|
|
|
find({
|
2020-03-01 07:01:12 +00:00
|
|
|
hostType: datasourceNuget.id,
|
2019-05-24 15:40:39 +00:00
|
|
|
url: 'https://nuget.local/api/sub-resource',
|
2019-02-22 09:34:01 +00:00
|
|
|
})
|
|
|
|
).toMatchSnapshot();
|
|
|
|
});
|
2019-05-24 15:40:39 +00:00
|
|
|
it('returns hosts', () => {
|
|
|
|
add({
|
2020-03-01 07:01:12 +00:00
|
|
|
hostType: datasourceNuget.id,
|
2019-05-24 15:40:39 +00:00
|
|
|
token: 'aaaaaa',
|
|
|
|
});
|
|
|
|
add({
|
2020-03-01 07:01:12 +00:00
|
|
|
hostType: datasourceNuget.id,
|
2019-05-24 15:40:39 +00:00
|
|
|
baseUrl: 'https://nuget.local/api',
|
|
|
|
token: 'abc',
|
|
|
|
});
|
|
|
|
add({
|
2020-03-01 07:01:12 +00:00
|
|
|
hostType: datasourceNuget.id,
|
2019-05-24 15:40:39 +00:00
|
|
|
hostName: 'my.local.registry',
|
|
|
|
token: 'def',
|
|
|
|
});
|
|
|
|
const res = hosts({
|
2020-03-01 07:01:12 +00:00
|
|
|
hostType: datasourceNuget.id,
|
2019-05-24 15:40:39 +00:00
|
|
|
});
|
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res).toHaveLength(2);
|
|
|
|
});
|
2018-07-06 05:26:36 +00:00
|
|
|
});
|
2020-02-11 13:37:35 +00:00
|
|
|
describe('findAll()', () => {
|
|
|
|
it('warns and returns empty for bad search', () => {
|
|
|
|
expect(findAll({ abc: 'def' } as any)).toEqual([]);
|
|
|
|
});
|
|
|
|
it('needs exact host matches', () => {
|
|
|
|
const hostRule = {
|
|
|
|
hostType: 'nuget',
|
|
|
|
hostName: 'nuget.org',
|
|
|
|
username: 'root',
|
|
|
|
password: 'p4$$w0rd',
|
|
|
|
token: undefined,
|
|
|
|
};
|
|
|
|
add(hostRule);
|
|
|
|
expect(findAll({ hostType: 'nuget' })).toHaveLength(1);
|
|
|
|
expect(findAll({ hostType: 'nuget' })[0]).toEqual(hostRule);
|
|
|
|
});
|
|
|
|
});
|
2018-07-06 05:26:36 +00:00
|
|
|
});
|