Skip to content

Conversation

@lhanusov
Copy link
Contributor

Part of: #34494

@lhanusov lhanusov requested review from a team as code owners May 19, 2025 11:03
@lhanusov lhanusov marked this pull request as draft May 19, 2025 11:03
@lhanusov lhanusov force-pushed the 34494/usertest branch 3 times, most recently from 3b55c86 to eb1e156 Compare May 27, 2025 12:13
@lhanusov lhanusov marked this pull request as ready for review May 27, 2025 12:14
@mhajas
Copy link
Contributor

mhajas commented May 27, 2025

@lhanusov FYI the FIPS failures are unrelated to this PR. It happened in other runs as well. It seems it is now passing. I wanted to restart the tests but it seems there are other failures that may be related so I did not do it.

@lhanusov lhanusov force-pushed the 34494/usertest branch 4 times, most recently from 65ab1ca to 822d4cf Compare June 5, 2025 06:48
@lhanusov
Copy link
Contributor Author

lhanusov commented Jun 6, 2025

@stianst I've changed the pages stuff and other things, but still there are some questionable, please take a look, thanks

UserModel user = session.users().getUserByUsername(realm, username);
List<CredentialModel> storedCredentialsByType = user.credentialManager().getStoredCredentialsByTypeStream(CredentialRepresentation.PASSWORD)
.collect(Collectors.toList());
System.out.println(storedCredentialsByType.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this print statement intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vaceksimon this was already there, I can remove it wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this, it seems to me that it was forgotten after a debugging session

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed, thanks

Comment on lines 84 to 88
} else if (expectedResourcePath.length == 1) {
Assertions.assertEquals(expectedResourcePath[0], event.getResourcePath());
} else {
Assertions.assertEquals(String.join("/", expectedResourcePath), event.getResourcePath());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is one parameter, doesn't String.join return just the same string?

String.join("/", "myString") // returns "myString"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. The issue is with the null parameters. According to Java, String.join("/", new String[] {"a", null, "a"}); is "a/null/a" 😄

What you can do is, instead of

AdminEventAssertion.assertEvent(adminEvents.poll(), OperationType.UPDATE, null, rep, ResourceType.REALM);

use

AdminEventAssertion.assertSuccess(adminEvents.poll())
    .operationType(OperationType.UPDATE)
    .representation(rep)
    .resourceType(ResourceType.REALM);

to avoid the null value in the AbstractUserTest methods. That should solve the problem, and this method can stay unchanged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestion 👍

}

