UNPKG

realtimecursor

Version:

Real-time collaboration system with cursor tracking and approval workflow

102 lines (88 loc) • 3.77 kB
const axios = require('axios'); const API_BASE_URL = 'http://localhost:3000'; // Test data const testUser = { firstName: 'John', lastName: 'Doe', email: 'john.doe@example.com', password: 'SecurePass123!' }; async function testAuthentication() { console.log('šŸ” Testing Authentication System...\n'); try { // Test 1: Register a new user console.log('šŸ“ Testing user registration...'); try { const registerResponse = await axios.post(`${API_BASE_URL}/auth/register`, testUser); console.log('āœ… Registration successful!'); console.log('User:', registerResponse.data.user); console.log('Token received:', registerResponse.data.token ? 'Yes' : 'No'); // Test 2: Try to register the same user again (should fail) console.log('\nšŸ”„ Testing duplicate registration...'); try { await axios.post(`${API_BASE_URL}/auth/register`, testUser); console.log('āŒ Duplicate registration should have failed!'); } catch (duplicateError) { console.log('āœ… Duplicate registration correctly rejected:', duplicateError.response.data.message); } // Test 3: Login with correct credentials console.log('\nšŸ”‘ Testing login with correct credentials...'); const loginResponse = await axios.post(`${API_BASE_URL}/auth/login`, { email: testUser.email, password: testUser.password }); console.log('āœ… Login successful!'); console.log('User:', loginResponse.data.user); // Test 4: Login with wrong password console.log('\n🚫 Testing login with wrong password...'); try { await axios.post(`${API_BASE_URL}/auth/login`, { email: testUser.email, password: 'wrongpassword' }); console.log('āŒ Login with wrong password should have failed!'); } catch (wrongPasswordError) { console.log('āœ… Wrong password correctly rejected:', wrongPasswordError.response.data.message); } // Test 5: Test protected route console.log('\nšŸ”’ Testing protected route...'); const token = loginResponse.data.token; const profileResponse = await axios.get(`${API_BASE_URL}/auth/me`, { headers: { Authorization: `Bearer ${token}` } }); console.log('āœ… Protected route access successful!'); console.log('Profile data:', profileResponse.data.user); } catch (registerError) { if (registerError.response?.data?.message?.includes('already exists')) { console.log('ā„¹ļø User already exists, testing login instead...'); // Test login if user already exists const loginResponse = await axios.post(`${API_BASE_URL}/auth/login`, { email: testUser.email, password: testUser.password }); console.log('āœ… Login successful with existing user!'); console.log('User:', loginResponse.data.user); } else { throw registerError; } } // Test 6: Test password validation console.log('\nšŸ” Testing password validation...'); try { await axios.post(`${API_BASE_URL}/auth/register`, { firstName: 'Test', lastName: 'User', email: 'test.weak@example.com', password: 'weak' // Should fail validation }); console.log('āŒ Weak password should have been rejected!'); } catch (weakPasswordError) { console.log('āœ… Weak password correctly rejected:', weakPasswordError.response.data.message); } console.log('\nšŸŽ‰ All authentication tests completed successfully!'); } catch (error) { console.error('āŒ Test failed:', error.response?.data || error.message); } } // Run the test testAuthentication();