Skip to content

Commit

Permalink
P4ADEV-1437 add JwtAuthenticationFilterTest
Browse files Browse the repository at this point in the history
  • Loading branch information
antocalo committed Nov 28, 2024
1 parent 62e402d commit 31004e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected void doFilterInternal(@NonNull HttpServletRequest request,@NonNull Htt
} catch (InvalidAccessTokenException e){
log.info("An invalid accessToken has been provided: " + e.getMessage());
} catch (Exception e){
log.error("Something gone wrong while retrieving UserInfo", e);
log.error("Something gone wrong while validate accessToken", e);
}
filterChain.doFilter(request, response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.gov.pagopa.payhub.pdnd.dto.auth.UserInfoDTO;
import it.gov.pagopa.payhub.pdnd.dto.auth.UserOrganizationRolesDTO;
import it.gov.pagopa.payhub.pdnd.exception.custom.InvalidAccessTokenException;
import it.gov.pagopa.payhub.pdnd.service.auth.AuthorizationService;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
Expand Down Expand Up @@ -83,4 +84,22 @@ void givenValidTokenWhenDoFilterInternalThenOk() throws ServletException, IOExce
SecurityContextHolder.getContext().getAuthentication()
);
}

@Test
void givenInvalidTokenWhenDoFilterInternalThenInvalidAccessTokenException() throws ServletException, IOException {
// Given
String accessToken = "ACCESSTOKEN";
MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(), "/path");
request.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);

MockHttpServletResponse response = new MockHttpServletResponse();

Mockito.doThrow(new InvalidAccessTokenException("An invalid accessToken has been provided")).when(authorizationService).validateToken(accessToken);

// When
jwtAuthenticationFilter.doFilterInternal(request, response, filterChainMock);

// Then
Mockito.verify(filterChainMock).doFilter(request, response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
classes = {AuthorizationService.class, AuthorizationServiceTest.AuthorizationServiceTestConfiguration.class},
webEnvironment = SpringBootTest.WebEnvironment.NONE)
@EnableWireMock({
@ConfigureWireMock(name = "auth-server", property = "app.auth.base-url")
@ConfigureWireMock(name = "auth-server", properties = "app.auth.base-url")
})
@EnableConfigurationProperties
class AuthorizationServiceTest {
Expand Down

0 comments on commit 31004e6

Please sign in to comment.