-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFRONTEND_TEST_CONSOLE.js
More file actions
224 lines (193 loc) · 7.2 KB
/
Copy pathFRONTEND_TEST_CONSOLE.js
File metadata and controls
224 lines (193 loc) · 7.2 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* FRONTEND DATA PERSISTENCE TEST
* Run this in the browser console to test data flow
* Open DevTools (F12) and paste the code below
*/
// ============================================
// TEST 1: HEALTH CHECK
// ============================================
console.log('%c=== TEST 1: HEALTH CHECK ===', 'color: blue; font-weight: bold; font-size: 14px');
fetch('http://localhost:3000/health')
.then(res => res.json())
.then(data => {
console.log('✓ Backend is responding', data);
})
.catch(err => {
console.error('✗ Backend health check failed:', err.message);
});
// ============================================
// TEST 2: API INFO
// ============================================
console.log('%c=== TEST 2: API INFO ===', 'color: blue; font-weight: bold; font-size: 14px');
fetch('http://localhost:3000/info')
.then(res => res.json())
.then(data => {
console.log('✓ API Info:', data);
})
.catch(err => {
console.error('✗ API info check failed:', err.message);
});
// ============================================
// TEST 3: CORS CHECK
// ============================================
console.log('%c=== TEST 3: CORS CHECK ===', 'color: blue; font-weight: bold; font-size: 14px');
fetch('http://localhost:3000/info', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(res => {
console.log('✓ CORS is enabled');
console.log(' Status:', res.status);
return res.json();
})
.catch(err => {
console.error('✗ CORS issue:', err.message);
});
// ============================================
// TEST 4: TEST USER REGISTRATION (EXAMPLE)
// ============================================
console.log('%c=== TEST 4: REGISTRATION ENDPOINT ===', 'color: blue; font-weight: bold; font-size: 14px');
async function testRegistration() {
const testUser = {
email: `test-${Date.now()}@example.com`,
password: 'TestPassword123!',
first_name: 'Test',
last_name: 'User'
};
try {
const response = await fetch('http://localhost:3000/api/v1/auth/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(testUser)
});
const data = await response.json();
if (response.ok) {
console.log('✓ Registration successful');
console.log(' User ID:', data.data?.user?.id);
console.log(' Email:', data.data?.user?.email);
console.log(' Token received:', !!data.data?.access_token);
console.log(' Full Response:', data);
// Save token to localStorage
if (data.data?.access_token) {
localStorage.setItem('access_token', data.data.access_token);
localStorage.setItem('refresh_token', data.data.refresh_token);
console.log('✓ Tokens saved to localStorage');
}
} else {
console.error('✗ Registration failed:', data);
}
} catch (err) {
console.error('✗ Registration error:', err.message);
}
}
// Uncomment to run registration test:
// testRegistration();
// ============================================
// TEST 5: TEST PROTECTED ENDPOINT
// ============================================
console.log('%c=== TEST 5: PROTECTED ENDPOINT ===', 'color: blue; font-weight: bold; font-size: 14px');
async function testProtectedEndpoint() {
const token = localStorage.getItem('access_token');
if (!token) {
console.warn('⚠ No token found in localStorage');
console.log('Run testRegistration() first to get a token');
return;
}
try {
const response = await fetch('http://localhost:3000/api/v1/user', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
});
const data = await response.json();
if (response.ok) {
console.log('✓ Protected endpoint accessed');
console.log(' Response:', data);
} else {
console.error('✗ Protected endpoint failed:', data);
}
} catch (err) {
console.error('✗ Protected endpoint error:', err.message);
}
}
// Uncomment to run protected endpoint test:
// testProtectedEndpoint();
// ============================================
// TEST 6: CHECK LOCAL STORAGE
// ============================================
console.log('%c=== TEST 6: LOCAL STORAGE ===', 'color: blue; font-weight: bold; font-size: 14px');
const accessToken = localStorage.getItem('access_token');
const refreshToken = localStorage.getItem('refresh_token');
if (accessToken) {
console.log('✓ Access Token found');
try {
const decoded = JSON.parse(atob(accessToken.split('.')[1]));
console.log(' Decoded:', decoded);
console.log(' Expires at:', new Date(decoded.exp * 1000));
} catch (e) {
console.log(' (Could not decode token)');
}
} else {
console.log('⚠ No access token found');
}
if (refreshToken) {
console.log('✓ Refresh Token found');
} else {
console.log('⚠ No refresh token found');
}
// ============================================
// TEST 7: NETWORK ERRORS DIAGNOSIS
// ============================================
console.log('%c=== TEST 7: NETWORK DIAGNOSIS ===', 'color: blue; font-weight: bold; font-size: 14px');
async function networkDiagnosis() {
console.log('Current Origin:', window.location.origin);
console.log('Backend URL: http://localhost:3000');
const endpoints = [
'http://localhost:3000/health',
'http://localhost:3000/info',
'http://localhost/health'
];
for (const endpoint of endpoints) {
try {
const response = await fetch(endpoint);
console.log(`✓ ${endpoint} - ${response.status}`);
} catch (err) {
console.error(`✗ ${endpoint} - ${err.message}`);
}
}
}
// Run diagnosis
networkDiagnosis();
// ============================================
// UTILITY FUNCTIONS
// ============================================
console.log('%c=== UTILITY FUNCTIONS ===', 'color: green; font-weight: bold; font-size: 14px');
// Clear all tokens
window.clearTokens = function() {
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
console.log('✓ Tokens cleared');
};
// Get current tokens
window.getTokens = function() {
return {
access: localStorage.getItem('access_token'),
refresh: localStorage.getItem('refresh_token')
};
};
// Test registration (bound to a function)
window.testReg = testRegistration;
window.testProtected = testProtectedEndpoint;
console.log('Available functions:');
console.log(' testReg() - Register a new test user');
console.log(' testProtected() - Test protected endpoint');
console.log(' getTokens() - Show current tokens');
console.log(' clearTokens() - Clear all tokens');
console.log(' networkDiagnosis() - Run network diagnosis');
console.log('%c=== TESTING COMPLETE ===', 'color: green; font-weight: bold; font-size: 14px');