Skip to content

Commit

Permalink
Merge branch 'main' into renovate/main-@elasticcharts
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme authored Dec 12, 2023
2 parents eb335e4 + fa3b6f4 commit 5a618d8
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,20 @@ describe('SecretFormRow', () => {

expect(onUsePlainText).toHaveBeenCalled();
});

it('should not display the cancel change button when no initial value is provided', () => {
const { queryByTestId } = render(
<SecretFormRow
title={title}
clear={clear}
onUsePlainText={onUsePlainText}
cancelEdit={cancelEdit}
initialValue={''}
>
<input type="text" />
</SecretFormRow>
);

expect(queryByTestId('secretCancelChangeBtn')).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const SecretFormRow: React.FC<{
onUsePlainText,
cancelEdit,
}) => {
const hasInitialValue = initialValue !== undefined;
const hasInitialValue = !!initialValue;
const [editMode, setEditMode] = useState(!initialValue);
const valueHiddenPanel = (
<EuiPanel color="subdued" borderRadius="none" hasShadow={false}>
Expand Down Expand Up @@ -98,7 +98,7 @@ export const SecretFormRow: React.FC<{
<>
{children}
{hasInitialValue && (
<EuiFlexGroup justifyContent="flexEnd">
<EuiFlexGroup justifyContent="flexEnd" data-test-subj="secretCancelChangeBtn">
<EuiFlexItem grow={false}>{cancelButton}</EuiFlexItem>
</EuiFlexGroup>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('OutputHealth', () => {

await waitFor(async () => {
expect(utils.getByTestId('outputHealthDegradedCallout').textContent).toContain(
'Unable to connect to "Remote ES" at https://remote-es:443. Please check the details are correct.'
'Unable to connect to "Remote ES" at https://remote-es:443.Please check the details are correct.'
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ export const OutputHealth: React.FunctionComponent<Props> = ({ output, showBadge
iconType="error"
data-test-subj="outputHealthDegradedCallout"
>
<p>
<p className="eui-textBreakWord">
{i18n.translate('xpack.fleet.output.calloutText', {
defaultMessage: 'Unable to connect to "{name}" at {host}.',
values: {
name: output.name,
host: output.hosts?.join(',') ?? '',
},
})}
</p>{' '}
</p>
<p>
{i18n.translate('xpack.fleet.output.calloutPromptText', {
defaultMessage: 'Please check the details are correct.',
Expand Down
245 changes: 245 additions & 0 deletions x-pack/plugins/fleet/server/routes/epm/file_handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { loggingSystemMock } from '@kbn/core-logging-server-mocks';
import { httpServerMock } from '@kbn/core-http-server-mocks';
import { elasticsearchServiceMock } from '@kbn/core-elasticsearch-server-mocks';
import { savedObjectsClientMock } from '@kbn/core-saved-objects-api-server-mocks';
import { Headers } from 'node-fetch';

import { getBundledPackageByPkgKey } from '../../services/epm/packages/bundled_packages';
import { getFile, getInstallation } from '../../services/epm/packages/get';
import type { FleetRequestHandlerContext } from '../..';
import { appContextService } from '../../services';
import { unpackBufferEntries, getArchiveEntry } from '../../services/epm/archive';
import { getAsset } from '../../services/epm/archive/storage';

import { getFileHandler } from './file_handler';

jest.mock('../../services/app_context');
jest.mock('../../services/epm/archive');
jest.mock('../../services/epm/archive/storage');
jest.mock('../../services/epm/packages/bundled_packages');
jest.mock('../../services/epm/packages/get');

const mockedGetBundledPackageByPkgKey = jest.mocked(getBundledPackageByPkgKey);
const mockedGetInstallation = jest.mocked(getInstallation);
const mockedGetFile = jest.mocked(getFile);
const mockedGetArchiveEntry = jest.mocked(getArchiveEntry);
const mockedUnpackBufferEntries = jest.mocked(unpackBufferEntries);
const mockedGetAsset = jest.mocked(getAsset);

function mockContext() {
const mockSavedObjectsClient = savedObjectsClientMock.create();
const mockElasticsearchClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
return {
fleet: {
internalSOClient: async () => mockSavedObjectsClient,
},
core: {
savedObjects: {
client: mockSavedObjectsClient,
},
elasticsearch: {
client: {
asInternalUser: mockElasticsearchClient,
},
},
},
} as unknown as FleetRequestHandlerContext;
}

describe('getFileHandler', () => {
beforeEach(() => {
const logger = loggingSystemMock.createLogger();
jest.mocked(appContextService).getLogger.mockReturnValue(logger);
mockedGetBundledPackageByPkgKey.mockReset();
mockedUnpackBufferEntries.mockReset();
mockedGetFile.mockReset();
mockedGetInstallation.mockReset();
mockedGetArchiveEntry.mockReset();
mockedGetAsset.mockReset();
});

it('should return the file for bundled package and an existing file', async () => {
mockedGetBundledPackageByPkgKey.mockResolvedValue({
getBuffer: () => Promise.resolve(),
} as any);
const request = httpServerMock.createKibanaRequest({
params: {
pkgName: 'test',
pkgVersion: '1.0.0',
filePath: 'README.md',
},
});
const buffer = Buffer.from(`TEST`);
mockedUnpackBufferEntries.mockResolvedValue([
{
path: 'test-1.0.0/README.md',
buffer,
},
]);
const response = httpServerMock.createResponseFactory();
const context = mockContext();
await getFileHandler(context, request, response);

expect(response.custom).toBeCalledWith(
expect.objectContaining({
statusCode: 200,
body: buffer,
headers: expect.objectContaining({
'content-type': 'text/markdown; charset=utf-8',
}),
})
);
});

it('should a 404 for bundled package with a non existing file', async () => {
mockedGetBundledPackageByPkgKey.mockResolvedValue({
getBuffer: () => Promise.resolve(),
} as any);
const request = httpServerMock.createKibanaRequest({
params: {
pkgName: 'test',
pkgVersion: '1.0.0',
filePath: 'idonotexists.md',
},
});
mockedUnpackBufferEntries.mockResolvedValue([
{
path: 'test-1.0.0/README.md',
buffer: Buffer.from(`TEST`),
},
]);
const response = httpServerMock.createResponseFactory();
const context = mockContext();
await getFileHandler(context, request, response);

expect(response.custom).toBeCalledWith(
expect.objectContaining({
statusCode: 404,
body: 'bundled package file not found: idonotexists.md',
})
);
});

it('should proxy registry 200 for non bundled and non installed package', async () => {
const request = httpServerMock.createKibanaRequest({
params: {
pkgName: 'test',
pkgVersion: '1.0.0',
filePath: 'idonotexists.md',
},
});
const response = httpServerMock.createResponseFactory();
const context = mockContext();

mockedGetFile.mockResolvedValue({
status: 200,
// @ts-expect-error
body: 'test',
headers: new Headers({
raw: '',
'content-type': 'text/markdown',
}),
});

await getFileHandler(context, request, response);

expect(response.custom).toBeCalledWith(
expect.objectContaining({
statusCode: 200,
body: 'test',
headers: expect.objectContaining({
'content-type': 'text/markdown',
}),
})
);
});

it('should proxy registry 404 for non bundled and non installed package', async () => {
const request = httpServerMock.createKibanaRequest({
params: {
pkgName: 'test',
pkgVersion: '1.0.0',
filePath: 'idonotexists.md',
},
});
const response = httpServerMock.createResponseFactory();
const context = mockContext();

mockedGetFile.mockResolvedValue({
status: 404,
// @ts-expect-error
body: 'not found',
headers: new Headers({
raw: '',
'content-type': 'text',
}),
});

await getFileHandler(context, request, response);

expect(response.custom).toBeCalledWith(
expect.objectContaining({
statusCode: 404,
body: 'not found',
headers: expect.objectContaining({
'content-type': 'text',
}),
})
);
});

it('should return the file from installation for installed package', async () => {
const request = httpServerMock.createKibanaRequest({
params: {
pkgName: 'test',
pkgVersion: '1.0.0',
filePath: 'README.md',
},
});
const response = httpServerMock.createResponseFactory();
const context = mockContext();

mockedGetInstallation.mockResolvedValue({ version: '1.0.0' } as any);
mockedGetArchiveEntry.mockReturnValue(Buffer.from('test'));

await getFileHandler(context, request, response);

expect(response.custom).toBeCalledWith(
expect.objectContaining({
statusCode: 200,
headers: expect.objectContaining({
'content-type': 'text/markdown; charset=utf-8',
}),
})
);
});

it('should a 404 if the file from installation do not exists for installed package', async () => {
const request = httpServerMock.createKibanaRequest({
params: {
pkgName: 'test',
pkgVersion: '1.0.0',
filePath: 'README.md',
},
});
const response = httpServerMock.createResponseFactory();
const context = mockContext();

mockedGetInstallation.mockResolvedValue({ version: '1.0.0' } as any);
await getFileHandler(context, request, response);

expect(response.custom).toBeCalledWith(
expect.objectContaining({
statusCode: 404,
body: 'installed package file not found: README.md',
})
);
});
});
Loading

0 comments on commit 5a618d8

Please sign in to comment.