forked from RiyaluxInnovates/PaksaFinancialSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_auth_flow.js
More file actions
73 lines (56 loc) · 2.38 KB
/
Copy pathtest_auth_flow.js
File metadata and controls
73 lines (56 loc) · 2.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
// Simple test script to verify authentication flow
// Run with: node test_auth_flow.js
const axios = require('axios');
const API_BASE_URL = 'http://localhost:8000';
const API_PREFIX = '/api/v1';
// Test credentials
const TEST_EMAIL = 'admin@paksa.finance';
const TEST_PASSWORD = 'changeme';
let accessToken = null;
let refreshToken = null;
async function testAuthFlow() {
console.log('=== Testing Authentication Flow ===');
try {
// Step 1: Login
console.log('\n1. Testing login...');
const loginResponse = await axios.post(`${API_BASE_URL}${API_PREFIX}/auth/login`, {
username: TEST_EMAIL,
password: TEST_PASSWORD
});
accessToken = loginResponse.data.access_token;
refreshToken = loginResponse.data.refresh_token;
console.log('✅ Login successful');
console.log(`Access token: ${accessToken.substring(0, 15)}...`);
console.log(`Refresh token: ${refreshToken.substring(0, 15)}...`);
// Step 2: Get user profile with access token
console.log('\n2. Testing profile access with token...');
const profileResponse = await axios.get(`${API_BASE_URL}${API_PREFIX}/users/me`, {
headers: { Authorization: `Bearer ${accessToken}` }
});
console.log('✅ Profile access successful');
console.log('User:', profileResponse.data);
// Step 3: Test token refresh
console.log('\n3. Testing token refresh...');
const refreshResponse = await axios.post(`${API_BASE_URL}${API_PREFIX}/auth/refresh`, {
refresh_token: refreshToken
});
const newAccessToken = refreshResponse.data.access_token;
console.log('✅ Token refresh successful');
console.log(`New access token: ${newAccessToken.substring(0, 15)}...`);
// Step 4: Verify new token works
console.log('\n4. Verifying new token works...');
const newProfileResponse = await axios.get(`${API_BASE_URL}${API_PREFIX}/users/me`, {
headers: { Authorization: `Bearer ${newAccessToken}` }
});
console.log('✅ New token verification successful');
console.log('User:', newProfileResponse.data);
console.log('\n✅ All authentication tests passed!');
} catch (error) {
console.error('❌ Test failed:', error.message);
if (error.response) {
console.error('Response data:', error.response.data);
console.error('Response status:', error.response.status);
}
}
}
testAuthFlow();