8000 Refactor SSSD tests to use JUnit5 Assertions. by lhanusov · Pull Request #48673 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.hamcrest.Matchers;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Rule;

import org.keycloak.events.Details;
import org.keycloak.events.Errors;
import org.keycloak.events.EventType;
Expand All @@ -37,6 +31,14 @@
import org.keycloak.testsuite.pages.LoginUpdateProfilePage;
import org.keycloak.testsuite.util.oauth.AccessTokenResponse;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.hamcrest.Matchers;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.jupiter.api.Assertions;

/**
* <p>Abstract base class for SSSD tests.</p>
*
Expand Down Expand Up @@ -76,13 +78,13 @@ protected void testLoginFailure(String username, String password) {
oauth.openLoginForm();
loginPage.login(username, password);
loginPage.assertCurrent();
Assert.assertEquals("Invalid username or password.", loginPage.getInputError());
Assertions.assertEquals(loginPage.getInputError(), "Invalid username or password.");
events.expect(EventType.LOGIN_ERROR).user(Matchers.any(String.class)).error(Errors.INVALID_USER_CREDENTIALS).assertEvent();
}

protected void testLoginSuccess(String username) {
oauth.doLogin(username, getPassword(username));
Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
Assertions.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
EventRepresentation loginEvent = events.expectLogin().user(Matchers.any(String.class))
.detail(Details.USERNAME, username).assertEvent();
AccessTokenResponse tokenResponse = sendTokenRequestAndGetResponse(loginEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@
*/
package org.keycloak.testsuite.sssd;

import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.core.Response;
import java.util.List;
import java.util.stream.Collectors;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.jboss.logging.Logger;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.core.Response;

