mirror of
https://github.com/keycloak/keycloak.git
synced 2026-01-10 15:32:05 -03:30
Migrated UserProfileAdminTest to the new framework (#34960)
Part of #34494 Signed-off-by: Miquel Simon <msimonma@redhat.com>
This commit is contained in:
parent
c6367ff0a6
commit
980d8a6d1c
@ -72,6 +72,12 @@
|
||||
<groupId>org.keycloak.test</groupId>
|
||||
<artifactId>keycloak-test-framework-db-postgres</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.keycloak.tests</groupId>
|
||||
<artifactId>keycloak-tests-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-suite</artifactId>
|
||||
|
||||
@ -0,0 +1,181 @@
|
||||
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.utils.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)
|
||||
private 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);
|
||||
// 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);
|
||||
});*/
|
||||
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);
|
||||
});*/
|
||||
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);
|
||||
// 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());
|
||||
Assertions.assertFalse(metadata.getAttributeMetadata(UserModel.USERNAME).isReadOnly());
|
||||
}
|
||||
|
||||
@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);
|
||||
});*/
|
||||
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);
|
||||
// TODO
|
||||
/*getCleanup().addCleanup(() -> testRealm().users().userProfile().update(null));*/
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
@ -34,6 +34,7 @@
|
||||
|
||||
<modules>
|
||||
<module>base</module>
|
||||
<module>utils</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
56
tests/utils/pom.xml
Executable file
56
tests/utils/pom.xml
Executable file
@ -0,0 +1,56 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
~ Copyright 2016 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.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<parent>
|
||||
<artifactId>keycloak-tests-parent</artifactId>
|
||||
<groupId>org.keycloak.tests</groupId>
|
||||
<version>999.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>keycloak-tests-utils</artifactId>
|
||||
<name>Keycloak Testsuite Utilities</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Keycloak Testsuite Utilities</description>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.keycloak.test</groupId>
|
||||
<artifactId>keycloak-test-framework-bom</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>import</scope>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.keycloak</groupId>
|
||||
<artifactId>keycloak-junit5</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
177
tests/utils/src/main/java/org/keycloak/test/utils/Assert.java
Normal file
177
tests/utils/src/main/java/org/keycloak/test/utils/Assert.java
Normal file
@ -0,0 +1,177 @@
|
||||
package org.keycloak.test.utils;
|
||||
|
||||
/*
|
||||
* Copyright 2016 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.
|
||||
*/
|
||||
|
||||
import org.hamcrest.MatcherAssert;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.keycloak.common.util.MultivaluedHashMap;
|
||||
import org.keycloak.representations.idm.ClientRepresentation;
|
||||
import org.keycloak.representations.idm.ClientScopeRepresentation;
|
||||
import org.keycloak.representations.idm.ComponentRepresentation;
|
||||
import org.keycloak.representations.idm.ConfigPropertyRepresentation;
|
||||
import org.keycloak.representations.idm.GroupRepresentation;
|
||||
import org.keycloak.representations.idm.IdentityProviderRepresentation;
|
||||
import org.keycloak.representations.idm.RealmRepresentation;
|
||||
import org.keycloak.representations.idm.RoleRepresentation;
|
||||
import org.keycloak.representations.idm.UserFederationProviderFactoryRepresentation;
|
||||
import org.keycloak.representations.idm.UserProfileAttributeMetadata;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
import org.keycloak.representations.info.ThemeInfoRepresentation;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.lessThanOrEqualTo;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
||||
*/
|
||||
public class Assert extends Assertions {
|
||||
|
||||
public static final Long DEFAULT_NUMBER_DEVIATION = 20L;
|
||||
|
||||
public static <T> void assertNames(Set<T> actual, String... expected) {
|
||||
Arrays.sort(expected);
|
||||
String[] actualNames = names(new LinkedList<Object>(actual));
|
||||
assertArrayEquals(expected, actualNames, "Expected: " + Arrays.toString(expected) + ", was: " + Arrays.toString(actualNames));
|
||||
}
|
||||
|
||||
public static <T> void assertNames(List<T> actual, String... expected) {
|
||||
Arrays.sort(expected);
|
||||
String[] actualNames = names(actual);
|
||||
assertArrayEquals(expected, actualNames, "Expected: " + Arrays.toString(expected) + ", was: " + Arrays.toString(actualNames));
|
||||
}
|
||||
|
||||
private static <T> String[] names(List<T> list) {
|
||||
String[] names = new String[list.size()];
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
names[i] = name(list.get(i));
|
||||
}
|
||||
Arrays.sort(names);
|
||||
return names;
|
||||
}
|
||||
|
||||
private static String name(Object o1) {
|
||||
if (o1 instanceof String) {
|
||||
return (String) o1;
|
||||
} else if (o1 instanceof RealmRepresentation) {
|
||||
return ((RealmRepresentation) o1).getRealm();
|
||||
} else if (o1 instanceof ClientRepresentation) {
|
||||
return ((ClientRepresentation) o1).getClientId();
|
||||
} else if (o1 instanceof IdentityProviderRepresentation) {
|
||||
return ((IdentityProviderRepresentation) o1).getAlias();
|
||||
} else if (o1 instanceof RoleRepresentation) {
|
||||
return ((RoleRepresentation) o1).getName();
|
||||
} else if (o1 instanceof UserRepresentation) {
|
||||
return ((UserRepresentation) o1).getUsername();
|
||||
} else if (o1 instanceof UserFederationProviderFactoryRepresentation) {
|
||||
return ((UserFederationProviderFactoryRepresentation) o1).getId();
|
||||
} else if (o1 instanceof GroupRepresentation) {
|
||||
return ((GroupRepresentation) o1).getName();
|
||||
} else if (o1 instanceof ComponentRepresentation) {
|
||||
return ((ComponentRepresentation) o1).getName();
|
||||
} else if (o1 instanceof ClientScopeRepresentation) {
|
||||
return ((ClientScopeRepresentation) o1).getName();
|
||||
} else if (o1 instanceof ThemeInfoRepresentation) {
|
||||
return ((ThemeInfoRepresentation) o1).getName();
|
||||
} else if (o1 instanceof UserProfileAttributeMetadata) {
|
||||
return ((UserProfileAttributeMetadata) o1).getName();
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Assert all the fields from map available. Array "expected" contains pairs when first value from pair is expected key
|
||||
* and second is the expected value from the map for target key.
|
||||
*
|
||||
* Example config = {"key1" -> "value1" , "key2" -> "value2" }
|
||||
* then assertMap(config, "key1", "value1", "key2", "value2" will return true
|
||||
*
|
||||
*/
|
||||
public static void assertMap(Map<String, String> config, String... expected) {
|
||||
if (expected == null) {
|
||||
expected = new String[] {};
|
||||
}
|
||||
|
||||
Assertions.assertEquals(config.size() * 2, expected.length);
|
||||
for (int i=0 ; i<expected.length ; i+=2) {
|
||||
String key = expected[i];
|
||||
String value = expected[i+1];
|
||||
Assertions.assertEquals(value, config.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert all the fields from map available. Array "expected" contains pairs when first value from pair is expected key
|
||||
* and second is the expected value from the map for target key.
|
||||
*
|
||||
* Example config = {"key1" -> "value1" , "key2" -> "value2" }
|
||||
* then assertMap(config, "key1", "value1", "key2", "value2" will return true
|
||||
*
|
||||
*/
|
||||
public static void assertMultivaluedMap(MultivaluedHashMap<String, String> config, String... expected) {
|
||||
if (expected == null) {
|
||||
expected = new String[] {};
|
||||
}
|
||||
|
||||
for (int i=0 ; i<expected.length ; i+=2) {
|
||||
String key = expected[i];
|
||||
String value = expected[i+1];
|
||||
Assertions.assertEquals(value, config.getFirst(key));
|
||||
}
|
||||
}
|
||||
|
||||
public static void assertProviderConfigProperty(ConfigPropertyRepresentation property, String name, String label, String defaultValue, String helpText, String type) {
|
||||
Assertions.assertEquals(name, property.getName());
|
||||
Assertions.assertEquals(label, property.getLabel());
|
||||
Assertions.assertEquals(defaultValue, property.getDefaultValue());
|
||||
Assertions.assertEquals(helpText, property.getHelpText());
|
||||
Assertions.assertEquals(type, property.getType());
|
||||
}
|
||||
|
||||
public static void assertExpiration(int actual, int expected) {
|
||||
assertExpiration((long) actual, (long) expected);
|
||||
}
|
||||
|
||||
public static void assertExpiration(long actual, long expected) {
|
||||
assertExpiration(actual, expected, DEFAULT_NUMBER_DEVIATION);
|
||||
}
|
||||
|
||||
public static void assertExpiration(long actual, long expected, long deviation) {
|
||||
MatcherAssert.assertThat(actual, allOf(greaterThanOrEqualTo(expected - deviation), lessThanOrEqualTo(expected + deviation)));
|
||||
}
|
||||
|
||||
public static void assertRoleAttributes(Map<String, List<String>> expected, Map<String, List<String>> actual) {
|
||||
MatcherAssert.assertThat(actual.keySet(), equalTo(expected.keySet()));
|
||||
for (String expectedKey : expected.keySet()) {
|
||||
MatcherAssert.assertThat(actual.get(expectedKey).size(), is(equalTo(expected.get(expectedKey).size())));
|
||||
MatcherAssert.assertThat(actual.get(expectedKey), containsInAnyOrder(expected.get(expectedKey).toArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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.utils;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.keycloak.util.JsonSerialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Utility for comparing JSON objects
|
||||
*
|
||||
* @author <a href="mailto:mposolda@redhat.com">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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,204 +0,0 @@
|
||||
/*
|
||||
*
|
||||
* * Copyright 2021 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.testsuite.admin.userprofile;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.keycloak.userprofile.config.UPConfigUtils.readSystemDefaultConfig;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.keycloak.admin.client.resource.RealmResource;
|
||||
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.testsuite.admin.AbstractAdminTest;
|
||||
import org.keycloak.representations.userprofile.config.UPAttribute;
|
||||
import org.keycloak.representations.userprofile.config.UPConfig;
|
||||
import org.keycloak.representations.userprofile.config.UPGroup;
|
||||
import org.keycloak.testsuite.util.JsonTestUtils;
|
||||
import org.keycloak.userprofile.config.UPConfigUtils;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
|
||||
*/
|
||||
public class UserProfileAdminTest extends AbstractAdminTest {
|
||||
|
||||
@Override
|
||||
public void configureTestRealm(RealmRepresentation testRealm) {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultConfigIfNoneSet() {
|
||||
JsonTestUtils.assertJsonEquals(readSystemDefaultConfig(), testRealm().users().userProfile().getConfiguration());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetDefaultConfig() {
|
||||
UPConfig config = UPConfigUtils.parseSystemDefaultConfig().addOrReplaceAttribute(new UPAttribute("test"));
|
||||
UserProfileResource userProfile = testRealm().users().userProfile();
|
||||
userProfile.update(config);
|
||||
getCleanup().addCleanup(() -> testRealm().users().userProfile().update(null));
|
||||
|
||||
JsonTestUtils.assertJsonEquals(config, userProfile.getConfiguration());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmailRequiredIfEmailAsUsernameEnabled() {
|
||||
RealmResource realm = testRealm();
|
||||
RealmRepresentation realmRep = realm.toRepresentation();
|
||||
Boolean registrationEmailAsUsername = realmRep.isRegistrationEmailAsUsername();
|
||||
realmRep.setRegistrationEmailAsUsername(true);
|
||||
realm.update(realmRep);
|
||||
getCleanup().addCleanup(() -> {
|
||||
realmRep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
|
||||
realm.update(realmRep);
|
||||
});
|
||||
UserProfileResource userProfile = realm.users().userProfile();
|
||||
UserProfileMetadata metadata = userProfile.getMetadata();
|
||||
assertTrue(metadata.getAttributeMetadata(UserModel.EMAIL).isRequired());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmailNotRequiredIfEmailAsUsernameDisabled() {
|
||||
RealmResource realm = testRealm();
|
||||
RealmRepresentation realmRep = realm.toRepresentation();
|
||||
Boolean registrationEmailAsUsername = realmRep.isRegistrationEmailAsUsername();
|
||||
realmRep.setRegistrationEmailAsUsername(false);
|
||||
realm.update(realmRep);
|
||||
getCleanup().addCleanup(() -> {
|
||||
realmRep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
|
||||
realm.update(realmRep);
|
||||
});
|
||||
UserProfileResource userProfile = realm.users().userProfile();
|
||||
UserProfileMetadata metadata = userProfile.getMetadata();
|
||||
assertFalse(metadata.getAttributeMetadata(UserModel.EMAIL).isRequired());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsernameRequiredAndWritableIfEmailAsUsernameDisabledAndEditUsernameAllowed() {
|
||||
RealmResource realm = testRealm();
|
||||
RealmRepresentation realmRep = realm.toRepresentation();
|
||||
Boolean registrationEmailAsUsername = realmRep.isRegistrationEmailAsUsername();
|
||||
realmRep.setRegistrationEmailAsUsername(false);
|
||||
realm.update(realmRep);
|
||||
getCleanup().addCleanup(() -> {
|
||||
realmRep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
|
||||
realm.update(realmRep);
|
||||
});
|
||||
Boolean editUsernameAllowed = realmRep.isEditUsernameAllowed();
|
||||
realmRep.setEditUsernameAllowed(true);
|
||||
realm.update(realmRep);
|
||||
getCleanup().addCleanup(() -> {
|
||||
realmRep.setEditUsernameAllowed(editUsernameAllowed);
|
||||
realm.update(realmRep);
|
||||
});
|
||||
UserProfileResource userProfile = realm.users().userProfile();
|
||||
UserProfileMetadata metadata = userProfile.getMetadata();
|
||||
assertTrue(metadata.getAttributeMetadata(UserModel.USERNAME).isRequired());
|
||||
assertFalse(metadata.getAttributeMetadata(UserModel.USERNAME).isReadOnly());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsernameRequiredAndWritableIfEmailAsUsernameDisabledAndEditUsernameDisabled() {
|
||||
RealmResource realm = testRealm();
|
||||
RealmRepresentation realmRep = realm.toRepresentation();
|
||||
Boolean registrationEmailAsUsername = realmRep.isRegistrationEmailAsUsername();
|
||||
realmRep.setRegistrationEmailAsUsername(false);
|
||||
realm.update(realmRep);
|
||||
getCleanup().addCleanup(() -> {
|
||||
realmRep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
|
||||
realm.update(realmRep);
|
||||
});
|
||||
Boolean editUsernameAllowed = realmRep.isEditUsernameAllowed();
|
||||
realmRep.setEditUsernameAllowed(false);
|
||||
realm.update(realmRep);
|
||||
getCleanup().addCleanup(() -> {
|
||||
realmRep.setEditUsernameAllowed(editUsernameAllowed);
|
||||
realm.update(realmRep);
|
||||
});
|
||||
UserProfileResource userProfile = realm.users().userProfile();
|
||||
UserProfileMetadata metadata = userProfile.getMetadata();
|
||||
assertTrue(metadata.getAttributeMetadata(UserModel.USERNAME).isRequired());
|
||||
assertFalse(metadata.getAttributeMetadata(UserModel.USERNAME).isReadOnly());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsernameNotRequiredIfEmailAsUsernameEnabled() {
|
||||
RealmResource realm = testRealm();
|
||||
RealmRepresentation realmRep = realm.toRepresentation();
|
||||
Boolean registrationEmailAsUsername = realmRep.isRegistrationEmailAsUsername();
|
||||
realmRep.setRegistrationEmailAsUsername(true);
|
||||
realm.update(realmRep);
|
||||
getCleanup().addCleanup(() -> {
|
||||
realmRep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
|
||||
realm.update(realmRep);
|
||||
});
|
||||
UserProfileResource userProfile = realm.users().userProfile();
|
||||
UserProfileMetadata metadata = userProfile.getMetadata();
|
||||
assertFalse(metadata.getAttributeMetadata(UserModel.USERNAME).isRequired());
|
||||
assertTrue(metadata.getAttributeMetadata(UserModel.USERNAME).isReadOnly());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroupsMetadata() {
|
||||
UPConfig config = testRealm().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 = testRealm().users().userProfile();
|
||||
userProfile.update(config);
|
||||
getCleanup().addCleanup(() -> testRealm().users().userProfile().update(null));
|
||||
|
||||
UserProfileMetadata metadata = testRealm().users().userProfile().getMetadata();
|
||||
List<UserProfileAttributeGroupMetadata> groups = metadata.getGroups();
|
||||
assertNotNull(groups);
|
||||
assertFalse(groups.isEmpty());
|
||||
assertEquals(config.getGroups().size(), groups.size());
|
||||
for (UPGroup group : config.getGroups()) {
|
||||
UserProfileAttributeGroupMetadata mGroup = metadata.getAttributeGroupMetadata(group.getName());
|
||||
assertNotNull(mGroup);
|
||||
assertEquals(group.getName(), mGroup.getName());
|
||||
assertEquals(group.getDisplayHeader(), mGroup.getDisplayHeader());
|
||||
assertEquals(group.getDisplayDescription(), mGroup.getDisplayDescription());
|
||||
if (group.getAnnotations() == null) {
|
||||
assertEquals(group.getAnnotations(), mGroup.getAnnotations());
|
||||
} else {
|
||||
assertEquals(group.getAnnotations().size(), mGroup.getAnnotations().size());
|
||||
}
|
||||
}
|
||||
assertEquals(config.getGroups().get(0).getName(), metadata.getAttributeMetadata(UserModel.FIRST_NAME).getGroup());
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user