diff --git a/veterans/app/api/sendEmail/route.test.ts b/veterans/app/api/sendEmail/route.test.ts index 44c4f023..9574729f 100644 --- a/veterans/app/api/sendEmail/route.test.ts +++ b/veterans/app/api/sendEmail/route.test.ts @@ -1,9 +1,17 @@ +jest.mock('nodemailer', () => ({ + createTransport: jest.fn().mockReturnValue({ + sendMail: jest.fn().mockResolvedValue({ messageId: 'test-id' }), + }), +})); + jest.mock('next/server', () => ({ NextResponse: { - json: jest.fn().mockImplementation((body, { status }) => ({ - status, - json: async () => body, - })), + json: jest.fn().mockImplementation((body, { status }) => { + return new Response(JSON.stringify(body), { + status, + headers: { 'Content-Type': 'application/json' }, + }); + }), }, })); @@ -14,12 +22,6 @@ describe('POST /api/sendEmail', () => { }); it('should return error if "to" is missing', async () => { - jest.doMock('resend', () => ({ - Resend: jest.fn(() => ({ - emails: { send: jest.fn() }, - })), - })); - const { POST } = await import('./route'); const req = { json: jest.fn().mockResolvedValue({ to: '', message: 'Hello' }), @@ -33,12 +35,6 @@ describe('POST /api/sendEmail', () => { }); it('should send an email successfully and return a success response', async () => { - jest.doMock('resend', () => ({ - Resend: jest.fn(() => ({ - emails: { send: jest.fn().mockResolvedValue({ status: 'success' }) }, - })), - })); - const { POST } = await import('./route'); const req = { json: jest @@ -51,35 +47,26 @@ describe('POST /api/sendEmail', () => { expect(response.status).toBe(200); expect(jsonResponse.message).toBe('Email sent successfully!'); + expect(jsonResponse.response).toHaveProperty('messageId', 'test-id'); }); - describe('Error Handling', () => { - beforeEach(() => { - jest.resetModules(); - jest.clearAllMocks(); + it('should return a 500 error if sending email fails', async () => { + const nodemailer = await import('nodemailer'); + (nodemailer.createTransport as jest.Mock).mockReturnValueOnce({ + sendMail: jest.fn().mockRejectedValue(new Error('Test error message')), }); - it('should return a 500 error if sending email fails', async () => { - jest.doMock('resend', () => ({ - Resend: jest.fn(() => ({ - emails: { - send: jest.fn().mockRejectedValue(new Error('Test error message')), - }, - })), - })); - - const { POST } = await import('./route'); - const req = { - json: jest - .fn() - .mockResolvedValue({ to: 'test@example.com', message: 'Hello' }), - } as unknown as Request; + const { POST } = await import('./route'); + const req = { + json: jest + .fn() + .mockResolvedValue({ to: 'test@example.com', message: 'Hello' }), + } as unknown as Request; - const response = await POST(req); - const jsonResponse = await response.json(); + const response = await POST(req); + const jsonResponse = await response.json(); - expect(response.status).toBe(500); - expect(jsonResponse.error).toBe('Test error message'); - }); + expect(response.status).toBe(500); + expect(jsonResponse.error).toBe('Test error message'); }); }); diff --git a/veterans/app/api/sendEmail/route.ts b/veterans/app/api/sendEmail/route.ts index c21dc15d..5294338d 100644 --- a/veterans/app/api/sendEmail/route.ts +++ b/veterans/app/api/sendEmail/route.ts @@ -1,7 +1,5 @@ import { NextResponse } from 'next/server'; -import { Resend } from 'resend'; - -const resend = new Resend(process.env.RESEND_API_KEY); +import nodemailer from 'nodemailer'; export async function POST(req: Request) { const { to, message } = await req.json(); @@ -14,13 +12,25 @@ export async function POST(req: Request) { } try { - const response = await resend.emails.send({ - from: 'RAZOM ', - to: [to], - subject: 'Your Verification Code', - text: message, + const transporter = nodemailer.createTransport({ + service: 'gmail', + port: 465, + secure: true, + auth: { + user: process.env.GMAIL_USER, + pass: process.env.GMAIL_PASSWORD, + }, }); + const mailOptions = { + from: process.env.GMAIL_USER, + to, + subject: 'Verify Your Email Address', + text: message, + }; + + const response = await transporter.sendMail(mailOptions); + return NextResponse.json( { message: 'Email sent successfully!', response }, { status: 200 }, diff --git a/veterans/app/layout.tsx b/veterans/app/layout.tsx index 60f370c2..fecb8958 100644 --- a/veterans/app/layout.tsx +++ b/veterans/app/layout.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import { Toaster } from 'react-hot-toast'; import { Lato, Golos_Text } from 'next/font/google'; import 'styles/global.css'; @@ -51,6 +52,7 @@ export default function RootLayout({ +
{children}