IT4C.dev/backend/src/server.spec.ts
Ulf Gebhardt 73f51b8bc4
feat(other): backend - mail api for it4c (#231)
* backend - mail api for it4c

Implements an fastify backend service with an email service.
This allows to send us emails received via contact form on the website.

* optional telephone

* missing text delimiter

* start command and correct build method to classicjs

* deploy for backend & adjust README.md

* debug deploy [1]

* debug deploy [2]

* debug deploy [3]

* debug deploy [4]

* debug deploy [5]

* finish deploy script

* watch when running npm run dev

* fix format validation

* debug sendmail[1]

* debug sendmail[2]

* debug sendmail[3]

* debug sendmail[4]

* debug sendmail[5]

* env for MAIL_HOST

* referece name in email subject

* fix format string

* eslint

* backend build & lint workflows

* order comments

* unit tests

* unit test workflow

* prettier

* alias paths

* fix esm support

* 100% tests

* corrected nodejs version

* use beforeEach to clearAllMocks

This simplifies the code and reduces redundancy

* fix wrong import
2025-03-07 10:32:56 +01:00

122 lines
3.1 KiB
TypeScript

// eslint-disable-next-line import/no-namespace
import * as nodemailer from 'nodemailer'
import { env } from './env'
import { createServer } from './server'
// Mock nodemailer
jest.mock('nodemailer')
const mockCreateTransport = nodemailer.createTransport as unknown as jest.Mock
const mockSendMail = jest.fn()
mockCreateTransport.mockReturnValue({
sendMail: mockSendMail,
})
const server = createServer(env)
describe('HTTP Server', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('GET / should return 404', async () => {
const response = await server.inject({
method: 'GET',
url: '/',
})
expect(response.statusCode).toBe(404)
})
test('POST /mail without body should return 400 Bad Request', async () => {
const response = await server.inject({
method: 'POST',
url: '/mail',
})
expect(response.statusCode).toBe(400)
expect(JSON.parse(response.body)).toEqual({
success: false,
error: 'Bad Request',
})
})
test('POST /mail with body should return 200 Success', async () => {
const response = await server.inject({
method: 'POST',
url: '/mail',
body: {
name: 'Peter Lustig',
email: 'peter@lustig.de',
text: 'This is my request',
telephone: '+420 55555 55555',
},
})
expect(mockSendMail).toHaveBeenCalledWith({
from: '"Peter Lustig" <peter@lustig.de>',
subject: '[IT4C] Received EMail from Peter Lustig',
text: 'This is my request\n\nTelephone: +420 55555 55555',
to: 'admin@it4c.dev',
})
expect(response.statusCode).toBe(200)
expect(JSON.parse(response.body)).toEqual({
success: true,
})
})
test('POST /mail without telephone should return 200 Success', async () => {
const response = await server.inject({
method: 'POST',
url: '/mail',
body: {
name: 'Peter Lustig',
email: 'peter@lustig.de',
text: 'This is my request',
},
})
expect(mockSendMail).toHaveBeenCalledWith({
from: '"Peter Lustig" <peter@lustig.de>',
subject: '[IT4C] Received EMail from Peter Lustig',
text: 'This is my request',
to: 'admin@it4c.dev',
})
expect(response.statusCode).toBe(200)
expect(JSON.parse(response.body)).toEqual({
success: true,
})
})
test('POST /mail with body and mail delivery failure should return 400', async () => {
mockSendMail.mockImplementationOnce(() => {
throw new Error('Mail Failure')
})
const response = await server.inject({
method: 'POST',
url: '/mail',
body: {
name: 'Peter Lustig',
email: 'peter@lustig.de',
text: 'This is my request',
telephone: '+420 55555 55555',
},
})
expect(mockSendMail).toHaveBeenCalledWith({
from: '"Peter Lustig" <peter@lustig.de>',
subject: '[IT4C] Received EMail from Peter Lustig',
text: 'This is my request\n\nTelephone: +420 55555 55555',
to: 'admin@it4c.dev',
})
expect(response.statusCode).toBe(400)
expect(JSON.parse(response.body)).toEqual({
error: 'Error: Mail Failure',
success: false,
})
})
})