Skip to content

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

Merged
jamesfredley merged 1 commit into7.0.xfrom
fix/scaffolding-test-flaky-login
Mar 13, 2026
Merged

fix: fix flaky scaffolding Geb tests and User.getAuthorities() bug#15480
jamesfredley merged 1 commit into7.0.xfrom
fix/scaffolding-test-flaky-login

Conversation

@jamesfredley
Copy link
Copy Markdown
Contributor

@jamesfredley jamesfredley commented Mar 3, 2026

Summary

  • Fix flaky UserControllerSpec and UserCommunityControllerSpec Geb tests by clearing stale session cookies in setup() before login via clearCookiesQuietly()
  • Fix User.getAuthorities() which incorrectly used roles.split('') splitting each character into a separate GrantedAuthority instead of splitting by comma delimiter

Root Cause

CI logs showed the specs run sequentially with a shared browser instance:

UserCommunityControllerSpec > User list PASSED
UserControllerSpec > User list FAILED
    geb.waiting.WaitTimeoutException: title == pageTitle
    'Please sign in' != 'User List'

The first spec's cleanup() logs out, leaving stale session cookies. When the second spec's setup() navigates to /login and submits the form, the inconsistent session state causes the login to silently fail - the browser ends up back on the login page instead of being authenticated.

Adding clearCookiesQuietly() before login in setup() ensures each spec starts with a clean browser state, eliminating the stale session interference.

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('') -> proper comma-split with null/blank guard
UserControllerSpec.groovy Add clearCookiesQuietly() in setup() before login
UserCommunityControllerSpec.groovy Add clearCookiesQuietly() in setup() before login

@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
Copy Markdown
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.

Comment thread grails-test-examples/scaffolding/grails-app/domain/com/example/User.groovy Outdated
Comment thread grails-test-examples/scaffolding/grails-app/domain/com/example/User.groovy Outdated
Comment thread grails-test-examples/scaffolding/grails-app/domain/com/example/User.groovy Outdated
Clear cookies before login in setup() to eliminate stale session state
from a previous spec's logout, which caused the second spec to see
'Please sign in' instead of the expected page.

Also fix User.getAuthorities() which incorrectly used roles.split('')
splitting each character into a separate GrantedAuthority instead of
splitting by comma delimiter.

Assisted-by: Claude Code <Claude@Claude.ai>
@jamesfredley jamesfredley force-pushed the fix/scaffolding-test-flaky-login branch from 35492ea to cc72ad5 Compare March 13, 2026 02:10
@testlens-app
Copy link
Copy Markdown

testlens-app bot commented Mar 13, 2026

✅ All tests passed ✅

🏷️ Commit: cc72ad5
▶️ Tests: 20314 executed
⚪️ Checks: 31/31 completed


Learn more about TestLens at testlens.app.

@jamesfredley jamesfredley merged commit a7a7ff9 into 7.0.x Mar 13, 2026
32 checks passed
@jamesfredley jamesfredley deleted the fix/scaffolding-test-flaky-login branch March 13, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants