145 lines
4.6 KiB
JavaScript
145 lines
4.6 KiB
JavaScript
const axios = require('axios');
|
|
|
|
const API_URL = 'http://localhost:3000/api';
|
|
|
|
async function testSignup() {
|
|
try {
|
|
console.log('Testing signup functionality...\n');
|
|
|
|
// Generate a random mobile number for testing
|
|
const randomMobile = '70' + Math.floor(10000000 + Math.random() * 90000000);
|
|
|
|
const signupData = {
|
|
mobile_number: randomMobile,
|
|
full_name: 'Test User Signup',
|
|
password: 'test123456',
|
|
email: 'testuser@example.com',
|
|
address: '123 Test Street, Test City',
|
|
emergency_contact: '8888888888'
|
|
};
|
|
|
|
console.log('Signing up with data:', {
|
|
...signupData,
|
|
password: '********'
|
|
});
|
|
|
|
const response = await axios.post(`${API_URL}/auth/signup`, signupData);
|
|
|
|
if (response.data.success) {
|
|
console.log('\n✅ Signup successful!');
|
|
console.log('User created:', {
|
|
id: response.data.data.user.id,
|
|
mobile_number: response.data.data.user.mobile_number,
|
|
full_name: response.data.data.user.full_name,
|
|
email: response.data.data.user.email,
|
|
role: response.data.data.user.role,
|
|
is_active: response.data.data.user.is_active
|
|
});
|
|
console.log('Token received:', response.data.data.token.substring(0, 20) + '...');
|
|
|
|
// Test login with the new account
|
|
console.log('\n\nTesting login with new account...');
|
|
const loginResponse = await axios.post(`${API_URL}/auth/login`, {
|
|
mobile_number: randomMobile,
|
|
password: 'test123456'
|
|
});
|
|
|
|
if (loginResponse.data.success) {
|
|
console.log('✅ Login successful with new account!');
|
|
console.log('User:', {
|
|
id: loginResponse.data.data.user.id,
|
|
mobile_number: loginResponse.data.data.user.mobile_number,
|
|
full_name: loginResponse.data.data.user.full_name,
|
|
role: loginResponse.data.data.user.role
|
|
});
|
|
} else {
|
|
console.log('❌ Login failed:', loginResponse.data.message);
|
|
}
|
|
|
|
// Test getting profile
|
|
console.log('\n\nTesting profile retrieval...');
|
|
const token = loginResponse.data.data.token;
|
|
const profileResponse = await axios.get(`${API_URL}/auth/profile`, {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
if (profileResponse.data.success) {
|
|
console.log('✅ Profile retrieved successfully!');
|
|
console.log('Profile data:', {
|
|
id: profileResponse.data.data.user.id,
|
|
mobile_number: profileResponse.data.data.user.mobile_number,
|
|
full_name: profileResponse.data.data.user.full_name,
|
|
email: profileResponse.data.data.user.email,
|
|
address: profileResponse.data.data.user.address,
|
|
emergency_contact: profileResponse.data.data.user.emergency_contact,
|
|
role: profileResponse.data.data.user.role
|
|
});
|
|
}
|
|
|
|
} else {
|
|
console.log('❌ Signup failed:', response.data.message);
|
|
}
|
|
|
|
// Test validation errors
|
|
console.log('\n\nTesting validation errors...');
|
|
|
|
// Test missing required field
|
|
try {
|
|
await axios.post(`${API_URL}/auth/signup`, {
|
|
mobile_number: '9999999990',
|
|
password: 'test123'
|
|
});
|
|
} catch (error) {
|
|
if (error.response && error.response.status === 400) {
|
|
console.log('✅ Validation error for missing full_name:', error.response.data.message);
|
|
}
|
|
}
|
|
|
|
// Test invalid mobile number
|
|
try {
|
|
await axios.post(`${API_URL}/auth/signup`, {
|
|
mobile_number: '123',
|
|
full_name: 'Test User',
|
|
password: 'test123'
|
|
});
|
|
} catch (error) {
|
|
if (error.response && error.response.status === 400) {
|
|
console.log('✅ Validation error for invalid mobile:', error.response.data.message);
|
|
}
|
|
}
|
|
|
|
// Test short password
|
|
try {
|
|
await axios.post(`${API_URL}/auth/signup`, {
|
|
mobile_number: '9999999991',
|
|
full_name: 'Test User',
|
|
password: '123'
|
|
});
|
|
} catch (error) {
|
|
if (error.response && error.response.status === 400) {
|
|
console.log('✅ Validation error for short password:', error.response.data.message);
|
|
}
|
|
}
|
|
|
|
// Test duplicate mobile number
|
|
try {
|
|
await axios.post(`${API_URL}/auth/signup`, signupData);
|
|
} catch (error) {
|
|
if (error.response && error.response.status === 400) {
|
|
console.log('✅ Validation error for duplicate mobile:', error.response.data.message);
|
|
}
|
|
}
|
|
|
|
console.log('\n\n✅ All signup tests completed successfully!');
|
|
|
|
} catch (error) {
|
|
console.error('❌ Test failed:', error.response?.data || error.message);
|
|
}
|
|
}
|
|
|
|
// Run the test
|
|
testSignup();
|
|
|