Skip to content

Commit

Permalink
Migrated UserProfileAdminTest to the new framework
Browse files Browse the repository at this point in the history
Part of keycloak#34494

Signed-off-by: Miquel Simon <[email protected]>
  • Loading branch information
miquelsi committed Nov 15, 2024
1 parent 9050172 commit 0411e31
Show file tree
Hide file tree
Showing 7 changed files with 504 additions and 204 deletions.
6 changes: 6 additions & 0 deletions tests/base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
1 change: 1 addition & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

<modules>
<module>base</module>
<module>utils</module>
</modules>

</project>
56 changes: 56 additions & 0 deletions tests/utils/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
Loading

0 comments on commit 0411e31

Please sign in to comment.