Files
tasks-backend/tests/test-auth-endpoints.js
2025-12-26 23:56:32 +02:00

154 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const axios = require('axios');
const API_URL = 'http://localhost:3000/api';
async function testAuthEndpoints() {
console.log('\n🧪 Testing Authentication Endpoints\n');
console.log('=' .repeat(60));
let authToken;
let userId;
try {
// Test 1: Register new user
console.log('\n1⃣ Testing POST /api/auth/register');
console.log('-'.repeat(60));
try {
const registerData = {
username: `testuser_${Date.now()}`,
email: `test${Date.now()}@example.com`,
password: 'Test@123'
};
console.log('Request:', JSON.stringify(registerData, null, 2));
const registerResponse = await axios.post(`${API_URL}/auth/register`, registerData);
console.log('✅ Status:', registerResponse.status);
console.log('✅ Response:', JSON.stringify(registerResponse.data, null, 2));
authToken = registerResponse.data.data.token;
userId = registerResponse.data.data.user.id;
} catch (error) {
console.log('❌ Error:', error.response?.data || error.message);
}
// Test 2: Duplicate email
console.log('\n2⃣ Testing duplicate email (should fail)');
console.log('-'.repeat(60));
try {
const duplicateData = {
username: 'anotheruser',
email: registerData.email, // Same email
password: 'Test@123'
};
await axios.post(`${API_URL}/auth/register`, duplicateData);
console.log('❌ Should have failed');
} catch (error) {
console.log('✅ Expected error:', error.response?.data?.message);
}
// Test 3: Invalid password
console.log('\n3⃣ Testing invalid password (should fail)');
console.log('-'.repeat(60));
try {
const weakPassword = {
username: 'newuser',
email: 'newuser@example.com',
password: 'weak' // Too weak
};
await axios.post(`${API_URL}/auth/register`, weakPassword);
console.log('❌ Should have failed');
} catch (error) {
console.log('✅ Expected error:', error.response?.data?.message);
}
// Test 4: Login
console.log('\n4⃣ Testing POST /api/auth/login');
console.log('-'.repeat(60));
try {
const loginData = {
email: registerData.email,
password: registerData.password
};
console.log('Request:', JSON.stringify(loginData, null, 2));
const loginResponse = await axios.post(`${API_URL}/auth/login`, loginData);
console.log('✅ Status:', loginResponse.status);
console.log('✅ Response:', JSON.stringify(loginResponse.data, null, 2));
} catch (error) {
console.log('❌ Error:', error.response?.data || error.message);
}
// Test 5: Invalid login
console.log('\n5⃣ Testing invalid login (should fail)');
console.log('-'.repeat(60));
try {
const invalidLogin = {
email: registerData.email,
password: 'WrongPassword123'
};
await axios.post(`${API_URL}/auth/login`, invalidLogin);
console.log('❌ Should have failed');
} catch (error) {
console.log('✅ Expected error:', error.response?.data?.message);
}
// Test 6: Verify token
console.log('\n6⃣ Testing GET /api/auth/verify');
console.log('-'.repeat(60));
try {
console.log('Token:', authToken.substring(0, 20) + '...');
const verifyResponse = await axios.get(`${API_URL}/auth/verify`, {
headers: {
'Authorization': `Bearer ${authToken}`
}
});
console.log('✅ Status:', verifyResponse.status);
console.log('✅ Response:', JSON.stringify(verifyResponse.data, null, 2));
} catch (error) {
console.log('❌ Error:', error.response?.data || error.message);
}
// Test 7: Verify without token
console.log('\n7⃣ Testing verify without token (should fail)');
console.log('-'.repeat(60));
try {
await axios.get(`${API_URL}/auth/verify`);
console.log('❌ Should have failed');
} catch (error) {
console.log('✅ Expected error:', error.response?.data?.message);
}
// Test 8: Logout
console.log('\n8⃣ Testing POST /api/auth/logout');
console.log('-'.repeat(60));
try {
const logoutResponse = await axios.post(`${API_URL}/auth/logout`);
console.log('✅ Status:', logoutResponse.status);
console.log('✅ Response:', JSON.stringify(logoutResponse.data, null, 2));
} catch (error) {
console.log('❌ Error:', error.response?.data || error.message);
}
console.log('\n' + '='.repeat(60));
console.log('✅ All authentication tests completed!');
console.log('='.repeat(60) + '\n');
} catch (error) {
console.error('\n❌ Test suite error:', error.message);
}
}
// Run tests
testAuthEndpoints();