Commit 48eaf4ef by Antonio Rueda

Versión final tras seguridad

parent e13d6bca
......@@ -14,14 +14,12 @@ import es.ujaen.dae.reservahoteles.rest.dto.DReserva;
import es.ujaen.dae.reservahoteles.rest.dto.DUsuario;
import es.ujaen.dae.reservahoteles.rest.dto.Mapeador;
import es.ujaen.dae.reservahoteles.servicios.ServicioReservas;
import jakarta.annotation.PostConstruct;
import jakarta.validation.ConstraintViolationException;
import java.time.LocalDate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -45,8 +43,6 @@ public class ControladorReservas {
@Autowired
ServicioReservas servicioReservas;
Usuario direccion;
// Definir un mapeado global para cualquier excepción de validación de beans
@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
@ExceptionHandler(ConstraintViolationException.class)
......@@ -65,9 +61,9 @@ public class ControladorReservas {
}
@GetMapping("/usuarios/{email}")
public ResponseEntity<DUsuario> loginCliente(@PathVariable String email, @RequestParam String clave) {
public ResponseEntity<DUsuario> obtenerCliente(@PathVariable String email) {
try {
Usuario usuario = servicioReservas.login(email, clave).orElseThrow(UsuarioNoRegistrado::new);
Usuario usuario = servicioReservas.buscarUsuario(email).orElseThrow(UsuarioNoRegistrado::new);
return ResponseEntity.ok(mapeador.dto(usuario));
}
catch(UsuarioNoRegistrado e) {
......@@ -77,7 +73,7 @@ public class ControladorReservas {
@PostMapping("/hoteles")
public ResponseEntity<Void> nuevoHotel(@RequestBody DHotel hotel) {
servicioReservas.nuevoHotel(direccion, mapeador.entidadNueva(hotel));
servicioReservas.nuevoHotel(mapeador.entidadNueva(hotel));
return ResponseEntity.status(HttpStatus.CREATED).build();
}
......
......@@ -80,32 +80,39 @@ public class TestControladorReservas {
);
assertThat(respuesta.getStatusCode()).isEqualTo(HttpStatus.CREATED);
var respuestaLogin = restTemplate.withBasicAuth("ppp@gmail.com", "miClAvE").getForEntity(
var respuestaLogin = restTemplate.withBasicAuth("ppp@gmail.com", usuario.clave()).getForEntity(
"/usuarios/{email}",
DUsuario.class,
"ppp@gmail.com"
usuario.email()
);
assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
respuestaLogin = restTemplate.getForEntity(
"/usuarios/{email}?clave={clave}",
respuestaLogin = restTemplate.withBasicAuth(usuario.email(), "xyz").getForEntity(
"/usuarios/{email}",
DUsuario.class,
usuario.email(),
"xyx"
usuario.email()
);
assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
respuestaLogin = restTemplate.getForEntity(
"/usuarios/{email}?clave={clave}",
respuestaLogin = restTemplate.withBasicAuth(usuario.email(), usuario.clave()).getForEntity(
"/usuarios/{email}",
DUsuario.class,
usuario.email(),
usuario.clave()
usuario.email()
);
assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(respuestaLogin.getBody().email()).isEqualTo(usuario.email());
respuestaLogin = restTemplate.withBasicAuth("direccion@hotelxyz.es", "SeCrEtO").getForEntity(
"/usuarios/{email}",
DUsuario.class,
usuario.email()
);
assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(respuestaLogin.getBody().email()).isEqualTo(usuario.email());
}
@Test
......@@ -114,14 +121,14 @@ public class TestControladorReservas {
var hotel1 = new DHotel(0, "Gran Hotel Almería", "Almería", "Almería", "04001", 25, 50, 100, 180);
var hotel2 = new DHotel(0, "Hotel Infanta Cristina", "Jaén", "Jaén", "23009", 30, 60, 120, 200);
var respuesta = restTemplate.postForEntity(
var respuesta = restTemplate.withBasicAuth("direccion@hotelxyz.es", "SeCrEtO").postForEntity(
"/hoteles",
hotel1,
DHotel.class
);
assertThat(respuesta.getStatusCode()).isEqualTo(HttpStatus.CREATED);
respuesta = restTemplate.postForEntity(
respuesta = restTemplate.withBasicAuth("direccion@hotelxyz.es", "SeCrEtO").postForEntity(
"/hoteles",
hotel2,
DHotel.class
......@@ -146,19 +153,19 @@ public class TestControladorReservas {
var hotel2 = new DHotel(0, "Hotel Espejo del Mar", "Almería", "Almería", "04001", 15, 35, 80, 110);
var hotel3 = new DHotel(0, "Hotel Infanta Cristina", "Jaén", "Jaén", "23009", 30, 60, 120, 200);
restTemplate.postForEntity(
restTemplate.withBasicAuth("direccion@hotelxyz.es", "SeCrEtO").postForEntity(
"/hoteles",
hotel1,
DHotel.class
);
restTemplate.postForEntity(
restTemplate.withBasicAuth("direccion@hotelxyz.es", "SeCrEtO").postForEntity(
"/hoteles",
hotel2,
DHotel.class
);
restTemplate.postForEntity(
restTemplate.withBasicAuth("direccion@hotelxyz.es", "SeCrEtO").postForEntity(
"/hoteles",
hotel3,
DHotel.class
......@@ -185,7 +192,7 @@ public class TestControladorReservas {
@DirtiesContext
void testReservaHotel() {
var hotel = new DHotel(0, "Bed and Breakfast Almería", "Almería", "Almería", "04001", 2, 2, 60, 100);
restTemplate.postForEntity(
restTemplate.withBasicAuth("direccion@hotelxyz.es", "SeCrEtO").postForEntity(
"/hoteles",
hotel,
DHotel.class
......@@ -210,7 +217,7 @@ public class TestControladorReservas {
LocalDate.now().plusDays(10), 0, 1,
usuario.email());
var respuestaReserva = restTemplate.postForEntity(
var respuestaReserva = restTemplate.withBasicAuth(usuario.email(), usuario.clave()).postForEntity(
"/hoteles/{id}/reservas",
reserva,
DReserva.class,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment