Skip to content

Commit

Permalink
P4ADEV-1167_add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
antocalo committed Oct 8, 2024
1 parent 075b08d commit 7c51f06
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import it.gov.pagopa.payhub.auth.exception.custom.InvalidTokenException;
import it.gov.pagopa.payhub.auth.service.exchange.AccessTokenBuilderService;
import it.gov.pagopa.payhub.auth.utils.JWTValidator;
import org.junit.jupiter.api.Assertions;

Expand Down Expand Up @@ -55,7 +56,7 @@ void givenValidJWTThenOk() {

Assertions.assertDoesNotThrow(() -> jwtValidator.validateInternalToken(validToken, PUBLIC_KEY));
Assertions.assertDoesNotThrow(() -> validateTokenService.validate(validToken));
Assertions.assertEquals(ValidateTokenService.ALLOWED_TYPE, jwt.getHeaderClaim("typ").asString());
Assertions.assertEquals(AccessTokenBuilderService.ACCESS_TOKEN_TYPE, jwt.getHeaderClaim("typ").asString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ void givenInvalidTokenThenThrowInvalidTokenException() {
void givenValidInternalJWTThenOk() throws Exception {
KeyPair keyPair = JWTValidatorUtils.generateKeyPair();
String validToken = utils.generateInternalToken(keyPair,new Date(System.currentTimeMillis() + 3600000));
Assertions.assertDoesNotThrow(() -> jwtValidator.validateInternalToken(validToken, JWTValidatorUtils.getPublicKey(keyPair)));
String publicKey = JWTValidatorUtils.getPublicKey(keyPair);

Assertions.assertDoesNotThrow(() -> jwtValidator.validateInternalToken(validToken, publicKey));
}

@Test
Expand All @@ -79,16 +81,18 @@ void givenInvalidInternalJWTThenInvalidTokenException() throws Exception {
String invalidToken = utils.generateInternalToken(otherKeyPair, new Date(System.currentTimeMillis() + 3600000));

KeyPair keyPair = JWTValidatorUtils.generateKeyPair();
String publicKey = JWTValidatorUtils.getPublicKey(keyPair);

assertThrows(InvalidTokenException.class, () -> jwtValidator.validateInternalToken(invalidToken, JWTValidatorUtils.getPublicKey(keyPair)));
assertThrows(InvalidTokenException.class, () -> jwtValidator.validateInternalToken(invalidToken, publicKey));
}

@Test
void givenTokenExpiredThenTokenExpiredException() throws Exception {
KeyPair keyPair = JWTValidatorUtils.generateKeyPair();
String invalidToken = utils.generateInternalToken(keyPair, new Date(System.currentTimeMillis() - 3600000));
String publicKey = JWTValidatorUtils.getPublicKey(keyPair);

assertThrows(TokenExpiredException.class, () -> jwtValidator.validateInternalToken(invalidToken, JWTValidatorUtils.getPublicKey(keyPair)));
assertThrows(TokenExpiredException.class, () -> jwtValidator.validateInternalToken(invalidToken, publicKey));
}

}

0 comments on commit 7c51f06

Please sign in to comment.