/** * Acceptance Test: Account Creation, Login, and Deletion (API-based) * * This test demonstrates the full workflow using API calls: * 1. Create a test account via registration API * 2. Log in using the authentication API * 3. Delete the test account */ import { test, expect } from '@playwright/test'; import { prisma } from '@/lib/prisma'; // Generate unique test account credentials function getTestCredentials() { const timestamp = Date.now(); const random = Math.random().toString(36).substring(7); return { email: `test-api-${timestamp}-${random}@example.com`, password: 'TestPassword1234!', name: `Test API User ${timestamp}` }; } test.describe.serial('Account Lifecycle API Acceptance Test', () => { let testEmail: string; let testPassword: string; let testName: string; test.beforeAll(async () => { const credentials = getTestCredentials(); testEmail = credentials.email; testPassword = credentials.password; testName = credentials.name; }); test.afterAll(async () => { // Clean up: delete test user if they still exist try { const user = await prisma.user.findUnique({ where: { email: testEmail } }); if (user) { await prisma.user.delete({ where: { id: user.id } }); console.log(`Cleaned up test user: ${testEmail}`); } } catch (error) { console.error('Cleanup error:', error); } }); test('1. Create test account via API', async ({ request }) => { console.log('Test 1 - testEmail:', testEmail); // Register via API const response = await request.post('http://localhost:3000/api/auth/sign-up/email', { data: { email: testEmail, password: testPassword, name: testName }, headers: { 'Origin': 'http://localhost:3000' } }); console.log('API Registration Response:', response.status(), await response.text()); expect(response.ok()).toBeTruthy(); const data = await response.json(); expect(data.user.email).toBe(testEmail); expect(data.user.name).toBe(testName); // Verify user was created in database const user = await prisma.user.findUnique({ where: { email: testEmail } }); console.log('User created in DB:', user ? 'yes' : 'no'); if (user) { console.log('User ID:', user.id); console.log('User role:', user.role); } expect(user).not.toBeNull(); expect(user?.email).toBe(testEmail); expect(user?.role).toBe('player'); }); test('2. Log in with test account via API', async ({ request }) => { console.log('Test 2 - testEmail:', testEmail); // Check if user exists first const user = await prisma.user.findUnique({ where: { email: testEmail } }); console.log('User exists for login:', user ? 'yes' : 'no'); if (user) { console.log('User ID:', user.id); } // Login via API const response = await request.post('http://localhost:3000/api/auth/sign-in/email', { data: { email: testEmail, password: testPassword }, headers: { 'Origin': 'http://localhost:3000' } }); console.log('Login API Response:', response.status(), await response.text()); expect(response.ok()).toBeTruthy(); const data = await response.json(); expect(data.user.email).toBe(testEmail); expect(data.token).toBeDefined(); }); test('3. Delete test account', async () => { // Delete via database const user = await prisma.user.findUnique({ where: { email: testEmail } }); if (user) { await prisma.user.delete({ where: { id: user.id } }); console.log(`Test user deleted: ${testEmail}`); } // Verify user was deleted const deletedUser = await prisma.user.findUnique({ where: { email: testEmail } }); expect(deletedUser).toBeNull(); }); });