From 4cfde64fe26a675582c8796b843a37cb27e48889 Mon Sep 17 00:00:00 2001 From: Viduni Wickramarachchi Date: Wed, 4 Dec 2024 08:31:11 -0500 Subject: [PATCH] [Obs AI Assistant] Update tests to definetely throw an error --- .../common/config.ts | 10 ++++++++++ .../tests/chat/chat.spec.ts | 2 ++ .../tests/complete/complete.spec.ts | 2 ++ .../tests/connectors/connectors.spec.ts | 2 ++ .../tests/conversations/conversations.spec.ts | 16 ++++++++++++++++ .../tests/knowledge_base/knowledge_base.spec.ts | 10 ++++++++++ .../knowledge_base/knowledge_base_setup.spec.ts | 2 ++ .../knowledge_base/knowledge_base_status.spec.ts | 2 ++ .../knowledge_base_user_instructions.spec.ts | 7 +++++++ 9 files changed, 53 insertions(+) diff --git a/x-pack/test/observability_ai_assistant_api_integration/common/config.ts b/x-pack/test/observability_ai_assistant_api_integration/common/config.ts index 455004e5339f..6505ad3e94d6 100644 --- a/x-pack/test/observability_ai_assistant_api_integration/common/config.ts +++ b/x-pack/test/observability_ai_assistant_api_integration/common/config.ts @@ -33,6 +33,16 @@ export type ObservabilityAIAssistantAPIClient = Awaited< export type ObservabilityAIAssistantServices = Awaited>['services']; +export class ForbiddenApiError extends Error { + status: number; + + constructor(message: string = 'Forbidden') { + super(message); + this.name = 'ForbiddenApiError'; + this.status = 403; + } +} + export function createObservabilityAIAssistantAPIConfig({ config, license, diff --git a/x-pack/test/observability_ai_assistant_api_integration/tests/chat/chat.spec.ts b/x-pack/test/observability_ai_assistant_api_integration/tests/chat/chat.spec.ts index c0a3da475e0c..cedd4c286dc1 100644 --- a/x-pack/test/observability_ai_assistant_api_integration/tests/chat/chat.spec.ts +++ b/x-pack/test/observability_ai_assistant_api_integration/tests/chat/chat.spec.ts @@ -11,6 +11,7 @@ import { PassThrough } from 'stream'; import { createLlmProxy, LlmProxy } from '../../common/create_llm_proxy'; 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 supertest = getService('supertest'); @@ -200,6 +201,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { }, }, }); + throw new ForbiddenApiError('Expected unauthorizedUser() to throw a 403 Forbidden error'); } catch (e) { expect(e.status).to.be(403); } diff --git a/x-pack/test/observability_ai_assistant_api_integration/tests/complete/complete.spec.ts b/x-pack/test/observability_ai_assistant_api_integration/tests/complete/complete.spec.ts index 5e59eed3a8c2..86e357e2e776 100644 --- a/x-pack/test/observability_ai_assistant_api_integration/tests/complete/complete.spec.ts +++ b/x-pack/test/observability_ai_assistant_api_integration/tests/complete/complete.spec.ts @@ -32,6 +32,7 @@ import { getConversationUpdatedEvent, } from '../conversations/helpers'; import { createProxyActionConnector, deleteActionConnector } from '../../common/action_connectors'; +import { ForbiddenApiError } from '../../common/config'; export default function ApiTest({ getService }: FtrProviderContext) { const supertest = getService('supertest'); @@ -502,6 +503,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { }, }, }); + throw new ForbiddenApiError('Expected unauthorizedUser() to throw a 403 Forbidden error'); } catch (e) { expect(e.status).to.be(403); } diff --git a/x-pack/test/observability_ai_assistant_api_integration/tests/connectors/connectors.spec.ts b/x-pack/test/observability_ai_assistant_api_integration/tests/connectors/connectors.spec.ts index 84b044d4b84a..42e1f8751719 100644 --- a/x-pack/test/observability_ai_assistant_api_integration/tests/connectors/connectors.spec.ts +++ b/x-pack/test/observability_ai_assistant_api_integration/tests/connectors/connectors.spec.ts @@ -9,6 +9,7 @@ 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'); @@ -60,6 +61,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { await observabilityAIAssistantAPIClient.unauthorizedUser({ endpoint: `GET ${CONNECTOR_API_URL}`, }); + throw new ForbiddenApiError('Expected unauthorizedUser() to throw a 403 Forbidden error'); } catch (e) { expect(e.status).to.be(403); } diff --git a/x-pack/test/observability_ai_assistant_api_integration/tests/conversations/conversations.spec.ts b/x-pack/test/observability_ai_assistant_api_integration/tests/conversations/conversations.spec.ts index 3db3be8ec671..bb85e99b9950 100644 --- a/x-pack/test/observability_ai_assistant_api_integration/tests/conversations/conversations.spec.ts +++ b/x-pack/test/observability_ai_assistant_api_integration/tests/conversations/conversations.spec.ts @@ -14,6 +14,7 @@ import { } from '@kbn/observability-ai-assistant-plugin/common/types'; import type { FtrProviderContext } from '../../common/ftr_provider_context'; import type { SupertestReturnType } from '../../common/observability_ai_assistant_api_client'; +import { ForbiddenApiError } from '../../common/config'; export default function ApiTest({ getService }: FtrProviderContext) { const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantAPIClient'); @@ -292,6 +293,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { }, }, }); + throw new ForbiddenApiError( + 'Expected unauthorizedUser() to throw a 403 Forbidden error' + ); } catch (e) { expect(e.status).to.be(403); } @@ -302,6 +306,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { await observabilityAIAssistantAPIClient.unauthorizedUser({ endpoint: 'POST /internal/observability_ai_assistant/conversations', }); + throw new ForbiddenApiError( + 'Expected unauthorizedUser() to throw a 403 Forbidden error' + ); } catch (e) { expect(e.status).to.be(403); } @@ -322,6 +329,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { }, }, }); + throw new ForbiddenApiError( + 'Expected unauthorizedUser() to throw a 403 Forbidden error' + ); } catch (e) { expect(e.status).to.be(403); } @@ -337,6 +347,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { }, }, }); + throw new ForbiddenApiError( + 'Expected unauthorizedUser() to throw a 403 Forbidden error' + ); } catch (e) { expect(e.status).to.be(403); } @@ -352,6 +365,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { }, }, }); + throw new ForbiddenApiError( + 'Expected unauthorizedUser() to throw a 403 Forbidden error' + ); } catch (e) { expect(e.status).to.be(403); } diff --git a/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base.spec.ts b/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base.spec.ts index 73d53ffa6d1f..9d80db3baeae 100644 --- a/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base.spec.ts +++ b/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base.spec.ts @@ -15,6 +15,7 @@ import { deleteInferenceEndpoint, deleteKnowledgeBaseModel, } from './helpers'; +import { ForbiddenApiError } from '../../common/config'; export default function ApiTest({ getService }: FtrProviderContext) { const ml = getService('ml'); @@ -225,6 +226,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { }, }, }); + throw new ForbiddenApiError( + 'Expected unauthorizedUser() to throw a 403 Forbidden error' + ); } catch (e) { expect(e.status).to.be(403); } @@ -238,6 +242,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { query: { query: '', sortBy: 'title', sortDirection: 'asc' }, }, }); + throw new ForbiddenApiError( + 'Expected unauthorizedUser() to throw a 403 Forbidden error' + ); } catch (e) { expect(e.status).to.be(403); } @@ -251,6 +258,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { path: { entryId: 'my-doc-id-1' }, }, }); + throw new ForbiddenApiError( + 'Expected unauthorizedUser() to throw a 403 Forbidden error' + ); } catch (e) { expect(e.status).to.be(403); } diff --git a/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_setup.spec.ts b/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_setup.spec.ts index 8220c4edc9c6..0d7625bb63ed 100644 --- a/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_setup.spec.ts +++ b/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_setup.spec.ts @@ -13,6 +13,7 @@ import { TINY_ELSER, deleteInferenceEndpoint, } from './helpers'; +import { ForbiddenApiError } from '../../common/config'; export default function ApiTest({ getService }: FtrProviderContext) { const ml = getService('ml'); @@ -74,6 +75,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { }, }, }); + throw new ForbiddenApiError('Expected unauthorizedUser() to throw a 403 Forbidden error'); } catch (e) { expect(e.status).to.be(403); } diff --git a/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_status.spec.ts b/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_status.spec.ts index 7ff78751d474..3f66931ca071 100644 --- a/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_status.spec.ts +++ b/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_status.spec.ts @@ -13,6 +13,7 @@ import { TINY_ELSER, deleteInferenceEndpoint, } from './helpers'; +import { ForbiddenApiError } from '../../common/config'; export default function ApiTest({ getService }: FtrProviderContext) { const ml = getService('ml'); @@ -89,6 +90,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { await observabilityAIAssistantAPIClient.unauthorizedUser({ endpoint: `GET ${KNOWLEDGE_BASE_STATUS_API_URL}`, }); + throw new ForbiddenApiError('Expected unauthorizedUser() to throw a 403 Forbidden error'); } catch (e) { expect(e.status).to.be(403); } diff --git a/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_user_instructions.spec.ts b/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_user_instructions.spec.ts index dbb3fa1fada0..d5022a052d78 100644 --- a/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_user_instructions.spec.ts +++ b/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_user_instructions.spec.ts @@ -23,6 +23,7 @@ import { getConversationCreatedEvent } from '../conversations/helpers'; import { LlmProxy, createLlmProxy } from '../../common/create_llm_proxy'; import { createProxyActionConnector, deleteActionConnector } from '../../common/action_connectors'; import { User } from '../../common/users/users'; +import { ForbiddenApiError } from '../../common/config'; export default function ApiTest({ getService }: FtrProviderContext) { const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantAPIClient'); @@ -377,6 +378,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { }, }, }); + throw new ForbiddenApiError( + 'Expected unauthorizedUser() to throw a 403 Forbidden error' + ); } catch (e) { expect(e.status).to.be(403); } @@ -387,6 +391,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { await observabilityAIAssistantAPIClient.unauthorizedUser({ endpoint: 'GET /internal/observability_ai_assistant/kb/user_instructions', }); + throw new ForbiddenApiError( + 'Expected unauthorizedUser() to throw a 403 Forbidden error' + ); } catch (e) { expect(e.status).to.be(403); }