From 06662e86f9c103148427fa7cad6a8f6877cf2cb5 Mon Sep 17 00:00:00 2001 From: Liliana Date: Mon, 29 Apr 2024 17:38:19 +0200 Subject: [PATCH] Checks to ensure that a user is not registered with blank fields and one who is already in the system. --- webapp/src/components/AddUser.js | 20 ++++++++++++++++++-- webapp/src/components/AddUser.test.js | 14 +++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/webapp/src/components/AddUser.js b/webapp/src/components/AddUser.js index 0936685a..010b1d7b 100644 --- a/webapp/src/components/AddUser.js +++ b/webapp/src/components/AddUser.js @@ -18,12 +18,18 @@ const AddUser = () => { const [openSnackbar, setOpenSnackbar] = useState(false); const addUser = async () => { - if (name.trim() === '' || surname.trim() === '') { - setError('Por favor, introduzca tanto el nombre como los apellidos.'); + if (name.trim() === '' || surname.trim() === '' || username.trim() === '' || password.trim() === '' || confirmPassword.trim() === '') { + setError('Todos los campos deben de estar rellenos.'); } else if(password !== confirmPassword){ setError('Las contraseñas no coinciden.'); } else { try { + const isAvailable = await checkUsernameAvailability(username, password); + if (isAvailable != false) { + setError('Usuario ya registrado.'); + return; + } + await axios.post(`${apiEndpoint}/adduser`, { username, password }); setOpenSnackbar(true); } catch (error) { @@ -32,6 +38,16 @@ const AddUser = () => { } }; + const checkUsernameAvailability = async (username, password) => { + try { + const response = await axios.post(`${apiEndpoint}/check-username`, { username }); + return response.data; + } catch (error) { + console.error("Error al comprobar la disponibilidad del nombre de usuario:", error); + return false; + } + } + const handleCloseSnackbar = () => { setOpenSnackbar(false); }; diff --git a/webapp/src/components/AddUser.test.js b/webapp/src/components/AddUser.test.js index cd13d7d1..a52f8e5f 100644 --- a/webapp/src/components/AddUser.test.js +++ b/webapp/src/components/AddUser.test.js @@ -25,9 +25,9 @@ describe('AddUser component', () => { mockAxios.onPost('http://localhost:8000/adduser').reply(200); // Simulate user input - fireEvent.change(nameInput, { target: { value: 'testUser' } }); - fireEvent.change(surnameInput, { target: { value: 'testUser' } }); - fireEvent.change(usernameInput, { target: { value: 'testUser' } }); + fireEvent.change(nameInput, { target: { value: 'testUsera' } }); + fireEvent.change(surnameInput, { target: { value: 'testUsera' } }); + fireEvent.change(usernameInput, { target: { value: 'testUsera' } }); fireEvent.change(passwordInput, { target: { value: 'testPassword' } }); fireEvent.change(confirmPasswordInput, { target: { value: 'testPassword' } }); @@ -50,7 +50,7 @@ describe('AddUser component', () => { // Wait for the Snackbar to be open await waitFor(() => { - expect(screen.getByText(/Por favor, introduzca tanto el nombre como los apellidos./i)).toBeInTheDocument(); + expect(screen.getByText(/Todos los campos deben de estar rellenos./i)).toBeInTheDocument(); }); }); @@ -80,9 +80,9 @@ describe('AddUser component', () => { mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' }); // Simulate user input - fireEvent.change(screen.getByLabelText(/Nombre/), { target: { value: 'testUser' } }); - fireEvent.change(screen.getByLabelText(/Apellidos/i), { target: { value: 'testUser' } }); - fireEvent.change(screen.getByLabelText(/Usuario/i), { target: { value: 'testUser' } }); + fireEvent.change(screen.getByLabelText(/Nombre/), { target: { value: 'a' } }); + fireEvent.change(screen.getByLabelText(/Apellidos/i), { target: { value: 'a' } }); + fireEvent.change(screen.getByLabelText(/Usuario/i), { target: { value: 'a' } }); fireEvent.change(screen.getAllByLabelText(/Contraseña/i)[0], { target: { value: 'testPassword' } }); fireEvent.change(screen.getByLabelText(/Repetir contraseña/i), { target: { value: 'testPassword' } });