Skip to content

Commit 77b6994

Browse files
fix(auth): clean up stale test users before signup in integration tests (#3441)
* fix(auth): clean up stale test users before signup in integration tests Auth integration tests used hardcoded emails without cleaning the database before each run. On shared MongoDB instances (e.g. ARC self-hosted runners), stale data from previous CI runs caused cascading test failures. Add pre-signup cleanup in all beforeAll/beforeEach hooks across the four auth integration test files to purge any leftover users by email before attempting signup. Closes #3437 * fix(auth): add @returns JSDoc to purgeUser helper
1 parent 5fc9ba4 commit 77b6994

4 files changed

Lines changed: 71 additions & 0 deletions

File tree

modules/auth/tests/auth.abilities.integration.tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ describe('Auth abilities integration tests:', () => {
4242
agent = request.agent(init.app);
4343
adminAgent = request.agent(init.app);
4444

45+
// clean up stale users from previous runs on shared databases
46+
for (const email of [userCredentials.email, adminCredentials.email]) {
47+
try {
48+
const existing = await UserService.getBrut({ email });
49+
if (existing) await UserService.remove(existing);
50+
} catch (_) { /* cleanup – ignore errors */ }
51+
}
52+
4553
// Create a regular user
4654
const userRes = await agent
4755
.post('/api/auth/signup')

modules/auth/tests/auth.authorization.integration.tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ describe('Authorization integration tests:', () => {
4040
adminAgent = request.agent(init.app);
4141
otherAgent = request.agent(init.app);
4242

43+
// clean up stale users from previous runs on shared databases
44+
for (const email of ['auth-test-user@test.com', 'auth-test-admin@test.com', 'auth-test-other@test.com']) {
45+
try {
46+
const existing = await UserService.getBrut({ email });
47+
if (existing) await UserService.remove(existing);
48+
} catch (_) { /* cleanup – ignore errors */ }
49+
}
50+
4351
// Create a normal user via agent
4452
const userRes = await agent
4553
.post('/api/auth/signup')

modules/auth/tests/auth.integration.tests.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ describe('Auth integration tests:', () => {
6666
_userEdited.email = credentials[1].email;
6767
_userEdited.password = credentials[1].password;
6868

69+
// clean up stale users from previous runs on shared databases
70+
for (const email of [credentials[0].email, credentials[1].email, 'register_new_user_@test.com']) {
71+
try {
72+
const existing = await UserService.getBrut({ email });
73+
if (existing) await UserService.remove(existing);
74+
} catch (_) { /* cleanup – ignore errors */ }
75+
}
76+
6977
// add user
7078
try {
7179
const result = await agent.post('/api/auth/signup').send(_user).expect(200);
@@ -645,6 +653,11 @@ describe('Auth integration tests:', () => {
645653
password: credentials[0].password,
646654
provider: 'local',
647655
};
656+
// clean up stale users from previous runs on shared databases
657+
try {
658+
const existing = await UserService.getBrut({ email: credentials[0].email });
659+
if (existing) await UserService.remove(existing);
660+
} catch (_) { /* cleanup – ignore errors */ }
648661
try {
649662
const result = await agent.post('/api/auth/signup').send(_user).expect(200);
650663
user = result.body.user;
@@ -722,6 +735,13 @@ describe('Auth integration tests:', () => {
722735
let secUser;
723736

724737
beforeEach(async () => {
738+
// clean up stale users from previous runs on shared databases
739+
for (const email of [secEmail, 'cookieflag@test.com']) {
740+
try {
741+
const existing = await UserService.getBrut({ email });
742+
if (existing) await UserService.remove(existing);
743+
} catch (_) { /* cleanup – ignore errors */ }
744+
}
725745
try {
726746
const result = await agent.post('/api/auth/signup').send({
727747
firstName: 'Sec',
@@ -873,6 +893,11 @@ describe('Auth integration tests:', () => {
873893
let verifyUser;
874894

875895
beforeEach(async () => {
896+
// clean up stale users from previous runs on shared databases
897+
try {
898+
const existing = await UserService.getBrut({ email: verifyEmail });
899+
if (existing) await UserService.remove(existing);
900+
} catch (_) { /* cleanup – ignore errors */ }
876901
try {
877902
const result = await agent.post('/api/auth/signup').send({
878903
firstName: 'Verify',
@@ -998,6 +1023,11 @@ describe('Auth integration tests:', () => {
9981023
let lockUser;
9991024

10001025
beforeEach(async () => {
1026+
// clean up stale users from previous runs on shared databases
1027+
try {
1028+
const existing = await UserService.getBrut({ email: lockEmail });
1029+
if (existing) await UserService.remove(existing);
1030+
} catch (_) { /* cleanup – ignore errors */ }
10011031
try {
10021032
const result = await agent.post('/api/auth/signup').send({
10031033
firstName: 'Lock',

modules/auth/tests/auth.signup.organization.integration.tests.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ describe('Auth signup organization integration tests:', () => {
4444
} catch (_) { /* cleanup — ignore errors */ }
4545
};
4646

47+
/**
48+
* Remove a user by email if they exist (cleanup stale data from previous runs).
49+
* @param {string} email - email address of the user to purge
50+
* @returns {Promise<void>}
51+
*/
52+
const purgeUser = async (email) => {
53+
try {
54+
const existing = await UserService.getBrut({ email });
55+
if (existing) await cleanupUser(existing);
56+
} catch (_) { /* cleanup – ignore errors */ }
57+
};
58+
4759
beforeAll(async () => {
4860
try {
4961
const init = await bootstrap();
@@ -66,6 +78,8 @@ describe('Auth signup organization integration tests:', () => {
6678
config.organizations = { enabled: false, autoCreate: true, domainMatching: true };
6779

6880
let user;
81+
// clean up stale users from previous runs on shared databases
82+
await purgeUser('silent-org@test.com');
6983
try {
7084
const result = await agent
7185
.post('/api/auth/signup')
@@ -108,6 +122,9 @@ describe('Auth signup organization integration tests:', () => {
108122
// First, create an existing organization with a specific domain
109123
let firstUser;
110124
let secondUser;
125+
// clean up stale users from previous runs on shared databases
126+
await purgeUser('first@matchdomain.com');
127+
await purgeUser('second@matchdomain.com');
111128
try {
112129
// Sign up first user to create the org with domain
113130
const result1 = await agent
@@ -162,6 +179,8 @@ describe('Auth signup organization integration tests:', () => {
162179
config.organizations = { enabled: true, autoCreate: true, domainMatching: true };
163180

164181
let user;
182+
// clean up stale users from previous runs on shared databases
183+
await purgeUser('solo@uniquedomain123.com');
165184
try {
166185
const result = await agent
167186
.post('/api/auth/signup')
@@ -200,6 +219,8 @@ describe('Auth signup organization integration tests:', () => {
200219
config.organizations = { enabled: true, autoCreate: true, domainMatching: false };
201220

202221
let user;
222+
// clean up stale users from previous runs on shared databases
223+
await purgeUser('personal@somecompany.com');
203224
try {
204225
const result = await agent
205226
.post('/api/auth/signup')
@@ -238,6 +259,8 @@ describe('Auth signup organization integration tests:', () => {
238259
config.organizations = { enabled: true, autoCreate: false, domainMatching: true };
239260

240261
let user;
262+
// clean up stale users from previous runs on shared databases
263+
await purgeUser('manual@setup.com');
241264
try {
242265
const result = await agent
243266
.post('/api/auth/signup')
@@ -273,6 +296,8 @@ describe('Auth signup organization integration tests:', () => {
273296
config.organizations = { enabled: false, autoCreate: true, domainMatching: true };
274297

275298
let user;
299+
// clean up stale users from previous runs on shared databases
300+
await purgeUser('response-check@test.com');
276301
try {
277302
const result = await agent
278303
.post('/api/auth/signup')

0 commit comments

Comments
 (0)