Skip to content

Commit

Permalink
Merge branch 'webapp_interface' of https://github.com/Arquisoft/wiq_es6c
Browse files Browse the repository at this point in the history
 into webapp_interface
  • Loading branch information
marco-qg committed Apr 29, 2024
2 parents fcf79dc + bf06245 commit 72c89a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
20 changes: 18 additions & 2 deletions webapp/src/components/AddUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
};
Expand Down
14 changes: 7 additions & 7 deletions webapp/src/components/AddUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' } });

Expand All @@ -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();
});
});

Expand Down Expand Up @@ -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' } });

Expand Down

0 comments on commit 72c89a6

Please sign in to comment.