Skip to content

Migrate the AccountRestServiceTest - #49296

Merged
rmartinc merged 1 commit into
keycloak:mainfrom
gaoyikeshuer:migration-test-48148
Jun 2, 2026
Merged

Migrate the AccountRestServiceTest#49296
rmartinc merged 1 commit into
keycloak:mainfrom
gaoyikeshuer:migration-test-48148

Conversation

@gaoyikeshuer

@gaoyikeshuer gaoyikeshuer commented May 25, 2026

Copy link
Copy Markdown
Contributor

This PR is aim to migrate AccountRestServiceTest. the two extending classes (Lightweight, withUserProfile) are technically not required by this issue, but I migrate them because they share AccountRestServiceTest as their parent, and moving the parent without them will create a half migrated state where they break.

Tests missing for full migration of the account package are:

AccountRestServiceCorsTest
AccountRestServiceReadOnlyAttributesTest
LinkedAccountsRestServiceTest
ResourcesRestServiceTest
SessionRestServiceTest
custom/* tests

Delete legacy AbstractRestServiceTest (only possible once all the above are migrated)

Closes #48148

@gaoyikeshuer
gaoyikeshuer force-pushed the migration-test-48148 branch 2 times, most recently from b14bc3f to 68f2fdb Compare May 25, 2026 13:06
@gaoyikeshuer
gaoyikeshuer force-pushed the migration-test-48148 branch 8 times, most recently from 7b2bf50 to 69ea8ea Compare May 26, 2026 11:43
@gaoyikeshuer
gaoyikeshuer marked this pull request as ready for review May 26, 2026 12:26
@gaoyikeshuer
gaoyikeshuer requested review from a team as code owners May 26, 2026 12:26
@gaoyikeshuer
gaoyikeshuer force-pushed the migration-test-48148 branch 2 times, most recently from ca73108 to 116f5b4 Compare May 26, 2026 13:12
@vaceksimon
vaceksimon self-requested a review May 26, 2026 13:47

@vaceksimon vaceksimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great effort on this! It’s a massive PR, so to keep things manageable, let's break the review into a couple of rounds. Let's get these current points squared away first, and then proceed with the rest.

One thing that might save you some effort here: you don't need to migrate all the extending classes just because you're migrating the abstract one. You can absolutely scope this down to just what AccountRestServiceTest needs, and we can let the next developers handle the rest.

Comment thread testsuite/integration-arquillian/tests/base/testsuites/jdk-suite
oauth.scope(null);
setRequiredActionEnabled(managedRealm.admin(), UserModel.RequiredAction.VERIFY_PROFILE, false);
managedRealm.admin().logoutAll();
events.clear();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@gaoyikeshuer gaoyikeshuer May 29, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, have removed them and @beforeeach. there are still two events.clear() calls in the tests but those are not dead. the events.clear() actually needed cause they drain login events before checking specific subsequent events. otherwise can't pass the ci/cd. let me know if you prefer a different pattern :)

Comment thread tests/base/src/test/java/org/keycloak/tests/account/AbstractRestServiceTest.java Outdated
Comment thread tests/base/src/test/java/org/keycloak/tests/account/AbstractRestServiceTest.java Outdated
Comment thread tests/base/src/test/java/org/keycloak/tests/account/AbstractRestServiceTest.java Outdated
Comment thread tests/base/src/test/java/org/keycloak/tests/account/AbstractRestServiceTest.java Outdated
Comment thread tests/base/src/test/java/org/keycloak/tests/account/AbstractRestServiceTest.java Outdated
@gaoyikeshuer
gaoyikeshuer force-pushed the migration-test-48148 branch 2 times, most recently from a6b6534 to 315e940 Compare May 27, 2026 10:18
@gaoyikeshuer
gaoyikeshuer requested a review from a team as a code owner May 27, 2026 10:18
@gaoyikeshuer
gaoyikeshuer force-pushed the migration-test-48148 branch 7 times, most recently from 69fb906 to 481e4cc Compare May 28, 2026 09:15
@gaoyikeshuer
gaoyikeshuer force-pushed the migration-test-48148 branch 3 times, most recently from 69d9b3d to ac3f585 Compare May 29, 2026 08:31
Comment thread tests/base/src/test/java/org/keycloak/tests/account/AccountRestServiceTest.java Outdated

@rmartinc rmartinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @gaoyikeshuer! I added a new commit in my test branch for this PR https://github.com/rmartinc/keycloak/tree/refs/heads/pr-49296. The main idea is adding the theme and provider to run the disabled test in another class. I have added some other minor changes to not do manual modifications in the realm and always use the updateWithCleanup. If you find something more on those lines, you can follow the same idea (although I think that with those changes the PR is OK to me).

Please include the changes in the test commit from my branch in this PR. You can just squash it or amend or do whatever you like with it.

I think that with that commit, this is OK to me.

Comment thread tests/base/src/test/java/org/keycloak/tests/account/AccountRestServiceTest.java Outdated

@vaceksimon vaceksimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Keep up the good work @gaoyikeshuer!

Comment thread tests/base/src/test/java/org/keycloak/tests/account/AccountRestServiceTest.java Outdated
Comment on lines +108 to +116
UserBuilder.create().username("test-user@localhost")
.email("test-user@localhost")
.name("Tom", "Brady")
.attribute("attr_required", "value")
.attribute("attr_required_by_role", "value")
.attribute("attr_required_by_scope", "value")
.realmRoles("user", "offline_access")
.clientRoles("account", "view-profile", "manage-account")
.password("password"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This user is referenced a lot in the tests. In this case, we can turn it into a managed user with this config:

@InjectUser(config = TestUserConfig.class)
ManagedUser testUser;

With that, you can replace registerUserCleanup(...) with a call to testUser.dirty().

Also using "test-user@localhost" and "password" directly can turn into testUser.getUsername() and testUser.getPassword()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@gaoyikeshuer @vaceksimon I think this is the only point that still stands. I leave it to you both, if you prefer to change to use the ManagedUser, OK, but it's also OK to me as it's now. 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @vaceksimon , thanks for this suggestion! I tried to use the @InjectUser before but it fails at UserSupplier:42 with "Creating user with roles or client roles is not supported!". this user needs the account/view-profile + account/manage-account client roles plus user + offline_access realm roles to function. so i've reverted that change. I think we will need to modify framework if we want @injectUser here. which is better to be in a separate PR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right. The role creation is unfortunately a limitation of the current admin API, which should be fixed with admin API v2. In this case, I'm ok with this

Comment on lines 1100 to 1102
events.poll();
events.poll();
events.poll();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a nitpick, and I only mention it so you know it exists; the same thing can be done with events.skip(3);

final OAuth2ErrorRepresentation error = response.asJson(OAuth2ErrorRepresentation.class);
assertThat(error.getError(), containsString("Invalid json representation for UserRepresentation. Unrecognized field \"invalid\" at line"));
}
}

@Test
public void testEmailWhenUpdateEmailEnabled() throws Exception {
reconnectAdminClient();
managedRealm.cleanup().add(r -> setRequiredActionEnabled(r, RequiredAction.UPDATE_EMAIL, false));
managedRealm.updateWithCleanup(r -> r);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a preferable way to achieve the same thing

Suggested change
managedRealm.updateWithCleanup(r -> r);
managedRealm.dirty();

String accessToken = tokenUtil.getToken();
org.keycloak.representations.idm.ClientRepresentation originalClientRep = AdminApiUtil.findClientByClientId(managedRealm.admin(), "direct-grant").toRepresentation();
managedRealm.cleanup().add(r -> AdminApiUtil.findClientByClientId(r, "direct-grant").update(originalClientRep));
managedRealm.updateWithCleanup(r -> r);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a preferable way to achieve the same thing.

Suggested change
managedRealm.updateWithCleanup(r -> r);
managedRealm.dirty();

@Test
public void testAccountAccessWithRealmNotBeforeAndClientNotBefore() throws IOException {
String accessToken = tokenUtil.getToken();
org.keycloak.representations.idm.ClientRepresentation originalClientRep = AdminApiUtil.findClientByClientId(managedRealm.admin(), "direct-grant").toRepresentation();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since the realm will be recreated with everything in it, the cleanup for the "direct-grant" client can be removed.

@gaoyikeshuer
gaoyikeshuer force-pushed the migration-test-48148 branch 3 times, most recently from 547db7b to 5e42776 Compare May 31, 2026 00:09
rmartinc
rmartinc previously approved these changes Jun 1, 2026

@rmartinc rmartinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @gaoyikeshuer! For me it's OK. Please @vaceksimon take a look when you have time to see if it's also OK for you.

@vaceksimon vaceksimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@gaoyikeshuer took a quick look. I will check the rest tomorrow once back in the office

@rmartinc rmartinc self-assigned this Jun 1, 2026

@rmartinc rmartinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @gaoyikeshuer! LGTM!

@vaceksimon vaceksimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Well done @gaoyikeshuer, thank you!

@rmartinc

rmartinc commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Thanks @gaoyikeshuer and @vaceksimon!

@rmartinc
rmartinc merged commit d4c89b3 into keycloak:main Jun 2, 2026
208 of 276 checks passed
sashyo pushed a commit to tide-foundation/keycloak-IGA that referenced this pull request Jul 20, 2026
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.

Migrate the AccountRestServiceTest

4 participants