|
1 | 1 | import { TestClient } from 'test/test-client'; |
2 | | -import { APIKeyPermissionType } from './api-key.entity'; |
| 2 | +import { APIKeyPermissionTarget, APIKeyPermissionType } from './api-key.entity'; |
3 | 3 |
|
4 | | -describe('APIKeyController (e2e)', () => { |
| 4 | +process.env.OBB_WIZARD_BASE_URL = 'http://localhost:8000'; |
| 5 | + |
| 6 | +// Mock the WizardAPIService to avoid needing the actual wizard service during tests |
| 7 | +jest.mock('../wizard/api.wizard.service', () => { |
| 8 | + return { |
| 9 | + WizardAPIService: jest.fn().mockImplementation(() => ({ |
| 10 | + request: jest.fn().mockResolvedValue({ success: true }), |
| 11 | + proxy: jest.fn().mockResolvedValue({ success: true }), |
| 12 | + search: jest.fn().mockResolvedValue({ results: [] }), |
| 13 | + })), |
| 14 | + }; |
| 15 | +}); |
| 16 | + |
| 17 | +// Mock fetch for any direct fetch calls |
| 18 | +global.fetch = jest.fn().mockResolvedValue({ |
| 19 | + ok: true, |
| 20 | + json: jest.fn().mockResolvedValue({ success: true }), |
| 21 | + text: jest.fn().mockResolvedValue('success'), |
| 22 | + status: 200, |
| 23 | + body: { |
| 24 | + getReader: jest.fn().mockReturnValue({ |
| 25 | + read: jest |
| 26 | + .fn() |
| 27 | + .mockResolvedValueOnce({ |
| 28 | + done: false, |
| 29 | + value: new TextEncoder().encode( |
| 30 | + 'data: {"response_type":"bos","role":"assistant","id":"test-msg-id"}\n', |
| 31 | + ), |
| 32 | + }) |
| 33 | + .mockResolvedValueOnce({ |
| 34 | + done: false, |
| 35 | + value: new TextEncoder().encode( |
| 36 | + 'data: {"response_type":"delta","message":{"content":"Hello"}}\n', |
| 37 | + ), |
| 38 | + }) |
| 39 | + .mockResolvedValueOnce({ |
| 40 | + done: false, |
| 41 | + value: new TextEncoder().encode('data: {"response_type":"eos"}\n'), |
| 42 | + }) |
| 43 | + .mockResolvedValueOnce({ |
| 44 | + done: false, |
| 45 | + value: new TextEncoder().encode('data: {"response_type":"done"}\n'), |
| 46 | + }) |
| 47 | + .mockResolvedValueOnce({ done: true, value: undefined }), |
| 48 | + cancel: jest.fn(), |
| 49 | + }), |
| 50 | + }, |
| 51 | +}) as jest.MockedFunction<typeof fetch>; |
| 52 | + |
| 53 | +describe('OpenAPIKeyController (e2e)', () => { |
5 | 54 | let client: TestClient; |
| 55 | + let chatApiKeyValue: string; |
| 56 | + let noChatApiKeyValue: string; |
6 | 57 |
|
7 | 58 | beforeAll(async () => { |
8 | 59 | client = await TestClient.create(); |
| 60 | + |
| 61 | + // Create an API key with CHAT CREATE permissions for testing |
| 62 | + const chatApiKeyData = { |
| 63 | + user_id: client.user.id, |
| 64 | + namespace_id: client.namespace.id, |
| 65 | + attrs: { |
| 66 | + root_resource_id: client.namespace.root_resource_id, |
| 67 | + permissions: [ |
| 68 | + { |
| 69 | + target: APIKeyPermissionTarget.CHAT, |
| 70 | + permissions: [APIKeyPermissionType.CREATE], |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + }; |
| 75 | + |
| 76 | + const chatResponse = await client |
| 77 | + .post('/api/v1/api-keys') |
| 78 | + .send(chatApiKeyData) |
| 79 | + .expect(201); |
| 80 | + |
| 81 | + chatApiKeyValue = chatResponse.body.value; |
| 82 | + |
| 83 | + // Create an API key without CHAT permissions for negative testing |
| 84 | + const noChatApiKeyData = { |
| 85 | + user_id: client.user.id, |
| 86 | + namespace_id: client.namespace.id, |
| 87 | + attrs: { |
| 88 | + root_resource_id: client.namespace.root_resource_id, |
| 89 | + permissions: [ |
| 90 | + { |
| 91 | + target: APIKeyPermissionTarget.RESOURCES, |
| 92 | + permissions: [APIKeyPermissionType.READ], |
| 93 | + }, |
| 94 | + ], |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + const noChatResponse = await client |
| 99 | + .post('/api/v1/api-keys') |
| 100 | + .send(noChatApiKeyData) |
| 101 | + .expect(201); |
| 102 | + |
| 103 | + noChatApiKeyValue = noChatResponse.body.value; |
9 | 104 | }); |
10 | 105 |
|
11 | 106 | afterAll(async () => { |
|
0 commit comments