Skip to content

Commit

Permalink
Merge branch 'develop' into jota
Browse files Browse the repository at this point in the history
  • Loading branch information
UO277938 committed Apr 25, 2024
2 parents 1b8efb3 + 402d234 commit baec6d2
Show file tree
Hide file tree
Showing 16 changed files with 1,256 additions and 78 deletions.
2 changes: 1 addition & 1 deletion gatewayservice/monitoring/prometheus/prometheus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ global:
scrape_configs:
- job_name: "example-nodejs-app"
static_configs:
- targets: ["gatewayservice:8000"]
- targets: ["gatewayservice-wiq5a:8000","userservice-wiq5a:8001","authservice-wiq5a:8002","questionservice-wiq5a:8003"]
1,247 changes: 1,202 additions & 45 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
},
"dependencies": {
"docker": "^1.0.0",
"express-prom-bundle": "^7.0.0",
"path-browserify": "^1.0.1",
"prom-client": "^15.1.2",
"start-server-and-test": "^2.0.3"
}
}
1 change: 1 addition & 0 deletions questionservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ee-first": "^1.1.1",
"encodeurl": "^1.0.2",
"es-define-property": "^1.0.0",
"express-prom-bundle": "^7.0.0",
"es-errors": "^1.3.0",
"escape-html": "^1.0.3",
"etag": "^1.8.1",
Expand Down
5 changes: 5 additions & 0 deletions questionservice/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let express = require('express');
const axios = require('axios');
const cors = require('cors');
const fs = require('fs');
const promBundle = require('express-prom-bundle');

const crypto = require('crypto');

Expand All @@ -16,6 +17,10 @@ let app = express();
app.disable("x-powered-by") //disable default information of express
app.use(cors(corsOptions));

//Prometheus configuration
const metricsMiddleware = promBundle({includeMethod: true});
app.use(metricsMiddleware);

// Cargamos las consultas SPARQL desde el fichero de configuración
const questions = JSON.parse(fs.readFileSync('questions.json', 'utf8'));

Expand Down
6 changes: 6 additions & 0 deletions userservice/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const User = require('./auth-model')
const promBundle = require('express-prom-bundle');


const app = express();
const port = 8002;
Expand All @@ -15,6 +17,10 @@ app.use(express.json());
const mongoUri = process.env.MONGODB_URI;
mongoose.connect(mongoUri);

//Prometheus configuration
const metricsMiddleware = promBundle({includeMethod: true});
app.use(metricsMiddleware);

// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
for (const field of requiredFields) {
Expand Down
1 change: 1 addition & 0 deletions userservice/authservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"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"
Expand Down
1 change: 1 addition & 0 deletions userservice/userservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"body-parser": "^1.20.2",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"express-prom-bundle": "^7.0.0",
"mongodb": "^6.5.0",
"mongoose": "^8.0.4"
},
Expand Down
5 changes: 5 additions & 0 deletions userservice/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const bodyParser = require('body-parser');
const User = require('./user-model');
const promBundle = require('express-prom-bundle');

const app = express();
const port = 8001;

// Middleware to parse JSON in request body
app.use(bodyParser.json());

//Prometheus configuration
const metricsMiddleware = promBundle({includeMethod: true});
app.use(metricsMiddleware);

// Connect to MongoDB
//const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb';
const mongoUri = process.env.MONGODB_URI;
Expand Down
4 changes: 2 additions & 2 deletions webapp/e2e/steps/01-register-form.steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ defineFeature(feature, test => {
when('I fill the data in the form and press submit', async () => {
await expect(page).toFill('input[name="username"]', username);
await expect(page).toFill('input[name="password"]', password);
await expect(page).toClick('button', { text: 'Add User' })
await expect(page).toClick('button', { text: 'Crear' })
});

then('A confirmation message should be shown in the screen', async () => {
await expect(page).toMatchElement("div", { text: "User added successfully" });
await expect(page).toMatchElement("div", { text: "Usuario creado correctamente" });
});
})

Expand Down
4 changes: 2 additions & 2 deletions webapp/e2e/steps/02-login.steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ defineFeature(feature, test => {
when('I enter valid username and password', async () => {
await expect(page).toFill('input[name="username"]', username);
await expect(page).toFill('input[name="password"]', password);
await expect(page).toClick('button', { text: 'Login' })
await expect(page).toClick('button', { text: 'Iniciar Sesión' })
});

then('A confirmation message should be shown in the screen', async () => {
await expect(page).toMatchElement("div", { text: "Hello "+username+"!" });
await expect(page).toMatchElement("div", { text: "¡Buenas, "+username+"!" });
});
})

