Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Nov 27, 2023
1 parent 9e8d2b6 commit 75dae6b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ describe('EditOutputFlyout', () => {
});

it('should render the flyout if the output provided is a remote ES output', async () => {
jest.spyOn(ExperimentalFeaturesService, 'get').mockReturnValue({ remoteESOutput: true });
jest
.spyOn(ExperimentalFeaturesService, 'get')
.mockReturnValue({ remoteESOutput: true, outputSecretsStorage: true });
const { utils } = renderFlyout({
type: 'remote_elasticsearch',
name: 'remote es output',
Expand All @@ -208,6 +210,8 @@ describe('EditOutputFlyout', () => {
expect(utils.queryByTestId('settingsOutputsFlyout.typeInput')?.textContent).toContain(
'Remote Elasticsearch'
);

expect(utils.queryByTestId('serviceTokenSecretInput')).not.toBeNull();
});

it('should not display remote ES output in type lists if serverless', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const OutputFormRemoteEsSection: React.FunctionComponent<Props> = (props)
onUsePlainText={onUsePlainText}
>
<EuiFieldText
data-test-subj="serviceTokenSecretInput"
fullWidth
{...inputs.serviceTokenSecretInput.props}
placeholder={i18n.translate(
Expand Down
37 changes: 37 additions & 0 deletions x-pack/plugins/fleet/server/routes/output/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,41 @@ describe('output handler', () => {

expect(res).toEqual({ body: { item: { id: 'output1' } } });
});

it('should return error if both service_token and secrets.service_token is provided for remote_elasticsearch output', async () => {
jest
.spyOn(appContextService, 'getCloud')
.mockReturnValue({ isServerlessEnabled: false } as any);

const res = await postOutputHandler(
mockContext,
{
body: {
type: 'remote_elasticsearch',
service_token: 'token1',
secrets: { service_token: 'token2' },
},
} as any,
mockResponse as any
);

expect(res).toEqual({
body: { message: 'Cannot specify both service_token and secrets.service_token' },
statusCode: 400,
});
});

it('should return ok if one of service_token and secrets.service_token is provided for remote_elasticsearch output', async () => {
jest
.spyOn(appContextService, 'getCloud')
.mockReturnValue({ isServerlessEnabled: false } as any);

const res = await postOutputHandler(
mockContext,
{ body: { type: 'remote_elasticsearch', secrets: { service_token: 'token2' } } } as any,
mockResponse as any
);

expect(res).toEqual({ body: { item: { id: 'output1' } } });
});
});
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/types/models/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const ElasticSearchUpdateSchema = {
export const RemoteElasticSearchSchema = {
...ElasticSearchSchema,
type: schema.literal(outputType.RemoteElasticsearch),
service_token: schema.string(),
service_token: schema.maybe(schema.string()),
secrets: schema.maybe(
schema.object({
service_token: schema.maybe(secretRefSchema),
Expand Down
17 changes: 17 additions & 0 deletions x-pack/test/fleet_api_integration/apis/outputs/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,23 @@ export default function (providerContext: FtrProviderContext) {
// @ts-ignore _source unknown type
expect(secret._source.value).to.equal('pass');
});

it('should create service_token secret correctly', async function () {
const res = await supertest
.post(`/api/fleet/outputs`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'Remote Elasticsearch With Service Token Secret',
type: 'remote_elasticsearch',
hosts: ['https://remote-es:9200'],
secrets: { service_token: 'token' },
});

const secretId = res.body.item.secrets.service_token.id;
const secret = await getSecretById(secretId);
// @ts-ignore _source unknown type
expect(secret._source.value).to.equal('token');
});
});

describe('DELETE /outputs/{outputId}', () => {
Expand Down

0 comments on commit 75dae6b

Please sign in to comment.