Skip to content

Commit

Permalink
fix ErrorHandling and LocalDateTime json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioT90 committed Feb 5, 2025
1 parent 4f48418 commit 10c12ea
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
3 changes: 0 additions & 3 deletions openapi/p4pa-auth.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,6 @@ components:
- lastName
- fiscalCode
- roles
- organizationIpaCode
- email
properties:
externalUserId:
Expand All @@ -684,8 +683,6 @@ components:
type: string
fiscalCode:
type: string
organizationIpaCode:
type: string
email:
type: string
roles:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

Expand Down Expand Up @@ -61,8 +62,8 @@ public ResponseEntity<String> handleConflictException(RuntimeException ex, HttpS
return ResponseEntity.status(httpStatus).body(null);
}

@ExceptionHandler({ValidationException.class, HttpMessageNotReadableException.class})
public ResponseEntity<AuthErrorDTO> handleViolationException(RuntimeException ex, HttpServletRequest request) {
@ExceptionHandler({ValidationException.class, HttpMessageNotReadableException.class, MethodArgumentNotValidException.class})
public ResponseEntity<AuthErrorDTO> handleViolationException(Exception ex, HttpServletRequest request) {
return handleAuthErrorException(ex, request, HttpStatus.BAD_REQUEST, AuthErrorDTO.ErrorEnum.INVALID_REQUEST);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ class AuthzControllerNoOrganizzationAccessModeTest {
@Test
void givenUnauthorizedUserWhenCreateOrganizationOperatorThenOk() throws Exception {
String organizationIpaCode = "IPACODE";
CreateOperatorRequest request = new CreateOperatorRequest();
request.setExternalUserId("EXTERNALUSERID");
Gson gson = new Gson();
String body = gson.toJson(request);
String body = buildCreateOperatorRequest();

Mockito.when(authnServiceMock.getUserInfo("accessToken"))
.thenReturn(UserInfo.builder()
.organizations(List.of(UserOrganizationRoles.builder()
Expand All @@ -86,16 +84,7 @@ void givenUnauthorizedUserWhenCreateOrganizationOperatorThenOk() throws Exceptio
@Test
void givenAuthorizedUserWhenCreateOrganizationOperatorThenOk() throws Exception {
String organizationIpaCode = "IPACODE";
CreateOperatorRequest createOperatorRequest = new CreateOperatorRequest();
createOperatorRequest.setExternalUserId("externalUserId");
createOperatorRequest.setFiscalCode("fiscalCode");
createOperatorRequest.setFirstName("firstName");
createOperatorRequest.setLastName("lastName");
createOperatorRequest.setEmail("[email protected]");
createOperatorRequest.setRoles(List.of("ROLE_ADMIN"));
Gson gson = new Gson();
String body = gson.toJson(createOperatorRequest);

String body = buildCreateOperatorRequest();

Mockito.when(authnServiceMock.getUserInfo("accessToken"))
.thenReturn(UserInfo.builder()
Expand All @@ -113,17 +102,25 @@ void givenAuthorizedUserWhenCreateOrganizationOperatorThenOk() throws Exception
.content(body)
).andExpect(status().isOk());
}

public static String buildCreateOperatorRequest() {
CreateOperatorRequest createOperatorRequest = new CreateOperatorRequest();
createOperatorRequest.setExternalUserId("externalUserId");
createOperatorRequest.setFiscalCode("fiscalCode");
createOperatorRequest.setFirstName("firstName");
createOperatorRequest.setLastName("lastName");
createOperatorRequest.setEmail("[email protected]");
createOperatorRequest.setRoles(List.of("ROLE_ADMIN"));
Gson gson = new Gson();
return gson.toJson(createOperatorRequest);
}

// end region

//createUser region
@Test
void givenAuthorizedUserWhenCreateUserThenOk() throws Exception {
UserDTO user = new UserDTO();
user.setExternalUserId("EXTERNALUSERID");
user.setFiscalCode("FISCALCODE");
user.setFirstName("FIRSTNAME");
user.setLastName("LASTNAME");
Gson gson = new Gson();
String body = gson.toJson(user);
String body = buildCreateUserRequest();

Mockito.when(authnServiceMock.getUserInfo("accessToken"))
.thenReturn(UserInfo.builder()
Expand All @@ -144,10 +141,8 @@ void givenAuthorizedUserWhenCreateUserThenOk() throws Exception {

@Test
void givenUnauthorizedUserWhenCreateUserThenOk() throws Exception {
UserDTO request = new UserDTO();
request.setExternalUserId("EXTERNALUSERID");
Gson gson = new Gson();
String body = gson.toJson(request);
String body = buildCreateUserRequest();

Mockito.when(authnServiceMock.getUserInfo("accessToken"))
.thenReturn(UserInfo.builder()
.organizations(List.of(UserOrganizationRoles.builder()
Expand All @@ -164,13 +159,23 @@ void givenUnauthorizedUserWhenCreateUserThenOk() throws Exception {
.content(String.valueOf((body)))
).andExpect(status().isUnauthorized());
}

public static String buildCreateUserRequest() {
UserDTO user = new UserDTO();
user.setExternalUserId("EXTERNALUSERID");
user.setFiscalCode("FISCALCODE");
user.setFirstName("FIRSTNAME");
user.setLastName("LASTNAME");
Gson gson = new Gson();
return gson.toJson(user);
}
//end region

//region createClient
@Test
void givenUnauthorizedUserWhenRegisterClientThenUnauthorizedException() throws Exception {
String organizationIpaCode = "IPACODE";
CreateClientRequest request = new CreateClientRequest();
request.setClientName("CLIENTNAME");
CreateClientRequest request = buildCreateClientRequest();
Gson gson = new Gson();
String body = gson.toJson(request);
Mockito.when(authnServiceMock.getUserInfo("accessToken"))
Expand All @@ -196,8 +201,7 @@ void givenAuthorizedUserWhenRegisterClientThenOk() throws Exception {
String uuidRegex =
"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
String organizationIpaCode = "IPA_TEST_2";
CreateClientRequest createClientRequest = new CreateClientRequest();
createClientRequest.setClientName("CLIENTNAME");
CreateClientRequest createClientRequest = buildCreateClientRequest();

UserInfo expectedUser = UserInfo.builder()
.userId("USERID")
Expand Down Expand Up @@ -233,6 +237,14 @@ void givenAuthorizedUserWhenRegisterClientThenOk() throws Exception {
assertEquals(uuidRandomForSecret, clientDTO.getClientSecret());
}

private static CreateClientRequest buildCreateClientRequest() {
CreateClientRequest createClientRequest = new CreateClientRequest();
createClientRequest.setClientName("CLIENTNAME");
return createClientRequest;
}
//endregion

//region getClientSecret
@Test
void givenAuthorizedUserWhenGetClientSecretThenOk() throws Exception {
String uuidRandomForClientSecret = UUID.randomUUID().toString();
Expand Down Expand Up @@ -263,5 +275,6 @@ void givenAuthorizedUserWhenGetClientSecretThenOk() throws Exception {

assertEquals(uuidRandomForClientSecret, result.getResponse().getContentAsString());
}
//endregion

}
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,7 @@ void givenOperatorNotFoundWhenGetOrganizationOperatorThenException() throws Exce
@Test
void givenIsNotImplementedWhenCreateOrganizationOperatorThenOk() throws Exception {
String organizationIpaCode = "IPACODE";
CreateOperatorRequest request = new CreateOperatorRequest();
request.setExternalUserId("EXTERNALUSERID");
String body = objectMapper.writeValueAsString(request);
String body = AuthzControllerNoOrganizzationAccessModeTest.buildCreateOperatorRequest();
Mockito.when(authnServiceMock.getUserInfo("accessToken"))
.thenReturn(UserInfo.builder()
.organizations(List.of(UserOrganizationRoles.builder()
Expand All @@ -247,9 +245,8 @@ void givenIsNotImplementedWhenCreateOrganizationOperatorThenOk() throws Exceptio
//region createUser
@Test
void givenIsNotImplementedWhenCreateUserThenError() throws Exception {
UserDTO request = new UserDTO();
request.setExternalUserId("EXTERNALUSERID");
String body = objectMapper.writeValueAsString(request);
String body = AuthzControllerNoOrganizzationAccessModeTest.buildCreateUserRequest();

Mockito.when(authnServiceMock.getUserInfo("accessToken"))
.thenReturn(UserInfo.builder()
.organizations(List.of(UserOrganizationRoles.builder()
Expand Down

0 comments on commit 10c12ea

Please sign in to comment.