mirror of
https://github.com/Ravinou/borgwarehouse
synced 2026-03-14 22:35:46 +01:00
40 lines
1.3 KiB
TypeScript
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' });
|
|
});
|
|
});
|