From 29233f33c89b8ac342b1983786ece54bffcdd730 Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Wed, 27 Apr 2022 10:07:31 +0200 Subject: [PATCH] Clear import/export properties at the end of the test This avoids the pollution of system properties that might lead to failures following tests. Closes #11670 --- .../TestingExportImportResource.java | 22 +++++------ .../BrokerLinkAndTokenExchangeTest.java | 38 ++++++++++--------- .../ClientPoliciesImportExportTest.java | 6 +++ 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingExportImportResource.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingExportImportResource.java index 4d0e008271..69a2be718f 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingExportImportResource.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingExportImportResource.java @@ -46,59 +46,59 @@ public interface TestingExportImportResource { @Path("/get-users-per-file") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public Integer getUsersPerFile(); + Integer getUsersPerFile(); @PUT @Path("/set-users-per-file") @Consumes(MediaType.APPLICATION_JSON) - public void setUsersPerFile(@QueryParam("usersPerFile") Integer usersPerFile); + void setUsersPerFile(@QueryParam("usersPerFile") Integer usersPerFile); @GET @Path("/get-dir") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public String getDir(); + String getDir(); @PUT @Path("/set-dir") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public String setDir(@QueryParam("dir") String dir); + String setDir(@QueryParam("dir") String dir); @PUT @Path("/set-import-strategy") @Consumes(MediaType.APPLICATION_JSON) - public void setStrategy(@QueryParam("importStrategy") Strategy strategy); + void setStrategy(@QueryParam("importStrategy") Strategy strategy); @PUT @Path("/export-import-provider") @Consumes(MediaType.APPLICATION_JSON) - public void setProvider(@QueryParam("exportImportProvider") String exportImportProvider); + void setProvider(@QueryParam("exportImportProvider") String exportImportProvider); @PUT @Path("/export-import-file") @Consumes(MediaType.APPLICATION_JSON) - public void setFile(@QueryParam("file") String file); + void setFile(@QueryParam("file") String file); @PUT @Path("/export-import-action") @Consumes(MediaType.APPLICATION_JSON) - public void setAction(@QueryParam("exportImportAction") String exportImportAction); + void setAction(@QueryParam("exportImportAction") String exportImportAction); @PUT @Path("/set-realm-name") @Consumes(MediaType.APPLICATION_JSON) - public void setRealmName(@QueryParam("realmName") String realmName); + void setRealmName(@QueryParam("realmName") String realmName); @GET @Path("/get-test-dir") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public String getExportImportTestDirectory(); + String getExportImportTestDirectory(); @GET @Path("/clear") @Produces(MediaType.APPLICATION_JSON) - public Response clear(); + void clear(); } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/BrokerLinkAndTokenExchangeTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/BrokerLinkAndTokenExchangeTest.java index 69ad09612d..3f6c84845d 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/BrokerLinkAndTokenExchangeTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/BrokerLinkAndTokenExchangeTest.java @@ -460,7 +460,7 @@ public class BrokerLinkAndTokenExchangeTest extends AbstractServletsAdapterTest @Test @UncaughtServerErrorExpected - public void testAccountLinkNoTokenStore() throws Exception { + public void testAccountLinkNoTokenStore() { testingClient.server().run(BrokerLinkAndTokenExchangeTest::turnOffTokenStore); RealmResource realm = adminClient.realms().realm(CHILD_IDP); @@ -500,8 +500,6 @@ public class BrokerLinkAndTokenExchangeTest extends AbstractServletsAdapterTest /** * KEYCLOAK-6026 - * - * @throws Exception */ @Test @UncaughtServerErrorExpected @@ -509,19 +507,22 @@ public class BrokerLinkAndTokenExchangeTest extends AbstractServletsAdapterTest ContainerAssume.assumeNotAuthServerRemote(); testExternalExchange(); - testingClient.testing().exportImport().setProvider(SingleFileExportProviderFactory.PROVIDER_ID); - String targetFilePath = testingClient.testing().exportImport().getExportImportTestDirectory() + File.separator + "singleFile-full.json"; - testingClient.testing().exportImport().setFile(targetFilePath); - testingClient.testing().exportImport().setAction(ExportImportConfig.ACTION_EXPORT); - testingClient.testing().exportImport().setRealmName(CHILD_IDP); - testingClient.testing().exportImport().runExport(); - adminClient.realms().realm(CHILD_IDP).remove(); - testingClient.testing().exportImport().setAction(ExportImportConfig.ACTION_IMPORT); + try { + testingClient.testing().exportImport().setProvider(SingleFileExportProviderFactory.PROVIDER_ID); + String targetFilePath = testingClient.testing().exportImport().getExportImportTestDirectory() + File.separator + "singleFile-full.json"; + testingClient.testing().exportImport().setFile(targetFilePath); + testingClient.testing().exportImport().setAction(ExportImportConfig.ACTION_EXPORT); + testingClient.testing().exportImport().setRealmName(CHILD_IDP); + testingClient.testing().exportImport().runExport(); - testingClient.testing().exportImport().runImport(); + adminClient.realms().realm(CHILD_IDP).remove(); + testingClient.testing().exportImport().setAction(ExportImportConfig.ACTION_IMPORT); - testingClient.testing().exportImport().clear(); + testingClient.testing().exportImport().runImport(); + } finally { + testingClient.testing().exportImport().clear(); + } testExternalExchange(); } @@ -552,8 +553,8 @@ public class BrokerLinkAndTokenExchangeTest extends AbstractServletsAdapterTest rep.getConfig().put("issuer", parentIssuer); adminClient.realm(CHILD_IDP).identityProviders().get(PARENT_IDP).update(rep); - String exchangedUserId = null; - String exchangedUsername = null; + String exchangedUserId; + String exchangedUsername; { // test signature validation @@ -699,7 +700,7 @@ public class BrokerLinkAndTokenExchangeTest extends AbstractServletsAdapterTest { // test unauthorized client gets 403 - Response response = exchangeUrl.request() + try (Response response = exchangeUrl.request() .header(HttpHeaders.AUTHORIZATION, BasicAuthHelper.createHeader(UNAUTHORIZED_CHILD_CLIENT, "password")) .post(Entity.form( new Form() @@ -708,8 +709,9 @@ public class BrokerLinkAndTokenExchangeTest extends AbstractServletsAdapterTest .param(OAuth2Constants.SUBJECT_TOKEN_TYPE, OAuth2Constants.JWT_TOKEN_TYPE) .param(OAuth2Constants.SUBJECT_ISSUER, PARENT_IDP) - )); - Assert.assertEquals(403, response.getStatus()); + ))) { + Assert.assertEquals(403, response.getStatus()); + } } } finally { httpClient.close(); diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/client/ClientPoliciesImportExportTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/client/ClientPoliciesImportExportTest.java index e3e026b4bc..638589c7bf 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/client/ClientPoliciesImportExportTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/client/ClientPoliciesImportExportTest.java @@ -17,6 +17,7 @@ package org.keycloak.testsuite.client; +import org.junit.After; import org.junit.Test; import org.keycloak.exportimport.ExportImportConfig; import org.keycloak.exportimport.singlefile.SingleFileExportProviderFactory; @@ -57,6 +58,11 @@ public class ClientPoliciesImportExportTest extends AbstractClientPoliciesTest { removeAllRealmsDespiteMaster(); } + @After + public void afterImportExport() { + testingClient.testing().exportImport().clear(); + } + @Test public void testSingleFileRealmExportImport() throws Throwable { testingClient.testing().exportImport().setProvider(SingleFileExportProviderFactory.PROVIDER_ID);