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
f8254628
authored
Apr 20, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat: [TestUsuarioControlador]: Testeada la función para modificar los datos del usuario
parent
9e6dbb62
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
95 additions
and
0 deletions
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestUsuariosController.java
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestUsuariosController.java
View file @
f8254628
...
@@ -226,4 +226,99 @@ public class TestUsuariosController {
...
@@ -226,4 +226,99 @@ public class TestUsuariosController {
assertThat
(
respuestaNoExiste
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
assertThat
(
respuestaNoExiste
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
}
}
@Test
@DirtiesContext
void
testModificarUsuario
()
{
// Registro del primer usuario
DTOUsuario
dto1
=
new
DTOUsuario
(
null
,
"modificar@test.com"
,
"Modificador"
,
"clave123"
);
ResponseEntity
<
Void
>
registro
=
restTemplateUsuarios
.
postForEntity
(
"/"
,
dto1
,
Void
.
class
);
assertThat
(
registro
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// Login para obtener token
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
HttpEntity
<
Map
<
String
,
String
>>
loginEntity
=
new
HttpEntity
<>(
Map
.
of
(
"clave"
,
"clave123"
),
headers
);
ResponseEntity
<
DTOLoginRespuesta
>
loginResponse
=
restTemplateUsuarios
.
postForEntity
(
"/{email}"
,
loginEntity
,
DTOLoginRespuesta
.
class
,
dto1
.
getEmail
()
);
assertThat
(
loginResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
token
=
loginResponse
.
getBody
().
getToken
();
// Preparar modificación válida
DTOModificarUsuario
dtoMod
=
new
DTOModificarUsuario
(
"nuevo@test.com"
,
"NuevoNombre"
,
"nuevaClave123"
,
"clave123"
);
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
headers
.
setBearerAuth
(
token
);
HttpEntity
<
DTOModificarUsuario
>
modEntity
=
new
HttpEntity
<>(
dtoMod
,
headers
);
ResponseEntity
<
DTOLoginRespuesta
>
respuestaMod
=
restTemplateUsuarios
.
exchange
(
"/"
,
HttpMethod
.
PUT
,
modEntity
,
DTOLoginRespuesta
.
class
);
assertThat
(
respuestaMod
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
respuestaMod
.
getBody
()).
isNotNull
();
assertThat
(
respuestaMod
.
getBody
().
getEmail
()).
isEqualTo
(
"nuevo@test.com"
);
assertThat
(
respuestaMod
.
getBody
().
getNombreUsuario
()).
isEqualTo
(
"NuevoNombre"
);
// 🔁 Actualizar el token tras modificación
String
nuevoToken
=
respuestaMod
.
getBody
().
getToken
();
headers
.
setBearerAuth
(
nuevoToken
);
// Registrar segundo usuario con email que vamos a intentar duplicar
DTOUsuario
dto2
=
new
DTOUsuario
(
null
,
"yausado@test.com"
,
"YaUsado"
,
"clave"
);
restTemplateUsuarios
.
postForEntity
(
"/"
,
dto2
,
Void
.
class
);
// Intentar modificar con email duplicado
DTOModificarUsuario
dtoDupEmail
=
new
DTOModificarUsuario
(
"yausado@test.com"
,
// ya existe
"NombreX"
,
"nuevaClave"
,
"nuevaClave123"
);
HttpEntity
<
DTOModificarUsuario
>
reqDupEmail
=
new
HttpEntity
<>(
dtoDupEmail
,
headers
);
ResponseEntity
<
String
>
respDupEmail
=
restTemplateUsuarios
.
exchange
(
"/"
,
HttpMethod
.
PUT
,
reqDupEmail
,
String
.
class
);
assertThat
(
respDupEmail
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CONFLICT
);
assertThat
(
respDupEmail
.
getBody
()).
isEqualTo
(
"correo"
);
// Intentar modificar con nombre de usuario duplicado
DTOModificarUsuario
dtoDupNombre
=
new
DTOModificarUsuario
(
"correoLibre@test.com"
,
"YaUsado"
,
// nombre en uso
"otraClave"
,
"nuevaClave123"
);
HttpEntity
<
DTOModificarUsuario
>
reqDupNombre
=
new
HttpEntity
<>(
dtoDupNombre
,
headers
);
ResponseEntity
<
String
>
respDupNombre
=
restTemplateUsuarios
.
exchange
(
"/"
,
HttpMethod
.
PUT
,
reqDupNombre
,
String
.
class
);
assertThat
(
respDupNombre
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CONFLICT
);
assertThat
(
respDupNombre
.
getBody
()).
isEqualTo
(
"nombre de usuario"
);
// Intentar modificar con contraseña incorrecta
DTOModificarUsuario
dtoBadPass
=
new
DTOModificarUsuario
(
"final@test.com"
,
"FinalUser"
,
"claveNueva"
,
"incorrecta"
);
HttpEntity
<
DTOModificarUsuario
>
reqBadPass
=
new
HttpEntity
<>(
dtoBadPass
,
headers
);
ResponseEntity
<
String
>
respBadPass
=
restTemplateUsuarios
.
exchange
(
"/"
,
HttpMethod
.
PUT
,
reqBadPass
,
String
.
class
);
assertThat
(
respBadPass
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FORBIDDEN
);
assertThat
(
respBadPass
.
getBody
()).
isEqualTo
(
"contraseña incorrecta"
);
}
}
}
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