Commit 3c4635e0 by Rubén Ramírez

feat: [TestUsuariosController]: Implementada el test de inicio de sesión en el controlador

parent bd99341a
......@@ -2,6 +2,7 @@ package com.ujaen.tfg.mangaffinity.rest;
import com.ujaen.tfg.mangaffinity.MangAffinityApplication;
import com.ujaen.tfg.mangaffinity.config.JpaTestConfig;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOLoginRespuesta;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOUsuario;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
......@@ -18,6 +19,8 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import jakarta.annotation.PostConstruct;
import java.util.Map;
@SpringBootTest(classes = {MangAffinityApplication.class, JpaTestConfig.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class TestUsuariosController {
......@@ -74,4 +77,45 @@ public class TestUsuariosController {
);
Assertions.assertThat(respuestaDuplicado.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
}
@Test
@DirtiesContext
void testIniciarSesion() {
// Caso 1: Usuario no encontrado
var emailInexistente = "noexiste@example.com";
var cuerpoInexistente = Map.of("clave", "password123");
var respuestaInexistente = restTemplateUsuarios.postForEntity(
"/" + emailInexistente,
cuerpoInexistente,
Void.class
);
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(),
cuerpoIncorrecto,
Void.class
);
Assertions.assertThat(respuestaIncorrecta.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
// Caso 3: Inicio de sesión exitoso
var cuerpoValido = Map.of("clave", "pedrito");
var respuestaValida = restTemplateUsuarios.postForEntity(
"/" + usuarioValido.getEmail(),
cuerpoValido,
DTOLoginRespuesta.class
);
Assertions.assertThat(respuestaValida.getStatusCode()).isEqualTo(HttpStatus.OK);
Assertions.assertThat(respuestaValida.getBody()).isNotNull();
Assertions.assertThat(respuestaValida.getBody().getToken()).isNotEmpty();
}
}
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