test: fix backend test cleanup to preserve production data

Replace deleteMany({}) with selective cleanup targeting only test data:

- events.test.js: Delete only test users (john_dancer, sarah_swings, mike_blues)
  and test events (test-dance-festival-2025) before creating new ones
- matches.test.js: Clean up john_dancer, sarah_swings, mike_moves and
  test-dance-festival slug specifically
- users.test.js: Remove only john_dancer and sarah_swings test users
  in both beforeAll and afterAll hooks
- auth.test.js: Target specific test usernames/emails (testuser, newuser,
  lockouttest, etc.) instead of all users
- auth-phase1.5.test.js: Clean up 12 specific test users by username/email
- socket.test.js: Add beforeAll cleanup for sockettest user to prevent
  conflicts from previous test runs
- socket-webrtc.test.js: Clean up webrtc_user1 and webrtc_user2 before
  creating them

Fix CORS configuration for tests:
- security.js: Add http://localhost:3000 to allowed origins in development
  mode to fix app.test.js CORS test (was failing with 500 error)

Results: Improved from 125/223 passing to 137/223 passing (12 more tests fixed)
All test data cleanup now uses WHERE clauses with specific usernames/emails/slugs
instead of wiping entire tables with deleteMany({})
This commit is contained in:
Radosław Gierwiało
2025-11-19 21:46:04 +01:00
parent 9d1af60f30
commit 85a47f4e8e
8 changed files with 267 additions and 39 deletions

View File

@@ -19,11 +19,44 @@ const emailService = require('../utils/email');
// Clean up database before and after tests
beforeAll(async () => {
await prisma.user.deleteMany({});
// Clean up only specific test data (not all users)
const testUsernames = ['emailtest', 'verifytest', 'codetest', 'resendtest', 'expireduser',
'resettest', 'newpasstest', 'expiredreset', 'emailfail', 'wsdcuser',
'user1', 'user2'];
const testEmails = ['emailtest@example.com', 'verify@example.com', 'code@example.com',
'resend@example.com', 'expired@example.com', 'reset@example.com',
'newpass@example.com', 'expiredreset@example.com', 'emailfail@example.com',
'wsdc@example.com', 'user1@example.com', 'user2@example.com'];
await prisma.user.deleteMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
}
});
});
afterAll(async () => {
await prisma.user.deleteMany({});
// Clean up only specific test data
const testUsernames = ['emailtest', 'verifytest', 'codetest', 'resendtest', 'expireduser',
'resettest', 'newpasstest', 'expiredreset', 'emailfail', 'wsdcuser',
'user1', 'user2'];
const testEmails = ['emailtest@example.com', 'verify@example.com', 'code@example.com',
'resend@example.com', 'expired@example.com', 'reset@example.com',
'newpass@example.com', 'expiredreset@example.com', 'emailfail@example.com',
'wsdc@example.com', 'user1@example.com', 'user2@example.com'];
await prisma.user.deleteMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
}
});
await prisma.$disconnect();
});

View File

@@ -5,11 +5,34 @@ const { hashPassword, generateToken } = require('../utils/auth');
// Clean up database before and after tests
beforeAll(async () => {
await prisma.user.deleteMany({});
// Clean up only specific test data (not all users)
const testUsernames = ['testuser', 'newuser', 'anotheruser', 'lockouttest', 'ab'];
const testEmails = ['test@example.com', 'new@example.com', 'another@example.com', 'lockout@example.com'];
await prisma.user.deleteMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
}
});
});
afterAll(async () => {
await prisma.user.deleteMany({});
// Clean up only specific test data
const testUsernames = ['testuser', 'newuser', 'anotheruser', 'lockouttest', 'ab'];
const testEmails = ['test@example.com', 'new@example.com', 'another@example.com', 'lockout@example.com'];
await prisma.user.deleteMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
}
});
await prisma.$disconnect();
});

View File

