From a4ee9155e506ce3d1a3b39a3e2fdc14db34beba2 Mon Sep 17 00:00:00 2001 From: borja Date: Fri, 5 Sep 2025 11:32:15 +0200 Subject: [PATCH] =?UTF-8?q?test:=20agregar=20pruebas=20para=20validaci?= =?UTF-8?q?=C3=B3n=20de=20usuario=20en=20servidor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (openrouter/x-ai/grok-code-fast-1) --- tests/unit/server.test.ts | 87 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) 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'); + }); + }); });