import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.representations.idm.ComponentRepresentation;
Expand All @@ -39,6 +34,14 @@
import org.keycloak.storage.UserStorageProvider;
import org.keycloak.testsuite.admin.ApiUtil;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.jboss.logging.Logger;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
Expand Down Expand Up @@ -154,14 +157,14 @@ public void changeReadOnlyProfile() {

RealmResource realm = adminClient.realm(REALM_NAME);
List<UserRepresentation> users = realm.users().search(username, true);
Assert.assertEquals(1, users.size());
Assertions.assertEquals(1, users.size());
UserRepresentation user = users.iterator().next();
user.setLastName("changed");

BadRequestException e = Assert.assertThrows(BadRequestException.class,
BadRequestException e = Assertions.assertThrows(BadRequestException.class,
() -> realm.users().get(users.iterator().next().getId()).update(user));
ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
Assert.assertEquals("error-user-attribute-read-only", error.getErrorMessage());
Assertions.assertEquals(error.getErrorMessage(), "error-user-attribute-read-only");
}

@Test
Expand All @@ -172,16 +175,16 @@ public void changeReadOnlyPassword() {

RealmResource realm = adminClient.realm(REALM_NAME);
List<UserRepresentation> users = realm.users().search(username, true);
Assert.assertEquals(1, users.size());
Assertions.assertEquals(1, users.size());
CredentialRepresentation newPassword = new CredentialRepresentation();
newPassword.setType(CredentialRepresentation.PASSWORD);
newPassword.setValue("new-password-123!");
newPassword.setTemporary(false);

BadRequestException e = Assert.assertThrows(BadRequestException.class,
BadRequestException e = Assertions.assertThrows(BadRequestException.class,
() -> realm.users().get(users.iterator().next().getId()).resetPassword(newPassword));
OAuth2ErrorRepresentation error = e.getResponse().readEntity(OAuth2ErrorRepresentation.class);
Assert.assertEquals("Can't reset password as account is read only", error.getError());
Assertions.assertEquals(error.getError(), "Can't reset password as account is read only");
}

private void verifyUserGroups(String username, List<String> groups) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
import org.keycloak.userprofile.config.UPConfigUtils;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runners.MethodSorters;

/**
Expand Down Expand Up @@ -102,13 +102,13 @@ public void test02DefaultSSSDUserProfile() throws Exception {
oauth.doLogin(username, getPassword(username));
WaitUtils.waitForPageToLoad();
updateProfilePage.assertCurrent();
Assert.assertEquals(getFirstName(username), updateProfilePage.getFirstName());
Assert.assertEquals(getLastName(username), updateProfilePage.getLastName());
Assert.assertEquals(getEmail(username), updateProfilePage.getEmail());
Assert.assertFalse(updateProfilePage.getElementById(UserModel.FIRST_NAME).isEnabled());
Assert.assertFalse(updateProfilePage.getElementById(UserModel.LAST_NAME).isEnabled());
Assert.assertFalse(updateProfilePage.getElementById(UserModel.EMAIL).isEnabled());
Assert.assertFalse(updateProfilePage.getElementById(UserModel.USERNAME).isEnabled());
Assertions.assertEquals(getFirstName(username), updateProfilePage.getFirstName());
Assertions.assertEquals(getLastName(username), updateProfilePage.getLastName());
Assertions.assertEquals(getEmail(username), updateProfilePage.getEmail());
Assertions.assertFalse(updateProfilePage.getElementById(UserModel.FIRST_NAME).isEnabled());
Assertions.assertFalse(updateProfilePage.getElementById(UserModel.LAST_NAME).isEnabled());
Assertions.assertFalse(updateProfilePage.getElementById(UserModel.EMAIL).isEnabled());
Assertions.assertFalse(updateProfilePage.getElementById(UserModel.USERNAME).isEnabled());
updateProfilePage.prepareUpdate().submit();

// check events
Expand All @@ -117,7 +117,7 @@ public void test02DefaultSSSDUserProfile() throws Exception {
events.expectRequiredAction(EventType.UPDATE_PROFILE)
.user(user.getId())
.assertEvent();
Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
Assertions.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
EventRepresentation loginEvent = events.expectLogin()
.user(Matchers.any(String.class))
.detail(Details.USERNAME, username)
Expand Down Expand Up @@ -145,13 +145,13 @@ public void test03DefaultInternalDBUserProfile() throws Exception {
oauth.doLogin("test-user@localhost", "password");
WaitUtils.waitForPageToLoad();
updateProfilePage.assertCurrent();
Assert.assertEquals("Tom", updateProfilePage.getFirstName());
Assert.assertEquals("Brady", updateProfilePage.getLastName());
Assert.assertEquals("test-user@localhost", updateProfilePage.getEmail());
Assert.assertTrue(updateProfilePage.getElementById(UserModel.FIRST_NAME).isEnabled());
Assert.assertTrue(updateProfilePage.getElementById(UserModel.LAST_NAME).isEnabled());
Assert.assertTrue(updateProfilePage.getElementById(UserModel.EMAIL).isEnabled());
Assert.assertTrue(updateProfilePage.getElementById(UserModel.USERNAME).isEnabled());
Assertions.assertEquals("Tom", updateProfilePage.getFirstName());
Assertions.assertEquals("Brady", updateProfilePage.getLastName());
Assertions.assertEquals("test-user@localhost", updateProfilePage.getEmail());
Assertions.assertTrue(updateProfilePage.getElementById(UserModel.FIRST_NAME).isEnabled());
Assertions.assertTrue(updateProfilePage.getElementById(UserModel.LAST_NAME).isEnabled());
Assertions.assertTrue(updateProfilePage.getElementById(UserModel.EMAIL).isEnabled());
Assertions.assertTrue(updateProfilePage.getElementById(UserModel.USERNAME).isEnabled());
updateProfilePage.prepareUpdate().submit();

// check events
Expand All @@ -160,7 +160,7 @@ public void test03DefaultInternalDBUserProfile() throws Exception {
events.expectRequiredAction(EventType.UPDATE_PROFILE)
.user(test.getId())
.assertEvent();
Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
Assertions.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
EventRepresentation loginEvent = events.expectLogin()
.user(Matchers.any(String.class))
.detail(Details.USERNAME, "test-user@localhost")
Expand Down Expand Up @@ -195,12 +195,12 @@ public void test04MixedSSSDUserProfile() throws Exception {
oauth.doLogin(username, getPassword(username));
WaitUtils.waitForPageToLoad();
updateProfilePage.assertCurrent();
Assert.assertEquals(getEmail(username), updateProfilePage.getEmail());
Assert.assertNull(updateProfilePage.getElementById(UserModel.FIRST_NAME));
Assert.assertNull(updateProfilePage.getElementById(UserModel.LAST_NAME));
Assert.assertFalse(updateProfilePage.getElementById(UserModel.EMAIL).isEnabled());
Assert.assertFalse(updateProfilePage.getElementById( 8BD0 UserModel.USERNAME).isEnabled());
Assert.assertTrue(updateProfilePage.getElementById("postal_code").isEnabled());
Assertions.assertEquals(getEmail(username), updateProfilePage.getEmail());
Assertions.assertNull(updateProfilePage.getElementById(UserModel.FIRST_NAME));
Assertions.assertNull(updateProfilePage.getElementById(UserModel.LAST_NAME));
Assertions.assertFalse(updateProfilePage.getElementById(UserModel.EMAIL).isEnabled());
Assertions.assertFalse(updateProfilePage.getElementById(UserModel.USERNAME).isEnabled());
Assertions.assertTrue(updateProfilePage.getElementById("postal_code").isEnabled());
updateProfilePage.prepareUpdate().otherProfileAttribute(Map.of("postal_code", "123456")).submit();
WaitUtils.waitForPageToLoad();
appPage.assertCurrent();
Expand All @@ -210,7 +210,7 @@ public void test04MixedSSSDUserProfile() throws Exception {
.user(user.getId())
.detail("updated_postal_code", "123456")
.assertEvent();
Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
Assertions.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
EventRepresentation loginEvent = events.expectLogin()
.user(Matchers.any(String.class))
.detail(Details.USERNAME, username)
Expand All @@ -237,21 +237,21 @@ public void test05MixedInternalDBUserProfile() throws Exception {
UserRepresentation test = testResource.toRepresentation(true);
assertUser(test, "test-user@localhost", "test-user@localhost", null, null, null);
assertProfileAttributes(test, null, false, "username", "email", "postal_code");
Assert.assertNull(test.getUserProfileMetadata().getAttributeMetadata(UserModel.FIRST_NAME));
Assert.assertNull(test.getUserProfileMetadata().getAttributeMetadata(UserModel.LAST_NAME));
Assertions.assertNull(test.getUserProfileMetadata().getAttributeMetadata(UserModel.FIRST_NAME));
Assertions.assertNull(test.getUserProfileMetadata().getAttributeMetadata(UserModel.LAST_NAME));

// for user, firstName and lastName are not visible, username, email read-only and postal_code editable
test.getRequiredActions().add(UserModel.RequiredAction.UPDATE_PROFILE.toString());
testResource.update(test);
oauth.doLogin("test-user@localhost", "password");
WaitUtils.waitForPageToLoad();
updateProfilePage.assertCurrent();
Assert.assertEquals("test-user@localhost", updateProfilePage.getEmail());
Assert.assertNull(updateProfilePage.getElementById(UserModel.FIRST_NAME));
Assert.assertNull(updateProfilePage.getElementById(UserModel.LAST_NAME));
Assert.assertTrue(updateProfilePage.getElementById(UserModel.EMAIL).isEnabled());
Assert.assertTrue(updateProfilePage.getElementById(UserModel.USERNAME).isEnabled());
Assert.assertTrue(updateProfilePage.getElementById("postal_code").isEnabled());
Assertions.assertEquals("test-user@localhost", updateProfilePage.getEmail());
Assertions.assertNull(updateProfilePage.getElementById(UserModel.FIRST_NAME));
Assertions.assertNull(updateProfilePage.getElementById(UserModel.LAST_NAME));
Assertions.assertTrue(updateProfilePage.getElementById(UserModel.EMAIL).isEnabled());
Assertions.assertTrue(updateProfilePage.getElementById(UserModel.USERNAME).isEnabled());
Assertions.assertTrue(updateProfilePage.getElementById("postal_code").isEnabled());
updateProfilePage.prepareUpdate().otherProfileAttribute(Map.of("postal_code", "123456")).submit();
WaitUtils.wa D92B itForPageToLoad();
appPage.assertCurrent();
Expand All @@ -261,7 +261,7 @@ public void test05MixedInternalDBUserProfile() throws Exception {
.user(test.getId())
.detail("updated_postal_code", "123456")
.assertEvent();
Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
Assertions.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
EventRepresentation loginEvent = events.expectLogin()
.user(Matchers.any(String.class))
.detail(Details.USERNAME, "test-user@localhost")
Expand All @@ -278,27 +278,27 @@ public void test05MixedInternalDBUserProfile() throws Exception {

private void assertUser(UserRepresentation user, String expectedUsername, String expectedEmail,
String expectedFirstName, String expectedLastname, String sssdId) {
Assert.assertNotNull(user);
Assert.assertEquals(expectedUsername, user.getUsername());
Assert.assertEquals(expectedFirstName, user.getFirstName());
Assert.assertEquals(expectedLastname, user.getLastName());
Assert.assertEquals(expectedEmail, user.getEmail());
Assert.assertEquals(sssdId, user.getFederationLink());
Assertions.assertNotNull(user);
Assertions.assertEquals(expectedUsername, user.getUsername());
Assertions.assertEquals(expectedFirstName, user.getFirstName());
Assertions.assertEquals(expectedLastname, user.getLastName());
Assertions.assertEquals(expectedEmail, user.getEmail());
Assertions.assertEquals(sssdId, user.getFederationLink());
}

private void assertProfileAttributes(UserRepresentation user, String expectedGroup, boolean expectReadOnly, String... attributes) {
for (String attrName : attributes) {
UserProfileAttributeMetadata attrMetadata = user.getUserProfileMetadata().getAttributeMetadata(attrName);
Assert.assertNotNull("Attribute " + attrName + " was not present for user " + user.getUsername(), attrMetadata);
Assert.assertEquals("Attribute " + attrName + " for user " + user.getUsername() + ". Expected read-only: " + expectReadOnly + " but was not", expectReadOnly, attrMetadata.isReadOnly());
Assert.assertEquals("Attribute " + attrName + " for user " + user.getUsername() + ". Expected group: " + expectedGroup + " but was " + attrMetadata.getGroup(), expectedGroup, attrMetadata.getGroup());
Assertions.assertNotNull(attrMetadata, "Attribute " + attrName + " was not present for user " + user.getUsername());
Assertions.assertEquals(expectReadOnly, attrMetadata.isReadOnly(), "Attribute " + attrName + " for user " + user.getUsername() + ". Expected read-only: " + expectReadOnly + " but was not");
Assertions.assertEquals(expectedGroup, attrMetadata.getGroup(), "Attribute " + attrName + " for user " + user.getUsername() + ". Expected group: " + expectedGroup + " but was " + attrMetadata.getGroup());
}
}

private String getSssdProviderId() {
List<ComponentRepresentation> comps = managedRealm.admin().components()
.query(TEST_REALM_NAME, UserStorageProvider.class.getName(), PROVIDER_NAME);
Assert.assertEquals(1, comps.size());
Assertions.assertEquals(1, comps.size());
return comps.iterator().next().getId();
}

Expand Down
Loading
0