Skip to content

Commit

Permalink
Use cleanup tasks in migrated UserProfileAdminTest (keycloak#35074)
Browse files Browse the repository at this point in the history
Part of keycloak#34494

Signed-off-by: stianst <[email protected]>
  • Loading branch information
stianst authored Nov 19, 2024
1 parent c8f43ec commit 1b8cdbd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public RealmConfigBuilder registrationEmailAsUsername(boolean registrationEmailA
return this;
}

public RealmConfigBuilder editUsernameAllowed(boolean editUsernameAllowed) {
rep.setEditUsernameAllowed(editUsernameAllowed);
return this;
}

public RealmConfigBuilder defaultSignatureAlgorithm(String algorithm) {
rep.setDefaultSignatureAlgorithm(algorithm);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.keycloak.test.admin.userprofile;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.keycloak.admin.client.resource.UserProfileResource;
import org.keycloak.models.UserModel;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.UserProfileAttributeGroupMetadata;
import org.keycloak.representations.idm.UserProfileMetadata;
import org.keycloak.representations.userprofile.config.UPAttribute;
Expand All @@ -24,7 +21,6 @@
import java.util.Map;

@KeycloakIntegrationTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class UserProfileAdminTest {

@InjectRealm(lifecycle = LifeCycle.CLASS)
Expand All @@ -38,62 +34,35 @@ public void testDefaultConfigIfNoneSet() {

@Test
public void testSetDefaultConfig() {
realm.cleanup().add(r -> r.users().userProfile().update(null));

UPConfig config = UPConfigUtils.parseSystemDefaultConfig().addOrReplaceAttribute(new UPAttribute("test"));
UserProfileResource userProfile = realm.admin().users().userProfile();
userProfile.update(config);
// TODO
/*getCleanup().addCleanup(() -> testRealm().users().userProfile().update(null));*/

JsonTestUtils.assertJsonEquals(config, userProfile.getConfiguration());
}

@Test
public void testEmailRequiredIfEmailAsUsernameEnabled() {
RealmRepresentation realmRep = realm.admin().toRepresentation();
realmRep.setRegistrationEmailAsUsername(true);
realm.admin().update(realmRep);
// TODO
/*getCleanup().addCleanup(() -> {
realmRep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
realm.update(realmRep);
});*/
realm.updateWithCleanup(r -> r.registrationEmailAsUsername(true));

UserProfileResource userProfile = realm.admin().users().userProfile();
UserProfileMetadata metadata = userProfile.getMetadata();
Assertions.assertTrue(metadata.getAttributeMetadata(UserModel.EMAIL).isRequired());
}

@Test
public void testEmailNotRequiredIfEmailAsUsernameDisabled() {
RealmRepresentation realmRep = realm.admin().toRepresentation();
realmRep.setRegistrationEmailAsUsername(false);
realm.admin().update(realmRep);
// TODO
/*getCleanup().addCleanup(() -> {
realmRep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
realm.update(realmRep);
});*/
UserProfileResource userProfile = realm.admin().users().userProfile();
UserProfileMetadata metadata = userProfile.getMetadata();
Assertions.assertFalse(metadata.getAttributeMetadata(UserModel.EMAIL).isRequired());
}

@Test
public void testUsernameRequiredAndWritableIfEmailAsUsernameDisabledAndEditUsernameAllowed() {
RealmRepresentation realmRep = realm.admin().toRepresentation();
realmRep.setRegistrationEmailAsUsername(false);
realm.admin().update(realmRep);
// TODO
/*getCleanup().addCleanup(() -> {
realmRep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
realm.update(realmRep);
});*/
realmRep.setEditUsernameAllowed(true);
realm.admin().update(realmRep);
// TODO
/*getCleanup().addCleanup(() -> {
realmRep.setEditUsernameAllowed(editUsernameAllowed);
realm.update(realmRep);
});*/
realm.updateWithCleanup(r -> r.editUsernameAllowed(true));

UserProfileResource userProfile = realm.admin().users().userProfile();
UserProfileMetadata metadata = userProfile.getMetadata();
Assertions.assertTrue(metadata.getAttributeMetadata(UserModel.USERNAME).isRequired());
Expand All @@ -102,21 +71,6 @@ public void testUsernameRequiredAndWritableIfEmailAsUsernameDisabledAndEditUsern

@Test
public void testUsernameRequiredAndWritableIfEmailAsUsernameDisabledAndEditUsernameDisabled() {
RealmRepresentation realmRep = realm.admin().toRepresentation();
realmRep.setRegistrationEmailAsUsername(false);
realm.admin().update(realmRep);
// TODO
/*getCleanup().addCleanup(() -> {
realmRep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
realm.update(realmRep);
});*/
realmRep.setEditUsernameAllowed(false);
realm.admin().update(realmRep);
// TODO
/*getCleanup().addCleanup(() -> {
realmRep.setEditUsernameAllowed(editUsernameAllowed);
realm.update(realmRep);
});*/
UserProfileResource userProfile = realm.admin().users().userProfile();
UserProfileMetadata metadata = userProfile.getMetadata();
Assertions.assertTrue(metadata.getAttributeMetadata(UserModel.USERNAME).isRequired());
Expand All @@ -125,14 +79,8 @@ public void testUsernameRequiredAndWritableIfEmailAsUsernameDisabledAndEditUsern

@Test
public void testUsernameNotRequiredIfEmailAsUsernameEnabled() {
RealmRepresentation realmRep = realm.admin().toRepresentation();
realmRep.setRegistrationEmailAsUsername(true);
realm.admin().update(realmRep);
// TODO
/*getCleanup().addCleanup(() -> {
realmRep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
realm.update(realmRep);
});*/
realm.updateWithCleanup(r -> r.registrationEmailAsUsername(true));

UserProfileResource userProfile = realm.admin().users().userProfile();
UserProfileMetadata metadata = userProfile.getMetadata();
Assertions.assertFalse(metadata.getAttributeMetadata(UserModel.USERNAME).isRequired());
Expand All @@ -141,6 +89,8 @@ public void testUsernameNotRequiredIfEmailAsUsernameEnabled() {

@Test
public void testGroupsMetadata() {
realm.cleanup().add(r -> r.users().userProfile().update(null));

UPConfig config = realm.admin().users().userProfile().getConfiguration();

for (int i = 0; i < 3; i++) {
Expand All @@ -156,8 +106,6 @@ public void testGroupsMetadata() {
firstName.setGroup(config.getGroups().get(0).getName());
UserProfileResource userProfile = realm.admin().users().userProfile();
userProfile.update(config);
// TODO
/*getCleanup().addCleanup(() -> testRealm().users().userProfile().update(null));*/

UserProfileMetadata metadata = realm.admin().users().userProfile().getMetadata();
List<UserProfileAttributeGroupMetadata> groups = metadata.getGroups();
Expand Down

0 comments on commit 1b8cdbd

Please sign in to comment.