-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-auth-complete.js
More file actions
148 lines (122 loc) Β· 4.38 KB
/
test-auth-complete.js
File metadata and controls
148 lines (122 loc) Β· 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env node
/**
* Auth Flow Test
* Tests the complete authentication flow including login with existing users
*/
const axios = require('axios');
const API_BASE = 'http://localhost:3001';
async function testExistingUserLogin() {
console.log('π§ͺ Testing login with existing user...');
// Try to login with the user mentioned in the error log
const credentials = {
username: 'RealStr1ke',
password: 'password123', // Common test password
};
try {
const response = await axios.post(`${API_BASE}/api/auth/login`, credentials);
if (response.data.success && response.data.data.user) {
console.log('β
Login SUCCESS!');
console.log(` User: ${response.data.data.user.name}`);
console.log(` Username: ${response.data.data.user.username}`);
console.log(` Token: ${response.data.data.tokens.accessToken.substring(0, 20)}...`);
return { success: true, token: response.data.data.tokens.accessToken };
}
} catch (error) {
console.log('β Login FAILED:');
if (error.response?.data?.message) {
console.log(` Error: ${error.response.data.message}`);
} else {
console.log(` Error: ${error.message}`);
}
return { success: false, error: error.response?.data?.message || error.message };
}
}
async function testCreateAndLogin() {
console.log('\nπ§ͺ Testing create user and immediate login...');
const timestamp = Date.now();
const testUser = {
name: `Test User ${timestamp}`,
username: `testuser${timestamp}`,
email: `test${timestamp}@example.com`,
password: 'password123',
};
try {
// Register user
console.log(' π Registering new user...');
const registerResponse = await axios.post(`${API_BASE}/api/auth/register`, testUser);
if (!registerResponse.data.success) {
console.log('β Registration failed');
return { success: false };
}
console.log(' β
Registration successful');
// Test immediate login
console.log(' π Testing immediate login...');
const loginResponse = await axios.post(`${API_BASE}/api/auth/login`, {
username: testUser.username,
password: testUser.password,
});
if (loginResponse.data.success && loginResponse.data.data.user) {
console.log('β
Immediate login SUCCESS!');
console.log(` User: ${loginResponse.data.data.user.name}`);
return { success: true, token: loginResponse.data.data.tokens.accessToken };
}
} catch (error) {
console.log('β Create and login test FAILED:');
console.log(` Error: ${error.response?.data?.message || error.message}`);
return { success: false };
}
}
async function testSessionValidation(token) {
console.log('\nπ§ͺ Testing session validation...');
try {
const response = await axios.get(`${API_BASE}/api/auth/session`, {
headers: {
'Authorization': `Bearer ${token}`,
},
});
if (response.data.success && response.data.data.tokenValid) {
console.log('β
Session validation SUCCESS!');
console.log(` User: ${response.data.data.user.name}`);
return { success: true };
}
} catch (error) {
console.log('β Session validation FAILED:');
console.log(` Error: ${error.response?.data?.message || error.message}`);
return { success: false };
}
}
async function runTests() {
console.log('π― Running Complete Auth Flow Tests\n');
const results = [];
// Test 1: Try login with existing problematic user
const existingUserResult = await testExistingUserLogin();
results.push(existingUserResult.success);
// Test 2: Create new user and test immediate login
const newUserResult = await testCreateAndLogin();
results.push(newUserResult.success);
// Test 3: Session validation with a valid token
if (newUserResult.success && newUserResult.token) {
const sessionResult = await testSessionValidation(newUserResult.token);
results.push(sessionResult.success);
} else {
results.push(false);
}
const passed = results.filter(Boolean).length;
const total = results.length;
console.log(`\nπ Results: ${passed}/${total} tests passed`);
if (passed === total) {
console.log('π All authentication tests PASSED!');
console.log('β
Backend authentication is working correctly');
console.log('β
Frontend can now safely authenticate users');
process.exit(0);
} else {
console.log('β οΈ Some tests FAILED - please check the issues above');
process.exit(1);
}
}
if (require.main === module) {
runTests().catch(error => {
console.error('π₯ Test runner crashed:', error.message);
process.exit(1);
});
}