Fix: Make test user creation idempotent

This commit is contained in:
gpt-engineer-app[bot]
2025-10-30 17:53:52 +00:00
parent 1f727d6a76
commit 3318fdaa3f

View File

@@ -34,7 +34,18 @@ export async function setupTestUser(
throw new Error('Service role key not configured');
}
// Create user in auth
// Check if user already exists
const { data: existingUsers } = await supabaseAdmin.auth.admin.listUsers();
const existingUser = existingUsers?.users.find(u => u.email === email);
let userId: string;
if (existingUser) {
// User exists - use their ID
userId = existingUser.id;
console.log(` Using existing test user: ${email}`);
} else {
// User doesn't exist - create new one
const { data: authData, error: authError } = await supabaseAdmin.auth.admin.createUser({
email,
password,
@@ -44,9 +55,11 @@ export async function setupTestUser(
if (authError) throw authError;
if (!authData.user) throw new Error('User creation failed');
const userId = authData.user.id;
userId = authData.user.id;
console.log(`✓ Created new test user: ${email}`);
}
// Create profile
// Create or update profile (ensures correct role and is_test_data flag)
const { error: profileError } = await supabaseAdmin
.from('profiles')
.upsert({