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
681bdbc7
authored
Feb 23, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat: [TestRecursosController]: Implementado el test de listar recurso en el controlador
parent
9fbf5a40
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
0 deletions
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestRecursosController.java
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestRecursosController.java
View file @
681bdbc7
...
...
@@ -414,8 +414,48 @@ public class TestRecursosController {
assertThat
(
respuestaNoAutenticado
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
}
@Test
@DirtiesContext
void
testListarRecursos
()
{
// Registro al administrador
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"admin@example.com"
,
"nombreUsuario"
,
"admin"
,
"contrasenia"
,
"adminpassword"
),
Void
.
class
);
// Iniciar sesión con administrador
var
authResponse
=
restTemplate
.
postForEntity
(
"/usuarios/admin@example.com"
,
Map
.
of
(
"clave"
,
"adminpassword"
),
DTOLoginRespuesta
.
class
);
assertThat
(
authResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
token
=
"Bearer "
+
authResponse
.
getBody
().
getToken
();
// Creo 10 recursos de prueba
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"Authorization"
,
token
);
for
(
int
i
=
1
;
i
<=
10
;
i
++)
{
Recurso
recurso
=
new
Recurso
(
"Titulo "
+
i
,
"Descripción "
+
i
,
LocalDate
.
of
(
2020
,
1
,
i
),
"Autor "
+
i
);
HttpEntity
<
Recurso
>
request
=
new
HttpEntity
<>(
recurso
,
headers
);
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
request
,
Void
.
class
);
}
// Caso 1: Obtener recursos con autenticación de administrador
HttpEntity
<
Void
>
authRequest
=
new
HttpEntity
<>(
headers
);
ResponseEntity
<
DTORecurso
[]>
respuestaAdmin
=
restTemplate
.
exchange
(
"/recursos"
,
HttpMethod
.
GET
,
authRequest
,
DTORecurso
[].
class
);
assertThat
(
respuestaAdmin
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
respuestaAdmin
.
getBody
()).
isNotNull
();
assertThat
(
respuestaAdmin
.
getBody
().
length
).
isLessThanOrEqualTo
(
15
);
// 🔹 Caso 2: Obtener recursos sin autenticación
ResponseEntity
<
Void
>
respuestaNoAuth
=
restTemplate
.
exchange
(
"/recursos"
,
HttpMethod
.
GET
,
HttpEntity
.
EMPTY
,
Void
.
class
);
assertThat
(
respuestaNoAuth
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
}
}
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