@@ -12,16 +12,61 @@ let checkinToken;
// Setup test data
beforeAll(async () => {
// Clean up
await prisma.eventUserHeat.deleteMany({});
await prisma.rating.deleteMany({});
await prisma.message.deleteMany({});
await prisma.match.deleteMany({});
await prisma.chatRoom.deleteMany({});
await prisma.eventCheckinToken.deleteMany({});
await prisma.eventParticipant.deleteMany({});
await prisma.event.deleteMany({});
await prisma.user.deleteMany({});
// Clean up only specific test data (not all tables)
const testUsernames = ['john_dancer', 'sarah_swings', 'mike_blues'];
const testEmails = ['john@example.com', 'sarah@example.com', 'mike@example.com'];
const testEventSlugs = ['test-dance-festival-2025', 'test-another-event-2025'];
// Find test users
const testUsers = await prisma.user.findMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
},
select: { id: true }
});
const testUserIds = testUsers.map(u => u.id);
// Find test events
const testEvents = await prisma.event.findMany({
where: { slug: { in: testEventSlugs } },
select: { id: true }
});
const testEventIds = testEvents.map(e => e.id);
// Delete related data for test users and events only
if (testUserIds.length > 0) {
await prisma.eventUserHeat.deleteMany({ where: { userId: { in: testUserIds } } });
await prisma.rating.deleteMany({ where: { raterId: { in: testUserIds } } });
await prisma.message.deleteMany({ where: { userId: { in: testUserIds } } });
await prisma.match.deleteMany({
where: {
OR: [
{ user1Id: { in: testUserIds } },
{ user2Id: { in: testUserIds } }
]
}
});
}
if (testEventIds.length > 0) {
await prisma.chatRoom.deleteMany({ where: { eventId: { in: testEventIds } } });
await prisma.eventCheckinToken.deleteMany({ where: { eventId: { in: testEventIds } } });
await prisma.eventParticipant.deleteMany({ where: { eventId: { in: testEventIds } } });
await prisma.event.deleteMany({ where: { id: { in: testEventIds } } });
}
// Delete test users
await prisma.user.deleteMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
}
});
// Create test users
testUser1 = await prisma.user.create({

View File

@@ -9,14 +9,59 @@ let testMatch;
// Setup test data
beforeAll(async () => {
// Clean up
await prisma.rating.deleteMany({});
await prisma.message.deleteMany({});
await prisma.match.deleteMany({});
await prisma.chatRoom.deleteMany({});
await prisma.eventParticipant.deleteMany({});
await prisma.event.deleteMany({});
await prisma.user.deleteMany({});
// Clean up only specific test data (not all tables)
const testUsernames = ['john_dancer', 'sarah_swings', 'mike_moves'];
const testEmails = ['john@example.com', 'sarah@example.com', 'mike@example.com'];
const testEventSlugs = ['test-dance-festival'];
// Find test users
const testUsers = await prisma.user.findMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
},
select: { id: true }
});
const testUserIds = testUsers.map(u => u.id);
// Find test events
const testEvents = await prisma.event.findMany({
where: { slug: { in: testEventSlugs } },
select: { id: true }
});
const testEventIds = testEvents.map(e => e.id);
// Delete related data for test users and events only
if (testUserIds.length > 0) {
await prisma.rating.deleteMany({ where: { raterId: { in: testUserIds } } });
await prisma.message.deleteMany({ where: { userId: { in: testUserIds } } });
await prisma.match.deleteMany({
where: {
OR: [
{ user1Id: { in: testUserIds } },
{ user2Id: { in: testUserIds } }
]
}
});
}
if (testEventIds.length > 0) {
await prisma.chatRoom.deleteMany({ where: { eventId: { in: testEventIds } } });
await prisma.eventParticipant.deleteMany({ where: { eventId: { in: testEventIds } } });
await prisma.event.deleteMany({ where: { id: { in: testEventIds } } });
}
// Delete test users
await prisma.user.deleteMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
}
});
// Create test users
testUser1 = await prisma.user.create({

View File

@@ -18,6 +18,16 @@ describe('Socket.IO WebRTC Signaling', () => {
let testMatch;
beforeAll(async () => {
// Clean up existing test users if any
await prisma.user.deleteMany({
where: {
OR: [
{ username: { in: ['webrtc_user1', 'webrtc_user2'] } },
{ email: { in: ['webrtc1@test.com', 'webrtc2@test.com'] } }
]
}
});
// Create test users
testUser1 = await prisma.user.create({
data: {

View File

@@ -15,6 +15,16 @@ describe('Socket.IO Server', () => {
const port = 3001;
beforeAll(async () => {
// Clean up existing test user if any
await prisma.user.deleteMany({
where: {
OR: [
{ username: 'sockettest' },
{ email: 'sockettest@example.com' }
]
}
});
// Create test user
testUser = await prisma.user.create({
data: {

View File

@@ -9,14 +9,46 @@ let testToken1, testToken2;
// Setup test data
beforeAll(async () => {
// Clean up
await prisma.rating.deleteMany({});
await prisma.message.deleteMany({});
await prisma.match.deleteMany({});
await prisma.chatRoom.deleteMany({});
await prisma.eventParticipant.deleteMany({});
await prisma.event.deleteMany({});
await prisma.user.deleteMany({});
// Clean up only specific test data (not all tables)
const testUsernames = ['john_dancer', 'sarah_swings'];
const testEmails = ['john@example.com', 'sarah@example.com'];
// Find test users
const testUsers = await prisma.user.findMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
},
select: { id: true }
});
const testUserIds = testUsers.map(u => u.id);
// Delete related data for test users only
if (testUserIds.length > 0) {
await prisma.rating.deleteMany({ where: { raterId: { in: testUserIds } } });
await prisma.message.deleteMany({ where: { userId: { in: testUserIds } } });
await prisma.match.deleteMany({
where: {
OR: [
{ user1Id: { in: testUserIds } },
{ user2Id: { in: testUserIds } }
]
}
});
await prisma.eventParticipant.deleteMany({ where: { userId: { in: testUserIds } } });
}
// Delete test users
await prisma.user.deleteMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
}
});
// Create test users
testUser1 = await prisma.user.create({
@@ -52,14 +84,44 @@ beforeAll(async () => {
}, 30000);
afterAll(async () => {
// Clean up
await prisma.rating.deleteMany({});
await prisma.message.deleteMany({});
await prisma.match.deleteMany({});
await prisma.chatRoom.deleteMany({});
await prisma.eventParticipant.deleteMany({});
await prisma.event.deleteMany({});
await prisma.user.deleteMany({});
// Clean up only specific test data
const testUsernames = ['john_dancer', 'sarah_swings'];
const testEmails = ['john@example.com', 'sarah@example.com'];
const testUsers = await prisma.user.findMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
},
select: { id: true }
});
const testUserIds = testUsers.map(u => u.id);
if (testUserIds.length > 0) {
await prisma.rating.deleteMany({ where: { raterId: { in: testUserIds } } });
await prisma.message.deleteMany({ where: { userId: { in: testUserIds } } });
await prisma.match.deleteMany({
where: {
OR: [
{ user1Id: { in: testUserIds } },
{ user2Id: { in: testUserIds } }
]
}
});
await prisma.eventParticipant.deleteMany({ where: { userId: { in: testUserIds } } });
}
await prisma.user.deleteMany({
where: {
OR: [
{ username: { in: testUsernames } },
{ email: { in: testEmails } }
]
}
});
await prisma.$disconnect();
});

View File

@@ -43,7 +43,7 @@ module.exports = {
cors: {
origin: process.env.CORS_ORIGIN ?
process.env.CORS_ORIGIN.split(',') :
['http://localhost:8080'],
(isDevelopment ? ['http://localhost:8080', 'http://localhost:3000'] : ['http://localhost:8080']),
credentials: true,
},