Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build

on:
push:
branches: [master]
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '11'
- uses: gradle/actions/setup-gradle@v3
- name: Build, test and verify coverage
run: ./gradlew build
- name: Upload JaCoCo report
if: always()
uses: actions/upload-artifact@v4
with:
name: jacoco-report
path: build/reports/jacoco/test/
41 changes: 40 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
plugins {
id 'org.springframework.boot' version '2.6.3'
id 'org.springframework.boot' version '2.7.18'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'jacoco'
id "com.netflix.dgs.codegen" version "5.0.6"
id "com.diffplug.spotless" version "6.2.1"
}
Expand Down Expand Up @@ -58,6 +59,44 @@ dependencies {

tasks.named('test') {
useJUnitPlatform()
finalizedBy jacocoTestReport
}

def jacocoExcludes = ['io/spring/graphql/types/**', 'io/spring/graphql/DgsConstants*']

jacocoTestReport {
dependsOn test
reports {
xml.required = true
html.required = true
}
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: jacocoExcludes)
}))
}
}

jacocoTestCoverageVerification {
dependsOn test
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: jacocoExcludes)
}))
}
violationRules {
rule {
limit {
counter = 'LINE'
value = 'COVEREDRATIO'
minimum = 0.50
}
}
}
Comment on lines +87 to +95

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: JaCoCo line-coverage threshold set well below actual baseline

The coverage gate is set to minimum = 0.50 (build.gradle:92) while the PR description states the actual baseline is ~0.53. This leaves ~3 percentage points of slack, meaning a meaningful coverage regression could pass the build unnoticed. Not a correctness bug, but worth confirming the threshold intentionally trails the baseline rather than tracking it.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional — the 0.50 threshold trails the 0.53 baseline slightly so the gate doesn't flake on small legitimate refactors while still catching meaningful regressions. Happy to tighten it (or raise it after adding GraphQL datafetcher tests) if a stricter gate is preferred.

}

tasks.named('check') {
dependsOn jacocoTestCoverageVerification
}

tasks.named('clean') {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
10 changes: 6 additions & 4 deletions src/main/java/io/spring/api/security/WebSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import org.springframework.http.HttpStatus;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
Expand All @@ -20,7 +20,7 @@

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
public class WebSecurityConfig {

@Bean
public JwtTokenFilter jwtTokenFilter() {
Comment on lines 25 to 26

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: JwtTokenFilter registered both as security filter and servlet filter (pre-existing)

jwtTokenFilter() is declared as a @Bean of a Filter type (src/main/java/io/spring/api/security/WebSecurityConfig.java:26-28) and also inserted into the security chain via addFilterBefore (:64). Spring Boot auto-detects Filter beans and registers them with the servlet container for all requests, so this filter effectively runs outside the security chain too. This was already the case before the migration and does not cause double execution because JwtTokenFilter extends OncePerRequestFilter, so it is not a regression. Noting it here in case future work assumes the filter only runs where the security matchers apply.

(Refers to lines 25-28)

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct — this dual registration is pre-existing behavior carried over unchanged from the adapter-based config, and OncePerRequestFilter prevents double execution. Leaving as-is to keep the migration behavior-preserving; suppressing the servlet-container registration (via a FilterRegistrationBean with setEnabled(false)) could be done as a follow-up if desired.

Expand All @@ -32,8 +32,8 @@ public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

http.csrf()
.disable()
Expand Down Expand Up @@ -62,6 +62,8 @@ protected void configure(HttpSecurity http) throws Exception {
.authenticated();

http.addFilterBefore(jwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);

return http.build();
}

@Bean
Expand Down
110 changes: 110 additions & 0 deletions src/test/java/io/spring/api/security/WebSecurityConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package io.spring.api.security;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import io.spring.api.TagsApi;
import io.spring.application.TagsQueryService;
import io.spring.core.service.JwtService;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.infrastructure.mybatis.readservice.UserReadService;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest(TagsApi.class)
@Import(WebSecurityConfig.class)
public class WebSecurityConfigTest {

@Autowired private MockMvc mvc;

@MockBean private UserRepository userRepository;
@MockBean private UserReadService userReadService;
@MockBean private JwtService jwtService;
@MockBean private TagsQueryService tagsQueryService;

private User user;

@BeforeEach
public void setUp() {
user = new User("john@jacob.com", "johnjacob", "123", "", "");
Mockito.when(userRepository.findById(ArgumentMatchers.eq(user.getId())))
.thenReturn(Optional.of(user));
Mockito.when(jwtService.getSubFromToken(ArgumentMatchers.eq("valid-token")))
.thenReturn(Optional.of(user.getId()));
Mockito.when(jwtService.getSubFromToken(ArgumentMatchers.eq("invalid-token")))
.thenReturn(Optional.empty());
}

@Test
public void should_permit_options_requests_without_token() throws Exception {
mvc.perform(options("/articles"))
.andExpect(
result ->
org.junit.jupiter.api.Assertions.assertNotEquals(
401, result.getResponse().getStatus()));
}

@Test
public void should_permit_graphql_endpoints_without_token() throws Exception {
mvc.perform(get("/graphiql"))
.andExpect(
result ->
org.junit.jupiter.api.Assertions.assertNotEquals(
401, result.getResponse().getStatus()));
mvc.perform(post("/graphql"))
.andExpect(
result ->
org.junit.jupiter.api.Assertions.assertNotEquals(
401, result.getResponse().getStatus()));
}

@Test
public void should_permit_user_registration_and_login_without_token() throws Exception {
mvc.perform(post("/users")).andExpect(status().isNotFound());
mvc.perform(post("/users/login")).andExpect(status().isNotFound());
}

@Test
public void should_permit_public_read_endpoints_without_token() throws Exception {
mvc.perform(get("/articles")).andExpect(status().isNotFound());
mvc.perform(get("/articles/some-slug")).andExpect(status().isNotFound());
mvc.perform(get("/profiles/johnjacob")).andExpect(status().isNotFound());
mvc.perform(get("/tags")).andExpect(status().isOk());
}

@Test
public void should_reject_articles_feed_without_token() throws Exception {
mvc.perform(get("/articles/feed")).andExpect(status().isUnauthorized());
}

@Test
public void should_reject_protected_endpoints_without_token() throws Exception {
mvc.perform(get("/user")).andExpect(status().isUnauthorized());
mvc.perform(post("/articles")).andExpect(status().isUnauthorized());
}

@Test
public void should_reject_protected_endpoints_with_invalid_token() throws Exception {
mvc.perform(get("/user").header("Authorization", "Token invalid-token"))
.andExpect(status().isUnauthorized());
}

@Test
public void should_allow_protected_endpoints_with_valid_token() throws Exception {
mvc.perform(get("/articles/feed").header("Authorization", "Token valid-token"))
.andExpect(status().isNotFound());
mvc.perform(get("/user").header("Authorization", "Token valid-token"))
.andExpect(status().isNotFound());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class DefaultJwtServiceTest {

@BeforeEach
public void setUp() {
jwtService = new DefaultJwtService("123123123123123123123123123123123123123123123123123123123123", 3600);
jwtService =
new DefaultJwtService("123123123123123123123123123123123123123123123123123123123123", 3600);
}

@Test
Expand Down
Loading