feat: harden content workflows and staging smoke tests
CI — Test & Build / 🧪 Run Tests & Generate Reports (push) Waiting to run
CI — Test & Build / 🏗️ Build Docker Image (push) Blocked by required conditions
CI — Test & Build / 🐳 Docker integration & API E2E (push) Blocked by required conditions
CI — Test & Build / 🌐 Staging Playwright smoke (push) Waiting to run
CI — Test & Build / 🧪 Run Tests & Generate Reports (push) Waiting to run
CI — Test & Build / 🏗️ Build Docker Image (push) Blocked by required conditions
CI — Test & Build / 🐳 Docker integration & API E2E (push) Blocked by required conditions
CI — Test & Build / 🌐 Staging Playwright smoke (push) Waiting to run
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
* Unit tests for Contact API route
|
||||
* These tests focus on testing the business logic without complex mocking
|
||||
*/
|
||||
import { POST } from './route'
|
||||
|
||||
const mockInsertOne = jest.fn()
|
||||
|
||||
// Mock the logger to avoid complex setup
|
||||
jest.mock('@/lib/logger', () => ({
|
||||
@@ -12,7 +15,57 @@ jest.mock('@/lib/logger', () => ({
|
||||
})
|
||||
}))
|
||||
|
||||
jest.mock('@/lib/mongodb', () => ({
|
||||
getCollection: jest.fn(async () => ({ insertOne: mockInsertOne })),
|
||||
}))
|
||||
|
||||
describe('/api/contact Unit Tests', () => {
|
||||
beforeEach(() => {
|
||||
mockInsertOne.mockReset()
|
||||
})
|
||||
|
||||
describe('MongoDB persistence', () => {
|
||||
const validData = {
|
||||
name: 'Test User',
|
||||
email: 'test@example.com',
|
||||
subject: 'Test Subject',
|
||||
message: 'This is a test message with enough content',
|
||||
gdprConsent: true,
|
||||
}
|
||||
|
||||
it('persists a valid submission before reporting success', async () => {
|
||||
mockInsertOne.mockResolvedValue({ insertedId: { toHexString: () => 'submission-123' } })
|
||||
const request = {
|
||||
headers: new Headers({ 'x-forwarded-for': 'persistence-success' }),
|
||||
json: async () => validData,
|
||||
} as any
|
||||
|
||||
const response = await POST(request)
|
||||
const body = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(body.submissionId).toBe('submission-123')
|
||||
expect(mockInsertOne).toHaveBeenCalledWith(expect.objectContaining({
|
||||
...validData,
|
||||
status: 'new',
|
||||
createdAt: expect.any(Date),
|
||||
}))
|
||||
})
|
||||
|
||||
it('returns a server error when persistence fails', async () => {
|
||||
mockInsertOne.mockRejectedValue(new Error('MongoDB unavailable'))
|
||||
const request = {
|
||||
headers: new Headers({ 'x-forwarded-for': 'persistence-failure' }),
|
||||
json: async () => validData,
|
||||
} as any
|
||||
|
||||
const response = await POST(request)
|
||||
|
||||
expect(response.status).toBe(500)
|
||||
expect(mockInsertOne).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
// TC-001: Email Format Validation Test (ZEE-48)
|
||||
describe('Input validation logic', () => {
|
||||
it('should validate required fields', () => {
|
||||
|
||||
Reference in New Issue
Block a user