-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_backend.js
More file actions
144 lines (122 loc) · 4.48 KB
/
Copy pathtest_backend.js
File metadata and controls
144 lines (122 loc) · 4.48 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
#!/usr/bin/env node
// Test script to prove backend functionality
require('dotenv').config({ path: './.env' });
const express = require('express');
const cors = require('cors');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const db = require('./backend/db');
const app = express();
app.use(express.json());
app.use(cors());
// Test database connection
async function testDatabase() {
try {
const result = await db.raw('SELECT 1+1 as result');
console.log('✅ Database connection successful:', result.rows[0]);
return true;
} catch (error) {
console.log('❌ Database connection failed:', error.message);
return false;
}
}
// Test admin user login
async function testAdminLogin() {
try {
const user = await db('users').where({ email: 'admin@example.com' }).first();
if (!user) {
console.log('❌ Admin user not found');
return false;
}
const isValid = await bcrypt.compare('adminpassword', user.password_hash);
if (!isValid) {
console.log('❌ Admin password incorrect');
return false;
}
console.log('✅ Admin user login test passed');
return true;
} catch (error) {
console.log('❌ Admin login test failed:', error.message);
return false;
}
}
// Test JWT token generation
function testJWT() {
try {
const token = jwt.sign({ id: 1, username: 'admin' }, process.env.JWT_SECRET, { expiresIn: '1h' });
const decoded = jwt.verify(token, process.env.JWT_SECRET);
console.log('✅ JWT token generation/verification successful');
return true;
} catch (error) {
console.log('❌ JWT test failed:', error.message);
return false;
}
}
// Test registration endpoint
app.post('/users/register', async (req, res) => {
try {
const { username, email, password } = req.body;
console.log('📧 Registration attempt:', { username, email });
const existing = await db('users').where({ email }).orWhere({ username }).first();
if (existing) {
return res.status(400).json({ message: 'User already exists' });
}
const hashedPassword = await bcrypt.hash(password, 10);
await db('users').insert({ username, email, password_hash: hashedPassword });
console.log('✅ User registered successfully');
res.status(201).json({ message: 'User registered successfully' });
} catch (error) {
console.log('❌ Registration failed:', error.message);
res.status(500).json({ message: 'Server error', error: error.message });
}
});
// Test login endpoint
app.post('/users/login', async (req, res) => {
try {
const { email, password } = req.body;
console.log('🔐 Login attempt:', { email });
const user = await db('users').where({ email }).first();
if (!user) {
return res.status(400).json({ message: 'Invalid credentials' });
}
const isMatch = await bcrypt.compare(password, user.password_hash);
if (!isMatch) {
return res.status(400).json({ message: 'Invalid credentials' });
}
const token = jwt.sign({ id: user.id, username: user.username }, process.env.JWT_SECRET, { expiresIn: '7d' });
console.log('✅ User logged in successfully');
res.json({ token, user: { id: user.id, username: user.username, email: user.email } });
} catch (error) {
console.log('❌ Login failed:', error.message);
res.status(500).json({ message: 'Server error', error: error.message });
}
});
// Run tests
async function runTests() {
console.log('🧪 Testing Twoot Backend Functionality...\n');
const dbTest = await testDatabase();
const adminTest = await testAdminLogin();
const jwtTest = testJWT();
if (dbTest && adminTest && jwtTest) {
console.log('\n🎉 ALL TESTS PASSED - Starting test server...');
const server = app.listen(9001, () => {
console.log('📡 Test server running on http://localhost:9001');
console.log('📋 Test endpoints:');
console.log(' POST http://localhost:9001/users/login');
console.log(' POST http://localhost:9001/users/register');
console.log('\n💡 Test with:');
console.log('curl -X POST http://localhost:9001/users/login -H "Content-Type: application/json" -d \'{"email":"admin@example.com","password":"adminpassword"}\'');
});
// Keep running for 30 seconds
setTimeout(() => {
server.close(() => {
console.log('\n✅ Test server stopped');
process.exit(0);
});
}, 30000);
} else {
console.log('\n❌ TESTS FAILED');
process.exit(1);
}
}
runTests();