From c5c0078cb66dbdcb9e06ee664d453efa291a43f9 Mon Sep 17 00:00:00 2001 From: macacia Date: Mon, 11 Nov 2024 12:58:08 +0100 Subject: [PATCH] P4ADEV-788 resolved sonar issues --- .../legacy/ValidateJWTLegacyServiceTest.java | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/legacy/ValidateJWTLegacyServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/legacy/ValidateJWTLegacyServiceTest.java index 73ecd6f..062178c 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/legacy/ValidateJWTLegacyServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/legacy/ValidateJWTLegacyServiceTest.java @@ -70,52 +70,40 @@ void GivenInvalidTokenThenInvalidTokenException() { @Test void GivenNonM2MAuthTokenThenInvalidTokenException() { - PublicKey publicKey = keyPair2.getPublic(); String token = JWTValidatorUtils.generateLegacyToken(keyPair2, "notA2A", Instant.now(), Instant.now().plusSeconds(3_600L), "jwtId"); - when(a2AClientLegacyPropConfig.getPublicKeysAsMap()).thenReturn(Map.of("A2A-IPA_TEST_1", publicKey)); Map claimsMap = JWT.decode(token).getClaims(); - Mockito.when(jwtValidatorMock.validate(token, publicKey)).thenReturn(claimsMap); + Mockito.when(jwtValidatorMock.validate(token, keyPair2.getPublic())).thenReturn(claimsMap); assertThrows(InvalidTokenException.class, () -> service.validate(token), "Invalid token type"); } @Test void GivenInvalidIatThenInvalidTokenException() { - PublicKey publicKey = keyPair2.getPublic(); String token = JWTValidatorUtils.generateLegacyToken(keyPair2, "a2a", Instant.now().plusSeconds(3_600L), Instant.now().plusSeconds(3_600_000L), "jwtId"); - - when(a2AClientLegacyPropConfig.getPublicKeysAsMap()).thenReturn(Map.of("A2A-IPA_TEST_1", publicKey)); - Map claimsMap = JWT.decode(token).getClaims(); - Mockito.when(jwtValidatorMock.validate(token, publicKey)).thenReturn(claimsMap); + Mockito.when(jwtValidatorMock.validate(token, keyPair2.getPublic())).thenReturn(claimsMap); assertThrows(InvalidTokenException.class, () -> service.validate(token), "Invalid field iat"); } @Test void GivenInvalidExpThenInvalidTokenException() { - PublicKey publicKey = keyPair2.getPublic(); String token = JWTValidatorUtils.generateLegacyToken(keyPair2, "a2a", Instant.now(), Instant.now().minusSeconds(3_600L), "jwtId"); - when(a2AClientLegacyPropConfig.getPublicKeysAsMap()).thenReturn(Map.of("A2A-IPA_TEST_1", publicKey)); - Map claimsMap = JWT.decode(token).getClaims(); - Mockito.when(jwtValidatorMock.validate(token, publicKey)).thenReturn(claimsMap); + Mockito.when(jwtValidatorMock.validate(token, keyPair2.getPublic())).thenReturn(claimsMap); assertThrows(InvalidTokenException.class, () -> service.validate(token), "Invalid field exp"); } @Test void GivenInvalidJtiThenInvalidTokenException() { - PublicKey publicKey = keyPair2.getPublic(); String token = JWTValidatorUtils.generateLegacyToken(keyPair2, "a2a", Instant.now(), Instant.now().plusSeconds(3_600L), ""); - when(a2AClientLegacyPropConfig.getPublicKeysAsMap()).thenReturn(Map.of("A2A-IPA_TEST_1", publicKey)); - Map claimsMap = JWT.decode(token).getClaims(); - Mockito.when(jwtValidatorMock.validate(token, publicKey)).thenReturn(claimsMap); + Mockito.when(jwtValidatorMock.validate(token, keyPair2.getPublic())).thenReturn(claimsMap); assertThrows(InvalidTokenException.class, () -> service.validate(token), "Invalid field jti"); }