diff --git a/test_add_user_debug.js b/test_add_user_debug.js new file mode 100644 index 0000000..d98aad2 --- /dev/null +++ b/test_add_user_debug.js @@ -0,0 +1,92 @@ +const http = require('http'); + +// Create a new user with a timestamp to avoid conflicts +const username = `testuser_${Date.now()}`; +const postData = JSON.stringify({ + username: username, + hash: '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8', // "password" + role: 'student' +}); + +console.log(`๐Ÿงช Testing addUser with username: ${username}`); + +const options = { + hostname: 'localhost', + port: 3001, + path: '/addUser', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(postData) + } +}; + +const req = http.request(options, (res) => { + console.log(`๐Ÿ” Response Status: ${res.statusCode}`); + + let data = ''; + res.on('data', (chunk) => { + data += chunk; + }); + + res.on('end', () => { + if (res.statusCode === 200) { + console.log(`โœ… Success: ${data}`); + } else { + console.log(`โŒ Error: ${data}`); + } + + // Now try to verify the user was created by calling getUsers + verifyUserCreated(username); + }); +}); + +req.on('error', (e) => { + console.error(`๐Ÿ”ด Request Error: ${e.message}`); +}); + +// Write data to request body +req.write(postData); +req.end(); + +function verifyUserCreated(username) { + const verifyOptions = { + hostname: 'localhost', + port: 3001, + path: '/getUsers', + method: 'GET' + }; + + const verifyReq = http.request(verifyOptions, (res) => { + let data = ''; + res.on('data', (chunk) => { + data += chunk; + }); + + res.on('end', () => { + try { + const users = JSON.parse(data); + const foundUser = users.find(u => u.username === username); + + if (foundUser) { + console.log(`โœ… User ${username} was successfully created and verified`); + } else { + console.log(`โŒ User ${username} was not found in the users list`); + console.log(`๐Ÿ“‹ Current users: ${users.map(u => u.username).join(', ')}`); + } + + process.exit(0); + } catch (e) { + console.error(`๐Ÿ”ด Error parsing users response: ${e.message}`); + process.exit(1); + } + }); + }); + + verifyReq.on('error', (e) => { + console.error(`๐Ÿ”ด Verification Error: ${e.message}`); + process.exit(1); + }); + + verifyReq.end(); +}