|
| 1 | +import { jest } from '@jest/globals'; |
| 2 | +import { ChatSurfaceService } from '../transport/surfaces/chatSurfaceService.js'; |
| 3 | + |
| 4 | +const OPTIONS = { |
| 5 | + sessionResource: { |
| 6 | + resourceId: 'sessions', |
| 7 | + idField: 'id', |
| 8 | + titleField: 'title', |
| 9 | + turnsField: 'turns', |
| 10 | + askerIdField: 'asker_id', |
| 11 | + createdAtField: 'created_at', |
| 12 | + }, |
| 13 | + turnResource: { |
| 14 | + resourceId: 'turns', |
| 15 | + idField: 'id', |
| 16 | + sessionIdField: 'session_id', |
| 17 | + createdAtField: 'created_at', |
| 18 | + promptField: 'prompt', |
| 19 | + responseField: 'response', |
| 20 | + }, |
| 21 | + chatExternalIdentityResource: { |
| 22 | + resourceId: 'external_identities', |
| 23 | + surfaces: { |
| 24 | + telegram: { provider: 'AdminForthAdapterTelegramOauth2' }, |
| 25 | + }, |
| 26 | + }, |
| 27 | +} as any; |
| 28 | + |
| 29 | +const ADAPTER = { name: 'telegram' } as any; |
| 30 | +const INCOMING = { |
| 31 | + surface: 'telegram', |
| 32 | + externalUserId: 'telegram-user-1', |
| 33 | + externalConversationId: 'telegram-chat-1', |
| 34 | + prompt: 'hello', |
| 35 | +} as any; |
| 36 | + |
| 37 | +function buildRequest() { |
| 38 | + return { |
| 39 | + body: {}, |
| 40 | + query: {}, |
| 41 | + headers: {}, |
| 42 | + cookies: [], |
| 43 | + requestUrl: '/adminapi/v1/agent/surface/telegram/webhook', |
| 44 | + response: { |
| 45 | + setHeader: jest.fn(), |
| 46 | + setStatus: jest.fn(), |
| 47 | + blobStream: jest.fn(), |
| 48 | + }, |
| 49 | + } as any; |
| 50 | +} |
| 51 | + |
| 52 | +function buildService(adminUserRecord: Record<string, unknown>, authorizationHooks: any[]) { |
| 53 | + const adminforth = { |
| 54 | + config: { |
| 55 | + auth: { |
| 56 | + usersResourceId: 'admin_users', |
| 57 | + usernameField: 'email', |
| 58 | + adminUserAuthorize: authorizationHooks, |
| 59 | + }, |
| 60 | + resources: [{ |
| 61 | + resourceId: 'admin_users', |
| 62 | + columns: [{ name: 'id', primaryKey: true }], |
| 63 | + }], |
| 64 | + }, |
| 65 | + resource(resourceId: string) { |
| 66 | + if (resourceId === 'external_identities') { |
| 67 | + return { |
| 68 | + list: jest.fn().mockResolvedValue([{ |
| 69 | + provider: 'AdminForthAdapterTelegramOauth2', |
| 70 | + externalUserId: 'telegram-user-1', |
| 71 | + adminUserId: 'user-1', |
| 72 | + }]), |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + return { |
| 77 | + get: jest.fn().mockResolvedValue(adminUserRecord), |
| 78 | + }; |
| 79 | + }, |
| 80 | + }; |
| 81 | + const sessionStore = { |
| 82 | + getOrCreateChatSurfaceSession: jest.fn().mockResolvedValue('session-1'), |
| 83 | + }; |
| 84 | + const handleTurn = jest.fn().mockResolvedValue(undefined); |
| 85 | + const service = new ChatSurfaceService( |
| 86 | + () => adminforth as any, |
| 87 | + OPTIONS, |
| 88 | + sessionStore as any, |
| 89 | + handleTurn, |
| 90 | + jest.fn(), |
| 91 | + ); |
| 92 | + |
| 93 | + return { service, sessionStore, handleTurn }; |
| 94 | +} |
| 95 | + |
| 96 | +describe('adminforth-agent chat surface authorization', () => { |
| 97 | + it('blocks a deactivated user before creating an agent session', async () => { |
| 98 | + const authorizationHook = jest.fn(async ({ adminUser }) => ({ |
| 99 | + allowed: adminUser.dbUser.is_active, |
| 100 | + })); |
| 101 | + const { service, sessionStore, handleTurn } = buildService( |
| 102 | + { id: 'user-1', email: 'user@example.com', is_active: false }, |
| 103 | + [authorizationHook], |
| 104 | + ); |
| 105 | + const sink = { emit: jest.fn(), close: jest.fn() }; |
| 106 | + |
| 107 | + await service.handleMessage(ADAPTER, INCOMING, sink as any, buildRequest()); |
| 108 | + |
| 109 | + expect(authorizationHook).toHaveBeenCalledWith(expect.objectContaining({ |
| 110 | + adminUser: expect.objectContaining({ pk: 'user-1' }), |
| 111 | + extra: expect.objectContaining({ |
| 112 | + meta: { chatSurface: 'telegram' }, |
| 113 | + }), |
| 114 | + })); |
| 115 | + expect(sessionStore.getOrCreateChatSurfaceSession).not.toHaveBeenCalled(); |
| 116 | + expect(handleTurn).not.toHaveBeenCalled(); |
| 117 | + expect(sink.emit).toHaveBeenCalledWith({ |
| 118 | + type: 'error', |
| 119 | + message: 'This chat account is not authorized to use AdminForth Agent.', |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('continues to the agent when all authorization hooks allow the user', async () => { |
| 124 | + const authorizationHook = jest.fn().mockResolvedValue({ allowed: true }); |
| 125 | + const { service, sessionStore, handleTurn } = buildService( |
| 126 | + { id: 'user-1', email: 'user@example.com', is_active: true }, |
| 127 | + [authorizationHook], |
| 128 | + ); |
| 129 | + const sink = { emit: jest.fn(), close: jest.fn() }; |
| 130 | + |
| 131 | + await service.handleMessage(ADAPTER, INCOMING, sink as any, buildRequest()); |
| 132 | + |
| 133 | + expect(sessionStore.getOrCreateChatSurfaceSession).toHaveBeenCalled(); |
| 134 | + expect(handleTurn).toHaveBeenCalledWith(expect.objectContaining({ |
| 135 | + sessionId: 'session-1', |
| 136 | + chatSurface: 'telegram', |
| 137 | + adminUser: expect.objectContaining({ pk: 'user-1' }), |
| 138 | + })); |
| 139 | + }); |
| 140 | +}); |
0 commit comments