Proper cleanup
This commit is contained in:
parent
1b9da5dff6
commit
3e58d3da8d
5 changed files with 85 additions and 99 deletions
|
@ -62,11 +62,6 @@ public class TestCleanup {
|
|||
}
|
||||
|
||||
|
||||
public TestCleanup addCleanup(Runnable r) {
|
||||
genericCleanups.add(r);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TestCleanup addCleanup(AutoCloseable c) {
|
||||
genericCleanups.add(() -> {
|
||||
try {
|
||||
|
|
|
@ -225,7 +225,7 @@ public class SAMLLoginResponseHandlingTest extends AbstractSAMLServletAdapterTes
|
|||
|
||||
try (Response response = protocolMappersResource.createMapper(mapper)) {
|
||||
String createdId = getCreatedId(response);
|
||||
getCleanup().addCleanup((Runnable) () -> {
|
||||
getCleanup().addCleanup(() -> {
|
||||
protocolMappersResource.delete(createdId);
|
||||
mapper.setConfig(origConfig);
|
||||
protocolMappersResource.createMapper(mapper).close();
|
||||
|
|
|
@ -2202,6 +2202,8 @@ public class UserTest extends AbstractAdminTest {
|
|||
@Test
|
||||
public void updateUserWithNewUsername() {
|
||||
switchEditUsernameAllowedOn(true);
|
||||
getCleanup().addCleanup(() -> switchEditUsernameAllowedOn(false));
|
||||
|
||||
String id = createUser();
|
||||
|
||||
UserResource user = realm.users().get(id);
|
||||
|
@ -2211,14 +2213,12 @@ public class UserTest extends AbstractAdminTest {
|
|||
|
||||
userRep = realm.users().get(id).toRepresentation();
|
||||
assertEquals("user11", userRep.getUsername());
|
||||
|
||||
// Revert
|
||||
switchEditUsernameAllowedOn(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUserWithoutUsername() {
|
||||
switchEditUsernameAllowedOn(true);
|
||||
getCleanup().addCleanup(() -> switchEditUsernameAllowedOn(false));
|
||||
|
||||
String id = createUser();
|
||||
|
||||
|
@ -2242,14 +2242,12 @@ public class UserTest extends AbstractAdminTest {
|
|||
assertEquals("user1@localhost", rep.getEmail());
|
||||
assertEquals("Firstname", rep.getFirstName());
|
||||
assertEquals("Lastname", rep.getLastName());
|
||||
|
||||
// Revert
|
||||
switchEditUsernameAllowedOn(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUserWithEmailAsUsername() {
|
||||
switchRegistrationEmailAsUsername(true);
|
||||
getCleanup().addCleanup(() -> switchRegistrationEmailAsUsername(false));
|
||||
|
||||
String id = createUser();
|
||||
|
||||
|
@ -2262,8 +2260,6 @@ public class UserTest extends AbstractAdminTest {
|
|||
|
||||
userRep = realm.users().get(id).toRepresentation();
|
||||
assertEquals("user11@localhost", userRep.getUsername());
|
||||
|
||||
switchRegistrationEmailAsUsername(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -130,36 +130,39 @@ public class RealmTest extends AbstractAdminTest {
|
|||
|
||||
@Test
|
||||
public void renameRealm() {
|
||||
getCleanup()
|
||||
.addCleanup(() -> adminClient.realms().realm("old").remove())
|
||||
.addCleanup(() -> adminClient.realms().realm("new").remove());
|
||||
|
||||
RealmRepresentation rep = new RealmRepresentation();
|
||||
rep.setId("old");
|
||||
rep.setRealm("old");
|
||||
|
||||
try {
|
||||
adminClient.realms().create(rep);
|
||||
adminClient.realms().create(rep);
|
||||
|
||||
rep.setRealm("new");
|
||||
adminClient.realm("old").update(rep);
|
||||
rep.setRealm("new");
|
||||
adminClient.realm("old").update(rep);
|
||||
|
||||
// Check client in master realm renamed
|
||||
Assert.assertEquals(0, adminClient.realm("master").clients().findByClientId("old-realm").size());
|
||||
Assert.assertEquals(1, adminClient.realm("master").clients().findByClientId("new-realm").size());
|
||||
// Check client in master realm renamed
|
||||
Assert.assertEquals(0, adminClient.realm("master").clients().findByClientId("old-realm").size());
|
||||
Assert.assertEquals(1, adminClient.realm("master").clients().findByClientId("new-realm").size());
|
||||
|
||||
ClientRepresentation adminConsoleClient = adminClient.realm("new").clients().findByClientId(Constants.ADMIN_CONSOLE_CLIENT_ID).get(0);
|
||||
assertEquals(Constants.AUTH_ADMIN_URL_PROP, adminConsoleClient.getRootUrl());
|
||||
assertEquals("/admin/new/console/", adminConsoleClient.getBaseUrl());
|
||||
assertEquals("/admin/new/console/*", adminConsoleClient.getRedirectUris().get(0));
|
||||
ClientRepresentation adminConsoleClient = adminClient.realm("new").clients().findByClientId(Constants.ADMIN_CONSOLE_CLIENT_ID).get(0);
|
||||
assertEquals(Constants.AUTH_ADMIN_URL_PROP, adminConsoleClient.getRootUrl());
|
||||
assertEquals("/admin/new/console/", adminConsoleClient.getBaseUrl());
|
||||
assertEquals("/admin/new/console/*", adminConsoleClient.getRedirectUris().get(0));
|
||||
|
||||
ClientRepresentation accountClient = adminClient.realm("new").clients().findByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).get(0);
|
||||
assertEquals(Constants.AUTH_BASE_URL_PROP, accountClient.getRootUrl());
|
||||
assertEquals("/realms/new/account/", accountClient.getBaseUrl());
|
||||
assertEquals("/realms/new/account/*", accountClient.getRedirectUris().get(0));
|
||||
} finally {
|
||||
adminClient.realms().realm(rep.getRealm()).remove();
|
||||
}
|
||||
ClientRepresentation accountClient = adminClient.realm("new").clients().findByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).get(0);
|
||||
assertEquals(Constants.AUTH_BASE_URL_PROP, accountClient.getRootUrl());
|
||||
assertEquals("/realms/new/account/", accountClient.getBaseUrl());
|
||||
assertEquals("/realms/new/account/*", accountClient.getRedirectUris().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createRealmEmpty() {
|
||||
getCleanup()
|
||||
.addCleanup(() -> adminClient.realms().realm("new-realm").remove());
|
||||
|
||||
RealmRepresentation rep = new RealmRepresentation();
|
||||
rep.setRealm("new-realm");
|
||||
|
||||
|
@ -191,36 +194,34 @@ public class RealmTest extends AbstractAdminTest {
|
|||
rep.setRealm("attributes");
|
||||
|
||||
adminClient.realms().create(rep);
|
||||
getCleanup()
|
||||
.addCleanup(() -> adminClient.realms().realm("attributes").remove());
|
||||
|
||||
try {
|
||||
RealmRepresentation rep2 = adminClient.realm("attributes").toRepresentation();
|
||||
if (rep2.getAttributes() != null) {
|
||||
Arrays.asList(CibaConfig.CIBA_BACKCHANNEL_TOKEN_DELIVERY_MODE,
|
||||
CibaConfig.CIBA_EXPIRES_IN,
|
||||
CibaConfig.CIBA_INTERVAL,
|
||||
CibaConfig.CIBA_AUTH_REQUESTED_USER_HINT).stream().forEach(i -> rep2.getAttributes().remove(i));
|
||||
}
|
||||
|
||||
Set<String> attributesKeys = rep2.getAttributes().keySet();
|
||||
|
||||
int expectedAttributesCount = 3;
|
||||
final Set<String> expectedAttributes = Sets.newHashSet(
|
||||
OAuth2DeviceConfig.OAUTH2_DEVICE_CODE_LIFESPAN,
|
||||
OAuth2DeviceConfig.OAUTH2_DEVICE_POLLING_INTERVAL,
|
||||
ParConfig.PAR_REQUEST_URI_LIFESPAN
|
||||
);
|
||||
|
||||
// This attribute is represented in Legacy store as attribute and for Map store as a field
|
||||
if (!StoreProvider.getCurrentProvider().isMapStore()) {
|
||||
expectedAttributes.add(OTPPolicy.REALM_REUSABLE_CODE_ATTRIBUTE);
|
||||
expectedAttributesCount++;
|
||||
}
|
||||
|
||||
assertThat(attributesKeys.size(), CoreMatchers.is(expectedAttributesCount));
|
||||
assertThat(attributesKeys, CoreMatchers.is(expectedAttributes));
|
||||
} finally {
|
||||
adminClient.realm("attributes").remove();
|
||||
RealmRepresentation rep2 = adminClient.realm("attributes").toRepresentation();
|
||||
if (rep2.getAttributes() != null) {
|
||||
Arrays.asList(CibaConfig.CIBA_BACKCHANNEL_TOKEN_DELIVERY_MODE,
|
||||
CibaConfig.CIBA_EXPIRES_IN,
|
||||
CibaConfig.CIBA_INTERVAL,
|
||||
CibaConfig.CIBA_AUTH_REQUESTED_USER_HINT).stream().forEach(i -> rep2.getAttributes().remove(i));
|
||||
}
|
||||
|
||||
Set<String> attributesKeys = rep2.getAttributes().keySet();
|
||||
|
||||
int expectedAttributesCount = 3;
|
||||
final Set<String> expectedAttributes = Sets.newHashSet(
|
||||
OAuth2DeviceConfig.OAUTH2_DEVICE_CODE_LIFESPAN,
|
||||
OAuth2DeviceConfig.OAUTH2_DEVICE_POLLING_INTERVAL,
|
||||
ParConfig.PAR_REQUEST_URI_LIFESPAN
|
||||
);
|
||||
|
||||
// This attribute is represented in Legacy store as attribute and for Map store as a field
|
||||
if (!StoreProvider.getCurrentProvider().isMapStore()) {
|
||||
expectedAttributes.add(OTPPolicy.REALM_REUSABLE_CODE_ATTRIBUTE);
|
||||
expectedAttributesCount++;
|
||||
}
|
||||
|
||||
assertThat(attributesKeys.size(), CoreMatchers.is(expectedAttributesCount));
|
||||
assertThat(attributesKeys, CoreMatchers.is(expectedAttributes));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -231,6 +232,8 @@ public class RealmTest extends AbstractAdminTest {
|
|||
rep.getSmtpServer().put("password", "secret");
|
||||
|
||||
adminClient.realms().create(rep);
|
||||
getCleanup()
|
||||
.addCleanup(() -> adminClient.realms().realm("realm-with-smtp").remove());
|
||||
|
||||
RealmRepresentation returned = adminClient.realm("realm-with-smtp").toRepresentation();
|
||||
assertEquals(ComponentRepresentation.SECRET_VALUE, returned.getSmtpServer().get("password"));
|
||||
|
@ -251,8 +254,6 @@ public class RealmTest extends AbstractAdminTest {
|
|||
|
||||
RealmRepresentation realm = adminClient.realms().findAll().stream().filter(r -> r.getRealm().equals("realm-with-smtp")).findFirst().get();
|
||||
assertEquals(ComponentRepresentation.SECRET_VALUE, realm.getSmtpServer().get("password"));
|
||||
|
||||
adminClient.realm("realm-with-smtp").remove();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -261,6 +262,8 @@ public class RealmTest extends AbstractAdminTest {
|
|||
rep.setRealm("new-realm");
|
||||
|
||||
adminClient.realms().create(rep);
|
||||
getCleanup()
|
||||
.addCleanup(() -> adminClient.realms().realm("new-realm").remove());
|
||||
|
||||
assertEquals(null, adminClient.realm("new-realm").toRepresentation().getPasswordPolicy());
|
||||
|
||||
|
@ -271,22 +274,17 @@ public class RealmTest extends AbstractAdminTest {
|
|||
adminClient.realms().create(rep);
|
||||
|
||||
assertEquals("length(8)", adminClient.realm("new-realm").toRepresentation().getPasswordPolicy());
|
||||
|
||||
adminClient.realms().realm("new-realm").remove();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createRealmFromJson() {
|
||||
RealmRepresentation rep = loadJson(getClass().getResourceAsStream("/admin-test/testrealm.json"), RealmRepresentation.class);
|
||||
try {
|
||||
adminClient.realms().create(rep);
|
||||
adminClient.realms().create(rep);
|
||||
getCleanup()
|
||||
.addCleanup(() -> adminClient.realms().realm("admin-test-1").remove());
|
||||
|
||||
RealmRepresentation created = adminClient.realms().realm("admin-test-1").toRepresentation();
|
||||
assertRealm(rep, created);
|
||||
|
||||
} finally {
|
||||
adminClient.realms().realm("admin-test-1").remove();
|
||||
}
|
||||
RealmRepresentation created = adminClient.realms().realm("admin-test-1").toRepresentation();
|
||||
assertRealm(rep, created);
|
||||
}
|
||||
|
||||
//KEYCLOAK-6146
|
||||
|
@ -312,7 +310,6 @@ public class RealmTest extends AbstractAdminTest {
|
|||
@Test
|
||||
public void createRealmWithPasswordPolicyFromJsonWithValidPasswords() {
|
||||
RealmRepresentation rep = loadJson(getClass().getResourceAsStream("/import/testrealm-keycloak-6146.json"), RealmRepresentation.class);
|
||||
rep.setId(KeycloakModelUtils.generateId());
|
||||
try (Creator<RealmResource> c = Creator.create(adminClient, rep)) {
|
||||
RealmRepresentation created = c.resource().toRepresentation();
|
||||
assertRealm(rep, created);
|
||||
|
@ -354,6 +351,10 @@ public class RealmTest extends AbstractAdminTest {
|
|||
*/
|
||||
@Test
|
||||
public void renameRealmTest() throws Exception {
|
||||
getCleanup()
|
||||
.addCleanup(() -> adminClient.realms().realm("test-immutable").remove())
|
||||
.addCleanup(() -> adminClient.realms().realm("test-immutable-old").remove());
|
||||
|
||||
RealmRepresentation realm1 = new RealmRepresentation();
|
||||
realm1.setRealm("test-immutable");
|
||||
adminClient.realms().create(realm1);
|
||||
|
@ -366,9 +367,6 @@ public class RealmTest extends AbstractAdminTest {
|
|||
realm2.setRealm("test-immutable");
|
||||
adminClient.realms().create(realm2);
|
||||
assertThat(adminClient.realms().realm("test-immutable").toRepresentation(), notNullValue());
|
||||
|
||||
adminClient.realms().realm("test-immutable-old").remove();
|
||||
adminClient.realms().realm("test-immutable").remove();
|
||||
}
|
||||
|
||||
private RealmEventsConfigRepresentation copyRealmEventsConfigRepresentation(RealmEventsConfigRepresentation rep) {
|
||||
|
@ -863,14 +861,12 @@ public class RealmTest extends AbstractAdminTest {
|
|||
RealmRepresentation rep = new RealmRepresentation();
|
||||
rep.setRealm("new-realm");
|
||||
|
||||
try {
|
||||
adminClient.realms().create(rep);
|
||||
adminClient.realms().create(rep);
|
||||
getCleanup()
|
||||
.addCleanup(() -> adminClient.realms().realm("new-realm").remove());
|
||||
|
||||
assertEquals(Constants.DEFAULT_SIGNATURE_ALGORITHM, adminClient.realm("master").toRepresentation().getDefaultSignatureAlgorithm());
|
||||
assertEquals(Constants.DEFAULT_SIGNATURE_ALGORITHM, adminClient.realm("new-realm").toRepresentation().getDefaultSignatureAlgorithm());
|
||||
} finally {
|
||||
adminClient.realms().realm(rep.getRealm()).remove();
|
||||
}
|
||||
assertEquals(Constants.DEFAULT_SIGNATURE_ALGORITHM, adminClient.realm("master").toRepresentation().getDefaultSignatureAlgorithm());
|
||||
assertEquals(Constants.DEFAULT_SIGNATURE_ALGORITHM, adminClient.realm("new-realm").toRepresentation().getDefaultSignatureAlgorithm());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -878,28 +874,26 @@ public class RealmTest extends AbstractAdminTest {
|
|||
RealmRepresentation rep = new RealmRepresentation();
|
||||
rep.setRealm("new-realm");
|
||||
|
||||
try {
|
||||
adminClient.realms().create(rep);
|
||||
adminClient.realms().create(rep);
|
||||
|
||||
RealmResource realm = adminClient.realms().realm("new-realm");
|
||||
RealmResource realm = adminClient.realms().realm("new-realm");
|
||||
getCleanup()
|
||||
.addCleanup(() -> adminClient.realms().realm("new-realm").remove());
|
||||
|
||||
rep = realm.toRepresentation();
|
||||
rep = realm.toRepresentation();
|
||||
|
||||
List<String> supportedApplications = rep.getOtpSupportedApplications();
|
||||
assertThat(supportedApplications, hasSize(3));
|
||||
assertThat(supportedApplications, containsInAnyOrder("totpAppGoogleName", "totpAppFreeOTPName", "totpAppMicrosoftAuthenticatorName"));
|
||||
List<String> supportedApplications = rep.getOtpSupportedApplications();
|
||||
assertThat(supportedApplications, hasSize(3));
|
||||
assertThat(supportedApplications, containsInAnyOrder("totpAppGoogleName", "totpAppFreeOTPName", "totpAppMicrosoftAuthenticatorName"));
|
||||
|
||||
rep.setOtpPolicyDigits(8);
|
||||
realm.update(rep);
|
||||
rep.setOtpPolicyDigits(8);
|
||||
realm.update(rep);
|
||||
|
||||
rep = realm.toRepresentation();
|
||||
rep = realm.toRepresentation();
|
||||
|
||||
supportedApplications = rep.getOtpSupportedApplications();
|
||||
assertThat(supportedApplications, hasSize(1));
|
||||
assertThat(supportedApplications, containsInAnyOrder("totpAppFreeOTPName"));
|
||||
} finally {
|
||||
adminClient.realms().realm(rep.getRealm()).remove();
|
||||
}
|
||||
supportedApplications = rep.getOtpSupportedApplications();
|
||||
assertThat(supportedApplications, hasSize(1));
|
||||
assertThat(supportedApplications, containsInAnyOrder("totpAppFreeOTPName"));
|
||||
}
|
||||
|
||||
private void setupTestAppAndUser() {
|
||||
|
|
|
@ -129,6 +129,7 @@ public class ComponentExportImportTest extends AbstractAuthTest {
|
|||
}
|
||||
});
|
||||
|
||||
getCleanup().addCleanup(testRealmResource()::remove);
|
||||
testRealmResource().remove();
|
||||
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue