Skip to content

Commit

Permalink
Migrating existing connectors.spec.ts test to deployment-agnostic test
Browse files Browse the repository at this point in the history
  • Loading branch information
arturoliduena committed Jan 3, 2025
1 parent 6612952 commit 1dc1987
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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 expect from '@kbn/expect';
import { ToolingLog } from '@kbn/tooling-log';
import { Agent } from 'supertest';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';

export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) {
const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantApi');
const supertest = getService('supertest');
const log = getService('log');

const CONNECTOR_API_URL = '/internal/observability_ai_assistant/connectors';

describe('List connectors', () => {
before(async () => {
await deleteAllActionConnectors(supertest);
});

after(async () => {
await deleteAllActionConnectors(supertest);
});

it('Returns a 2xx for enterprise license', async () => {
const { status } = await observabilityAIAssistantAPIClient.editor({
endpoint: `GET ${CONNECTOR_API_URL}`,
});

expect(status).to.be(200);
});

it('returns an empty list of connectors', async () => {
const res = await observabilityAIAssistantAPIClient.editor({
endpoint: `GET ${CONNECTOR_API_URL}`,
});

expect(res.body.length).to.be(0);
});

it("returns the gen ai connector if it's been created", async () => {
const connectorId = await createProxyActionConnector({ supertest, log, port: 1234 });

const res = await observabilityAIAssistantAPIClient.editor({
endpoint: `GET ${CONNECTOR_API_URL}`,
});

expect(res.body.length).to.be(1);

await deleteActionConnector({ supertest, connectorId, log });
});
});
}

async function deleteAllActionConnectors(supertest: Agent): Promise<any> {
const res = await supertest.get(`/api/actions/connectors`);

const body = res.body as Array<{ id: string; connector_type_id: string; name: string }>;
return Promise.all(
body.map(({ id }) => {
return supertest.delete(`/api/actions/connector/${id}`).set('kbn-xsrf', 'foo');
})
);
}

async function deleteActionConnector({
supertest,
connectorId,
log,
}: {
supertest: Agent;
connectorId: string;
log: ToolingLog;
}) {
try {
await supertest
.delete(`/api/actions/connector/${connectorId}`)
.set('kbn-xsrf', 'foo')
.expect(204);
} catch (e) {
log.error(`Failed to delete action connector with id ${connectorId} due to: ${e}`);
throw e;
}
}

async function createProxyActionConnector({
log,
supertest,
port,
}: {
log: ToolingLog;
supertest: Agent;
port: number;
}) {
try {
const res = await supertest
.post('/api/actions/connector')
.set('kbn-xsrf', 'foo')
.send({
name: 'OpenAI Proxy',
connector_type_id: '.gen-ai',
config: {
apiProvider: 'OpenAI',
apiUrl: `http://localhost:${port}`,
},
secrets: {
apiKey: 'my-api-key',
},
})
.expect(200);

const connectorId = res.body.id as string;
return connectorId;
} catch (e) {
log.error(`Failed to create action connector due to: ${e}`);
throw e;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function aiAssistantApiIntegrationTests({
loadTestFile,
}: DeploymentAgnosticFtrProviderContext) {
describe('observability AI Assistant', function () {
loadTestFile(require.resolve('./conversations'));
loadTestFile(require.resolve('./conversations/conversations.spec.ts'));
loadTestFile(require.resolve('./connectors/connectors.spec.ts'));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import expect from '@kbn/expect';
import type { Agent as SuperTestAgent } from 'supertest';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import { createProxyActionConnector, deleteActionConnector } from '../../common/action_connectors';
import { ForbiddenApiError } from '../../common/config';

export default function ApiTest({ getService }: FtrProviderContext) {
const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantAPIClient');
const supertest = getService('supertest');
const log = getService('log');

const CONNECTOR_API_URL = '/internal/observability_ai_assistant/connectors';

Expand All @@ -27,34 +25,6 @@ export default function ApiTest({ getService }: FtrProviderContext) {
await deleteAllActionConnectors(supertest);
});

it('Returns a 2xx for enterprise license', async () => {
await observabilityAIAssistantAPIClient
.editor({
endpoint: `GET ${CONNECTOR_API_URL}`,
})
.expect(200);
});

it('returns an empty list of connectors', async () => {
const res = await observabilityAIAssistantAPIClient.editor({
endpoint: `GET ${CONNECTOR_API_URL}`,
});

expect(res.body.length).to.be(0);
});

it("returns the gen ai connector if it's been created", async () => {
const connectorId = await createProxyActionConnector({ supertest, log, port: 1234 });

const res = await observabilityAIAssistantAPIClient.editor({
endpoint: `GET ${CONNECTOR_API_URL}`,
});

expect(res.body.length).to.be(1);

await deleteActionConnector({ supertest, connectorId, log });
});

describe('security roles and access privileges', () => {
it('should deny access for users without the ai_assistant privilege', async () => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* 2.0.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import { createProxyActionConnector, deleteActionConnector } from '../../common/action_connectors';
import type {
InternalRequestHeader,
RoleCredentials,
Expand Down Expand Up @@ -44,46 +42,6 @@ export default function ApiTest({ getService }: FtrProviderContext) {
await svlUserManager.invalidateM2mApiKeyWithRoleScope(roleAuthc);
});

it('Returns a 2xx for enterprise license', async () => {
await observabilityAIAssistantAPIClient
.slsEditor({
endpoint: `GET /internal/observability_ai_assistant/connectors`,
})
.expect(200);
});

it('returns an empty list of connectors', async () => {
const res = await observabilityAIAssistantAPIClient.slsEditor({
endpoint: `GET /internal/observability_ai_assistant/connectors`,
});

expect(res.body.length).to.be(0);
});

it("returns the gen ai connector if it's been created", async () => {
const connectorId = await createProxyActionConnector({
supertest: supertestWithoutAuth,
log,
port: 1234,
internalReqHeader,
roleAuthc,
});

const res = await observabilityAIAssistantAPIClient.slsEditor({
endpoint: `GET /internal/observability_ai_assistant/connectors`,
});

expect(res.body.length).to.be(1);

await deleteActionConnector({
supertest: supertestWithoutAuth,
connectorId,
log,
internalReqHeader,
roleAuthc,
});
});

describe('security roles and access privileges', () => {
it('should deny access for users without the ai_assistant privilege', async () => {
await observabilityAIAssistantAPIClient
Expand Down

0 comments on commit 1dc1987

Please sign in to comment.