From 0c494a5f060b3ee4eaa60b26dd391f6294097eec Mon Sep 17 00:00:00 2001 From: Santiago21112001 Date: Fri, 26 Apr 2024 16:36:02 +0200 Subject: [PATCH] solucionado problema variable entorno secret-key al ejecutar e2e --- users/authservice/auth-service.js | 7 +++++-- webapp/e2e/test-environment-setup.js | 2 ++ webapp/package.json | 2 +- webapp/src/components/pages/Creditos.js | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/users/authservice/auth-service.js b/users/authservice/auth-service.js index 900d87a..8bae569 100644 --- a/users/authservice/auth-service.js +++ b/users/authservice/auth-service.js @@ -14,6 +14,8 @@ app.use(express.json()); const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb'; mongoose.connect(mongoUri); +const secretKey = process.env.SECRET_KEY + // Function to validate required fields in the request body function validateRequiredFields(req, requiredFields) { for (const field of requiredFields) { @@ -37,20 +39,21 @@ app.post('/login', async (req, res) => { // Check if the user exists and verify the password if (user && await bcrypt.compare(password, user.password)) { // Generate a JWT token - const token = jwt.sign({ userId: user._id,username:username }, process.env.SECRET_KEY, { expiresIn: '1h' }); + const token = jwt.sign({ userId: user._id,username:username }, secretKey, { expiresIn: '1h' }); // Respond with the token and user information res.json({ token: token, username: username, createdAt: user.createdAt }); } else { res.status(401).json({ error: 'Invalid credentials' }); } } catch (error) { + console.error(error) res.status(500).json({ error: 'Internal Server Error' }); } }); app.get('/validate/:token', (req, res) => { try { - const {iat, exp, ...result} = jwt.verify(req.params.token, process.env.SECRET_KEY); + const {iat, exp, ...result} = jwt.verify(req.params.token, secretKey); res.json({ data: result, valid: true }); } catch (error) { res.json({ valid: false }); diff --git a/webapp/e2e/test-environment-setup.js b/webapp/e2e/test-environment-setup.js index c6096b9..b717a52 100644 --- a/webapp/e2e/test-environment-setup.js +++ b/webapp/e2e/test-environment-setup.js @@ -12,6 +12,8 @@ async function startServer() { mongoserver = await MongoMemoryServer.create(); const mongoUri = mongoserver.getUri(); process.env.MONGODB_URI = mongoUri; + process.env.SECRET_KEY = 'F0f`7a@G90J{m-$4Wml@gy!cCcgUc#=K' + console.log(process.env.MONGODB_URI) userservice = await require("../../users/userservice/user-service"); authservice = await require("../../users/authservice/auth-service"); historyservice = await require("../../users/historyservice/history-service"); diff --git a/webapp/package.json b/webapp/package.json index bc79485..12c9a96 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -22,7 +22,7 @@ "build": "react-scripts build", "prod": "serve -s build", "test": "react-scripts test --transformIgnorePatterns 'node_modules/(?!axios)/'", - "test:e2e": "start-server-and-test 'node e2e/test-environment-setup.js' http://localhost:8000/health prod 3000 \"cd e2e && jest\"", + "test:e2e": "start-server-and-test 'node e2e/test-environment-setup.js' http://localhost:8000/health prod 3000 \"cd e2e && jest --runInBand\"", "eject": "react-scripts eject" }, "eslintConfig": { diff --git a/webapp/src/components/pages/Creditos.js b/webapp/src/components/pages/Creditos.js index f606e04..81bd467 100644 --- a/webapp/src/components/pages/Creditos.js +++ b/webapp/src/components/pages/Creditos.js @@ -1,4 +1,5 @@ -import React, { useRef, useEffect } from 'react'; +//import React, { useRef, useEffect } from 'react'; +import React from 'react'; import '../../App.css'; function Creditos() {