Commit 52982c49 by Rubén Ramírez

fix: [TestUsuariosController]: Corregidos test del controlador recurso

parent 39f49635
......@@ -13,7 +13,7 @@ import lombok.Setter;
@NoArgsConstructor
@AllArgsConstructor
public class DTOUsuario {
private Long id = null; // Permito que sea nulo al crear
private Long id;
@Email
private String email;
......@@ -24,4 +24,11 @@ public class DTOUsuario {
@NotBlank
private String contrasenia;
public DTOUsuario( String email, String nombreUsuario, String contrasenia) {
id = null;
this.email = email;
this.nombreUsuario = nombreUsuario;
this.contrasenia = contrasenia;
}
}
\ No newline at end of file
......@@ -35,7 +35,9 @@ public class UsuariosController {
try {
Usuario usuario = mapper.entity(dtoUsuario);
servicioUsuarios.crearUsuario(usuario);
return ResponseEntity.status(HttpStatus.CREATED).body(mapper.dto(usuario));
Usuario usuarioGuardado = servicioUsuarios.buscaUsuario(usuario.getEmail()); //Obtiene un id
return ResponseEntity.status(HttpStatus.CREATED).body(mapper.dto(usuarioGuardado));
} catch (UsuarioYaRegistrado e) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
......
......@@ -2,10 +2,13 @@ package com.ujaen.tfg.mangaffinity.rest;
import com.ujaen.tfg.mangaffinity.MangAffinityApplication;
import com.ujaen.tfg.mangaffinity.config.JpaTestConfig;
import com.ujaen.tfg.mangaffinity.entidades.Usuario;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOLoginRespuesta;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOUsuario;
import com.ujaen.tfg.mangaffinity.rest.DTO.Mapper;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
......@@ -31,7 +34,8 @@ public class TestUsuariosController {
int localPort;
TestRestTemplate restTemplateUsuarios;
@Autowired
private Mapper mapper;
@PostConstruct
......@@ -81,6 +85,9 @@ public class TestUsuariosController {
@Test
@DirtiesContext
void testIniciarSesion() {
var usuarioValido = new DTOUsuario("pedro@gmail.com", "Pedro", "pedrito");
restTemplateUsuarios.postForEntity("/", usuarioValido, DTOUsuario.class); // Registramos usuario
// Caso 1: Usuario no encontrado
var emailInexistente = "noexiste@example.com";
var cuerpoInexistente = Map.of("clave", "password123");
......@@ -92,9 +99,6 @@ public class TestUsuariosController {
Assertions.assertThat(respuestaInexistente.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
// Caso 2: Contraseña incorrecta
var usuarioValido = new DTOUsuario(null, "pedro@gmail.com", "Pedro", "pedrito");
restTemplateUsuarios.postForEntity("/", usuarioValido, DTOUsuario.class); // Registramos usuario
var cuerpoIncorrecto = Map.of("clave", "incorrecta");
var respuestaIncorrecta = restTemplateUsuarios.postForEntity(
"/" + usuarioValido.getEmail(),
......
......@@ -28,7 +28,6 @@ public class TestServicioUsuarios {
@Autowired
JwtUtil jwtUtil;
private static final Logger logger = LoggerFactory.getLogger(TestServicioUsuarios.class);
@Test
@DirtiesContext
......
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