92 lines
No EOL
2.3 KiB
JavaScript
92 lines
No EOL
2.3 KiB
JavaScript
const axios = require('axios');
|
|
|
|
class TestDataHelper {
|
|
constructor(baseUrl = 'http://localhost:8000/api') {
|
|
this.baseUrl = baseUrl;
|
|
this.testUsers = [];
|
|
}
|
|
|
|
/**
|
|
* Create a test user via API
|
|
*/
|
|
async createTestUser(userData = {}) {
|
|
const defaultUser = {
|
|
name: 'Test User',
|
|
email: `test.${Date.now()}@example.com`,
|
|
password: 'password123'
|
|
};
|
|
|
|
const user = { ...defaultUser, ...userData };
|
|
|
|
try {
|
|
// First try to register via normal registration endpoint
|
|
const response = await axios.post(`${this.baseUrl}/register`, {
|
|
...user,
|
|
password_confirmation: user.password
|
|
});
|
|
|
|
if (response.data.success) {
|
|
this.testUsers.push(user.email);
|
|
return {
|
|
success: true,
|
|
user: {
|
|
email: user.email,
|
|
password: user.password,
|
|
name: user.name
|
|
}
|
|
};
|
|
}
|
|
} catch (error) {
|
|
// If user already exists, that's okay for login tests
|
|
if (error.response?.status === 422 &&
|
|
error.response?.data?.message?.includes('already been taken')) {
|
|
return {
|
|
success: true,
|
|
user: {
|
|
email: user.email,
|
|
password: user.password,
|
|
name: user.name
|
|
},
|
|
existing: true
|
|
};
|
|
}
|
|
|
|
console.error('Failed to create test user:', error.response?.data || error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ensure a specific test user exists (for login tests)
|
|
*/
|
|
async ensureTestUserExists() {
|
|
const testUser = {
|
|
name: 'E2E Test User',
|
|
email: 'e2e.test@example.com',
|
|
password: 'TestPass123!'
|
|
};
|
|
|
|
await this.createTestUser(testUser);
|
|
return testUser;
|
|
}
|
|
|
|
/**
|
|
* Clean up test users created during tests
|
|
*/
|
|
async cleanup() {
|
|
// This would need a cleanup endpoint or database access
|
|
// For now, we'll keep track of created users for manual cleanup
|
|
console.log('Test users created:', this.testUsers);
|
|
}
|
|
|
|
/**
|
|
* Run database seeder (requires artisan command access)
|
|
*/
|
|
async seedDatabase() {
|
|
// This would need to run artisan command
|
|
// Could be done via a special endpoint or SSH
|
|
console.log('Note: Run "php artisan db:seed --class=TestUserSeeder" to seed test users');
|
|
}
|
|
}
|
|
|
|
module.exports = TestDataHelper; |