renovate/lib/datasource/hex/index.spec.ts

175 lines
5.2 KiB
TypeScript

import { getPkgReleases } from '..';
import * as httpMock from '../../../test/http-mock';
import { hostRules, loadJsonFixture } from '../../../test/util';
import { EXTERNAL_HOST_ERROR } from '../../constants/error-messages';
import { HexDatasource } from '.';
const certifiResponse = loadJsonFixture('certifi.json');
const privatePackageResponse = loadJsonFixture('private_package.json');
jest.mock('../../util/host-rules');
const baseUrl = 'https://hex.pm/api';
const datasource = HexDatasource.id;
describe('datasource/hex/index', () => {
beforeEach(() => {
hostRules.hosts.mockReturnValue([]);
hostRules.find.mockReturnValue({});
});
afterEach(() => {
jest.resetAllMocks();
});
describe('getReleases', () => {
it('returns null for empty result', async () => {
httpMock
.scope(baseUrl)
.get('/packages/non_existent_package')
.reply(200, null);
expect(
await getPkgReleases({
datasource,
depName: 'non_existent_package',
})
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for missing fields', async () => {
httpMock
.scope(baseUrl)
.get('/packages/non_existent_package')
.reply(200, {});
expect(
await getPkgReleases({
datasource,
depName: 'non_existent_package',
})
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for 404', async () => {
httpMock.scope(baseUrl).get('/packages/some_package').reply(404);
expect(
await getPkgReleases({ datasource, depName: 'some_package' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for 401', async () => {
httpMock.scope(baseUrl).get('/packages/some_package').reply(401);
expect(
await getPkgReleases({ datasource, depName: 'some_package' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 429', async () => {
httpMock.scope(baseUrl).get('/packages/some_crate').reply(429);
await expect(
getPkgReleases({ datasource, depName: 'some_crate' })
).rejects.toThrow(EXTERNAL_HOST_ERROR);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 5xx', async () => {
httpMock.scope(baseUrl).get('/packages/some_crate').reply(502);
await expect(
getPkgReleases({ datasource, depName: 'some_crate' })
).rejects.toThrow(EXTERNAL_HOST_ERROR);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for unknown error', async () => {
httpMock.scope(baseUrl).get('/packages/some_package').replyWithError('');
expect(
await getPkgReleases({ datasource, depName: 'some_package' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null with wrong auth token', async () => {
httpMock
.scope(baseUrl, {
reqheaders: {
authorization: 'abc',
},
})
.get('/packages/certifi')
.reply(401);
hostRules.find.mockReturnValueOnce({
authType: 'Token-Only',
token: 'abc',
});
const res = await getPkgReleases({
datasource,
depName: 'certifi',
});
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('processes real data', async () => {
httpMock
.scope(baseUrl)
.get('/packages/certifi')
.reply(200, certifiResponse);
const res = await getPkgReleases({
datasource,
depName: 'certifi',
});
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('process public repo without auth', async () => {
httpMock
.scope(baseUrl)
.get('/packages/certifi')
.reply(200, certifiResponse);
hostRules.find.mockReturnValueOnce({});
const res = await getPkgReleases({
datasource,
depName: 'certifi',
});
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('processes a private repo with auth', async () => {
httpMock
.scope(baseUrl, {
reqheaders: {
authorization: 'abc',
},
})
.get('/repos/renovate_test/packages/private_package')
.reply(200, privatePackageResponse);
hostRules.find.mockReturnValueOnce({
authType: 'Token-Only',
token: 'abc',
});
const result = await getPkgReleases({
datasource,
depName: 'private_package:renovate_test',
});
expect(result).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
expect(result).toEqual({
homepage: 'https://hex.pm/packages/renovate_test/private_package',
registryUrl: 'https://hex.pm/',
releases: [
{ releaseTimestamp: '2021-08-04T15:26:26.500Z', version: '0.1.0' },
{ releaseTimestamp: '2021-08-04T17:46:00.274Z', version: '0.1.1' },
],
});
});
});
});