mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 05:51:12 -05:00
Fix: Make test user creation idempotent
This commit is contained in:
33
tests/fixtures/database.ts
vendored
33
tests/fixtures/database.ts
vendored
@@ -34,19 +34,32 @@ export async function setupTestUser(
|
|||||||
throw new Error('Service role key not configured');
|
throw new Error('Service role key not configured');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create user in auth
|
// Check if user already exists
|
||||||
const { data: authData, error: authError } = await supabaseAdmin.auth.admin.createUser({
|
const { data: existingUsers } = await supabaseAdmin.auth.admin.listUsers();
|
||||||
email,
|
const existingUser = existingUsers?.users.find(u => u.email === email);
|
||||||
password,
|
|
||||||
email_confirm: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (authError) throw authError;
|
let userId: string;
|
||||||
if (!authData.user) throw new Error('User creation failed');
|
|
||||||
|
|
||||||
const userId = authData.user.id;
|
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,
|
||||||
|
email_confirm: true,
|
||||||
|
});
|
||||||
|
|
||||||
// Create profile
|
if (authError) throw authError;
|
||||||
|
if (!authData.user) throw new Error('User creation failed');
|
||||||
|
|
||||||
|
userId = authData.user.id;
|
||||||
|
console.log(`✓ Created new test user: ${email}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create or update profile (ensures correct role and is_test_data flag)
|
||||||
const { error: profileError } = await supabaseAdmin
|
const { error: profileError } = await supabaseAdmin
|
||||||
.from('profiles')
|
.from('profiles')
|
||||||
.upsert({
|
.upsert({
|
||||||
|
|||||||
Reference in New Issue
Block a user