Skip to content

fix: fix flaky scaffolding Geb tests and User.getAuthorities() bug#15480

Open
jamesfredley wants to merge 3 commits into7.0.xfrom
fix/scaffolding-test-flaky-login
Open

fix: fix flaky scaffolding Geb tests and User.getAuthorities() bug#15480
jamesfredley wants to merge 3 commits into7.0.xfrom
fix/scaffolding-test-flaky-login

Conversation

@jamesfredley
Copy link
Contributor

@jamesfredley jamesfredley commented Mar 3, 2026

Summary

  • Fix User.getAuthorities() which incorrectly used roles.split('') - this split each character of the role string into a separate GrantedAuthority (e.g., 'R', 'O', 'L', 'E', '_', 'U', 'S', 'E', 'R') instead of splitting by comma delimiter. Changed to roles.split(',') with trim() for correct role parsing.
  • Fix flaky UserControllerSpec and UserCommunityControllerSpec Geb tests. The tests used setup() to perform login, but the HTTP session was lost between the Spock lifecycle boundary (setup() -> test method), causing the browser to see "Please sign in" instead of the expected page. Moved login into each test's when block via an ensureLoggedIn() helper to eliminate the session gap.

CI Failures

This test has been failing intermittently across multiple CI runs on 7.0.x:

Run Job Branch/PR
Functional Tests (25) UserControllerSpec > User list FAILED PR #15477
Functional Tests (25) UserControllerSpec > User list FAILED PR #15477
Functional Tests (17) UserControllerSpec > User list FAILED 7.0.x (Merge #15460)
Functional Tests (25) UserControllerSpec > User list FAILED 7.0.x (Merge #15455)

Changes

File Change
User.groovy roles.split('') -> roles.split(',') with trim()
UserControllerSpec.groovy Move login from setup() to when block
UserCommunityControllerSpec.groovy Move login from setup() to when block

Testing

  • All scaffolding integration tests pass locally with --rerun-tasks -PgebAtCheckWaiting
  • Code style checks pass (./gradlew codeStyle)

Fix User.getAuthorities() which used roles.split('') splitting each
character into a separate authority instead of splitting by comma.
Changed to roles.split(',') with trim() for correct role parsing.

Move login from Spock setup() into each test's when block to avoid
session loss between the lifecycle boundary and the test method,
which caused flaky failures on Java 25 where the browser would see
'Please sign in' instead of the expected page.

Assisted-by: Claude Code <Claude@Claude.ai>
@jamesfredley jamesfredley self-assigned this Mar 3, 2026
@jamesfredley jamesfredley marked this pull request as ready for review March 3, 2026 01:14
Copilot AI review requested due to automatic review settings March 3, 2026 01:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an incorrect authority parsing implementation in the scaffolding example User domain and stabilizes two flaky Geb integration specs by performing login inside each test rather than in Spock setup().

Changes:

  • Fix User.getAuthorities() to split role strings on commas (and trim entries).
  • Replace setup()-based login with an ensureLoggedIn() helper called from each spec’s when: block.
  • Apply the same test stabilization pattern to both UserControllerSpec and UserCommunityControllerSpec.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
grails-test-examples/scaffolding/grails-app/domain/com/example/User.groovy Adjusts role parsing logic used to build Spring Security GrantedAuthority instances.
grails-test-examples/scaffolding/src/integrationTest/groovy/com/example/UserControllerSpec.groovy Moves login into the feature method to avoid session loss between Spock lifecycle phases.
grails-test-examples/scaffolding/src/integrationTest/groovy/com/example/UserCommunityControllerSpec.groovy Same login timing change to remove intermittent “Please sign in” failures.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Rename ensureLoggedIn() to loginAsTestUser() for clarity since the
method always performs login rather than checking auth state first.

Make getAuthorities() null/blank-safe by returning an empty list when
roles is null or empty, and filtering out blank entries from split.

Assisted-by: Claude Code <Claude@Claude.ai>
Collection<? extends GrantedAuthority> getAuthorities() {
roles.split('').collect { new SimpleGrantedAuthority(it) }
if (!roles) {
return Collections.emptyList()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just '[]' since this is groovy?

Collection<? extends GrantedAuthority> getAuthorities() {
roles.split('').collect { new SimpleGrantedAuthority(it) }
if (!roles) {
return Collections.emptyList()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just '[]' since this is groovy?


void setup() {
private void loginAsTestUser() {
to(LoginPage).login()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why would the http session be different between setup and the one test? @matrei any idea?

Copy link
Contributor

Choose a reason for hiding this comment

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

@jdaugherty No, I would think it should work to login in void setup().

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