Add tests for security layer and exception handling#616
Add tests for security layer and exception handling#616devin-ai-integration[bot] wants to merge 2 commits into
Conversation
- JwtTokenFilter: null header, malformed header, valid token with/without user, existing auth - WebSecurityConfig: OPTIONS permitted, graphql/graphiql public, articles GET public, feed auth required, users/login public, other endpoints auth required - CustomizeExceptionHandler: InvalidRequest, InvalidAuthentication, MethodArgumentNotValid, ConstraintViolation - ErrorResourceSerializer: single error, grouped errors on same field, errors on different fields - AuthorizationService: owner/non-owner article write, article owner/comment author/unrelated user comment delete Co-Authored-By: Kyu Choi <kyuhwanchoi0423@gmail.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
| jwtTokenFilter.doFilterInternal(request, response, filterChain); | ||
|
|
||
| verify(filterChain).doFilter(request, response); | ||
| assert SecurityContextHolder.getContext().getAuthentication() == null; |
There was a problem hiding this comment.
🟡 Java assert keyword used instead of JUnit assertions makes security test verifications unreliable
All 6 verification statements in JwtTokenFilterTest use the Java assert keyword instead of JUnit assertions (assertNull, assertNotNull, assertEquals, etc.). Java assert statements are only evaluated when the JVM is started with the -ea flag. While Gradle's test task defaults to enableAssertions=true, these assertions will be silently skipped in environments that don't set -ea (e.g., some IDE test runners, non-Gradle CI pipelines). This means the tests could pass without actually verifying any security behavior — authentication could be set when it shouldn't be, or not set when it should be, and the tests would still be green. Every other test file in this PR correctly uses JUnit assertions.
All affected lines in JwtTokenFilterTest.java
- Line 46:
assert SecurityContextHolder.getContext().getAuthentication() == null; - Line 56:
assert SecurityContextHolder.getContext().getAuthentication() == null; - Line 71:
assert SecurityContextHolder.getContext().getAuthentication() != null; - Line 72:
assert SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals(user); - Line 87:
assert SecurityContextHolder.getContext().getAuthentication() == null; - Line 105:
assert SecurityContextHolder.getContext().getAuthentication() == existingAuth;
Prompt for agents
Replace all Java `assert` statements in JwtTokenFilterTest.java with proper JUnit 5 assertions. The file is missing JUnit assertion imports entirely.
Add the following import:
import static org.junit.jupiter.api.Assertions.*;
Then replace each `assert` statement:
- Line 46: `assert X == null` → `assertNull(SecurityContextHolder.getContext().getAuthentication())`
- Line 56: same as line 46
- Line 71: `assert X != null` → `assertNotNull(SecurityContextHolder.getContext().getAuthentication())`
- Line 72: `assert X.equals(user)` → `assertEquals(user, SecurityContextHolder.getContext().getAuthentication().getPrincipal())`
- Line 87: same as line 46
- Line 105: `assert X == existingAuth` → `assertSame(existingAuth, SecurityContextHolder.getContext().getAuthentication())`
This ensures assertions are always evaluated regardless of JVM flags, provides informative failure messages, and aligns with the convention used in every other test file in this PR.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Fixed in c259130 — replaced all 6 Java assert statements with proper JUnit 5 assertions (assertNull, assertNotNull, assertEquals, assertSame).
Co-Authored-By: Kyu Choi <kyuhwanchoi0423@gmail.com>
Summary
Adds unit tests for 5 previously untested classes in the security and exception handling layers:
All 94 tests pass locally (84 existing + 10 new test classes with 27 total test methods).
Review & Testing Checklist for Human
./gradlew testto confirm all tests pass in your environmentNotes
@SpringBootTest+@AutoConfigureMockMvcto load the full security filter chain, while other tests use lightweight@ExtendWith(MockitoExtension.class)or plain JUnitLink to Devin session: https://app.devin.ai/sessions/10dd396ee2f24d20b6aadf313126b78f
Requested by: @choikh0423
Devin Review