-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-auth.js
More file actions
197 lines (166 loc) · 6.02 KB
/
Copy pathtest-auth.js
File metadata and controls
197 lines (166 loc) · 6.02 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
#!/usr/bin/env node
/**
* Authentication System Integration Test
* Tests all authentication endpoints and demonstrates backend concepts
*/
const http = require('http');
const API_BASE = 'http://localhost:5001/api/v1';
function makeRequest(options, data = null) {
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
let body = '';
res.on('data', (chunk) => (body += chunk));
res.on('end', () => {
try {
const jsonData = JSON.parse(body);
resolve({ status: res.statusCode, data: jsonData, headers: res.headers });
} catch (e) {
resolve({ status: res.statusCode, data: body, headers: res.headers });
}
});
});
req.on('error', reject);
if (data) {
req.write(JSON.stringify(data));
}
req.end();
});
}
async function runAuthTests() {
console.log('🧪 Starting Authentication Integration Tests\n');
const testUser = {
email: `test-${Date.now()}@example.com`,
password: 'testpass123',
name: 'Integration Test User',
};
let userToken = '';
try {
// Test 1: User Registration
console.log('1️⃣ Testing User Registration...');
const registerOptions = {
hostname: 'localhost',
port: 5001,
path: '/api/v1/auth/register',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
};
const registerResponse = await makeRequest(registerOptions, testUser);
console.log(` Status: ${registerResponse.status}`);
console.log(` Message: ${registerResponse.data.message}`);
if (registerResponse.status === 201 && registerResponse.data.success) {
console.log(' ✅ Registration successful');
console.log(` ✅ User ID: ${registerResponse.data.user.id.substring(0, 10)}...`);
console.log(` ✅ JWT Token generated: ${registerResponse.data.token.substring(0, 20)}...`);
userToken = registerResponse.data.token;
} else {
console.log(' ❌ Registration failed');
return;
}
// Test 2: User Login
console.log('\n2️⃣ Testing User Login...');
const loginOptions = {
hostname: 'localhost',
port: 5001,
path: '/api/v1/auth/login',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
};
const loginResponse = await makeRequest(loginOptions, {
email: testUser.email,
password: testUser.password,
});
console.log(` Status: ${loginResponse.status}`);
console.log(` Message: ${loginResponse.data.message}`);
if (loginResponse.status === 200 && loginResponse.data.success) {
console.log(' ✅ Login successful');
userToken = loginResponse.data.token;
} else {
console.log(' ❌ Login failed');
return;
}
// Test 3: Protected Endpoint Access
console.log('\n3️⃣ Testing Protected Endpoint Access...');
const profileOptions = {
hostname: 'localhost',
port: 5001,
path: '/api/v1/auth/profile',
method: 'GET',
headers: {
Authorization: `Bearer ${userToken}`,
},
};
const profileResponse = await makeRequest(profileOptions);
console.log(` Status: ${profileResponse.status}`);
if (profileResponse.status === 200 && profileResponse.data.success) {
console.log(' ✅ Protected endpoint access successful');
console.log(` ✅ User profile retrieved: ${profileResponse.data.user.email}`);
} else {
console.log(' ❌ Protected endpoint access failed');
}
// Test 4: Invalid Token Rejection
console.log('\n4️⃣ Testing Invalid Token Rejection...');
const invalidTokenOptions = {
hostname: 'localhost',
port: 5001,
path: '/api/v1/auth/profile',
method: 'GET',
headers: {
Authorization: 'Bearer invalid-token-here',
},
};
const invalidResponse = await makeRequest(invalidTokenOptions);
console.log(` Status: ${invalidResponse.status}`);
if (invalidResponse.status === 401 && !invalidResponse.data.success) {
console.log(' ✅ Invalid token correctly rejected');
} else {
console.log(' ❌ Invalid token should be rejected');
}
// Test 5: Token Verification
console.log('\n5️⃣ Testing Token Verification...');
const verifyOptions = {
hostname: 'localhost',
port: 5001,
path: '/api/v1/auth/verify',
method: 'GET',
headers: {
Authorization: `Bearer ${userToken}`,
},
};
const verifyResponse = await makeRequest(verifyOptions);
console.log(` Status: ${verifyResponse.status}`);
if (verifyResponse.status === 200 && verifyResponse.data.success) {
console.log(' ✅ Token verification successful');
} else {
console.log(' ❌ Token verification failed');
}
// Test 6: Invalid Login Credentials
console.log('\n6️⃣ Testing Invalid Login Credentials...');
const invalidLoginResponse = await makeRequest(loginOptions, {
email: testUser.email,
password: 'wrongpassword',
});
console.log(` Status: ${invalidLoginResponse.status}`);
if (invalidLoginResponse.status === 401 && !invalidLoginResponse.data.success) {
console.log(' ✅ Invalid credentials correctly rejected');
} else {
console.log(' ❌ Invalid credentials should be rejected');
}
// Backend Concepts Demo
console.log('\n🎓 Backend Engineering Concepts Demonstrated:');
console.log(' 🔐 Password Hashing: Bcrypt with salt rounds');
console.log(' 🎫 JWT Tokens: Stateless authentication');
console.log(' 🛡️ Middleware: Authorization protection');
console.log(' 📊 Database: User persistence with Prisma');
console.log(' ⚡ HTTP Status Codes: Proper REST responses');
console.log(' 🔄 CRUD Operations: Create users, read profiles');
console.log('\n✅ All Authentication Tests Completed Successfully! 🎉');
} catch (error) {
console.error('\n❌ Test Suite Failed:', error.message);
}
}
// Run the tests
runAuthTests();