add changes
This commit is contained in:
153
tests/test-auth-endpoints.js
Normal file
153
tests/test-auth-endpoints.js
Normal file
@@ -0,0 +1,153 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user