borgwarehouse/tests/supertest/sendTestEmail.test.ts
2025-04-20 23:08:06 +02:00

40 lines
1.3 KiB
TypeScript

import { createMocks } from 'node-mocks-http';
import handler from '~/pages/api/account/sendTestEmail';
import { getServerSession } from 'next-auth/next';
jest.mock('next-auth/next');
jest.mock('~/helpers/functions/nodemailerSMTP', () => ({
__esModule: true,
default: jest.fn(() => ({
sendMail: jest.fn().mockResolvedValue({ messageId: 'fake-message-id' }),
})),
}));
describe('Email API', () => {
it('should return 401 if not authenticated', async () => {
// Mock unauthenticated session
(getServerSession as jest.Mock).mockResolvedValue(null);
// Simulate a POST request
const { req, res } = createMocks({ method: 'POST' });
await handler(req, res);
// Expect 401 unauthorized
expect(res._getStatusCode()).toBe(401);
});
it('should send an email if authenticated', async () => {
// Mock unauthenticated session
(getServerSession as jest.Mock).mockResolvedValue({
user: { email: 'ada-lovelace@example.com', name: 'Lovelace' },
});
// Simulate a POST request
const { req, res } = createMocks({ method: 'POST' });
await handler(req, res);
// Expect 200 and a success message
expect(res._getStatusCode()).toBe(200);
expect(res._getJSONData()).toEqual({ message: 'Mail successfully sent' });
});
});