-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-integration.js
More file actions
234 lines (199 loc) Β· 6.47 KB
/
test-integration.js
File metadata and controls
234 lines (199 loc) Β· 6.47 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
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env node
/**
* Atlas Integration Test Script
* Tests the full authentication and API integration flow
*/
const axios = require('axios');
const API_BASE = 'http://localhost:3001';
const FRONTEND_BASE = 'http://localhost:3000';
// Colors for console output
const colors = {
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
reset: '\x1b[0m',
};
function log(message, color = colors.reset) {
console.log(`${color}${message}${colors.reset}`);
}
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function testAPIHealthCheck() {
log('\nπ Testing API Health Check...', colors.blue);
try {
const response = await axios.get(`${API_BASE}/health`);
if (response.data.success) {
log('β
API Health Check: PASSED', colors.green);
log(` Database: ${response.data.database.connected ? 'Connected' : 'Disconnected'}`);
return true;
}
} catch (error) {
log('β API Health Check: FAILED', colors.red);
log(` Error: ${error.message}`);
return false;
}
}
async function testUserRegistration() {
log('\nπ Testing User Registration...', colors.blue);
try {
const testUser = {
name: 'Test User',
username: 'testuser' + Date.now(),
email: `test${Date.now()}@example.com`,
password: 'password123',
};
const response = await axios.post(`${API_BASE}/api/auth/register`, testUser);
if (response.data.success && response.data.data.user) {
log('β
User Registration: PASSED', colors.green);
log(` User ID: ${response.data.data.user.id}`);
log(` Username: ${response.data.data.user.username}`);
log(` Access Token: ${response.data.data.tokens.accessToken.substring(0, 20)}...`);
return response.data.data;
}
} catch (error) {
log('β User Registration: FAILED', colors.red);
log(` Error: ${error.response?.data?.message || error.message}`);
return null;
}
}
async function testUserLogin(username, password) {
log('\nπ Testing User Login...', colors.blue);
try {
const response = await axios.post(`${API_BASE}/api/auth/login`, {
username,
password,
});
if (response.data.success && response.data.data.user) {
log('β
User Login: PASSED', colors.green);
log(` User ID: ${response.data.data.user.id}`);
log(` Access Token: ${response.data.data.tokens.accessToken.substring(0, 20)}...`);
return response.data.data;
}
} catch (error) {
log('β User Login: FAILED', colors.red);
log(` Error: ${error.response?.data?.message || error.message}`);
return null;
}
}
async function testProtectedRoute(accessToken) {
log('\nπ Testing Protected Route (/api/auth/me)...', colors.blue);
try {
const response = await axios.get(`${API_BASE}/api/auth/me`, {
headers: {
'Authorization': `Bearer ${accessToken}`,
},
});
if (response.data.success && response.data.data.user) {
log('β
Protected Route: PASSED', colors.green);
log(` User: ${response.data.data.user.name}`);
return true;
}
} catch (error) {
log('β Protected Route: FAILED', colors.red);
log(` Error: ${error.response?.data?.message || error.message}`);
return false;
}
}
async function testEventsAPI(accessToken) {
log('\nπ Testing Events API...', colors.blue);
try {
const response = await axios.get(`${API_BASE}/api/events?limit=5`, {
headers: {
'Authorization': `Bearer ${accessToken}`,
},
});
if (response.data.success && response.data.data.events) {
log('β
Events API: PASSED', colors.green);
log(` Events returned: ${response.data.data.events.length}`);
return true;
}
} catch (error) {
log('β Events API: FAILED', colors.red);
log(` Error: ${error.response?.data?.message || error.message}`);
return false;
}
}
async function testOrganizationsAPI(accessToken) {
log('\nπ Testing Organizations API...', colors.blue);
try {
const response = await axios.get(`${API_BASE}/api/organizations?limit=5`, {
headers: {
'Authorization': `Bearer ${accessToken}`,
},
});
if (response.data.success && response.data.data.organizations) {
log('β
Organizations API: PASSED', colors.green);
log(` Organizations returned: ${response.data.data.organizations.length}`);
return true;
}
} catch (error) {
log('β Organizations API: FAILED', colors.red);
log(` Error: ${error.response?.data?.message || error.message}`);
return false;
}
}
async function testFrontendHealth() {
log('\nπ Testing Frontend Health...', colors.blue);
try {
const response = await axios.get(FRONTEND_BASE, { timeout: 5000 });
if (response.status === 200) {
log('β
Frontend Health: PASSED', colors.green);
return true;
}
} catch (error) {
log('β Frontend Health: FAILED', colors.red);
log(` Error: ${error.message}`);
log(' Make sure the frontend development server is running on port 3000');
return false;
}
}
async function runIntegrationTests() {
log('π― Atlas Integration Tests Started', colors.blue);
log('=' .repeat(50));
const results = [];
// Test API Health
results.push(await testAPIHealthCheck());
// Test Frontend Health
results.push(await testFrontendHealth());
// Test Registration Flow
const registrationData = await testUserRegistration();
results.push(!!registrationData);
if (registrationData) {
const { user, tokens } = registrationData;
// Test Login Flow
const loginData = await testUserLogin(user.username, 'password123');
results.push(!!loginData);
const accessToken = loginData ? loginData.tokens.accessToken : tokens.accessToken;
// Test Protected Routes
results.push(await testProtectedRoute(accessToken));
// Test API Endpoints
results.push(await testEventsAPI(accessToken));
results.push(await testOrganizationsAPI(accessToken));
} else {
results.push(false, false, false, false); // Skip dependent tests
}
// Summary
log('\n' + '=' .repeat(50));
log('π Test Results Summary:', colors.blue);
const passed = results.filter(Boolean).length;
const total = results.length;
log(` Passed: ${passed}/${total}`, passed === total ? colors.green : colors.yellow);
if (passed === total) {
log('π All tests PASSED! Atlas integration is working correctly.', colors.green);
process.exit(0);
} else {
log('β οΈ Some tests FAILED. Please check the errors above.', colors.yellow);
process.exit(1);
}
}
// Run the tests
if (require.main === module) {
runIntegrationTests().catch(error => {
log('π₯ Integration tests crashed:', colors.red);
console.error(error);
process.exit(1);
});
}
module.exports = { runIntegrationTests };