Commit 48eaf4ef by Antonio Rueda

Versión final tras seguridad

parent e13d6bca
...@@ -14,14 +14,12 @@ import es.ujaen.dae.reservahoteles.rest.dto.DReserva; ...@@ -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.DUsuario;
import es.ujaen.dae.reservahoteles.rest.dto.Mapeador; import es.ujaen.dae.reservahoteles.rest.dto.Mapeador;
import es.ujaen.dae.reservahoteles.servicios.ServicioReservas; import es.ujaen.dae.reservahoteles.servicios.ServicioReservas;
import jakarta.annotation.PostConstruct;
import jakarta.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; 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.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
...@@ -45,8 +43,6 @@ public class ControladorReservas { ...@@ -45,8 +43,6 @@ public class ControladorReservas {
@Autowired @Autowired
ServicioReservas servicioReservas; ServicioReservas servicioReservas;
Usuario direccion;
// Definir un mapeado global para cualquier excepción de validación de beans // Definir un mapeado global para cualquier excepción de validación de beans
@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY) @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
@ExceptionHandler(ConstraintViolationException.class) @ExceptionHandler(ConstraintViolationException.class)
...@@ -65,9 +61,9 @@ public class ControladorReservas { ...@@ -65,9 +61,9 @@ public class ControladorReservas {
} }
@GetMapping("/usuarios/{email}") @GetMapping("/usuarios/{email}")
public ResponseEntity<DUsuario> loginCliente(@PathVariable String email, @RequestParam String clave) { public ResponseEntity<DUsuario> obtenerCliente(@PathVariable String email) {
try { try {
Usuario usuario = servicioReservas.login(email, clave).orElseThrow(UsuarioNoRegistrado::new); Usuario usuario = servicioReservas.buscarUsuario(email).orElseThrow(UsuarioNoRegistrado::new);
return ResponseEntity.ok(mapeador.dto(usuario)); return ResponseEntity.ok(mapeador.dto(usuario));
} }
catch(UsuarioNoRegistrado e) { catch(UsuarioNoRegistrado e) {
...@@ -77,7 +73,7 @@ public class ControladorReservas { ...@@ -77,7 +73,7 @@ public class ControladorReservas {
@PostMapping("/hoteles") @PostMapping("/hoteles")
public ResponseEntity<Void> nuevoHotel(@RequestBody DHotel hotel) { public ResponseEntity<Void> nuevoHotel(@RequestBody DHotel hotel) {
servicioReservas.nuevoHotel(direccion, mapeador.entidadNueva(hotel)); servicioReservas.nuevoHotel(mapeador.entidadNueva(hotel));
return ResponseEntity.status(HttpStatus.CREATED).build(); return ResponseEntity.status(HttpStatus.CREATED).build();
} }
......
...@@ -80,32 +80,39 @@ public class TestControladorReservas { ...@@ -80,32 +80,39 @@ public class TestControladorReservas {
); );
assertThat(respuesta.getStatusCode()).isEqualTo(HttpStatus.CREATED); 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}", "/usuarios/{email}",
DUsuario.class, DUsuario.class,
"ppp@gmail.com" usuario.email()
); );
assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
respuestaLogin = restTemplate.getForEntity( respuestaLogin = restTemplate.withBasicAuth(usuario.email(), "xyz").getForEntity(
"/usuarios/{email}?clave={clave}", "/usuarios/{email}",
DUsuario.class, DUsuario.class,
usuario.email(), usuario.email()
"xyx"
); );
assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
respuestaLogin = restTemplate.getForEntity( respuestaLogin = restTemplate.withBasicAuth(usuario.email(), usuario.clave()).getForEntity(
"/usuarios/{email}?clave={clave}", "/usuarios/{email}",
DUsuario.class, DUsuario.class,
usuario.email(), usuario.email()
usuario.clave()
); );
assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(respuestaLogin.getBody().email()).isEqualTo(usuario.email()); 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 @Test
...@@ -114,14 +121,14 @@ public class TestControladorReservas { ...@@ -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 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 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", "/hoteles",
hotel1, hotel1,
DHotel.class DHotel.class
); );
assertThat(respuesta.getStatusCode()).isEqualTo(HttpStatus.CREATED); assertThat(respuesta.getStatusCode()).isEqualTo(HttpStatus.CREATED);
respuesta = restTemplate.postForEntity( respuesta = restTemplate.withBasicAuth("direccion@hotelxyz.es", "SeCrEtO").postForEntity(
"/hoteles", "/hoteles",
hotel2, hotel2,
DHotel.class DHotel.class
...@@ -146,19 +153,19 @@ public class TestControladorReservas { ...@@ -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 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); 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", "/hoteles",
hotel1, hotel1,
DHotel.class DHotel.class
); );
restTemplate.postForEntity( restTemplate.withBasicAuth("direccion@hotelxyz.es", "SeCrEtO").postForEntity(
"/hoteles", "/hoteles",
hotel2, hotel2,
DHotel.class DHotel.class
); );
restTemplate.postForEntity( restTemplate.withBasicAuth("direccion@hotelxyz.es", "SeCrEtO").postForEntity(
"/hoteles", "/hoteles",
hotel3, hotel3,
DHotel.class DHotel.class
...@@ -185,7 +192,7 @@ public class TestControladorReservas { ...@@ -185,7 +192,7 @@ public class TestControladorReservas {
@DirtiesContext @DirtiesContext
void testReservaHotel() { void testReservaHotel() {
var hotel = new DHotel(0, "Bed and Breakfast Almería", "Almería", "Almería", "04001", 2, 2, 60, 100); 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", "/hoteles",
hotel, hotel,
DHotel.class DHotel.class
...@@ -210,7 +217,7 @@ public class TestControladorReservas { ...@@ -210,7 +217,7 @@ public class TestControladorReservas {
LocalDate.now().plusDays(10), 0, 1, LocalDate.now().plusDays(10), 0, 1,
usuario.email()); usuario.email());
var respuestaReserva = restTemplate.postForEntity( var respuestaReserva = restTemplate.withBasicAuth(usuario.email(), usuario.clave()).postForEntity(
"/hoteles/{id}/reservas", "/hoteles/{id}/reservas",
reserva, reserva,
DReserva.class, 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