the-tip-top-backend/test/integration/newsletter.test.js
soufiane 614abeb196 test: add comprehensive unit and integration tests
Backend Tests Added:
- Unit tests for helpers.js (tokens, validation, pagination)
- Unit tests for middleware (auth, errorHandler, validate)
- Integration tests for auth endpoints
- Integration tests for game endpoints
- Integration tests for admin endpoints
- Integration tests for employee endpoints
- Integration tests for draw endpoints
- Integration tests for newsletter/contact endpoints

Also added:
- cross-env for Windows compatibility
- Test scripts update

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 11:23:43 +01:00

120 lines
3.3 KiB
JavaScript

/**
* Tests d'intégration pour les endpoints newsletter et contact
*/
import request from 'supertest';
import app from '../../index.js';
describe('Newsletter API', () => {
describe('POST /api/newsletter/subscribe', () => {
it('should reject subscription with invalid email', async () => {
const res = await request(app)
.post('/api/newsletter/subscribe')
.send({ email: 'invalid-email' });
expect(res.statusCode).toBe(400);
expect(res.body.success).toBe(false);
});
it('should reject subscription with missing email', async () => {
const res = await request(app)
.post('/api/newsletter/subscribe')
.send({});
expect(res.statusCode).toBe(400);
expect(res.body.success).toBe(false);
});
});
describe('POST /api/newsletter/unsubscribe', () => {
it('should reject unsubscription with invalid email', async () => {
const res = await request(app)
.post('/api/newsletter/unsubscribe')
.send({ email: 'invalid-email' });
expect(res.statusCode).toBe(400);
expect(res.body.success).toBe(false);
});
it('should reject unsubscription with missing email', async () => {
const res = await request(app)
.post('/api/newsletter/unsubscribe')
.send({});
expect(res.statusCode).toBe(400);
expect(res.body.success).toBe(false);
});
});
describe('GET /api/newsletter/subscribers', () => {
it('should reject request without authentication', async () => {
const res = await request(app).get('/api/newsletter/subscribers');
expect(res.statusCode).toBe(401);
});
});
describe('GET /api/newsletter/count', () => {
it('should reject request without authentication', async () => {
const res = await request(app).get('/api/newsletter/count');
expect(res.statusCode).toBe(401);
});
});
});
describe('Contact API', () => {
describe('POST /api/contact', () => {
it('should reject contact form with missing fields', async () => {
const res = await request(app)
.post('/api/contact')
.send({
name: 'John Doe',
});
expect(res.statusCode).toBe(400);
});
it('should reject contact form with invalid email', async () => {
const res = await request(app)
.post('/api/contact')
.send({
name: 'John Doe',
email: 'invalid-email',
subject: 'Test Subject',
message: 'Test message',
});
expect(res.statusCode).toBe(400);
});
});
});
describe('Health Check Endpoints', () => {
describe('GET /', () => {
it('should return 200 with API status', async () => {
const res = await request(app).get('/');
expect(res.statusCode).toBe(200);
expect(res.body).toHaveProperty('message');
});
});
describe('GET /db-check', () => {
it('should return database status', async () => {
const res = await request(app).get('/db-check');
// May return 200 or 500 depending on DB connection
expect([200, 500]).toContain(res.statusCode);
});
});
describe('GET /metrics', () => {
it('should return Prometheus metrics', async () => {
const res = await request(app).get('/metrics');
expect(res.statusCode).toBe(200);
expect(res.headers['content-type']).toContain('text/plain');
});
});
});