the-tip-top-backend/test/integration/draw.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

121 lines
3.4 KiB
JavaScript

/**
* Tests d'intégration pour les endpoints de tirage au sort
*/
import request from 'supertest';
import app from '../../index.js';
describe('Draw API', () => {
describe('GET /api/draw/eligible-participants', () => {
it('should reject request without authentication', async () => {
const res = await request(app).get('/api/draw/eligible-participants');
expect(res.statusCode).toBe(401);
expect(res.body.success).toBe(false);
});
it('should reject request with non-admin token', async () => {
const res = await request(app)
.get('/api/draw/eligible-participants')
.set('Authorization', 'Bearer invalid-token');
expect(res.statusCode).toBe(401);
});
});
describe('GET /api/draw/check-existing', () => {
it('should reject request without authentication', async () => {
const res = await request(app).get('/api/draw/check-existing');
expect(res.statusCode).toBe(401);
});
});
describe('POST /api/draw/conduct', () => {
it('should reject request without authentication', async () => {
const res = await request(app)
.post('/api/draw/conduct')
.send({
criteria: {},
prizeName: 'Grand Prize',
prizeValue: 1000,
});
expect(res.statusCode).toBe(401);
});
});
describe('GET /api/draw/history', () => {
it('should reject request without authentication', async () => {
const res = await request(app).get('/api/draw/history');
expect(res.statusCode).toBe(401);
});
});
describe('PUT /api/draw/:id/notify', () => {
it('should reject request without authentication', async () => {
const res = await request(app).put('/api/draw/1/notify');
expect(res.statusCode).toBe(401);
});
});
describe('PUT /api/draw/:id/claim', () => {
it('should reject request without authentication', async () => {
const res = await request(app).put('/api/draw/1/claim');
expect(res.statusCode).toBe(401);
});
});
describe('GET /api/draw/:id/report', () => {
it('should reject request without authentication', async () => {
const res = await request(app).get('/api/draw/1/report');
expect(res.statusCode).toBe(401);
});
});
describe('DELETE /api/draw/:id', () => {
it('should reject request without authentication', async () => {
const res = await request(app).delete('/api/draw/1');
expect(res.statusCode).toBe(401);
});
});
});
describe('Draw Business Logic', () => {
describe('Eligibility Criteria', () => {
it('should define valid eligibility rules', () => {
// A participant is eligible if they have at least one CLAIMED ticket
const eligibilityRules = {
minClaimedTickets: 1,
ticketStatus: 'CLAIMED',
};
expect(eligibilityRules.minClaimedTickets).toBeGreaterThan(0);
expect(eligibilityRules.ticketStatus).toBe('CLAIMED');
});
});
describe('Random Selection', () => {
it('should select one winner from eligible participants', () => {
const participants = [
{ id: 1, name: 'User 1' },
{ id: 2, name: 'User 2' },
{ id: 3, name: 'User 3' },
];
const selectWinner = (participants) => {
const randomIndex = Math.floor(Math.random() * participants.length);
return participants[randomIndex];
};
const winner = selectWinner(participants);
expect(participants).toContainEqual(winner);
});
});
});