Skip to content

Fix auth service bugs and apply the password policy - #33

Merged
HeftyB merged 4 commits into
mainfrom
fix/auth-service
Sep 18, 2026
Merged

HeftyB merged 4 commits into
mainfrom
fix/auth-service

Conversation

@HeftyB

@HeftyB HeftyB commented Sep 18, 2026

Copy link
Copy Markdown
Owner

Closes #14

What

Fixes five bugs in UserServiceImp and applies the Passay password policy, which was defined but never used. Each change lands test-first: a commit with the failing tests, then the commit that makes them pass.

Bug Effect Fix
checkIfValidOldPassword called matches(hash, raw) The current password was never accepted matches(raw, hash)
emailExist used findByEmailContainingIgnoreCase am@example.com was refused because sam@example.com exists Exact match with the existing findByEmailIgnoreCase
getUserByPasswordResetToken called .get() before Optional.ofNullable Unknown tokens threw NoSuchElementException findByToken(token).map(PasswordResetToken::getUser)
getUser used String.format("", token) Blank not-found message Message names the token
generateNewVerificationToken had a %s with no argument The not-found path threw MissingFormatArgumentException instead of DataNotFoundException Pass the token

Password policy. @ValidPassword (8–30 characters, no whitespace, via Passay) is now applied to UserDTO.password alongside @NotNull. The validator now reports Passay's rule messages, such as "Password must be 8 or more characters in length.", instead of a generic "Invalid Password", and leaves null to @NotNull.

The fifth bug was not in the portfolio review; the tests found it.

Scope note

Nothing outside the service layer calls these methods yet. Registration, password reset and change-password have no controllers or pages, so these bugs were latent rather than user-facing. The tests make sure the flows work once a UI is added.

Tests

Test class Type Covers
UserServiceImpTest (8) Spring context + H2 The five bugs, plus the matching positive cases (wrong password rejected, duplicate email rejected ignoring case, known reset token resolves)
UserDTOValidationTest (8) Plain Bean Validation Accepts 8–30 characters without whitespace; rejects short, long, whitespace and missing passwords; reports which rule failed

UserServiceImpTest runs against the real service and database rather than mocks, so the email check exercises the actual query. 34 tests passing overall.

🤖 Generated with Claude Code

HeftyB and others added 4 commits September 18, 2026 19:05
Runs against the real service and H2, so the email lookup exercises
the actual query rather than a mock. Five tests fail today:

- checkIfValidOldPassword passes (hash, raw) to matches(), which
  takes (raw, hash), so the current password is never accepted
- emailExist uses a substring match, so registering am@example.com
  is refused because sam@example.com exists
- getUserByPasswordResetToken calls .get() on an empty Optional and
  throws instead of returning empty
- getUser builds its not-found message with String.format("", ...),
  which is always blank
- generateNewVerificationToken's format string has a %s but no
  argument, so the not-found path throws MissingFormatArgumentException

[Ticket: #14]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
- checkIfValidOldPassword: pass (raw, encoded) to matches()
- emailExist: use the exact-match findByEmailIgnoreCase that already
  exists on UserRepository
- getUserByPasswordResetToken: map the Optional instead of calling
  get() on it
- getUser and generateNewVerificationToken: include the token in the
  not-found message; the latter no longer throws
  MissingFormatArgumentException in place of DataNotFoundException

[Ticket: #14]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The Passay-backed @validpassword constraint exists but is not applied
to anything, so UserDTO accepts any password, including none. The
policy it implements is 8-30 characters with no whitespace.

[Ticket: #14]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Annotate UserDTO.password with @NotNull and @validpassword. In the
validator:

- Treat null as valid so @NotNull reports it, instead of Passay
  failing on a null PasswordData.
- Report Passay's own rule messages ("Password must be 8 or more
  characters in length.") instead of the generic "Invalid Password".
  This was the intent of the commented-out code, which used Guava's
  Joiner, a library this project does not have.

[Ticket: #14]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@HeftyB HeftyB added this to the Portfolio readiness milestone Sep 18, 2026
@HeftyB HeftyB added bug Something isn't working security Security-relevant change testing Tests and test infrastructure labels Sep 18, 2026
@HeftyB HeftyB self-assigned this Sep 18, 2026
@HeftyB
HeftyB merged commit 23f5dad into main Sep 18, 2026
1 check passed
@HeftyB
HeftyB deleted the fix/auth-service branch September 18, 2026 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working security Security-relevant change testing Tests and test infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix authentication bugs in UserServiceImp

1 participant