-
Notifications
You must be signed in to change notification settings - Fork 4
Refactor: Replace brittle constraint-name string matching with proactive repository checks in AuthService #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,12 +49,12 @@ public class AuthService { | |||||||||||||||||
| @CacheEvict(value = "usernameCheck", key = "#request.username.toLowerCase()") | ||||||||||||||||||
| public AuthResponse signUp(SignUpRequest request) { | ||||||||||||||||||
| AuthResponse authResponse = new AuthResponse(); | ||||||||||||||||||
| if (request.getUsername() != null && userRepository.findByUsernameIgnoreCase(request.getUsername()).isPresent()) { | ||||||||||||||||||
| authResponse.setMessage("Username already exists (case-insensitive)"); | ||||||||||||||||||
| if (request.getUsername() != null && userRepository.existsByUsernameIgnoreCase(request.getUsername())) { | ||||||||||||||||||
| authResponse.setMessage("Username already exists"); | ||||||||||||||||||
|
||||||||||||||||||
| authResponse.setMessage("Username already exists"); | |
| authResponse.setMessage("Username already exists (case-insensitive)"); |
Copilot
AI
Apr 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CacheEvict uses key "#request.username.toLowerCase()" which will throw a SpEL evaluation error if signUp is ever called with a null username (and the method body already allows for null via request.getUsername() != null checks). To make this null-safe, add a condition = "#request.username != null" (or otherwise guard the key expression) so the cache eviction doesn't fail before the method executes.
Copilot
AI
Apr 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the DataIntegrityViolationException handler, the error is logged without the exception object, which drops the stack trace and makes production debugging harder. Consider logging e as the throwable (and optionally using a stable identifier rather than only e.getMessage()).
| log.error("Data integrity violation during sign up: {}", e.getMessage()); | |
| log.error("Data integrity violation during sign up: {}", e.getMessage(), e); |
Copilot
AI
Apr 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After removing constraint-name matching, this catch-all now always returns the generic message "Data integrity violation". Since the PR intent is to keep this as a race-condition safety net, you can still provide a user-friendly duplicate email/username message by re-checking existsByUsernameIgnoreCase/existsByEmail inside the catch and only falling back to the generic message if neither exists. This keeps the logic resilient without depending on driver/constraint strings.
| authResponse.setMessage("Data integrity violation"); | |
| if (request.getUsername() != null && userRepository.existsByUsernameIgnoreCase(request.getUsername())) { | |
| authResponse.setMessage("Username already exists"); | |
| } else if (userRepository.existsByEmail(request.getEmail())) { | |
| authResponse.setMessage("Email already exists"); | |
| } else { | |
| authResponse.setMessage("Data integrity violation"); | |
| } |
Copilot
AI
Apr 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to signUp, this DataIntegrityViolationException log line only records e.getMessage() and omits the throwable, which can hide the stack trace and root cause. Consider including e in the log call so the full exception is captured.
| log.error("Data integrity violation while setting username for email {}: {}", email, e.getMessage()); | |
| log.error("Data integrity violation while setting username for email {}: {}", email, e.getMessage(), e); |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,7 +11,6 @@ | |||||||
| import org.junit.jupiter.api.BeforeEach; | ||||||||
| import org.junit.jupiter.api.Test; | ||||||||
| import org.mockito.*; | ||||||||
| import org.springframework.dao.DataIntegrityViolationException; | ||||||||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||||||||
|
|
||||||||
| import java.util.Optional; | ||||||||
|
|
@@ -68,14 +67,13 @@ void testSignUp_duplicateEmail() { | |||||||
| request.setPassword("mypassword"); | ||||||||
| request.setEmail("fred@example.com"); | ||||||||
|
|
||||||||
|
||||||||
| when(userRepository.existsByUsernameIgnoreCase("fredmaina123")).thenReturn(false); |
Uh oh!
There was an error while loading. Please reload this page.