[test-improver] Add @WebMvcTest tests for UserController (21 tests, 2 bug pins)#56
Draft
github-actions[bot] wants to merge 1 commit into
Conversation
Cover all 9 endpoints with 21 tests using @WebMvcTest + @MockBean UserService. Documents two security/contract bugs as bug-pin tests: 1. POST /api/users/register returns 200 instead of 201 Created 2. POST /api/users/password-reset exposes reset token in response body Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 Test Improver — automated AI assistant focused on improving tests.
Goal & Rationale
UserControllerhad zero effective test coverage — 9 endpoints completely untested at the HTTP layer. This PR adds 21@WebMvcTesttests covering all endpoints and documents two security/contract bugs as bug-pin tests.Approach
Used
@WebMvcTest(UserController.class)with@MockBean UserService. Tests the HTTP layer in isolation (fast, no full Spring context, no database).Tests added
getAllUsers_returnsJsonListGET /api/usersgetAllUsers_emptyList_returnsEmptyArrayGET /api/usersgetUser_existingId_returns200WithBodyGET /api/users/{id}getUser_missingId_returns404GET /api/users/{id}registerUser_validBody_BUG_returns200InsteadOf201POST /api/users/registerregisterUser_duplicateUsername_returns400POST /api/users/registerlogin_validCredentials_returns200WithUserAndTokenPOST /api/users/login{user, token}in bodylogin_invalidCredentials_returns401POST /api/users/loginlogin_unknownUser_returns401POST /api/users/loginupdateUser_existingId_returns200PUT /api/users/{id}updateUser_missingId_returns400PUT /api/users/{id}deleteUser_existingId_returns204DELETE /api/users/{id}deleteUser_missingId_returns400DELETE /api/users/{id}requestPasswordReset_knownEmail_BUG_returnsTokenInBodyPOST /api/users/password-resetrequestPasswordReset_unknownEmail_returns400POST /api/users/password-resetresetPassword_validToken_returns200POST /api/users/password-reset/confirmresetPassword_invalidToken_returns400POST /api/users/password-reset/confirmsearchUsers_matchingQuery_returnsResultsGET /api/users/searchsearchUsers_noMatches_returnsEmptyArrayGET /api/users/searchgetUsersByRole_returnsFilteredListGET /api/users/by-role/{role}getUsersByRole_noUsersForRole_returnsEmptyArrayGET /api/users/by-role/{role}Bug-pin tests
1.
POST /api/users/registerreturns 200 instead of 201 CreatedUserController.registerUser()callsResponseEntity.ok(user)— same pattern asProjectController.createProject(). When fixed, changeisOk()toisCreated()in the test.2.
POST /api/users/password-resetreturns the reset token in the response bodyUserController.requestPasswordReset()includes the raw reset token in the JSON response:Reset tokens must only be sent out-of-band (email). Returning the token over the API lets an attacker who knows a victim's email immediately obtain their reset token without email access. When fixed, assert
jsonPath("$.token").doesNotExist()instead.Coverage Impact
UserControllerline coverageTrade-offs
@WebMvcTestkeeps tests fast (~1s total run time) with no DB or full context.Test Status
Pre-existing failures (not caused by this PR):
DateUtilsTest.testGetQuarter— off-by-one bug (Issue 🔴 [BUG] getQuarter() returns 0-3 instead of 1-4 (off-by-one bug) #6)TaskServiceTest.testGetTaskStatistics— division by zero (Issue 🔴 [BUG] ArithmeticException: Division by zero on empty project dashboard #3)Reproducibility
Add this agentic workflows to your repo
To install this agentic workflow, run