diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 5dff392..918b9df 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -37,27 +37,16 @@ app.post('/login', async (req, res) => { } }); - -function validateCredentials(username, password) { - // Verifica si la contraseña es erronea - if (password.length < 8) { - return false; - } - - return true; -} - app.post('/adduser', async (req, res) => { - - const isValidUser = validateCredentials(req.body.username, req.body.password); + try { + const { username, password, valido, mensajeError } = req.body; - if (!isValidUser) { - // Si las credenciales son inválidas, devuelve un error 401 - res.status(401).json({ error: 'Credenciales incorrectas. La contraseña debe contener al menos 8 caracteres' }); - return; // Termina la ejecución de la función para evitar ejecutar el código restante - } + if (!valido) { + // Si las credenciales son inválidas, devuelve un error 401 + res.status(401).json({ error: mensajeError }); + return; // Termina la ejecución de la función para evitar ejecutar el código restante + } - try { // Forward the add user request to the user service const userResponse = await axios.post(userServiceUrl+'/adduser', req.body); res.json(userResponse.data); @@ -110,6 +99,15 @@ app.get('/getUserData', async (req, res) => { } }); +app.get('/getUsernames', async (req, res) => { + try{ + const getUserDataResponse = await axios.get(userServiceUrl+ `/getUsernames`) + res.json(getUserDataResponse.data); + }catch(error){ + res.status(error.response.status).json({error: error.response.data.error}); + } +}); + // Read the OpenAPI YAML file synchronously let openapiPath='./openapi.yaml'; if (fs.existsSync(openapiPath)) { diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index 3a08129..c3e55c5 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -49,17 +49,6 @@ describe('Gateway Service', () => { expect(response.status).toBe(500); expect(response.body).toEqual({ error: errorMessage }); }); - - // Test /adduser endpoint - it('deberia añadir usuario correctamente', async () => { - const response = await request(app) - .post('/adduser') - .send({ username: 'newuser', password: 'newpassword' }); - - // Verificamos que la respuesta tenga un código de estado 200 y un ID de usuario - expect(response.statusCode).toBe(200); - expect(response.body.userId).toBe('mockedUserId'); - }); // Probamos con una pregunta errónea it('debería devolver error con esa pregunta', async () => { diff --git a/questionservice/questionservice/package-lock.json b/questionservice/questionservice/package-lock.json new file mode 100644 index 0000000..e90bc8d --- /dev/null +++ b/questionservice/questionservice/package-lock.json @@ -0,0 +1,99 @@ +{ + "name": "questionservice", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "questionservice": "file:.." + } + }, + "..": { + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "accepts": "^1.3.8", + "array-flatten": "^1.1.1", + "asynckit": "^0.4.0", + "axios": "^1.6.7", + "body-parser": "^1.20.2", + "bytes": "^3.1.2", + "call-bind": "^1.0.7", + "combined-stream": "^1.0.8", + "content-disposition": "^0.5.4", + "content-type": "^1.0.5", + "cookie": "^0.5.0", + "cookie-signature": "^1.0.6", + "cors": "^2.8.5", + "crypto": "^1.0.1", + "debug": "^2.6.9", + "define-data-property": "^1.1.4", + "delayed-stream": "^1.0.0", + "depd": "^2.0.0", + "destroy": "^1.2.0", + "ee-first": "^1.1.1", + "encodeurl": "^1.0.2", + "es-define-property": "^1.0.0", + "es-errors": "^1.3.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "express": "^4.18.3", + "express-prom-bundle": "^7.0.0", + "finalhandler": "^1.2.0", + "follow-redirects": "^1.15.5", + "form-data": "^4.0.0", + "forwarded": "^0.2.0", + "fresh": "^0.5.2", + "function-bind": "^1.1.2", + "get-intrinsic": "^1.2.4", + "gopd": "^1.0.1", + "has-property-descriptors": "^1.0.2", + "has-proto": "^1.0.3", + "has-symbols": "^1.0.3", + "hasown": "^2.0.1", + "http-errors": "^2.0.0", + "iconv-lite": "^0.4.24", + "inherits": "^2.0.4", + "ipaddr.js": "^1.9.1", + "jest": "^29.7.0", + "log": "^6.3.1", + "media-typer": "^0.3.0", + "merge-descriptors": "^1.0.1", + "methods": "^1.1.2", + "mime": "^1.6.0", + "mime-db": "^1.52.0", + "mime-types": "^2.1.35", + "ms": "^2.0.0", + "negotiator": "^0.6.3", + "object-assign": "^4.1.1", + "object-inspect": "^1.13.1", + "on-finished": "^2.4.1", + "parseurl": "^1.3.3", + "path-to-regexp": "^0.1.7", + "proxy-addr": "^2.0.7", + "proxy-from-env": "^1.1.0", + "qs": "^6.11.0", + "range-parser": "^1.2.1", + "raw-body": "^2.5.2", + "safe-buffer": "^5.2.1", + "safer-buffer": "^2.1.2", + "send": "^0.18.0", + "serve-static": "^1.15.0", + "set-function-length": "^1.2.1", + "setprototypeof": "^1.2.0", + "side-channel": "^1.0.6", + "statuses": "^2.0.1", + "supertest": "^6.3.4", + "toidentifier": "^1.0.1", + "type-is": "^1.6.18", + "unpipe": "^1.0.0", + "utils-merge": "^1.0.1", + "vary": "^1.1.2" + } + }, + "node_modules/questionservice": { + "resolved": "..", + "link": true + } + } +} diff --git a/questionservice/questionservice/package.json b/questionservice/questionservice/package.json new file mode 100644 index 0000000..a0e8caf --- /dev/null +++ b/questionservice/questionservice/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "questionservice": "file:.." + } +} diff --git a/userservice/authservice/userservice/authservice/package-lock.json b/userservice/authservice/userservice/authservice/package-lock.json new file mode 100644 index 0000000..8c66fd9 --- /dev/null +++ b/userservice/authservice/userservice/authservice/package-lock.json @@ -0,0 +1,35 @@ +{ + "name": "authservice", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "authservice": "file:../.." + } + }, + "../..": { + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "bcrypt": "^5.1.1", + "body-parser": "^1.20.2", + "dotenv": "^16.4.5", + "express": "^4.18.2", + "express-prom-bundle": "^7.0.0", + "jsonwebtoken": "^9.0.2", + "mongodb": "^6.5.0", + "mongoose": "^8.0.4" + }, + "devDependencies": { + "jest": "^29.7.0", + "mongodb-memory-server": "^9.1.5", + "supertest": "^6.3.4" + } + }, + "node_modules/authservice": { + "resolved": "../..", + "link": true + } + } +} diff --git a/userservice/authservice/userservice/authservice/package.json b/userservice/authservice/userservice/authservice/package.json new file mode 100644 index 0000000..81b6679 --- /dev/null +++ b/userservice/authservice/userservice/authservice/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "authservice": "file:../.." + } +} diff --git a/userservice/userservice/user-service.js b/userservice/userservice/user-service.js index 5fe4f2e..4b88f7f 100644 --- a/userservice/userservice/user-service.js +++ b/userservice/userservice/user-service.js @@ -83,6 +83,19 @@ app.get('/getUserData', async (req, res) => { } }); +app.get('/getUsernames', async (req, res) => { + try { + const users = await User.find(); + if (!users || users.length===0) { + return res.status(200).json({ success: true, message: 'No hay usuarios en la base de datos' }); + } + const usernames = users.map(user => user.username); + return res.status(200).json({ usernames }); + } catch (error) { + return res.status(500).json({ success: false, message: 'Error al obtener los datos de usuario' }); + } +}); + const server = app.listen(port, () => { console.log(`User Service listening at http://localhost:${port}`); }); diff --git a/webapp/src/components/AddUser.js b/webapp/src/components/AddUser.js index 0eadcac..f6eedb8 100644 --- a/webapp/src/components/AddUser.js +++ b/webapp/src/components/AddUser.js @@ -10,16 +10,38 @@ const AddUser = () => { const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [openSnackbar, setOpenSnackbar] = useState(false); + const [mensajeError, setMensajeError] =useState(''); const addUser = async () => { try { - await axios.post(`${apiEndpoint}/adduser`, { username, password }); + var valido = await validateCredentials(username, password); + await axios.post(`${apiEndpoint}/adduser`, { username, password, valido, mensajeError }); setOpenSnackbar(true); } catch (error) { setError(error.response.data.error); } }; + + + async function validateCredentials(username, password){ + try { + const response = await axios.get(`${apiEndpoint}/getUsernames`); + const usernames = response.data.usernames; + if (usernames.includes(username)){ + setMensajeError('Credenciales incorrectas. El nombre de usuario esta en uso') + return false; + } + if (password.length < 8) { + setMensajeError('Credenciales incorrectas. La contraseña debe contener al menos 8 caracteres') + return false; + } + return true; + } catch (error) { + setError('Error al cargar la información'); + } + }; + const handleCloseSnackbar = () => { setOpenSnackbar(false); };