Skip to content

Commit

Permalink
Add tests for fetchInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
kpollich committed Sep 22, 2022
1 parent 607014a commit 00cbadf
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion x-pack/plugins/fleet/server/services/epm/registry/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

import { loggingSystemMock } from '@kbn/core-logging-server-mocks';

import { PackageNotFoundError } from '../../../errors';
import { PackageNotFoundError, RegistryResponseError } from '../../../errors';

import * as Archive from '../archive';

import {
splitPkgKey,
fetchFindLatestPackageOrUndefined,
fetchFindLatestPackageOrThrow,
fetchInfo,
getLicensePath,
} from '.';

Expand All @@ -21,6 +24,11 @@ const mockLogger = mockLoggerFactory.get('mock logger');

const mockGetBundledPackageByName = jest.fn();
const mockFetchUrl = jest.fn();

const MockArchive = Archive as jest.Mocked<typeof Archive>;

jest.mock('../archive');

jest.mock('../..', () => ({
appContextService: {
getLogger: () => mockLogger,
Expand Down Expand Up @@ -150,6 +158,10 @@ describe('fetch package', () => {
});

describe('getLicensePath', () => {
beforeEach(() => {
MockArchive.getPathParts = jest.requireActual('../archive').getPathParts;
});

it('returns first license path if found', () => {
const path = getLicensePath([
'/package/good-1.0.0/NOTICE.txt',
Expand All @@ -171,3 +183,40 @@ describe('getLicensePath', () => {
expect(path).toEqual(undefined);
});
});

describe('fetchInfo', () => {
beforeEach(() => {
jest.resetAllMocks();

mockFetchUrl.mockRejectedValueOnce(new RegistryResponseError('Not found', 404));
mockGetBundledPackageByName.mockResolvedValueOnce({
name: 'test-package',
version: '1.0.0',
buffer: Buffer.from(''),
});
MockArchive.generatePackageInfoFromArchiveBuffer.mockResolvedValueOnce({
paths: [],
packageInfo: {
name: 'test-package',
title: 'Test Package',
version: '1.0.0',
description: 'Test package',
owner: { github: 'elastic' },
format_version: '1.0.0',
},
});
});

it('falls back to bundled package when one exists', async () => {
const fetchedInfo = await fetchInfo('test-package', '1.0.0');
expect(fetchedInfo).toBeTruthy();
});

it('throws when no corresponding bundled package exists', async () => {
try {
await fetchInfo('test-package', '1.0.0');
} catch (e) {
expect(e).toBeInstanceOf(PackageNotFoundError);
}
});
});

0 comments on commit 00cbadf

Please sign in to comment.