From 3b0621e58f826e9e02bdb22bdf98bba01051b624 Mon Sep 17 00:00:00 2001 From: Antonio Tarricone <110115827+antoniotarricone@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:09:49 +0100 Subject: [PATCH] chore: Refactoring of IT engine. (#156) --- pom.xml | 890 +++++++++--------- .../mil/auth/resource/TokenResourceIT.java | 65 +- .../resource/TokenResourceIntegration.java | 184 ++++ src/test/gherkin/get_access_token.feature | 50 - 4 files changed, 680 insertions(+), 509 deletions(-) rename src/{test => it}/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIT.java (73%) create mode 100644 src/it/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIntegration.java delete mode 100644 src/test/gherkin/get_access_token.feature diff --git a/pom.xml b/pom.xml index 66f5e9a5..f4dad803 100644 --- a/pom.xml +++ b/pom.xml @@ -1,441 +1,487 @@ - - - 4.0.0 - - it.pagopa.swclient.mil - auth - 2.14.0 - mil-auth - Authorization Microservice for Multi-channel Integration Layer + + 4.0.0 + it.pagopa.swclient.mil + auth + 2.14.0 + mil-auth + Authorization Microservice for Multi-channel Integration Layer of SW Client Project. - https://github.com/pagopa/mil-auth - - PagoPA S.p.A. - https://www.pagopa.it - - - - GNU AFFERO GENERAL PUBLIC LICENSE - Version 3 - - - - - Antonio Tarricone - Antonio Tarricone@pagopa.it - PagoPA S.p.A. - https://www.pagopa.it - - - - + https://github.com/pagopa/mil-auth + + PagoPA S.p.A. + https://www.pagopa.it + + + + GNU AFFERO GENERAL PUBLIC LICENSE - Version 3 + + + + + Antonio Tarricone + Antonio Tarricone@pagopa.it + PagoPA S.p.A. + https://www.pagopa.it + + + - 21 - ${java.version} - ${java.version} - ${java.version} + 21 + ${java.version} + ${java.version} + ${java.version} - 3.13.0 - 3.5.1 - 4.0.0.4121 - 0.8.12 - 1.2.1 + 3.13.0 + 3.5.1 + 4.0.0.4121 + 0.8.12 + 1.2.1 - UTF-8 - UTF-8 + UTF-8 + UTF-8 - quarkus-bom - io.quarkus.platform - 3.15.1 + quarkus-bom + io.quarkus.platform + 3.15.1 - 2.8.0 - 5.7.0 - 1.18.34 - 3.8.3.1 - 3.26.3 - 2.73.6 - 2.43.0 + 2.8.0 + 5.7.0 + 1.18.34 + 3.8.3.1 + 3.26.3 + 2.73.6 + 2.43.0 - https://sonarcloud.io:443/ - pagopa - pagopa_mil-idp - true - 300 - target/jacoco-report/jacoco.xml - e1,e2 - java:S116 - src/test/java/**/*.java - java:S117 - src/test/java/**/*.java - true - false - - - - - - ${quarkus.platform.group-id} - ${quarkus.platform.artifact-id} - ${quarkus.platform.version} - pom - import - - - - - - - io.quarkus - quarkus-rest - - - io.quarkus - quarkus-rest-jackson - - - io.quarkus - quarkus-hibernate-validator - - - io.quarkus - quarkus-arc - - - io.quarkus - quarkus-rest-client - - - io.quarkus - quarkus-rest-client-jackson - - - io.quarkus - quarkus-cache - - - io.quarkus - quarkus-smallrye-jwt - - - org.projectlombok - lombok - ${lombok.version} - provided - - - it.pagopa.swclient.mil - common - ${common.version} - - - it.pagopa.swclient.mil - azure-services - ${azure-services.version} - - - com.nimbusds - nimbus-jose-jwt - - - org.bouncycastle - bcpkix-lts8on - ${bouncycastle.version} - - - io.quarkus - quarkus-mongodb-panache - + https://sonarcloud.io:443/ + pagopa + pagopa_mil-idp + true + 300 + target/jacoco-report/jacoco.xml + e1,e2 + java:S116 + src/test/java/**/*.java + java:S117 + src/test/java/**/*.java + true + false + + + + + ${quarkus.platform.group-id} + ${quarkus.platform.artifact-id} + ${quarkus.platform.version} + pom + import + + + + + + io.quarkus + quarkus-rest + + + io.quarkus + quarkus-rest-jackson + + + io.quarkus + quarkus-hibernate-validator + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-rest-client + + + io.quarkus + quarkus-rest-client-jackson + + + io.quarkus + quarkus-cache + + + io.quarkus + quarkus-smallrye-jwt + + + org.projectlombok + lombok + ${lombok.version} + provided + + + it.pagopa.swclient.mil + common + ${common.version} + + + it.pagopa.swclient.mil + azure-services + ${azure-services.version} + + + com.nimbusds + nimbus-jose-jwt + + + org.bouncycastle + bcpkix-lts8on + ${bouncycastle.version} + + + io.quarkus + quarkus-mongodb-panache + - - io.quarkus - quarkus-junit5 - test - - - io.quarkus - quarkus-test-security-jwt - test - - - io.rest-assured - rest-assured - test - - - org.assertj - assertj-core - ${assertj-core.version} - test - - - io.quarkus - quarkus-junit5-mockito - test + + io.quarkus + quarkus-junit5 + test + + + io.quarkus + quarkus-test-security-jwt + test + + + io.rest-assured + rest-assured + test + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + io.quarkus + quarkus-junit5-mockito + test - - - org.mockito - mockito-subclass - - - - - io.quarkus - quarkus-jacoco - test - - - com.atlassian.oai - swagger-request-validator-restassured - ${swagger-request-validator.version} - test - + + + org.mockito + mockito-subclass + + + + + io.quarkus + quarkus-jacoco + test + + + com.atlassian.oai + swagger-request-validator-restassured + ${swagger-request-validator.version} + test + - - io.quarkus - quarkus-logging-json - + + io.quarkus + quarkus-logging-json + - - io.quarkus - quarkus-opentelemetry - + + io.quarkus + quarkus-opentelemetry + - - io.quarkiverse.opentelemetry.exporter - quarkus-opentelemetry-exporter-azure - ${otel-exporter-azure.version} - - - - - - central - https://repo.maven.apache.org/maven2 - - - github - https://maven.pkg.github.com/pagopa/mil-common - - - github-mil-azure-services - https://maven.pkg.github.com/pagopa/mil-azure-services - - - - - - central - https://repo.maven.apache.org/maven2 - - - github - https://maven.pkg.github.com/pagopa/depcheck - - - - - - - ${quarkus.platform.group-id} - quarkus-maven-plugin - ${quarkus.platform.version} - true - - - - build - generate-code - generate-code-tests - native-image-agent - - - - - - maven-compiler-plugin - ${compiler-plugin.version} - - - -parameters - - - - - maven-surefire-plugin - ${surefire-plugin.version} - - ${skipUTs} - - - org.jboss.logmanager.LogManager - ${maven.home} - - - - - maven-failsafe-plugin - ${surefire-plugin.version} - - - - integration-test - verify - - - - - ${project.build.directory}/${project.build.finalName}-runner - - org.jboss.logmanager.LogManager - ${maven.home} - - - - - - - it.pagopa.maven - depcheck - ${depcheck-plugin.version} - - - validate - - verify - - - - - dep-sha256.json - dep-sha256-man.json - false - false - - - - org.jacoco - jacoco-maven-plugin - ${jacoco-maven-plugin.version} - - - jacoco-check - - check - - test - - - ${project.build.directory}/jacoco-quarkus.exec - - - CLASS - - - LINE - COVEREDRATIO - 0.90 - - - BRANCH - COVEREDRATIO - 0.80 - - - - - METHOD - - - COMPLEXITY - TOTALCOUNT - 15 - - - - - ${skipUTs} - - - - - - - - - native - - - native - - - - false - true - - + + io.quarkiverse.opentelemetry.exporter + quarkus-opentelemetry-exporter-azure + ${otel-exporter-azure.version} + + + + + central + https://repo.maven.apache.org/maven2 + + + github + https://maven.pkg.github.com/pagopa/mil-common + + + github-mil-azure-services + https://maven.pkg.github.com/pagopa/mil-azure-services + + + + + central + https://repo.maven.apache.org/maven2 + + + github + https://maven.pkg.github.com/pagopa/depcheck + + + + + + ${quarkus.platform.group-id} + quarkus-maven-plugin + ${quarkus.platform.version} + true + + + + build + generate-code + generate-code-tests + native-image-agent + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.6.0 + + + add-integration-test-sources + generate-test-sources + + add-test-source + + + + src/it/java + + + + + add-integration-test-resources + generate-test-resources + + add-test-resource + + + + + true + src/it/resources + + + + + + + + maven-compiler-plugin + ${compiler-plugin.version} + + + -parameters + + + + + maven-surefire-plugin + ${surefire-plugin.version} + + ${skipUTs} + + org.jboss.logmanager.LogManager + ${maven.home} + + + + + maven-failsafe-plugin + ${surefire-plugin.version} + + + + integration-test + verify + + + + ${project.build.directory}/${project.build.finalName}-runner + org.jboss.logmanager.LogManager + ${maven.home} + + + + + + + it.pagopa.maven + depcheck + ${depcheck-plugin.version} + + + validate + + verify + + + + + dep-sha256.json + dep-sha256-man.json + false + false + + + + org.jacoco + jacoco-maven-plugin + ${jacoco-maven-plugin.version} + + + jacoco-check + + check + + test + + ${project.build.directory}/jacoco-quarkus.exec + + + CLASS + + + LINE + COVEREDRATIO + 0.90 + + + BRANCH + COVEREDRATIO + 0.80 + + + + + METHOD + + + COMPLEXITY + TOTALCOUNT + 15 + + + + + ${skipUTs} + + + + + + + + + native + + + native + + + + false + true + + - - validate - - - validate - - - - + + validate + + + validate + + + + - - org.sonarsource.scanner.maven - sonar-maven-plugin - ${sonar-plugin.version} - - - sca_with_sonar + + org.sonarsource.scanner.maven + sonar-maven-plugin + ${sonar-plugin.version} + + + sca_with_sonar - test - - sonar - - - - - - - - - it - - - it - - - - - - maven-surefire-plugin - ${surefire-plugin.version} - - - org.jboss.logmanager.LogManager - ${maven.home} - - - **/*Test.java - - - **/*IT.java - - - - - - - - + test + + sonar + + + + + + + + + it + + + it + + + + + + maven-surefire-plugin + ${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + ${maven.home} + + + **/*Test.java + + + **/*IT.java + + + + + + + + it2 + + + it2 + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + it.pagopa.swclient.mil.auth.resource.TokenResourceIntegration + + + + + + + \ No newline at end of file diff --git a/src/test/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIT.java b/src/it/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIT.java similarity index 73% rename from src/test/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIT.java rename to src/it/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIT.java index ec78ae71..700be9e3 100644 --- a/src/test/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIT.java +++ b/src/it/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIT.java @@ -6,14 +6,11 @@ package it.pagopa.swclient.mil.auth.resource; import static io.restassured.RestAssured.given; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import java.io.File; import org.assertj.core.util.Files; -import org.eclipse.microprofile.config.inject.ConfigProperty; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -22,14 +19,11 @@ import com.atlassian.oai.validator.restassured.OpenApiValidationFilter; import com.nimbusds.jose.util.StandardCharset; -import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; import it.pagopa.swclient.mil.auth.bean.AuthFormParamName; import it.pagopa.swclient.mil.auth.bean.AuthJsonPropertyName; import it.pagopa.swclient.mil.auth.bean.GrantType; import it.pagopa.swclient.mil.auth.bean.Scope; -import it.pagopa.swclient.mil.auth.bean.TokenType; -import jakarta.ws.rs.core.MediaType; /** * To run this from your workstation, connect to CSTAR-DEV by VPN and: @@ -37,57 +31,43 @@ * // @formatter:off * * mvn verify \ - -DskipUTs=true \ - -DskipITs=false \ - -Dbase_uri=https://cstar-d-mcshared-auth-ca.blueforest-569cf489.westeurope.azurecontainerapps.io:443 \ - -Dadmin_client_id=f0ef1b15-c54a-4552-9e9a-2ca4d83260d7 \ - -Dadmin_client_secret=7fc345f5-0f6f-4df1-9f1c-21a6a8a95da0 \ - -Dtoken_info_client_id=null \ - -Dtoken_info_client_secret=null \ - -Dtest_username=null \ - -Dtest_password=null + * -DskipUTs=true \ + * -DskipITs=false \ + * -Dbase_uri=${base_uri} \ + * -Dadmin_client_id=${admin_client_id} \ + * -Dadmin_client_secret=${admin_client_secret} \ + * -Dtoken_info_client_id=${token_info_client_id} \ + * -Dtoken_info_client_secret=${token_info_client_secret} \ + * -Dtest_username=${test_username} \ + * -Dtest_password=${test_password} * * // @formatter:on * - * @author antonio.tarricone + * @author Antonio Tarricone */ -@QuarkusTest class TokenResourceIT { /* * */ - @ConfigProperty(name = "base_uri", defaultValue = "null") - String baseUri; - - @ConfigProperty(name = "port", defaultValue = "443") - int port; + private static String baseUri; /* * */ - @ConfigProperty(name = "admin_client_id", defaultValue = "null") - String adminClientId; - - @ConfigProperty(name = "admin_client_secret", defaultValue = "null") - String adminClientSecret; + private static String adminClientId; + private static String adminClientSecret; /* * */ - @ConfigProperty(name = "token_info_client_id", defaultValue = "null") - String tokenInfoClientId; - - @ConfigProperty(name = "token_info_client_secret", defaultValue = "null") - String tokenInfoClientSecret; + private static String tokenInfoClientId; + private static String tokenInfoClientSecret; /* * */ - @ConfigProperty(name = "test_username", defaultValue = "null") - String testUsername; - - @ConfigProperty(name = "test_password", defaultValue = "null") - String testPassword; + private static String testUsername; + private static String testPassword; /** * @@ -101,6 +81,17 @@ static void loadOpenApiDescriptor() { StandardCharset.UTF_8))/*, new RequestLoggingFilter(System.out), new ResponseLoggingFilter(System.out)*/); + + baseUri = System.getProperty("base_uri"); + + adminClientId = System.getProperty("admin_client_id"); + adminClientSecret = System.getProperty("admin_client_secret"); + + tokenInfoClientId = System.getProperty("token_info_client_id"); + tokenInfoClientSecret = System.getProperty("token_info_client_secret"); + + testUsername = System.getProperty("test_username"); + testPassword = System.getProperty("test_password"); } /** diff --git a/src/it/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIntegration.java b/src/it/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIntegration.java new file mode 100644 index 00000000..2aff4d9e --- /dev/null +++ b/src/it/java/it/pagopa/swclient/mil/auth/resource/TokenResourceIntegration.java @@ -0,0 +1,184 @@ +/* + * TokenResourceIT.java + * + * 5 dic 2024 + */ +package it.pagopa.swclient.mil.auth.resource; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.nullValue; + +import java.io.File; + +import org.assertj.core.util.Files; + +import com.atlassian.oai.validator.restassured.OpenApiValidationFilter; +import com.nimbusds.jose.util.StandardCharset; + +import io.restassured.RestAssured; +import it.pagopa.swclient.mil.auth.bean.AuthFormParamName; +import it.pagopa.swclient.mil.auth.bean.AuthJsonPropertyName; +import it.pagopa.swclient.mil.auth.bean.GrantType; +import it.pagopa.swclient.mil.auth.bean.Scope; + +/** + * To run this from your workstation, connect to CSTAR-DEV by VPN and: + * + * // @formatter:off + * + * mvn test-compile -q exec:java -Pit2 -Dexec.classpathScope=test + -Dbase_uri=https://cstar-d-mcshared-auth-ca.blueforest-569cf489.westeurope.azurecontainerapps.io:443 \ + -Dadmin_client_id=f0ef1b15-c54a-4552-9e9a-2ca4d83260d7 \ + -Dadmin_client_secret=7fc345f5-0f6f-4df1-9f1c-21a6a8a95da0 \ + -Dtoken_info_client_id=null \ + -Dtoken_info_client_secret=null \ + -Dtest_username=null \ + -Dtest_password=null + * + * // @formatter:on + * + * @author antonio.tarricone + */ +public class TokenResourceIntegration { + /* + * + */ + private String baseUri; + + /* + * + */ + private String adminClientId; + private String adminClientSecret; + + /* + * + */ + private String tokenInfoClientId; + private String tokenInfoClientSecret; + + /* + * + */ + private String testUsername; + private String testPassword; + + /** + * + */ + public TokenResourceIntegration() { + RestAssured.filters( + new OpenApiValidationFilter( + Files.contentOf( + new File("src/main/resources/META-INF/openapi.yaml"), + StandardCharset.UTF_8))); + + baseUri = System.getProperty("base_uri"); + + adminClientId = System.getProperty("admin_client_id"); + adminClientSecret = System.getProperty("admin_client_secret"); + + tokenInfoClientId = System.getProperty("token_info_client_id"); + tokenInfoClientSecret = System.getProperty("token_info_client_secret"); + + testUsername = System.getProperty("test_username"); + testPassword = System.getProperty("test_password"); + } + + /** + * + */ + private TokenResourceIntegration given_rightClientCredentials_when_theEndPointIsInvoked_then_getAccessToken() { + given() + .baseUri(baseUri) + .formParam(AuthFormParamName.CLIENT_ID, adminClientId) + .formParam(AuthFormParamName.GRANT_TYPE, GrantType.CLIENT_CREDENTIALS) + .formParam(AuthFormParamName.CLIENT_SECRET, adminClientSecret) + .when() + .post("/token") + .then() + .statusCode(200) + .body(AuthJsonPropertyName.REFRESH_TOKEN, nullValue()); + return this; + } + + /** + * + */ + private TokenResourceIntegration given_wrongClientId_when_theEndPointIsInvoked_then_getError() { + given() + .baseUri(baseUri) + .formParam(AuthFormParamName.CLIENT_ID, "00000000-0000-0000-0000-000000000000") + .formParam(AuthFormParamName.GRANT_TYPE, GrantType.CLIENT_CREDENTIALS) + .formParam(AuthFormParamName.CLIENT_SECRET, adminClientSecret) + .when() + .post("/token") + .then() + .statusCode(401); + return this; + } + + /** + * + */ + private TokenResourceIntegration given_wrongClientSecret_when_theEndPointIsInvoked_then_getError() { + given() + .baseUri(baseUri) + .formParam(AuthFormParamName.CLIENT_ID, adminClientId) + .formParam(AuthFormParamName.GRANT_TYPE, GrantType.CLIENT_CREDENTIALS) + .formParam(AuthFormParamName.CLIENT_SECRET, "00000000-0000-0000-0000-000000000000") + .when() + .post("/token") + .then() + .statusCode(401); + return this; + } + + /** + * + */ + private TokenResourceIntegration given_rightClientCredentialsAndOfflineAccessIsRequired_when_theEndPointIsInvoked_then_getAccessToken() { + given() + .baseUri(baseUri) + .formParam(AuthFormParamName.CLIENT_ID, adminClientId) + .formParam(AuthFormParamName.GRANT_TYPE, GrantType.CLIENT_CREDENTIALS) + .formParam(AuthFormParamName.CLIENT_SECRET, adminClientSecret) + .formParam(AuthFormParamName.SCOPE, Scope.OFFLINE_ACCESS) + .when() + .post("/token") + .then() + .statusCode(400); + return this; + } + + /** + * + */ + private TokenResourceIntegration given_rightClientCredentialsAndFiscalCode_when_theEndPointIsInvoked_then_getAccessToken() { + given() + .baseUri(baseUri) + .formParam(AuthFormParamName.CLIENT_ID, adminClientId) + .formParam(AuthFormParamName.GRANT_TYPE, GrantType.CLIENT_CREDENTIALS) + .formParam(AuthFormParamName.CLIENT_SECRET, adminClientSecret) + .formParam(AuthFormParamName.FISCAL_CODE, "RSSMRA85T10A562S") + .when() + .post("/token") + .then() + .statusCode(200) + .body(AuthJsonPropertyName.REFRESH_TOKEN, nullValue()); + return this; + } + + /** + * + * @param args + */ + public static void main(String[] args) { + new TokenResourceIntegration() + .given_rightClientCredentials_when_theEndPointIsInvoked_then_getAccessToken() + .given_wrongClientId_when_theEndPointIsInvoked_then_getError() + .given_wrongClientSecret_when_theEndPointIsInvoked_then_getError() + .given_rightClientCredentialsAndOfflineAccessIsRequired_when_theEndPointIsInvoked_then_getAccessToken() + .given_rightClientCredentialsAndFiscalCode_when_theEndPointIsInvoked_then_getAccessToken(); + } +} diff --git a/src/test/gherkin/get_access_token.feature b/src/test/gherkin/get_access_token.feature deleted file mode 100644 index 1c1a4a76..00000000 --- a/src/test/gherkin/get_access_token.feature +++ /dev/null @@ -1,50 +0,0 @@ -Feature: As Server Application, I want to authenticate myself, so that I can get and access token - - - Rule: There is not a fiscal code to protect - - - Scenario: Access token without refresh token is requested with correct credentials and roles exist - Given correct client credentials - And client has roles - And no fiscal code - When access token is requested - And refresh token is not requested - Then get access token - And access token does not have fiscal code claim - And do not get refresh token - - - Scenario: Access token with refresh token is requested with correct credentials and roles exist - Given correct client credentials - And client has roles - And no fiscal code - When access token is requested - And refresh token is requested - Then get access token - And access token does not have fiscal code claim - And get refresh token - - - Scenario: Access token without refresh token is requested with correct credentials and roles do not exist - - - Scenario: Access token without refresh token is requested with wrong credentials do not exit - - - Scenario: Access token with refresh token is requested with wrong credentials - - - Rule: There is a fiscal code to protect - - - Scenario: Access token without refresh token is requested with correct credentials - - - Scenario: Access token with refresh token is requested with correct credentials - - - Scenario: Access token without refresh token is requested with wrong credentials - - - Scenario: Access token with refresh token is requested with wrong credentials \ No newline at end of file