Expand Down
2 changes: 1 addition & 1 deletion webapp/e2e/steps/03-home.steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defineFeature(feature, test => {
when('I navigate to the Home page', async () => {
await expect(page).toFill('input[name="username"]', username);
await expect(page).toFill('input[name="password"]', password);
await expect(page).toClick('button', { text: 'Login' })
await expect(page).toClick('button', { text: 'Iniciar Sesión' })

await expect(page).toClick('a[href="/"]', { text: 'WIQ 5A' });
await page.waitForNavigation();
Expand Down
10 changes: 5 additions & 5 deletions webapp/src/components/AddUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ const AddUser = () => {
return (
<Container component="main" maxWidth="xs" sx={{ marginTop: 4 }}>
<Typography component="h1" variant="h5">
Add User
Registrar Usuario
</Typography>
<TextField
name="username"
margin="normal"
fullWidth
label="Username"
label="Usuario"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
<TextField
name="password"
margin="normal"
fullWidth
label="Password"
label="Contraseña"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<Button variant="contained" color="primary" onClick={addUser}>
Add User
Crear
</Button>
<Snackbar open={openSnackbar} autoHideDuration={6000} onClose={handleCloseSnackbar} message="User added successfully" />
<Snackbar open={openSnackbar} autoHideDuration={6000} onClose={handleCloseSnackbar} message="Usuario creado correctamente" />
{error && (
<Snackbar open={!!error} autoHideDuration={6000} onClose={() => setError('')} message={`Error: ${error}`} />
)}
Expand Down
10 changes: 5 additions & 5 deletions webapp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ const Login = ({isLogged, setIsLogged, username, setUsername}) => {
) : (
<div>
<Typography component="h1" variant="h5">
Login
Iniciar Sesión
</Typography>
<TextField
name="username"
margin="normal"
fullWidth
label="Username"
label="Usuario"
value={username}

onChange={(e) => setUsername(e.target.value)}
Expand All @@ -62,15 +62,15 @@ const Login = ({isLogged, setIsLogged, username, setUsername}) => {
name="password"
margin="normal"
fullWidth
label="Password"
label="Contraseña"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<Button variant="contained" color="primary" onClick={loginUser}>
Login
Iniciar Sesión
</Button>
<Snackbar open={openSnackbar} autoHideDuration={6000} onClose={handleCloseSnackbar} message="Login successful" />
<Snackbar open={openSnackbar} autoHideDuration={6000} onClose={handleCloseSnackbar} message="Iniciada sesión exitosamente" />
{error && (
<Snackbar open={!!error} autoHideDuration={6000} onClose={() => setError('')} message={`Error: ${error}`} />
)}
Expand Down
14 changes: 7 additions & 7 deletions webapp/src/components/tests/AddUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ describe('AddUser component', () => {
it('should add user successfully', async () => {
render(<AddUser />);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const addUserButton = screen.getByRole('button', { name: /Add User/i });
const usernameInput = screen.getByLabelText(/Usuario/i);
const passwordInput = screen.getByLabelText(/Contraseña/i);
const addUserButton = screen.getByRole('button', { name: /Crear/i });

// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);
Expand All @@ -30,16 +30,16 @@ describe('AddUser component', () => {

// Wait for the Snackbar to be open
await waitFor(() => {
expect(screen.getByText(/User added successfully/i)).toBeInTheDocument();
expect(screen.getByText(/Usuario creado correctamente/i)).toBeInTheDocument();
});
});

it('should handle error when adding user', async () => {
render(<AddUser />);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const addUserButton = screen.getByRole('button', { name: /Add User/i });
const usernameInput = screen.getByLabelText(/Usuario/i);
const passwordInput = screen.getByLabelText(/Contraseña/i);
const addUserButton = screen.getByRole('button', { name: /Crear/i });

// Mock the axios.post request to simulate an error response
mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' });
Expand Down
20 changes: 10 additions & 10 deletions webapp/src/components/tests/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ describe('Login component', () => {
it('should render correctly', async () => {
render(<Login isLogged={false} setIsLogged={jest.fn()} username={''} setUsername={jest.fn()}/>);

expect(screen.getByLabelText(/Username/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Password/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Login/i })).toBeInTheDocument();
expect(screen.getByLabelText(/Usuario/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Contraseña/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Iniciar Sesión/i })).toBeInTheDocument();
})

it('should log in successfully', async () => {
render(<Login isLogged={false} setIsLogged={jest.fn()} username={'testUser'} setUsername={jest.fn()}/>);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Login/i });
const usernameInput = screen.getByLabelText(/Usuario/i);
const passwordInput = screen.getByLabelText(/Contraseña/i);
const loginButton = screen.getByRole('button', { name: /Iniciar Sesión/i });

// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/login').reply(200, { createdAt: '2024-01-01T12:34:56Z' });
Expand All @@ -38,7 +38,7 @@ describe('Login component', () => {
});

// Verify that the user information is displayed
expect(screen.getByText(/Login successful/i)).toBeInTheDocument();
expect(screen.getByText(/Iniciada sesión exitosamente/i)).toBeInTheDocument();
//expect(screen.getByText(/Your account was created on 1\/1\/2024/i)).toBeInTheDocument();
});

Expand All @@ -53,9 +53,9 @@ describe('Login component', () => {
it('should handle error when logging in', async () => {
render(<Login isLogged={false} setIsLogged={jest.fn()} username={'testUer'} setUsername={jest.fn()}/>);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Login/i });
const usernameInput = screen.getByLabelText(/Usuario/i);
const passwordInput = screen.getByLabelText(/Contraseña/i);
const loginButton = screen.getByRole('button', { name: /Iniciar Sesión/i });


mockAxios.onPost('http://localhost:8000/login').reply(401, { error: 'Unauthorized' });
Expand Down

0 comments on commit baec6d2

Please sign in to comment.