@Test
public void createUserWithTempolaryCredentials() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void createUserWithTempolaryCredentials() {
public void createUserWithTemporaryCredentials() {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks


@Test
public void testGetAndMoveCredentials() {
UserResource user = ApiUtil.findUserByUsernameId(managedRealm.admin(), userOtp2.getUsername());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would be faster?

Suggested change
UserResource user = ApiUtil.findUserByUsernameId(managedRealm.admin(), userOtp2.getUsername());
UserResource user = userOtp2.admin();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, thanks

addFederatedIdentity(id, "social-provider-id", link);

// Verify social link is here
user = managedRealm.admin().users().get(id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to get the UserResource here again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, thanks

assertTrue(user.toRepresentation().getRequiredActions().isEmpty());

UserRepresentation userRep = user.toRepresentation();
userRep.getRequiredActions().add("UPDATE_PASSWORD");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's strange that we use a hardcoded "UPDATE_PASSWORD" action here and also in other tests. Do we have a constant somewhere, or maybe use UserModel.RequiredAction.UPDATE_PASSWORD.toString() like in the last test method testDefaultRequiredActionAdded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, thanks

Comment on lines 183 to 185
WebApplicationException ex = assertThrows(WebApplicationException.class, () -> createUser(userRep2));
assertThat(ex.getResponse().getStatusInfo().getStatusCode(), equalTo(400));
assertThat(ex.getResponse().readEntity(ErrorRepresentation.class).getErrorMessage(), equalTo("error-invalid-length"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of rewriting ApiUtil#getCreatedId to throw WebApplicationException, I'd change this and similar statements to this:

Suggested change
WebApplicationException ex = assertThrows(WebApplicationException.class, () -> createUser(userRep2));
assertThat(ex.getResponse().getStatusInfo().getStatusCode(), equalTo(400));
assertThat(ex.getResponse().readEntity(ErrorRepresentation.class).getErrorMessage(), equalTo("error-invalid-length"));
Response response = managedRealm.admin().users().create(userRep2);
assertThat(response.getStatus(), equalTo(400));
assertThat(response.readEntity(ErrorRepresentation.class).getErrorMessage(), equalTo("error-invalid-length"));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice proposal, thanks :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file used? I don't see it anywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed, thanks

import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.ClientErrorException;
import jakarta.ws.rs.NotFoundException;
import org.junit.Assert;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also a forgotten JUnit4 Assert import

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, thanks

Copy link

@keycloak-github-bot keycloak-github-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.forms.BruteForceTest#testExceedMaxTemporaryLockouts

Keycloak CI - Base IT (5)

java.lang.AssertionError: 
type
Expected: is "LOGIN_ERROR"
     but: was "USER_DISABLED_BY_PERMANENT_LOCKOUT"
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
...

Report flaky test

Comment on lines 111 to 115
if (rep.getCredentials() == null) {
rep.setCredentials(new LinkedList<>());
}

rep.getCredentials().add(credential);
Copy link
Contributor

@stianst stianst Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Collections.combine like how it's done for passwords:

public UserConfigBuilder password(String password) {
rep.setCredentials(Collections.combine(rep.getCredentials(), Representations.toCredential(CredentialRepresentation.PASSWORD, password)));
return this;
}

}

public UserConfigBuilder federatedLink(String identityProvider, String federatedUserId, String federatedUsername) {
if (rep.getFederatedIdentities() == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Collections.combine instead like mentioned below for totpSecret


public void assertCurrent() {
String name = getClass().getSimpleName();
Assertions.assertTrue(isActivePage(), "Expected " + name + " but was " + driver.getTitle() + " (" + driver.getCurrentUrl() + ")");
Copy link
Contributor

@stianst stianst Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just use the data-page-id stuff in the message here as well. Perhaps:

        Assertions.assertTrue(isActivePage(), "Expected " + getExpectedPageId() + " but was " + getCurrentPageId());

Or, maybe this is even better:

Assertions.assertEquals(getExpectedPageId(), getCurrentPageId(), "Not on expected page");

return body.getAttribute("data-page-id");
}

public String getPageTitle() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the usage of getPageTitle it's asserting that it's on a specific page, so perhaps just do loginPage.assertCurrent() instead in the test of asserting the page title?

: null;
}

public void clickToContinueDe() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is horrible; is there no id set on the link that we can use instead of looking up the link based on link-text?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:-D, I've just copy/pasted what was already there, I will check the id instead and come back with a new solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've decided to remove those unused links. It will be added once those tests are migrated.

@lhanusov lhanusov force-pushed the 34494/usertest branch 2 times, most recently from ffc9c7f to 594864c Compare July 15, 2025 13:42
@keycloak-github-bot
Copy link

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.federation.ldap.LDAPReadOnlyTest#testReadOnlyUserGetsPermanentlyLocked

Keycloak CI - Base IT (5)

java.lang.AssertionError
	at org.junit.Assert.fail(Assert.java:87)
	at org.junit.Assert.assertTrue(Assert.java:42)
	at org.junit.Assert.assertFalse(Assert.java:65)
	at org.junit.Assert.assertFalse(Assert.java:75)
...

Report flaky test

Copy link

@keycloak-github-bot keycloak-github-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreported flaky test detected, please review

@stianst stianst merged commit 788e981 into keycloak:main Jul 16, 2025
81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants