2023-09-08 05:40:04 -06:00
|
|
|
import { mockDeep } from 'jest-mock-extended';
|
2020-12-11 05:29:43 -07:00
|
|
|
import * as httpMock from '../../../../test/http-mock';
|
2021-08-17 23:46:56 -06:00
|
|
|
import { mocked } from '../../../../test/util';
|
2020-05-08 23:55:00 -06:00
|
|
|
import * as _hostRules from '../../../util/host-rules';
|
2022-02-26 02:16:54 -07:00
|
|
|
import { toBase64 } from '../../../util/string';
|
2021-04-06 07:49:06 -06:00
|
|
|
import { PRESET_INVALID_JSON, PRESET_NOT_FOUND } from '../util';
|
2020-05-08 23:55:00 -06:00
|
|
|
import * as github from '.';
|
2020-02-27 06:19:58 -07:00
|
|
|
|
2023-09-08 05:40:04 -06:00
|
|
|
jest.mock('../../../util/host-rules', () => mockDeep());
|
2020-02-27 06:19:58 -07:00
|
|
|
|
2020-04-15 03:38:52 -06:00
|
|
|
const hostRules = mocked(_hostRules);
|
2020-02-27 06:19:58 -07:00
|
|
|
|
2020-06-09 02:13:52 -06:00
|
|
|
const githubApiHost = github.Endpoint;
|
2020-06-07 05:00:49 -06:00
|
|
|
const basePath = '/repos/some/repo/contents';
|
|
|
|
|
2021-08-17 23:46:56 -06:00
|
|
|
describe('config/presets/github/index', () => {
|
2020-04-13 23:05:30 -06:00
|
|
|
beforeEach(() => {
|
2020-06-07 05:00:49 -06:00
|
|
|
hostRules.find.mockReturnValue({ token: 'abc' });
|
2020-04-13 23:05:30 -06:00
|
|
|
});
|
2020-06-07 05:00:49 -06:00
|
|
|
|
2020-04-28 23:00:53 -06:00
|
|
|
describe('fetchJSONFile()', () => {
|
|
|
|
it('returns JSON', async () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/some-filename.json`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('{"from":"api"}'),
|
2020-06-07 05:00:49 -06:00
|
|
|
});
|
|
|
|
|
2020-04-28 23:00:53 -06:00
|
|
|
const res = await github.fetchJSONFile(
|
|
|
|
'some/repo',
|
2020-06-07 05:00:49 -06:00
|
|
|
'some-filename.json',
|
2021-11-05 07:12:47 -06:00
|
|
|
githubApiHost,
|
2023-11-07 08:50:29 -07:00
|
|
|
undefined,
|
2020-04-28 23:00:53 -06:00
|
|
|
);
|
2021-08-09 02:21:51 -06:00
|
|
|
expect(res).toEqual({ from: 'api' });
|
2020-04-28 23:00:53 -06:00
|
|
|
});
|
|
|
|
});
|
2020-05-09 01:04:39 -06:00
|
|
|
|
2020-02-27 06:19:58 -07:00
|
|
|
describe('getPreset()', () => {
|
|
|
|
it('tries default then renovate', async () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/default.json`)
|
2020-07-02 23:05:17 -06:00
|
|
|
.reply(404, {})
|
2020-06-07 05:00:49 -06:00
|
|
|
.get(`${basePath}/renovate.json`)
|
2020-07-02 23:05:17 -06:00
|
|
|
.reply(200, {});
|
2020-06-07 05:00:49 -06:00
|
|
|
|
2022-02-28 09:39:44 -07:00
|
|
|
await expect(github.getPreset({ repo: 'some/repo' })).rejects.toThrow();
|
2020-02-27 06:19:58 -07:00
|
|
|
});
|
2020-06-07 05:00:49 -06:00
|
|
|
|
2022-04-29 02:34:55 -06:00
|
|
|
it('throws if invalid content', async () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/default.json`)
|
2022-04-29 02:34:55 -06:00
|
|
|
.reply(200, { content: toBase64('invalid') });
|
2020-06-07 05:00:49 -06:00
|
|
|
|
2022-02-28 09:39:44 -07:00
|
|
|
await expect(github.getPreset({ repo: 'some/repo' })).rejects.toThrow(
|
2023-11-07 08:50:29 -07:00
|
|
|
PRESET_INVALID_JSON,
|
2022-02-28 09:39:44 -07:00
|
|
|
);
|
2020-02-27 06:19:58 -07:00
|
|
|
});
|
2020-06-07 05:00:49 -06:00
|
|
|
|
2020-02-27 06:19:58 -07:00
|
|
|
it('throws if fails to parse', async () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/default.json`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('not json'),
|
2020-06-07 05:00:49 -06:00
|
|
|
});
|
|
|
|
|
2022-02-28 09:39:44 -07:00
|
|
|
await expect(github.getPreset({ repo: 'some/repo' })).rejects.toThrow(
|
2023-11-07 08:50:29 -07:00
|
|
|
PRESET_INVALID_JSON,
|
2022-02-28 09:39:44 -07:00
|
|
|
);
|
2020-02-27 06:19:58 -07:00
|
|
|
});
|
2020-06-07 05:00:49 -06:00
|
|
|
|
2020-02-27 06:19:58 -07:00
|
|
|
it('should return default.json', async () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/default.json`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('{"foo":"bar"}'),
|
2020-06-07 05:00:49 -06:00
|
|
|
});
|
|
|
|
|
2022-02-28 09:39:44 -07:00
|
|
|
const content = await github.getPreset({ repo: 'some/repo' });
|
2020-02-27 06:19:58 -07:00
|
|
|
expect(content).toEqual({ foo: 'bar' });
|
|
|
|
});
|
2020-06-07 05:00:49 -06:00
|
|
|
|
2020-04-28 03:41:56 -06:00
|
|
|
it('should query preset within the file', async () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/somefile.json`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('{"somename":{"foo":"bar"}}'),
|
2020-06-07 05:00:49 -06:00
|
|
|
});
|
2020-05-09 01:04:39 -06:00
|
|
|
const content = await github.getPreset({
|
2022-02-28 09:39:44 -07:00
|
|
|
repo: 'some/repo',
|
2020-05-09 01:04:39 -06:00
|
|
|
presetName: 'somefile/somename',
|
|
|
|
});
|
2020-04-28 03:41:56 -06:00
|
|
|
expect(content).toEqual({ foo: 'bar' });
|
|
|
|
});
|
2020-06-07 05:00:49 -06:00
|
|
|
|
2022-05-02 08:34:09 -06:00
|
|
|
it('should query preset within the file when .json extension provided', async () => {
|
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/somefile.json`)
|
|
|
|
.reply(200, {
|
|
|
|
content: toBase64('{"foo":"bar"}'),
|
|
|
|
});
|
|
|
|
const content = await github.getPreset({
|
|
|
|
repo: 'some/repo',
|
|
|
|
presetName: 'somefile.json',
|
|
|
|
});
|
|
|
|
expect(content).toEqual({ foo: 'bar' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should query preset within the file when .json5 extension provided', async () => {
|
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/somefile.json5`)
|
|
|
|
.reply(200, {
|
|
|
|
content: toBase64('{foo:"bar"}'),
|
|
|
|
});
|
|
|
|
const content = await github.getPreset({
|
|
|
|
repo: 'some/repo',
|
|
|
|
presetName: 'somefile.json5',
|
|
|
|
});
|
|
|
|
expect(content).toEqual({ foo: 'bar' });
|
|
|
|
});
|
|
|
|
|
2020-04-28 03:41:56 -06:00
|
|
|
it('should query subpreset', async () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/somefile.json`)
|
|
|
|
.reply(200, {
|
2020-04-28 03:41:56 -06:00
|
|
|
content: Buffer.from(
|
2023-11-07 08:50:29 -07:00
|
|
|
'{"somename":{"somesubname":{"foo":"bar"}}}',
|
2020-04-28 03:41:56 -06:00
|
|
|
).toString('base64'),
|
2020-06-07 05:00:49 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
const content = await github.getPreset({
|
2022-02-28 09:39:44 -07:00
|
|
|
repo: 'some/repo',
|
2020-05-09 01:04:39 -06:00
|
|
|
presetName: 'somefile/somename/somesubname',
|
|
|
|
});
|
2020-04-28 03:41:56 -06:00
|
|
|
expect(content).toEqual({ foo: 'bar' });
|
|
|
|
});
|
2020-06-07 05:00:49 -06:00
|
|
|
|
2020-02-27 06:19:58 -07:00
|
|
|
it('should return custom.json', async () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/custom.json`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('{"foo":"bar"}'),
|
2020-06-07 05:00:49 -06:00
|
|
|
});
|
2020-07-02 23:27:00 -06:00
|
|
|
const content = await github.getPreset({
|
2022-02-28 09:39:44 -07:00
|
|
|
repo: 'some/repo',
|
2020-07-02 23:27:00 -06:00
|
|
|
presetName: 'custom',
|
|
|
|
});
|
|
|
|
expect(content).toEqual({ foo: 'bar' });
|
2020-06-07 05:00:49 -06:00
|
|
|
});
|
|
|
|
|
2021-03-20 17:03:10 -06:00
|
|
|
it('should query custom paths', async () => {
|
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/path/custom.json`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('{"foo":"bar"}'),
|
2021-03-20 17:03:10 -06:00
|
|
|
});
|
|
|
|
const content = await github.getPreset({
|
2022-02-28 09:39:44 -07:00
|
|
|
repo: 'some/repo',
|
2021-03-20 17:03:10 -06:00
|
|
|
presetName: 'custom',
|
|
|
|
presetPath: 'path',
|
|
|
|
});
|
|
|
|
expect(content).toEqual({ foo: 'bar' });
|
|
|
|
});
|
|
|
|
|
2020-06-09 02:13:52 -06:00
|
|
|
it('should throws not-found', async () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/somefile.json`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('{}'),
|
2020-06-07 05:00:49 -06:00
|
|
|
});
|
2020-07-02 23:27:00 -06:00
|
|
|
await expect(
|
|
|
|
github.getPreset({
|
2022-02-28 09:39:44 -07:00
|
|
|
repo: 'some/repo',
|
2020-07-02 23:27:00 -06:00
|
|
|
presetName: 'somefile/somename/somesubname',
|
2023-11-07 08:50:29 -07:00
|
|
|
}),
|
2020-07-02 23:27:00 -06:00
|
|
|
).rejects.toThrow(PRESET_NOT_FOUND);
|
2020-02-27 06:19:58 -07:00
|
|
|
});
|
2020-04-21 07:44:32 -06:00
|
|
|
});
|
2020-05-09 01:04:39 -06:00
|
|
|
|
2020-04-21 07:44:32 -06:00
|
|
|
describe('getPresetFromEndpoint()', () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
it('uses default endpoint', async () => {
|
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/default.json`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('{"from":"api"}'),
|
2020-06-07 05:00:49 -06:00
|
|
|
});
|
|
|
|
expect(
|
2023-11-07 08:50:29 -07:00
|
|
|
await github.getPresetFromEndpoint('some/repo', 'default', undefined),
|
2020-06-07 05:00:49 -06:00
|
|
|
).toEqual({ from: 'api' });
|
|
|
|
});
|
|
|
|
|
2020-04-13 23:05:30 -06:00
|
|
|
it('uses custom endpoint', async () => {
|
2020-06-07 05:00:49 -06:00
|
|
|
httpMock
|
|
|
|
.scope('https://api.github.example.org')
|
|
|
|
.get(`${basePath}/default.json`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('{"from":"api"}'),
|
2020-06-07 05:00:49 -06:00
|
|
|
});
|
|
|
|
expect(
|
|
|
|
await github
|
|
|
|
.getPresetFromEndpoint(
|
|
|
|
'some/repo',
|
|
|
|
'default',
|
2021-03-20 17:03:10 -06:00
|
|
|
undefined,
|
2021-11-05 07:12:47 -06:00
|
|
|
'https://api.github.example.org',
|
2023-11-07 08:50:29 -07:00
|
|
|
undefined,
|
2021-11-05 07:12:47 -06:00
|
|
|
)
|
2023-11-07 08:50:29 -07:00
|
|
|
.catch(() => ({ from: 'api' })),
|
2021-11-05 07:12:47 -06:00
|
|
|
).toEqual({ from: 'api' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses default endpoint with a tag', async () => {
|
|
|
|
httpMock
|
|
|
|
.scope(githubApiHost)
|
|
|
|
.get(`${basePath}/default.json?ref=someTag`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('{"from":"api"}'),
|
2021-11-05 07:12:47 -06:00
|
|
|
});
|
|
|
|
expect(
|
|
|
|
await github.getPresetFromEndpoint(
|
|
|
|
'some/repo',
|
|
|
|
'default',
|
|
|
|
undefined,
|
|
|
|
githubApiHost,
|
2023-11-07 08:50:29 -07:00
|
|
|
'someTag',
|
|
|
|
),
|
2021-11-05 07:12:47 -06:00
|
|
|
).toEqual({ from: 'api' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses custom endpoint with a tag', async () => {
|
|
|
|
httpMock
|
|
|
|
.scope('https://api.github.example.org')
|
|
|
|
.get(`${basePath}/default.json?ref=someTag`)
|
|
|
|
.reply(200, {
|
2022-02-26 02:16:54 -07:00
|
|
|
content: toBase64('{"from":"api"}'),
|
2021-11-05 07:12:47 -06:00
|
|
|
});
|
|
|
|
expect(
|
|
|
|
await github
|
|
|
|
.getPresetFromEndpoint(
|
|
|
|
'some/repo',
|
|
|
|
'default',
|
|
|
|
undefined,
|
|
|
|
'https://api.github.example.org',
|
2023-11-07 08:50:29 -07:00
|
|
|
'someTag',
|
2020-06-07 05:00:49 -06:00
|
|
|
)
|
2023-11-07 08:50:29 -07:00
|
|
|
.catch(() => ({ from: 'api' })),
|
2020-06-07 05:00:49 -06:00
|
|
|
).toEqual({ from: 'api' });
|
2020-04-13 23:05:30 -06:00
|
|
|
});
|
2020-02-27 06:19:58 -07:00
|
|
|
});
|
|
|
|
});
|