-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest-backend.js
More file actions
240 lines (201 loc) · 8.03 KB
/
Copy pathtest-backend.js
File metadata and controls
240 lines (201 loc) · 8.03 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
235
236
237
238
239
240
#!/usr/bin/env node
// SpellBloc Backend Tester - Test authentication and email system
const https = require('https');
const http = require('http');
class BackendTester {
constructor() {
this.baseUrl = 'http://localhost:3001';
this.testEmail = 'llinsomoudu@gmail.com';
this.testResults = [];
}
async runAllTests() {
console.log('🧪 SpellBloc Backend Testing Suite');
console.log('===================================\n');
// Test 1: Server Health Check
await this.testServerHealth();
// Test 2: Test Email Endpoint
await this.testEmailEndpoint();
// Test 3: User Signup
await this.testUserSignup();
// Test 4: User Login
await this.testUserLogin();
// Display results
this.displayResults();
}
async testServerHealth() {
console.log('🏥 Test 1: Server Health Check');
try {
const response = await this.makeRequest('GET', '/api/users');
if (response.statusCode === 200) {
this.logSuccess('Server Health', 'Server is running and responding');
} else {
this.logError('Server Health', `Server returned status ${response.statusCode}`);
}
} catch (error) {
this.logError('Server Health', `Server not responding: ${error.message}`);
}
}
async testEmailEndpoint() {
console.log('\n📧 Test 2: Email System Test');
try {
const response = await this.makeRequest('POST', '/api/test-email', {
email: this.testEmail
});
if (response.statusCode === 200) {
const data = JSON.parse(response.data);
this.logSuccess('Email System', `Test email sent! ID: ${data.emailId}`);
} else {
this.logError('Email System', `Email test failed: ${response.data}`);
}
} catch (error) {
this.logError('Email System', `Email test error: ${error.message}`);
}
}
async testUserSignup() {
console.log('\n👤 Test 3: User Signup');
try {
const testUser = {
parentName: 'Test Parent',
parentEmail: this.testEmail,
password: 'testpass123',
childName: 'Test Child',
childAge: 5
};
const response = await this.makeRequest('POST', '/api/auth/signup', testUser);
if (response.statusCode === 201) {
const data = JSON.parse(response.data);
this.logSuccess('User Signup', `Account created for ${data.user.parentName}`);
this.testToken = data.token; // Save token for login test
} else if (response.statusCode === 400 && response.data.includes('already exists')) {
this.logSuccess('User Signup', 'User already exists (expected for repeat tests)');
} else {
this.logError('User Signup', `Signup failed: ${response.data}`);
}
} catch (error) {
this.logError('User Signup', `Signup error: ${error.message}`);
}
}
async testUserLogin() {
console.log('\n🔐 Test 4: User Login');
try {
const loginData = {
email: this.testEmail,
password: 'testpass123'
};
const response = await this.makeRequest('POST', '/api/auth/login', loginData);
if (response.statusCode === 200) {
const data = JSON.parse(response.data);
this.logSuccess('User Login', `Login successful for ${data.user.parentName}`);
} else {
this.logError('User Login', `Login failed: ${response.data}`);
}
} catch (error) {
this.logError('User Login', `Login error: ${error.message}`);
}
}
async makeRequest(method, path, body = null) {
return new Promise((resolve, reject) => {
const url = new URL(this.baseUrl + path);
const postData = body ? JSON.stringify(body) : null;
const options = {
hostname: url.hostname,
port: url.port,
path: url.pathname,
method: method,
headers: {
'Content-Type': 'application/json',
...(postData && { 'Content-Length': Buffer.byteLength(postData) })
}
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve({
statusCode: res.statusCode,
data: data
});
});
});
req.on('error', (error) => {
reject(error);
});
if (postData) {
req.write(postData);
}
req.end();
});
}
logSuccess(testName, message) {
console.log(` ✅ ${testName}: ${message}`);
this.testResults.push({ testName, success: true, message });
}
logError(testName, message) {
console.log(` ❌ ${testName}: ${message}`);
this.testResults.push({ testName, success: false, message });
}
displayResults() {
console.log('\n' + '='.repeat(50));
console.log('📊 BACKEND TEST RESULTS');
console.log('='.repeat(50));
const passCount = this.testResults.filter(r => r.success).length;
const totalCount = this.testResults.length;
this.testResults.forEach(test => {
const status = test.success ? '✅' : '❌';
console.log(`${status} ${test.testName}`);
});
console.log(`\n📈 Summary: ${passCount}/${totalCount} tests passed`);
if (passCount === totalCount) {
console.log('\n🎉 ALL TESTS PASSED!');
console.log('✅ Your SpellBloc backend is working perfectly!');
console.log('✅ Authentication system ready');
console.log('✅ Email integration working');
console.log('✅ Ready for production use');
} else {
console.log('\n⚠️ Some tests failed. Check the errors above.');
console.log('💡 Make sure the server is running: npm start');
}
console.log('\n📧 Check your email for test messages!');
console.log(`📬 Email: ${this.testEmail}`);
}
}
// Command line interface
async function main() {
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
console.log('🧪 SpellBloc Backend Tester');
console.log('============================');
console.log('');
console.log('Usage:');
console.log(' node test-backend.js - Run all tests');
console.log(' node test-backend.js --help - Show this help');
console.log('');
console.log('Prerequisites:');
console.log(' • Backend server must be running (npm start)');
console.log(' • Server should be accessible at http://localhost:3001');
return;
}
const tester = new BackendTester();
console.log('🔍 Checking if server is running...');
try {
await tester.makeRequest('GET', '/api/users');
console.log('✅ Server is running!\n');
await tester.runAllTests();
} catch (error) {
console.log('❌ Server is not running!');
console.log('');
console.log('🚀 To start the server:');
console.log(' 1. Run: npm install');
console.log(' 2. Run: npm start');
console.log(' 3. Then run this test again');
console.log('');
console.log('Or use the startup script: ./start-server.sh');
}
}
// Run if called directly
if (require.main === module) {
main().catch(console.error);
}
module.exports = BackendTester;