forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated UserProfileAdminTest to the new framework
Part of keycloak#34494 Signed-off-by: Miquel Simon <[email protected]>
- Loading branch information
Showing
3 changed files
with
225 additions
and
204 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
tests/base/src/test/java/org/keycloak/test/admin/userprofile/UserProfileAdminTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
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; | ||
import org.keycloak.representations.userprofile.config.UPConfig; | ||
import org.keycloak.representations.userprofile.config.UPGroup; | ||
import org.keycloak.test.framework.annotations.InjectRealm; | ||
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest; | ||
import org.keycloak.test.framework.injection.LifeCycle; | ||
import org.keycloak.test.framework.realm.ManagedRealm; | ||
import org.keycloak.test.util.JsonTestUtils; | ||
import org.keycloak.userprofile.config.UPConfigUtils; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@KeycloakIntegrationTest | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
public class UserProfileAdminTest { | ||
|
||
@InjectRealm(lifecycle = LifeCycle.CLASS) | ||
ManagedRealm realm; | ||
|
||
@Test | ||
@Order(1) | ||
public void testDefaultConfigIfNoneSet() { | ||
JsonTestUtils.assertJsonEquals(UPConfigUtils.readSystemDefaultConfig(), realm.admin().users().userProfile().getConfiguration()); | ||
} | ||
|
||
@Test | ||
public void testSetDefaultConfig() { | ||
UPConfig config = UPConfigUtils.parseSystemDefaultConfig().addOrReplaceAttribute(new UPAttribute("test")); | ||
UserProfileResource userProfile = realm.admin().users().userProfile(); | ||
userProfile.update(config); | ||
|
||
JsonTestUtils.assertJsonEquals(config, userProfile.getConfiguration()); | ||
} | ||
|
||
@Test | ||
public void testEmailRequiredIfEmailAsUsernameEnabled() { | ||
RealmRepresentation realmRep = realm.admin().toRepresentation(); | ||
realmRep.setRegistrationEmailAsUsername(true); | ||
realm.admin().update(realmRep); | ||
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); | ||
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); | ||
realmRep.setEditUsernameAllowed(true); | ||
realm.admin().update(realmRep); | ||
UserProfileResource userProfile = realm.admin().users().userProfile(); | ||
UserProfileMetadata metadata = userProfile.getMetadata(); | ||
Assertions.assertTrue(metadata.getAttributeMetadata(UserModel.USERNAME).isRequired()); | ||
Assertions.assertFalse(metadata.getAttributeMetadata(UserModel.USERNAME).isReadOnly()); | ||
} | ||
|
||
@Test | ||
public void testUsernameRequiredAndWritableIfEmailAsUsernameDisabledAndEditUsernameDisabled() { | ||
RealmRepresentation realmRep = realm.admin().toRepresentation(); | ||
realmRep.setRegistrationEmailAsUsername(false); | ||
realm.admin().update(realmRep); | ||
realmRep.setEditUsernameAllowed(false); | ||
realm.admin().update(realmRep); | ||
UserProfileResource userProfile = realm.admin().users().userProfile(); | ||
UserProfileMetadata metadata = userProfile.getMetadata(); | ||
Assertions.assertTrue(metadata.getAttributeMetadata(UserModel.USERNAME).isRequired()); | ||
Assertions.assertFalse(metadata.getAttributeMetadata(UserModel.USERNAME).isReadOnly()); | ||
} | ||
|
||
@Test | ||
public void testUsernameNotRequiredIfEmailAsUsernameEnabled() { | ||
RealmRepresentation realmRep = realm.admin().toRepresentation(); | ||
realmRep.setRegistrationEmailAsUsername(true); | ||
realm.admin().update(realmRep); | ||
UserProfileResource userProfile = realm.admin().users().userProfile(); | ||
UserProfileMetadata metadata = userProfile.getMetadata(); | ||
Assertions.assertFalse(metadata.getAttributeMetadata(UserModel.USERNAME).isRequired()); | ||
Assertions.assertTrue(metadata.getAttributeMetadata(UserModel.USERNAME).isReadOnly()); | ||
} | ||
|
||
@Test | ||
public void testGroupsMetadata() { | ||
UPConfig config = realm.admin().users().userProfile().getConfiguration(); | ||
|
||
for (int i = 0; i < 3; i++) { | ||
UPGroup group = new UPGroup(); | ||
group.setName("name-" + i); | ||
group.setDisplayHeader("displayHeader-" + i); | ||
group.setDisplayDescription("displayDescription-" + i); | ||
group.setAnnotations(Map.of("k1", "v1", "k2", "v2", "k3", "v3")); | ||
config.addGroup(group); | ||
} | ||
|
||
UPAttribute firstName = config.getAttribute(UserModel.FIRST_NAME); | ||
firstName.setGroup(config.getGroups().get(0).getName()); | ||
UserProfileResource userProfile = realm.admin().users().userProfile(); | ||
userProfile.update(config); | ||
|
||
UserProfileMetadata metadata = realm.admin().users().userProfile().getMetadata(); | ||
List<UserProfileAttributeGroupMetadata> groups = metadata.getGroups(); | ||
Assertions.assertNotNull(groups); | ||
Assertions.assertFalse(groups.isEmpty()); | ||
Assertions.assertEquals(config.getGroups().size(), groups.size()); | ||
for (UPGroup group : config.getGroups()) { | ||
UserProfileAttributeGroupMetadata mGroup = metadata.getAttributeGroupMetadata(group.getName()); | ||
Assertions.assertNotNull(mGroup); | ||
Assertions.assertEquals(group.getName(), mGroup.getName()); | ||
Assertions.assertEquals(group.getDisplayHeader(), mGroup.getDisplayHeader()); | ||
Assertions.assertEquals(group.getDisplayDescription(), mGroup.getDisplayDescription()); | ||
if (group.getAnnotations() == null) { | ||
Assertions.assertEquals(group.getAnnotations(), mGroup.getAnnotations()); | ||
} else { | ||
Assertions.assertEquals(group.getAnnotations().size(), mGroup.getAnnotations().size()); | ||
} | ||
} | ||
Assertions.assertEquals(config.getGroups().get(0).getName(), metadata.getAttributeMetadata(UserModel.FIRST_NAME).getGroup()); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
tests/base/src/test/java/org/keycloak/test/util/JsonTestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.keycloak.test.util; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.keycloak.util.JsonSerialization; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Utility for comparing JSON objects | ||
* | ||
* @author <a href="mailto:[email protected]">Marek Posolda</a> | ||
*/ | ||
public class JsonTestUtils { | ||
|
||
/** | ||
* @param o1 | ||
* @param o2 | ||
* @return true if JSON objects are "equal" to each other | ||
* @param <T> | ||
*/ | ||
public static <T> void assertJsonEquals(T o1, T o2) { | ||
try { | ||
String o1Stripped = JsonSerialization.writeValueAsString(o1); | ||
String o2Stripped = JsonSerialization.writeValueAsString(o2); | ||
Assertions.assertEquals(o1Stripped, o2Stripped); | ||
} catch (IOException ioe) { | ||
throw new RuntimeException(ioe); | ||
} | ||
} | ||
|
||
/** | ||
* Compare Object in the JSON node with the "unparsed" String version of that object | ||
* | ||
* @param o1 String with JSON. Assumption is, that it can be "read" to the same object class of object o1 | ||
* @param o2 | ||
* @return | ||
*/ | ||
public static void assertJsonEquals(String o1, Object o2) { | ||
try { | ||
Object o1Object = JsonSerialization.readValue(o1, o2.getClass()); | ||
assertJsonEquals(o1Object, o2); | ||
} catch (IOException ioe) { | ||
throw new RuntimeException(ioe); | ||
} | ||
} | ||
|
||
/** | ||
* Compares if 2 strings logically refers same JSON. | ||
* | ||
* @param o1 | ||
* @param o2 | ||
* @param clazz Java class, which strings o1 and o2 can be read into | ||
* @return | ||
*/ | ||
public static void assertJsonEquals(String o1, String o2, Class<?> clazz) { | ||
try { | ||
Object o1Object = JsonSerialization.readValue(o1, clazz); | ||
Object o2Object = JsonSerialization.readValue(o2, clazz); | ||
assertJsonEquals(o1Object, o2Object); | ||
} catch (IOException ioe) { | ||
throw new RuntimeException(ioe); | ||
} | ||
} | ||
} |
Oops, something went wrong.