|
| 1 | +// @ts-expect-error bun:test é reconhecido apenas pelo runner do Bun |
| 2 | +import { describe, it, expect, beforeEach, vi } from 'bun:test'; |
| 3 | +import { MetaObject } from '@imports/model/MetaObject'; |
| 4 | +import queueManager from '@imports/queue/QueueManager'; |
| 5 | + |
| 6 | +// Mock WhatsApp module BEFORE importing delivery |
| 7 | +const mockSendOtpViaWhatsApp = vi.fn(); |
| 8 | +vi.mock('@imports/auth/otp/whatsapp', () => ({ |
| 9 | + sendOtpViaWhatsApp: mockSendOtpViaWhatsApp, |
| 10 | +})); |
| 11 | + |
| 12 | +// Now import after mock |
| 13 | +import { sendOtp, DeliveryResult } from '@imports/auth/otp/delivery'; |
| 14 | + |
| 15 | +describe('OTP Delivery Service', () => { |
| 16 | + const mockUser = { |
| 17 | + _id: 'test-user-id', |
| 18 | + name: 'Test User', |
| 19 | + emails: [{ address: 'test@example.com' }], |
| 20 | + }; |
| 21 | + |
| 22 | + const mockOtpRequest = { |
| 23 | + _id: 'test-otp-id', |
| 24 | + expiresAt: new Date(Date.now() + 5 * 60 * 1000), |
| 25 | + }; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + vi.clearAllMocks(); |
| 29 | + |
| 30 | + // Mock MetaObject.Collections |
| 31 | + if (MetaObject.Collections == null) { |
| 32 | + (MetaObject as any).Collections = {}; |
| 33 | + } |
| 34 | + (MetaObject.Collections.User as any) = { |
| 35 | + findOne: vi.fn().mockResolvedValue(mockUser), |
| 36 | + }; |
| 37 | + (MetaObject.Collections.Message as any) = { |
| 38 | + insertOne: vi.fn().mockResolvedValue({}), |
| 39 | + }; |
| 40 | + |
| 41 | + // Mock Namespace with WhatsApp config |
| 42 | + (MetaObject.Namespace as any) = { |
| 43 | + otpConfig: { |
| 44 | + expirationMinutes: 5, |
| 45 | + emailTemplateId: 'email/otp.html', |
| 46 | + emailFrom: 'test@example.com', |
| 47 | + whatsapp: { |
| 48 | + accessToken: 'test-token', |
| 49 | + phoneNumberId: 'test-phone-id', |
| 50 | + templateId: 'test-template-id', |
| 51 | + }, |
| 52 | + }, |
| 53 | + }; |
| 54 | + }); |
| 55 | + |
| 56 | + describe('sendOtp - Fallback chain', () => { |
| 57 | + it('should try WhatsApp first, then RabbitMQ, then Email', async () => { |
| 58 | + // WhatsApp fails |
| 59 | + mockSendOtpViaWhatsApp.mockResolvedValue({ success: false, error: 'WhatsApp API error' }); |
| 60 | + |
| 61 | + // RabbitMQ fails - configure QueueConfig |
| 62 | + const mockSendMessage = vi.fn().mockResolvedValue({ success: false }); |
| 63 | + (queueManager.sendMessage as any) = mockSendMessage; |
| 64 | + |
| 65 | + (MetaObject.Namespace as any).otpConfig = { |
| 66 | + ...MetaObject.Namespace.otpConfig, |
| 67 | + rabbitmqQueue: 'otp-queue', |
| 68 | + }; |
| 69 | + (MetaObject.Namespace as any).QueueConfig = { |
| 70 | + resources: { |
| 71 | + rabbitmq: { |
| 72 | + type: 'rabbitmq', |
| 73 | + url: 'amqp://localhost', |
| 74 | + queues: [{ name: 'otp-queue' }], |
| 75 | + }, |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + const result = await sendOtp('+5511999999999', undefined, '123456', 'test-user-id', mockOtpRequest.expiresAt); |
| 80 | + |
| 81 | + expect(mockSendOtpViaWhatsApp).toHaveBeenCalled(); |
| 82 | + expect(mockSendMessage).toHaveBeenCalled(); |
| 83 | + expect(MetaObject.Collections.Message.insertOne).toHaveBeenCalled(); |
| 84 | + expect(result.success).toBe(true); |
| 85 | + expect(result.method).toBe('email'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should succeed on WhatsApp if available', async () => { |
| 89 | + mockSendOtpViaWhatsApp.mockResolvedValue({ success: true }); |
| 90 | + |
| 91 | + const result = await sendOtp('+5511999999999', undefined, '123456', 'test-user-id', mockOtpRequest.expiresAt); |
| 92 | + |
| 93 | + expect(mockSendOtpViaWhatsApp).toHaveBeenCalled(); |
| 94 | + expect(result.success).toBe(true); |
| 95 | + expect(result.method).toBe('whatsapp'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should fallback to RabbitMQ if WhatsApp fails', async () => { |
| 99 | + mockSendOtpViaWhatsApp.mockResolvedValue({ success: false, error: 'WhatsApp unavailable' }); |
| 100 | + const mockSendMessage = vi.fn().mockResolvedValue({ success: true }); |
| 101 | + (queueManager.sendMessage as any) = mockSendMessage; |
| 102 | + |
| 103 | + (MetaObject.Namespace as any).otpConfig = { |
| 104 | + expirationMinutes: 5, |
| 105 | + emailTemplateId: 'email/otp.html', |
| 106 | + emailFrom: 'test@example.com', |
| 107 | + whatsapp: { |
| 108 | + accessToken: 'test-token', |
| 109 | + phoneNumberId: 'test-phone-id', |
| 110 | + templateId: 'test-template-id', |
| 111 | + }, |
| 112 | + rabbitmqQueue: 'otp-queue', |
| 113 | + }; |
| 114 | + (MetaObject.Namespace as any).QueueConfig = { |
| 115 | + resources: { |
| 116 | + rabbitmq: { |
| 117 | + type: 'rabbitmq', |
| 118 | + url: 'amqp://localhost', |
| 119 | + queues: [{ name: 'otp-queue' }], |
| 120 | + }, |
| 121 | + }, |
| 122 | + }; |
| 123 | + |
| 124 | + const result = await sendOtp('+5511999999999', undefined, '123456', 'test-user-id', mockOtpRequest.expiresAt); |
| 125 | + |
| 126 | + expect(mockSendOtpViaWhatsApp).toHaveBeenCalled(); |
| 127 | + expect(mockSendMessage).toHaveBeenCalled(); |
| 128 | + expect(result.success).toBe(true); |
| 129 | + expect(result.method).toBe('rabbitmq'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should return error if all delivery methods fail', async () => { |
| 133 | + mockSendOtpViaWhatsApp.mockResolvedValue({ success: false, error: 'WhatsApp error' }); |
| 134 | + (queueManager.sendMessage as any) = vi.fn().mockResolvedValue({ success: false }); |
| 135 | + (MetaObject.Collections.Message.insertOne as any) = vi.fn().mockRejectedValue(new Error('Email error')); |
| 136 | + |
| 137 | + const result = await sendOtp('+5511999999999', undefined, '123456', 'test-user-id', mockOtpRequest.expiresAt); |
| 138 | + |
| 139 | + expect(result.success).toBe(false); |
| 140 | + expect(result.error).toContain('All delivery methods failed'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should handle user without email gracefully', async () => { |
| 144 | + mockSendOtpViaWhatsApp.mockResolvedValue({ success: false, error: 'WhatsApp error' }); |
| 145 | + (queueManager.sendMessage as any) = vi.fn().mockResolvedValue({ success: false }); |
| 146 | + |
| 147 | + (MetaObject.Collections.User.findOne as any) = vi.fn().mockResolvedValue({ |
| 148 | + ...mockUser, |
| 149 | + emails: [], |
| 150 | + }); |
| 151 | + |
| 152 | + const result = await sendOtp('+5511999999999', undefined, '123456', 'test-user-id', mockOtpRequest.expiresAt); |
| 153 | + |
| 154 | + expect(result.success).toBe(false); |
| 155 | + expect(result.error).toContain('User does not have an email address'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should send directly via email when requested by email', async () => { |
| 159 | + (MetaObject.Collections.Message.insertOne as any) = vi.fn().mockResolvedValue({}); |
| 160 | + |
| 161 | + const result = await sendOtp(undefined, 'user@example.com', '123456', 'test-user-id', mockOtpRequest.expiresAt); |
| 162 | + |
| 163 | + expect(mockSendOtpViaWhatsApp).not.toHaveBeenCalled(); |
| 164 | + expect(MetaObject.Collections.Message.insertOne).toHaveBeenCalled(); |
| 165 | + expect(result.success).toBe(true); |
| 166 | + expect(result.method).toBe('email'); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments