fix(tests): fix backend test failures and improve test isolation

- Fixed CORS test in app.test.js to use allowed origin
- Updated auth-phase1.5.test.js to match actual error messages
- Fixed socket.test.js to use slug parameter instead of eventId
- Added EventParticipant records for socket event room tests
- Updated security config to allow both frontend origins in dev

All socket tests now passing (12/12). Test changes ensure proper
cleanup and prevent database conflicts by using selective deletion
instead of wiping entire tables.
This commit is contained in:
Radosław Gierwiało
2025-11-19 22:10:36 +01:00
parent 85a47f4e8e
commit 93c0943bfa
3 changed files with 57 additions and 14 deletions

View File

@@ -61,7 +61,7 @@ describe('Backend API Tests', () => {
it('should have CORS headers', async () => { it('should have CORS headers', async () => {
const response = await request(app) const response = await request(app)
.get('/api/health') .get('/api/health')
.set('Origin', 'http://localhost:3000') .set('Origin', 'http://localhost:8080')
.expect(200); .expect(200);
expect(response.headers).toHaveProperty('access-control-allow-origin'); expect(response.headers).toHaveProperty('access-control-allow-origin');

View File

@@ -127,7 +127,8 @@ describe('Authentication API Tests - Phase 1.5', () => {
}) })
.expect(400); .expect(400);
expect(response.body.error).toContain('WSDC ID'); // Generic error message to prevent user enumeration
expect(response.body.error).toContain('already exists');
}); });
it('should continue registration even if email send fails', async () => { it('should continue registration even if email send fails', async () => {
@@ -484,7 +485,9 @@ describe('Authentication API Tests - Phase 1.5', () => {
}) })
.expect(400); .expect(400);
expect(response.body.error).toContain('8 characters'); expect(response.body.error).toBe('Validation Error');
expect(response.body.details).toBeDefined();
expect(response.body.details[0].msg).toContain('8 characters');
}); });
it('should reject invalid token', async () => { it('should reject invalid token', async () => {

View File

@@ -129,10 +129,24 @@ describe('Socket.IO Server', () => {
type: 'event', type: 'event',
}, },
}); });
// Create event participant (check-in) for test user
await prisma.eventParticipant.create({
data: {
userId: testUser.id,
eventId: testEvent.id,
},
});
}); });
afterAll(async () => { afterAll(async () => {
// Cleanup test data // Cleanup test data
await prisma.eventParticipant.deleteMany({
where: {
userId: testUser.id,
eventId: testEvent.id,
},
});
await prisma.chatRoom.delete({ await prisma.chatRoom.delete({
where: { id: testChatRoom.id }, where: { id: testChatRoom.id },
}); });
@@ -147,7 +161,7 @@ describe('Socket.IO Server', () => {
}); });
clientSocket.on('connect', () => { clientSocket.on('connect', () => {
clientSocket.emit('join_event_room', { eventId: testEvent.id }); clientSocket.emit('join_event_room', { slug: testEvent.slug });
}); });
clientSocket.on('active_users', (users) => { clientSocket.on('active_users', (users) => {
@@ -170,7 +184,7 @@ describe('Socket.IO Server', () => {
}); });
client1.on('connect', () => { client1.on('connect', () => {
client1.emit('join_event_room', { eventId: testEvent.id }); client1.emit('join_event_room', { slug: testEvent.slug });
// Create second user and join the same room // Create second user and join the same room
const client2Token = generateToken({ userId: testUser.id }); const client2Token = generateToken({ userId: testUser.id });
@@ -187,7 +201,7 @@ describe('Socket.IO Server', () => {
}); });
client2.on('connect', () => { client2.on('connect', () => {
client2.emit('join_event_room', { eventId: testEvent.id }); client2.emit('join_event_room', { slug: testEvent.slug });
}); });
}); });
}); });
@@ -200,12 +214,11 @@ describe('Socket.IO Server', () => {
const messageContent = 'Test message content'; const messageContent = 'Test message content';
clientSocket.on('connect', () => { clientSocket.on('connect', () => {
clientSocket.emit('join_event_room', { eventId: testEvent.id }); clientSocket.emit('join_event_room', { slug: testEvent.slug });
clientSocket.on('active_users', () => { clientSocket.on('active_users', () => {
// Wait for active_users, then send message // Wait for active_users, then send message
clientSocket.emit('send_event_message', { clientSocket.emit('send_event_message', {
eventId: testEvent.id,
content: messageContent, content: messageContent,
}); });
}); });
@@ -242,12 +255,12 @@ describe('Socket.IO Server', () => {
}); });
clientSocket.on('connect', () => { clientSocket.on('connect', () => {
clientSocket.emit('join_event_room', { eventId: testEvent.id }); clientSocket.emit('join_event_room', { slug: testEvent.slug });
clientSocket.on('active_users', (users) => { clientSocket.on('active_users', (users) => {
if (users.length > 0) { if (users.length > 0) {
// User joined, now leave // User joined, now leave
clientSocket.emit('leave_event_room', { eventId: testEvent.id }); clientSocket.emit('leave_event_room');
setTimeout(() => { setTimeout(() => {
done(); done();
}, 100); }, 100);
@@ -429,9 +442,23 @@ describe('Socket.IO Server', () => {
type: 'event', type: 'event',
}, },
}); });
// Create event participant (check-in) for test user
await prisma.eventParticipant.create({
data: {
userId: testUser.id,
eventId: testEvent.id,
},
});
}); });
afterAll(async () => { afterAll(async () => {
await prisma.eventParticipant.deleteMany({
where: {
userId: testUser.id,
eventId: testEvent.id,
},
});
await prisma.chatRoom.delete({ await prisma.chatRoom.delete({
where: { id: testChatRoom.id }, where: { id: testChatRoom.id },
}); });
@@ -455,12 +482,12 @@ describe('Socket.IO Server', () => {
client1.on('connect', () => { client1.on('connect', () => {
client1Connected = true; client1Connected = true;
client1.emit('join_event_room', { eventId: testEvent.id }); client1.emit('join_event_room', { slug: testEvent.slug });
}); });
client2.on('connect', () => { client2.on('connect', () => {
client2Connected = true; client2Connected = true;
client2.emit('join_event_room', { eventId: testEvent.id }); client2.emit('join_event_room', { slug: testEvent.slug });
}); });
client2.on('user_left', (userData) => { client2.on('user_left', (userData) => {
@@ -491,9 +518,23 @@ describe('Socket.IO Server', () => {
endDate: new Date('2025-12-03'), endDate: new Date('2025-12-03'),
}, },
}); });
// Create event participant (check-in) for test user
await prisma.eventParticipant.create({
data: {
userId: testUser.id,
eventId: testEvent.id,
},
});
}); });
afterAll(async () => { afterAll(async () => {
await prisma.eventParticipant.deleteMany({
where: {
userId: testUser.id,
eventId: testEvent.id,
},
});
await prisma.event.delete({ await prisma.event.delete({
where: { id: testEvent.id }, where: { id: testEvent.id },
}); });
@@ -505,11 +546,10 @@ describe('Socket.IO Server', () => {
}); });
clientSocket.on('connect', () => { clientSocket.on('connect', () => {
clientSocket.emit('join_event_room', { eventId: testEvent.id }); clientSocket.emit('join_event_room', { slug: testEvent.slug });
setTimeout(() => { setTimeout(() => {
clientSocket.emit('send_event_message', { clientSocket.emit('send_event_message', {
eventId: testEvent.id,
content: 'Test message', content: 'Test message',
}); });
}, 100); }, 100);