Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Rubén Ramírez
/
MangAffinity
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c5023577
authored
Mar 28, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
fix: [TestUsuariosController]:Mejorados todos los tests para hacer todas las comprobaciones
parent
06a23eb2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
37 deletions
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestUsuariosController.java
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestUsuariosController.java
View file @
c5023577
...
@@ -48,17 +48,16 @@ public class TestUsuariosController {
...
@@ -48,17 +48,16 @@ public class TestUsuariosController {
@LocalServerPort
@LocalServerPort
int
localPort
;
int
localPort
;
@Autowired
@Autowired
private
TestRestTemplate
restTemplate
;
private
TestRestTemplate
restTemplate
;
@Autowired
@Autowired
TestRestTemplate
restTemplateUsuarios
;
TestRestTemplate
restTemplateUsuarios
;
@Autowired
@Autowired
private
JdbcTemplate
jdbcTemplate
;
private
JdbcTemplate
jdbcTemplate
;
@PostConstruct
@PostConstruct
void
crearRestTemplate
()
{
void
crearRestTemplate
()
{
var
restTemplateBuilder
=
new
RestTemplateBuilder
()
var
restTemplateBuilder
=
new
RestTemplateBuilder
()
...
@@ -70,16 +69,16 @@ public class TestUsuariosController {
...
@@ -70,16 +69,16 @@ public class TestUsuariosController {
@Test
@Test
@DirtiesContext
@DirtiesContext
void
testRegistrarUsuario
()
{
void
testRegistrarUsuario
()
{
//
Caso 1: Intentar
registrar un usuario con datos inválidos; un email no válido
//
Intento
registrar un usuario con datos inválidos; un email no válido
var
usuarioInvalido
=
new
DTOUsuario
(
null
,
""
,
"UsuarioInvalido"
,
"invalido"
);
var
usuarioInvalido
=
new
DTOUsuario
(
null
,
""
,
"UsuarioInvalido"
,
"invalido"
);
var
respuestaInvalida
=
restTemplateUsuarios
.
postForEntity
(
var
respuestaInvalida
=
restTemplateUsuarios
.
postForEntity
(
"/"
,
"/"
,
usuarioInvalido
,
usuarioInvalido
,
Void
.
class
Void
.
class
);
);
Assertions
.
assertThat
(
respuestaInvalida
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNPROCESSABLE_ENTITY
);
Assertions
.
assertThat
(
respuestaInvalida
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FORBIDDEN
);
//
Caso 2: Registrar
un usuario correctamente
//
Registro
un usuario correctamente
var
usuarioValido
=
new
DTOUsuario
(
null
,
"pedro@gmail.com"
,
"Pedro"
,
"pedrito"
);
var
usuarioValido
=
new
DTOUsuario
(
null
,
"pedro@gmail.com"
,
"Pedro"
,
"pedrito"
);
ResponseEntity
<
Void
>
respuestaValida
=
restTemplateUsuarios
.
postForEntity
(
ResponseEntity
<
Void
>
respuestaValida
=
restTemplateUsuarios
.
postForEntity
(
"/"
,
"/"
,
...
@@ -88,7 +87,7 @@ public class TestUsuariosController {
...
@@ -88,7 +87,7 @@ public class TestUsuariosController {
);
);
Assertions
.
assertThat
(
respuestaValida
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
Assertions
.
assertThat
(
respuestaValida
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
//
Caso 3: Intentar
registrar un usuario con el mismo email
//
Intento
registrar un usuario con el mismo email
var
usuarioDuplicado
=
new
DTOUsuario
(
null
,
"pedro@gmail.com"
,
"Pedro"
,
"pedrito"
);
var
usuarioDuplicado
=
new
DTOUsuario
(
null
,
"pedro@gmail.com"
,
"Pedro"
,
"pedrito"
);
var
respuestaDuplicado
=
restTemplateUsuarios
.
postForEntity
(
var
respuestaDuplicado
=
restTemplateUsuarios
.
postForEntity
(
"/"
,
"/"
,
...
@@ -96,6 +95,7 @@ public class TestUsuariosController {
...
@@ -96,6 +95,7 @@ public class TestUsuariosController {
Void
.
class
Void
.
class
);
);
Assertions
.
assertThat
(
respuestaDuplicado
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CONFLICT
);
Assertions
.
assertThat
(
respuestaDuplicado
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CONFLICT
);
}
}
@Test
@Test
...
@@ -110,7 +110,7 @@ public class TestUsuariosController {
...
@@ -110,7 +110,7 @@ public class TestUsuariosController {
);
);
Assertions
.
assertThat
(
respuestaRegistro
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
Assertions
.
assertThat
(
respuestaRegistro
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
//
// Caso 1: Intentar
iniciar sesión con un usuario que no existe
//
Intento
iniciar sesión con un usuario que no existe
var
emailInexistente
=
"noexiste@example.com"
;
var
emailInexistente
=
"noexiste@example.com"
;
HttpHeaders
headers
=
new
HttpHeaders
();
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
...
@@ -124,7 +124,7 @@ public class TestUsuariosController {
...
@@ -124,7 +124,7 @@ public class TestUsuariosController {
);
);
Assertions
.
assertThat
(
respuestaInexistente
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
Assertions
.
assertThat
(
respuestaInexistente
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
//
// Caso 2: Intentar
iniciar sesión con una contraseña incorrecta
//
Intento
iniciar sesión con una contraseña incorrecta
var
requestIncorrecto
=
new
HttpEntity
<>(
Map
.
of
(
"clave"
,
"incorrecta"
),
headers
);
var
requestIncorrecto
=
new
HttpEntity
<>(
Map
.
of
(
"clave"
,
"incorrecta"
),
headers
);
ResponseEntity
<
Void
>
respuestaIncorrecta
=
restTemplateUsuarios
.
postForEntity
(
ResponseEntity
<
Void
>
respuestaIncorrecta
=
restTemplateUsuarios
.
postForEntity
(
"/{email}"
,
"/{email}"
,
...
@@ -134,7 +134,7 @@ public class TestUsuariosController {
...
@@ -134,7 +134,7 @@ public class TestUsuariosController {
);
);
Assertions
.
assertThat
(
respuestaIncorrecta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
Assertions
.
assertThat
(
respuestaIncorrecta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
//
// Caso 3:
Inicio de sesión exitoso
// Inicio de sesión exitoso
var
requestValido
=
new
HttpEntity
<>(
Map
.
of
(
"clave"
,
"pedrita"
),
headers
);
var
requestValido
=
new
HttpEntity
<>(
Map
.
of
(
"clave"
,
"pedrita"
),
headers
);
ResponseEntity
<
DTOLoginRespuesta
>
respuestaValida
=
restTemplateUsuarios
.
postForEntity
(
ResponseEntity
<
DTOLoginRespuesta
>
respuestaValida
=
restTemplateUsuarios
.
postForEntity
(
"/{email}"
,
"/{email}"
,
...
@@ -150,34 +150,6 @@ public class TestUsuariosController {
...
@@ -150,34 +150,6 @@ public class TestUsuariosController {
@Test
@Test
@DirtiesContext
@DirtiesContext
void
testObtenerIdPorEmail
()
{
// Registro un usuario
ResponseEntity
<
Void
>
respuestaRegistro
=
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"nuevo.usuario@example.com"
,
"nombreUsuario"
,
"nuevoUsuarioTest"
,
"contrasenia"
,
"nuevaPassword"
),
Void
.
class
);
assertThat
(
respuestaRegistro
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
ResponseEntity
<
Long
>
respuestaUsuarioId
=
restTemplate
.
exchange
(
"/usuarios/email/{email}"
,
HttpMethod
.
GET
,
null
,
Long
.
class
,
"nuevo.usuario@example.com"
);
// Valido respuesta
assertThat
(
respuestaUsuarioId
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
Long
usuarioId
=
respuestaUsuarioId
.
getBody
();
assertThat
(
usuarioId
).
isNotNull
();
// Pruebo con con un email inexistente
ResponseEntity
<
Long
>
respuestaNoExiste
=
restTemplate
.
exchange
(
"/usuarios/email/{email}"
,
HttpMethod
.
GET
,
null
,
Long
.
class
,
"noexiste@example.com"
);
assertThat
(
respuestaNoExiste
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
}
@Test
@DirtiesContext
void
testObtenerBibliotecaDeUsuario
()
{
void
testObtenerBibliotecaDeUsuario
()
{
// Registro un usuario
// Registro un usuario
ResponseEntity
<
Void
>
respuestaRegistro
=
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
ResponseEntity
<
Void
>
respuestaRegistro
=
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
...
@@ -194,6 +166,7 @@ public class TestUsuariosController {
...
@@ -194,6 +166,7 @@ public class TestUsuariosController {
assertThat
(
authResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
authResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
token
=
"Bearer "
+
authResponse
.
getBody
().
getToken
();
String
token
=
"Bearer "
+
authResponse
.
getBody
().
getToken
();
// Obtengo el ID del usuario a partir del email
ResponseEntity
<
Long
>
respuestaUsuarioId
=
restTemplate
.
exchange
(
ResponseEntity
<
Long
>
respuestaUsuarioId
=
restTemplate
.
exchange
(
"/usuarios/email/{email}"
,
HttpMethod
.
GET
,
null
,
Long
.
class
,
"usuario@example.com"
"/usuarios/email/{email}"
,
HttpMethod
.
GET
,
null
,
Long
.
class
,
"usuario@example.com"
);
);
...
@@ -223,6 +196,34 @@ public class TestUsuariosController {
...
@@ -223,6 +196,34 @@ public class TestUsuariosController {
assertThat
(
respuestaBiblioteca
.
getBody
().
getUsuarioId
()).
isEqualTo
(
usuarioId
);
assertThat
(
respuestaBiblioteca
.
getBody
().
getUsuarioId
()).
isEqualTo
(
usuarioId
);
}
}
@Test
@DirtiesContext
void
testObtenerIdPorEmail
()
{
// Registro un usuario
ResponseEntity
<
Void
>
respuestaRegistro
=
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"nuevo.usuario@example.com"
,
"nombreUsuario"
,
"nuevoUsuarioTest"
,
"contrasenia"
,
"nuevaPassword"
),
Void
.
class
);
assertThat
(
respuestaRegistro
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// Obtener el ID del usuario registrado por email
ResponseEntity
<
Long
>
respuestaUsuarioId
=
restTemplate
.
exchange
(
"/usuarios/email/{email}"
,
HttpMethod
.
GET
,
null
,
Long
.
class
,
"nuevo.usuario@example.com"
);
// Verificar que la respuesta sea correcta
assertThat
(
respuestaUsuarioId
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
Long
usuarioId
=
respuestaUsuarioId
.
getBody
();
assertThat
(
usuarioId
).
isNotNull
();
// Prueba con un email inexistente
ResponseEntity
<
Long
>
respuestaNoExiste
=
restTemplate
.
exchange
(
"/usuarios/email/{email}"
,
HttpMethod
.
GET
,
null
,
Long
.
class
,
"noexiste@example.com"
);
// Verificar que el email no encontrado retorne un 404
assertThat
(
respuestaNoExiste
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment