From 47b325f79e9f84c65d9ec26349c497ee1575e4a1 Mon Sep 17 00:00:00 2001 From: "pasqualino.cristaudo" <> Date: Mon, 4 Dec 2023 18:37:39 +0100 Subject: [PATCH] Added JWT's tests --- .../pull/service/auth/JWTConverterTest.java | 274 ++++++++++++++++++ .../pull/service/config/BeanBuilder.java | 10 + .../pull/service/filter/JWTFilterTest.java | 48 ++- .../service/impl/SignalServiceImplTest.java | 4 +- 4 files changed, 329 insertions(+), 7 deletions(-) create mode 100644 src/test/java/it/pagopa/interop/signalhub/pull/service/auth/JWTConverterTest.java diff --git a/src/test/java/it/pagopa/interop/signalhub/pull/service/auth/JWTConverterTest.java b/src/test/java/it/pagopa/interop/signalhub/pull/service/auth/JWTConverterTest.java new file mode 100644 index 0000000..e08a372 --- /dev/null +++ b/src/test/java/it/pagopa/interop/signalhub/pull/service/auth/JWTConverterTest.java @@ -0,0 +1,274 @@ +package it.pagopa.interop.signalhub.pull.service.auth; + +import com.auth0.jwk.Jwk; +import com.auth0.jwk.JwkException; +import com.auth0.jwk.JwkProvider; +import com.auth0.jwt.JWT; +import com.auth0.jwt.JWTVerifier; +import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.exceptions.JWTDecodeException; +import com.auth0.jwt.exceptions.JWTVerificationException; +import com.auth0.jwt.interfaces.DecodedJWT; +import com.auth0.jwt.interfaces.Verification; +import it.pagopa.interop.signalhub.pull.service.config.BeanBuilder; +import it.pagopa.interop.signalhub.pull.service.config.SignalHubPullConfig; +import it.pagopa.interop.signalhub.pull.service.exception.JWTException; +import it.pagopa.interop.signalhub.pull.service.exception.PDNDGenericException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; +import org.springframework.mock.web.server.MockServerWebExchange; +import reactor.test.StepVerifier; + +import java.security.NoSuchAlgorithmException; +import java.util.function.Function; +import java.util.function.Predicate; + + +@ExtendWith(MockitoExtension.class) +class JWTConverterTest { + @InjectMocks + private JWTConverter jwtConverter; + @Mock + private JwkProvider jwkProvider; + @Mock + private SignalHubPullConfig signalHubPullConfig; + + + @Test + void applyWhenJwtIsCorrectTest() throws JwkException, NoSuchAlgorithmException { + DecodedJWT jwt = JWT.decode("eyJ0eXAiOiJhdCtqd3QiLCJhbGciOiJSUzI1NiIsInVzZSI6InNpZyIsImtpZCI6IjMyZDhhMzIxLTE1NjgtNDRmNS05NTU4LWE5MDcyZjUxOWQyZCJ9.eyJhdWQiOiJpbnRlcm9wLXNpZ25hbGh1Yi1wdWxsLXNpZ25hbCIsInN1YiI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsIm5iZiI6MTcwMTQyNDM4OCwicHVycG9zZUlkIjoiOTI3YmM2Y2UtNjU5NS00ODU4LTkyMmItODgyMGU3MzgwZjZhIiwiaXNzIjoidWF0LmludGVyb3AucGFnb3BhLml0IiwiZXhwIjoxNzAxNDI2MTg4LCJpYXQiOjE3MDE0MjQzODgsImNsaWVudF9pZCI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsImp0aSI6IjRlNjQxNzlkLTRiOWEtNDVjZS1hYTBjLWYxMzVkNzZkNGJkNCJ9.gSTG_JuDVLgaPaXmc0FEGUTy2x5TBc86lOTcvmsl95aagWcr2UEfKy_0Q0I4BDIsFMbYKiy8B6igJryMXg43WMbPx8aXNQOkUWqzlVpPA8yTYYeppiLuMLr8HHv3yDbRoAEq-CRmyOLyNN0s_yCSk1UpqCMxJCo6nhMnAN-UNZYjCsWatMxMIzwrNRiEUNORBgVw6sVo9djFQ1f3ejUyM-2M2lCv4OxudzdHKuVmQGBPNU7kVAsfakHbbJhWxgPW8sqbIiwaUoq4JNF0dgFdSxjzW5CZuwv8cDUsNNskq4o1dkhZnWQZQYDQ7yH8RtjanejgWSykycnw6xxZxyuZ7g"); + + MockServerWebExchange exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url") + .header("Authorization", "Bearer ".concat(jwt.getToken()))); + + String audience = "interop-signalhub-pull-signal"; + Mockito.when(signalHubPullConfig.getAudience()) + .thenReturn(audience); + + MockedStatic jwtMockedStatic = Mockito.mockStatic(JWT.class); + jwtMockedStatic + .when(() -> JWT.decode(Mockito.any())) + .thenReturn(jwt); + + Jwk jwk = Mockito.mock(Jwk.class); + Mockito.when(jwkProvider.get(Mockito.any())) + .thenReturn(jwk); + + Mockito.when(jwk.getPublicKey()) + .thenReturn(BeanBuilder.getPublicKey()); + + Algorithm algorithm = Mockito.mock(Algorithm.class); + + MockedStatic algorithmMockedStatic = Mockito.mockStatic(Algorithm.class); + algorithmMockedStatic + .when(() -> Algorithm.RSA256(Mockito.any(), Mockito.any())) + .thenReturn(algorithm); + + Verification verification = Mockito.mock(Verification.class); + jwtMockedStatic + .when(() -> JWT.require(Mockito.any())) + .thenReturn(verification); + + JWTVerifier jwtVerifier = Mockito.mock(JWTVerifier.class); + Mockito.when(verification.build()) + .thenReturn(jwtVerifier); + + Mockito.when(jwtVerifier.verify(jwt)) + .thenReturn(jwt); + + DecodedJWT djwt = jwtConverter.apply(exchange) + .block(); + Assertions.assertNotNull(djwt); + Assertions.assertEquals(jwt, djwt); + + algorithmMockedStatic.close(); + jwtMockedStatic.close(); + } + + @Test + void applyWhenJwtIsEmptyTest() { + DecodedJWT jwt = JWT.decode("eyJ0eXAiOiJhdCtqd3QiLCJhbGciOiJSUzI1NiIsInVzZSI6InNpZyIsImtpZCI6IjMyZDhhMzIxLTE1NjgtNDRmNS05NTU4LWE5MDcyZjUxOWQyZCJ9.eyJhdWQiOiJpbnRlcm9wLXNpZ25hbGh1Yi1wdWxsLXNpZ25hbCIsInN1YiI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsIm5iZiI6MTcwMTQyNDM4OCwicHVycG9zZUlkIjoiOTI3YmM2Y2UtNjU5NS00ODU4LTkyMmItODgyMGU3MzgwZjZhIiwiaXNzIjoidWF0LmludGVyb3AucGFnb3BhLml0IiwiZXhwIjoxNzAxNDI2MTg4LCJpYXQiOjE3MDE0MjQzODgsImNsaWVudF9pZCI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsImp0aSI6IjRlNjQxNzlkLTRiOWEtNDVjZS1hYTBjLWYxMzVkNzZkNGJkNCJ9.gSTG_JuDVLgaPaXmc0FEGUTy2x5TBc86lOTcvmsl95aagWcr2UEfKy_0Q0I4BDIsFMbYKiy8B6igJryMXg43WMbPx8aXNQOkUWqzlVpPA8yTYYeppiLuMLr8HHv3yDbRoAEq-CRmyOLyNN0s_yCSk1UpqCMxJCo6nhMnAN-UNZYjCsWatMxMIzwrNRiEUNORBgVw6sVo9djFQ1f3ejUyM-2M2lCv4OxudzdHKuVmQGBPNU7kVAsfakHbbJhWxgPW8sqbIiwaUoq4JNF0dgFdSxjzW5CZuwv8cDUsNNskq4o1dkhZnWQZQYDQ7yH8RtjanejgWSykycnw6xxZxyuZ7g"); + + MockServerWebExchange exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url") + .header("Authorization", "Bearer ".concat(jwt.getToken()))); + + MockedStatic jwtUtilMockedStatic = Mockito.mockStatic(JWTUtil.class); + jwtUtilMockedStatic + .when(() -> JWTUtil.getAuthorizationPayload(Mockito.any())) + .thenReturn("Bearer ".concat("")); + + Predicate predicate = pred -> true; + jwtUtilMockedStatic + .when(() -> JWTUtil.matchBearerLength()) + .thenReturn(predicate); + + Function function = f -> ""; + jwtUtilMockedStatic + .when(() -> JWTUtil.getBearerValue()) + .thenReturn(function); + + Function toDecode = val -> null; + jwtUtilMockedStatic + .when(() -> JWTUtil.decodeJwt()) + .thenReturn(toDecode); + + StepVerifier.create(jwtConverter.apply(exchange)) + .expectError(PDNDGenericException.class) + .verify(); + + jwtUtilMockedStatic.close(); + } + + @Test + void applyWhenJwtIsNotPresentTest() { + MockServerWebExchange exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url") + .header("Authorization", "Bearer ")); + StepVerifier.create(jwtConverter.apply(exchange)) + .expectError(PDNDGenericException.class) + .verify(); + + exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url") + .header("Authorization", "")); + StepVerifier.create(jwtConverter.apply(exchange)) + .expectError(PDNDGenericException.class) + .verify(); + + exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url") + .header("Authorization", "Bearer ".concat(""))); + StepVerifier.create(jwtConverter.apply(exchange)) + .expectError(PDNDGenericException.class) + .verify(); + } + + @Test + void applyWhenOnDecodeJWTThrowJWTExceptionTest() { + DecodedJWT jwt = JWT.decode("eyJ0eXAiOiJhdCtqd3QiLCJhbGciOiJSUzI1NiIsInVzZSI6InNpZyIsImtpZCI6IjMyZDhhMzIxLTE1NjgtNDRmNS05NTU4LWE5MDcyZjUxOWQyZCJ9.eyJhdWQiOiJpbnRlcm9wLXNpZ25hbGh1Yi1wdWxsLXNpZ25hbCIsInN1YiI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsIm5iZiI6MTcwMTQyNDM4OCwicHVycG9zZUlkIjoiOTI3YmM2Y2UtNjU5NS00ODU4LTkyMmItODgyMGU3MzgwZjZhIiwiaXNzIjoidWF0LmludGVyb3AucGFnb3BhLml0IiwiZXhwIjoxNzAxNDI2MTg4LCJpYXQiOjE3MDE0MjQzODgsImNsaWVudF9pZCI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsImp0aSI6IjRlNjQxNzlkLTRiOWEtNDVjZS1hYTBjLWYxMzVkNzZkNGJkNCJ9.gSTG_JuDVLgaPaXmc0FEGUTy2x5TBc86lOTcvmsl95aagWcr2UEfKy_0Q0I4BDIsFMbYKiy8B6igJryMXg43WMbPx8aXNQOkUWqzlVpPA8yTYYeppiLuMLr8HHv3yDbRoAEq-CRmyOLyNN0s_yCSk1UpqCMxJCo6nhMnAN-UNZYjCsWatMxMIzwrNRiEUNORBgVw6sVo9djFQ1f3ejUyM-2M2lCv4OxudzdHKuVmQGBPNU7kVAsfakHbbJhWxgPW8sqbIiwaUoq4JNF0dgFdSxjzW5CZuwv8cDUsNNskq4o1dkhZnWQZQYDQ7yH8RtjanejgWSykycnw6xxZxyuZ7g"); + + MockServerWebExchange exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url") + .header("Authorization", "Bearer ".concat(jwt.getToken()))); + + MockedStatic jwtMockedStatic = Mockito.mockStatic(JWT.class); + jwtMockedStatic + .when(() -> JWT.decode(Mockito.any())) + .thenThrow(new JWTDecodeException("")); + + StepVerifier.create(jwtConverter.apply(exchange)) + .expectError(JWTException.class) + .verify(); + + jwtMockedStatic.close(); + } + + @Test + void applyWhenOnVerifyJWTThrowJWTExceptionTest() throws JwkException, NoSuchAlgorithmException { + DecodedJWT jwt = JWT.decode("eyJ0eXAiOiJhdCtqd3QiLCJhbGciOiJSUzI1NiIsInVzZSI6InNpZyIsImtpZCI6IjMyZDhhMzIxLTE1NjgtNDRmNS05NTU4LWE5MDcyZjUxOWQyZCJ9.eyJhdWQiOiJpbnRlcm9wLXNpZ25hbGh1Yi1wdWxsLXNpZ25hbCIsInN1YiI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsIm5iZiI6MTcwMTQyNDM4OCwicHVycG9zZUlkIjoiOTI3YmM2Y2UtNjU5NS00ODU4LTkyMmItODgyMGU3MzgwZjZhIiwiaXNzIjoidWF0LmludGVyb3AucGFnb3BhLml0IiwiZXhwIjoxNzAxNDI2MTg4LCJpYXQiOjE3MDE0MjQzODgsImNsaWVudF9pZCI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsImp0aSI6IjRlNjQxNzlkLTRiOWEtNDVjZS1hYTBjLWYxMzVkNzZkNGJkNCJ9.gSTG_JuDVLgaPaXmc0FEGUTy2x5TBc86lOTcvmsl95aagWcr2UEfKy_0Q0I4BDIsFMbYKiy8B6igJryMXg43WMbPx8aXNQOkUWqzlVpPA8yTYYeppiLuMLr8HHv3yDbRoAEq-CRmyOLyNN0s_yCSk1UpqCMxJCo6nhMnAN-UNZYjCsWatMxMIzwrNRiEUNORBgVw6sVo9djFQ1f3ejUyM-2M2lCv4OxudzdHKuVmQGBPNU7kVAsfakHbbJhWxgPW8sqbIiwaUoq4JNF0dgFdSxjzW5CZuwv8cDUsNNskq4o1dkhZnWQZQYDQ7yH8RtjanejgWSykycnw6xxZxyuZ7g"); + + MockServerWebExchange exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url") + .header("Authorization", "Bearer ".concat(jwt.getToken()))); + + String audience = "interop-signalhub-pull-signal"; + Mockito.when(signalHubPullConfig.getAudience()) + .thenReturn(audience); + + MockedStatic jwtMockedStatic = Mockito.mockStatic(JWT.class); + jwtMockedStatic + .when(() -> JWT.decode(Mockito.any())) + .thenReturn(jwt); + + Jwk jwk = Mockito.mock(Jwk.class); + Mockito.when(jwkProvider.get(Mockito.any())) + .thenReturn(jwk); + + Mockito.when(jwk.getPublicKey()) + .thenReturn(BeanBuilder.getPublicKey()); + + Algorithm algorithm = Mockito.mock(Algorithm.class); + + MockedStatic algorithmMockedStatic = Mockito.mockStatic(Algorithm.class); + algorithmMockedStatic + .when(() -> Algorithm.RSA256(Mockito.any(), Mockito.any())) + .thenReturn(algorithm); + + Verification verification = Mockito.mock(Verification.class); + jwtMockedStatic + .when(() -> JWT.require(Mockito.any())) + .thenReturn(verification); + + JWTVerifier jwtVerifier = Mockito.mock(JWTVerifier.class); + Mockito.when(verification.build()) + .thenReturn(jwtVerifier); + + Mockito.when(jwtVerifier.verify(jwt)) + .thenThrow(new JWTVerificationException("")); + + StepVerifier.create(jwtConverter.apply(exchange)) + .expectError(JWTException.class) + .verify(); + + algorithmMockedStatic.close(); + jwtMockedStatic.close(); + } + + @Test + void applyWhenOnGetPublicKeyThrowJWTExceptionTest() throws JwkException, NoSuchAlgorithmException { + DecodedJWT jwt = JWT.decode("eyJ0eXAiOiJhdCtqd3QiLCJhbGciOiJSUzI1NiIsInVzZSI6InNpZyIsImtpZCI6IjMyZDhhMzIxLTE1NjgtNDRmNS05NTU4LWE5MDcyZjUxOWQyZCJ9.eyJhdWQiOiJpbnRlcm9wLXNpZ25hbGh1Yi1wdWxsLXNpZ25hbCIsInN1YiI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsIm5iZiI6MTcwMTQyNDM4OCwicHVycG9zZUlkIjoiOTI3YmM2Y2UtNjU5NS00ODU4LTkyMmItODgyMGU3MzgwZjZhIiwiaXNzIjoidWF0LmludGVyb3AucGFnb3BhLml0IiwiZXhwIjoxNzAxNDI2MTg4LCJpYXQiOjE3MDE0MjQzODgsImNsaWVudF9pZCI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsImp0aSI6IjRlNjQxNzlkLTRiOWEtNDVjZS1hYTBjLWYxMzVkNzZkNGJkNCJ9.gSTG_JuDVLgaPaXmc0FEGUTy2x5TBc86lOTcvmsl95aagWcr2UEfKy_0Q0I4BDIsFMbYKiy8B6igJryMXg43WMbPx8aXNQOkUWqzlVpPA8yTYYeppiLuMLr8HHv3yDbRoAEq-CRmyOLyNN0s_yCSk1UpqCMxJCo6nhMnAN-UNZYjCsWatMxMIzwrNRiEUNORBgVw6sVo9djFQ1f3ejUyM-2M2lCv4OxudzdHKuVmQGBPNU7kVAsfakHbbJhWxgPW8sqbIiwaUoq4JNF0dgFdSxjzW5CZuwv8cDUsNNskq4o1dkhZnWQZQYDQ7yH8RtjanejgWSykycnw6xxZxyuZ7g"); + + MockServerWebExchange exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url") + .header("Authorization", "Bearer ".concat(jwt.getToken()))); + + String audience = "interop-signalhub-pull-signal"; + Mockito.when(signalHubPullConfig.getAudience()) + .thenReturn(audience); + + MockedStatic jwtMockedStatic = Mockito.mockStatic(JWT.class); + jwtMockedStatic + .when(() -> JWT.decode(Mockito.any())) + .thenReturn(jwt); + + Mockito.when(jwkProvider.get(Mockito.any())) + .thenThrow(new JwkException("")); + + StepVerifier.create(jwtConverter.apply(exchange)) + .expectError(JWTException.class) + .verify(); + + jwtMockedStatic.close(); + } + + @Test + void applyWhenJwtIsNotValidTest() { + DecodedJWT jwt = JWT.decode("eyJ0eXAiOiJhdCtqd3QiLCJhbGciOiJSUzI1NiIsInVzZSI6InNpZyIsImtpZCI6IjMyZDhhMzIxLTE1NjgtNDRmNS05NTU4LWE5MDcyZjUxOWQyZCJ9.eyJhdWQiOiJpbnRlcm9wLXNpZ25hbGh1Yi1wdWxsLXNpZ25hbCIsInN1YiI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsIm5iZiI6MTcwMTQyNDM4OCwicHVycG9zZUlkIjoiOTI3YmM2Y2UtNjU5NS00ODU4LTkyMmItODgyMGU3MzgwZjZhIiwiaXNzIjoidWF0LmludGVyb3AucGFnb3BhLml0IiwiZXhwIjoxNzAxNDI2MTg4LCJpYXQiOjE3MDE0MjQzODgsImNsaWVudF9pZCI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsImp0aSI6IjRlNjQxNzlkLTRiOWEtNDVjZS1hYTBjLWYxMzVkNzZkNGJkNCJ9.gSTG_JuDVLgaPaXmc0FEGUTy2x5TBc86lOTcvmsl95aagWcr2UEfKy_0Q0I4BDIsFMbYKiy8B6igJryMXg43WMbPx8aXNQOkUWqzlVpPA8yTYYeppiLuMLr8HHv3yDbRoAEq-CRmyOLyNN0s_yCSk1UpqCMxJCo6nhMnAN-UNZYjCsWatMxMIzwrNRiEUNORBgVw6sVo9djFQ1f3ejUyM-2M2lCv4OxudzdHKuVmQGBPNU7kVAsfakHbbJhWxgPW8sqbIiwaUoq4JNF0dgFdSxjzW5CZuwv8cDUsNNskq4o1dkhZnWQZQYDQ7yH8RtjanejgWSykycnw6xxZxyuZ7g"); + + MockServerWebExchange exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url") + .header("Authorization", "Bearer ".concat(jwt.getToken()))); + + StepVerifier.create(jwtConverter.apply(exchange)) + .expectError(PDNDGenericException.class) + .verify(); + } +} \ No newline at end of file diff --git a/src/test/java/it/pagopa/interop/signalhub/pull/service/config/BeanBuilder.java b/src/test/java/it/pagopa/interop/signalhub/pull/service/config/BeanBuilder.java index 00ca92e..806d2eb 100644 --- a/src/test/java/it/pagopa/interop/signalhub/pull/service/config/BeanBuilder.java +++ b/src/test/java/it/pagopa/interop/signalhub/pull/service/config/BeanBuilder.java @@ -7,6 +7,10 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; + @Slf4j @Configuration @@ -18,6 +22,12 @@ public MockServer getMockServer(@Value("${mockserver.bean.port}") int port){ return new MockServer(port); } + public static PublicKey getPublicKey() throws NoSuchAlgorithmException { + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); + keyPairGenerator.initialize(2048); + return keyPairGenerator.generateKeyPair().getPublic(); + } + public static PrincipalAgreement getPrincipal(){ PrincipalAgreement p = new PrincipalAgreement(); p.setPrincipalId("ABC-0009"); diff --git a/src/test/java/it/pagopa/interop/signalhub/pull/service/filter/JWTFilterTest.java b/src/test/java/it/pagopa/interop/signalhub/pull/service/filter/JWTFilterTest.java index 4c8a405..d729060 100644 --- a/src/test/java/it/pagopa/interop/signalhub/pull/service/filter/JWTFilterTest.java +++ b/src/test/java/it/pagopa/interop/signalhub/pull/service/filter/JWTFilterTest.java @@ -3,17 +3,18 @@ import com.auth0.jwt.JWT; import com.auth0.jwt.interfaces.DecodedJWT; -import it.pagopa.interop.signalhub.pull.service.auth.JWTAuthManager; -import it.pagopa.interop.signalhub.pull.service.auth.JWTConverter; -import it.pagopa.interop.signalhub.pull.service.auth.PrincipalAgreement; -import it.pagopa.interop.signalhub.pull.service.auth.PrincipalAgreementValidator; +import it.pagopa.interop.signalhub.pull.service.auth.*; +import it.pagopa.interop.signalhub.pull.service.cache.model.JWTCache; import it.pagopa.interop.signalhub.pull.service.config.BeanBuilder; +import it.pagopa.interop.signalhub.pull.service.exception.JWTException; +import it.pagopa.interop.signalhub.pull.service.exception.PDNDGenericException; import it.pagopa.interop.signalhub.pull.service.service.InteropService; import it.pagopa.interop.signalhub.pull.service.service.JWTService; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.*; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; import org.springframework.mock.http.server.reactive.MockServerHttpRequest; import org.springframework.mock.web.server.MockServerWebExchange; import org.springframework.security.authentication.ReactiveAuthenticationManager; @@ -23,7 +24,11 @@ import org.springframework.security.web.server.context.ServerSecurityContextRepository; import org.springframework.web.server.WebFilterChain; import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; +import java.util.function.Function; + +import static it.pagopa.interop.signalhub.pull.service.exception.ExceptionTypeEnum.JWT_NOT_VALID; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -46,8 +51,9 @@ class JWTFilterTest { @InjectMocks private JWTFilter jwtFilter; + @Test - void filterTest() { + void filterOnSuccessTest() { DecodedJWT jwt = JWT.decode("eyJ0eXAiOiJhdCtqd3QiLCJhbGciOiJSUzI1NiIsInVzZSI6InNpZyIsImtpZCI6IjMyZDhhMzIxLTE1NjgtNDRmNS05NTU4LWE5MDcyZjUxOWQyZCJ9.eyJhdWQiOiJpbnRlcm9wLXNpZ25hbGh1Yi1wdXNoLXNpZ25hbCIsInN1YiI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsIm5iZiI6MTY5OTQ1NjYzNCwicHVycG9zZUlkIjoiYjY5M2ViNmUtNzNkZC00YjU4LWE0MmEtM2UwNjRhNmE0Y2FiIiwiaXNzIjoidWF0LmludGVyb3AucGFnb3BhLml0IiwiZXhwIjoxNjk5NDU4NDM0LCJpYXQiOjE2OTk0NTY2MzQsImNsaWVudF9pZCI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsImp0aSI6ImFjNDVjZDE2LThmMzgtNDFhYS1hODg2LWYyZWY5OWM4ZDE1MyJ9.u0j4xsRY-8sQKRvBr-QjP6FFTwycWz7bgN6t8wyv9cSk9LZUsmOO5pxrudwVL0I5zAjo2uYjEcvNv7VYHynn01mXR1zi2vDO9Se83fQ479_4HhNroENbI7wBwfzm51teUq7cQ1f19o4CKO5esnw0RdjOpWx9yCFyNhmqcVHRhHRiRAcuyxC0E5QMGGbNcoZEljfzNy6QIruF9dUbeeHKqyF_RA-zhKCuQB7bnTDnQPyYd3mrOktYIcnGjeE1ynJJGHvCeM-P84WwbnnbSzQKYpEJHYa4TS-hRm2H3MIv1fLkAjnM8qWkjIwh6AZB-EemboE5CzRx_86Z_WQ2n0QXMw"); MockServerWebExchange exchange = MockServerWebExchange.from( @@ -82,5 +88,37 @@ void filterTest() { } + @Test + void filterOnJWTExceptionTest() { + DecodedJWT jwt = JWT.decode("eyJ0eXAiOiJhdCtqd3QiLCJhbGciOiJSUzI1NiIsInVzZSI6InNpZyIsImtpZCI6IjMyZDhhMzIxLTE1NjgtNDRmNS05NTU4LWE5MDcyZjUxOWQyZCJ9.eyJhdWQiOiJpbnRlcm9wLXNpZ25hbGh1Yi1wdXNoLXNpZ25hbCIsInN1YiI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsIm5iZiI6MTY5OTQ1NjYzNCwicHVycG9zZUlkIjoiYjY5M2ViNmUtNzNkZC00YjU4LWE0MmEtM2UwNjRhNmE0Y2FiIiwiaXNzIjoidWF0LmludGVyb3AucGFnb3BhLml0IiwiZXhwIjoxNjk5NDU4NDM0LCJpYXQiOjE2OTk0NTY2MzQsImNsaWVudF9pZCI6ImY3YzFhZDIwLWIwZDktNDIxMi1iMGIwLTQ2NTkyODgzNTY2MyIsImp0aSI6ImFjNDVjZDE2LThmMzgtNDFhYS1hODg2LWYyZWY5OWM4ZDE1MyJ9.u0j4xsRY-8sQKRvBr-QjP6FFTwycWz7bgN6t8wyv9cSk9LZUsmOO5pxrudwVL0I5zAjo2uYjEcvNv7VYHynn01mXR1zi2vDO9Se83fQ479_4HhNroENbI7wBwfzm51teUq7cQ1f19o4CKO5esnw0RdjOpWx9yCFyNhmqcVHRhHRiRAcuyxC0E5QMGGbNcoZEljfzNy6QIruF9dUbeeHKqyF_RA-zhKCuQB7bnTDnQPyYd3mrOktYIcnGjeE1ynJJGHvCeM-P84WwbnnbSzQKYpEJHYa4TS-hRm2H3MIv1fLkAjnM8qWkjIwh6AZB-EemboE5CzRx_86Z_WQ2n0QXMw"); + + MockServerWebExchange exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url") + .header("Authorization", "Bearer ".concat(jwt.getToken()))); + + WebFilterChain filterChain = filterExchange -> Mono.empty(); + Mockito.when(jwtConverter.apply(Mockito.any())) + .thenThrow(new JWTException(JWT_NOT_VALID, JWT_NOT_VALID.getMessage(), HttpStatus.FORBIDDEN, jwt.getToken())); + JWTCache jwtCacheMono = Mockito.mock(JWTCache.class); + Mockito.when(jwtService.saveOnCache(Mockito.any())) + .thenReturn(Mono.just(jwtCacheMono)); + + StepVerifier.create(jwtFilter.filter(exchange, filterChain)) + .expectError(PDNDGenericException.class) + .verify(); + } + + @Test + void filterWhithoutAuthorizationTest() { + MockServerWebExchange exchange = MockServerWebExchange.from( + MockServerHttpRequest + .get("/your-url")); + + WebFilterChain filterChain = filterExchange -> Mono.empty(); + + StepVerifier.create(jwtFilter.filter(exchange,filterChain)) + .verifyComplete(); + } } \ No newline at end of file diff --git a/src/test/java/it/pagopa/interop/signalhub/pull/service/service/impl/SignalServiceImplTest.java b/src/test/java/it/pagopa/interop/signalhub/pull/service/service/impl/SignalServiceImplTest.java index 92f390e..4f858a6 100644 --- a/src/test/java/it/pagopa/interop/signalhub/pull/service/service/impl/SignalServiceImplTest.java +++ b/src/test/java/it/pagopa/interop/signalhub/pull/service/service/impl/SignalServiceImplTest.java @@ -69,8 +69,8 @@ void whenCallPushSignalAndSignalIdAlreadyExist() { } @Test - void whenCounterReturnBiggestSignalId() { - Mockito.when(signalRepository.maxSignal(Mockito.any())) + void whenCounterReturnAllSignalId() { + Mockito.when(signalRepository.countAllSignal(Mockito.any())) .thenReturn(Mono.just(8)); signalService.counter(Mockito.any())