diff --git a/tests/unit/server.test.ts b/tests/unit/server.test.ts index 33bab3f..85e48c9 100644 --- a/tests/unit/server.test.ts +++ b/tests/unit/server.test.ts @@ -54,8 +54,8 @@ describe('WebhookServer', () => { { event: 'messages.upsert', instance: null } ]; - for (const payload of invalidPayloads) { - const request = createTestRequest(payload); + for (const invalidPayload of invalidPayloads) { + const request = createTestRequest(invalidPayload); const response = await WebhookServer.handleRequest(request); expect(response.status).toBe(400); } @@ -518,4 +518,87 @@ describe('WebhookServer', () => { expect(response.status).toBe(200); expect(mockAdd).not.toHaveBeenCalled(); }); + + // New tests for user validation + describe('User validation in handleMessageUpsert', () => { + let mockEnsureUserExists: any; + let consoleSpy: any; + + beforeEach(() => { + mockEnsureUserExists = mock(() => '1234567890'); + // Mock the function in the module + const dbModule = await import('../../src/db'); + dbModule.ensureUserExists = mockEnsureUserExists; + + consoleSpy = mock(() => {}); + console.log = consoleSpy; + }); + + afterEach(() => { + mockEnsureUserExists.mockRestore(); + consoleSpy.mockRestore(); + }); + + test('should proceed with valid user', async () => { + mockEnsureUserExists.mockReturnValue('1234567890'); + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: '1234567890@s.whatsapp.net' + }, + message: { conversation: '/tarea nueva Test' } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockEnsureUserExists).toHaveBeenCalledWith('1234567890@s.whatsapp.net'); + expect(mockAdd).toHaveBeenCalled(); + }); + + test('should ignore message if user validation fails', async () => { + mockEnsureUserExists.mockReturnValue(null); + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'invalid-user@s.whatsapp.net' + }, + message: { conversation: '/tarea nueva Test' } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockEnsureUserExists).toHaveBeenCalledWith('invalid-user@s.whatsapp.net'); + expect(mockAdd).not.toHaveBeenCalled(); + expect(consoleSpy).toHaveBeenCalledWith('⚠️ Failed to ensure user exists, ignoring message'); + }); + + test('should handle database errors during user validation', async () => { + mockEnsureUserExists.mockImplementation(() => { throw new Error('DB error'); }); + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: '1234567890@s.whatsapp.net' + }, + message: { conversation: '/tarea nueva Test' } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockEnsureUserExists).toHaveBeenCalledWith('1234567890@s.whatsapp.net'); + expect(mockAdd).not.toHaveBeenCalled(); + expect(consoleSpy).toHaveBeenCalledWith('⚠️ Failed to ensure user exists, ignoring message'); + }); + }); });