aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2023-01-14 15:42:03 +0300
committerGravatar Vitaly Takmazov2023-01-14 15:42:03 +0300
commit23a41aebadbe13e8992856c6be84ce6d01de0175 (patch)
tree28ccc1446a8d4c1b9141454ea74703e65a9bff22
parent834fef2a7794d769144c7c087a8b1fa0a2a9bb21 (diff)
Configure test coverage reports
-rw-r--r--pom.xml23
-rw-r--r--src/main/java/com/juick/config/WebConfig.java5
-rw-r--r--src/main/java/com/juick/www/api/Index.java5
-rw-r--r--src/main/java/com/juick/www/controllers/Compat.java12
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java1
5 files changed, 40 insertions, 6 deletions
diff --git a/pom.xml b/pom.xml
index 2c828b45..cb76d74f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -44,6 +44,10 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-validation</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
@@ -388,6 +392,25 @@
</execution>
</executions>
</plugin>
+ <plugin>
+ <groupId>org.jacoco</groupId>
+ <artifactId>jacoco-maven-plugin</artifactId>
+ <version>0.8.8</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>prepare-agent</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>report</id>
+ <phase>prepare-package</phase>
+ <goals>
+ <goal>report</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</build>
<profiles>
diff --git a/src/main/java/com/juick/config/WebConfig.java b/src/main/java/com/juick/config/WebConfig.java
index 136faea9..2040afd6 100644
--- a/src/main/java/com/juick/config/WebConfig.java
+++ b/src/main/java/com/juick/config/WebConfig.java
@@ -35,6 +35,7 @@ import org.springframework.format.FormatterRegistry;
import org.springframework.http.CacheControl;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -66,6 +67,10 @@ public class WebConfig implements WebMvcConfigurer {
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(visitorArgumentResolver());
}
+ @Bean
+ public MethodValidationPostProcessor methodValidationPostProcessor() {
+ return new MethodValidationPostProcessor();
+ }
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
try {
diff --git a/src/main/java/com/juick/www/api/Index.java b/src/main/java/com/juick/www/api/Index.java
index 921ebca4..7430c22d 100644
--- a/src/main/java/com/juick/www/api/Index.java
+++ b/src/main/java/com/juick/www/api/Index.java
@@ -19,6 +19,7 @@ package com.juick.www.api;
import io.swagger.v3.oas.annotations.Hidden;
+import jakarta.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
@@ -56,4 +57,8 @@ public class Index {
ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUriString());
return ResponseEntity.badRequest().body("Invalid media type");
}
+ @ExceptionHandler(ConstraintViolationException.class)
+ public ResponseEntity<String> badRequest(final ConstraintViolationException exception) {
+ return ResponseEntity.badRequest().body(exception.getMessage());
+ }
}
diff --git a/src/main/java/com/juick/www/controllers/Compat.java b/src/main/java/com/juick/www/controllers/Compat.java
index 4a7390f5..edd7bc29 100644
--- a/src/main/java/com/juick/www/controllers/Compat.java
+++ b/src/main/java/com/juick/www/controllers/Compat.java
@@ -21,8 +21,10 @@ import com.juick.service.ActivityPubService;
import com.juick.service.WebfingerService;
import com.juick.util.HttpNotFoundException;
import com.juick.www.api.activity.model.Context;
+import jakarta.validation.constraints.NotEmpty;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
+import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@@ -31,6 +33,7 @@ import org.springframework.web.servlet.view.RedirectView;
import javax.inject.Inject;
@Controller
+@Validated
public class Compat {
@Inject
private WebfingerService webfingerService;
@@ -42,12 +45,9 @@ public class Compat {
return new RedirectView("/post");
}
@GetMapping("/mention")
- public RedirectView mention(@RequestParam String username) {
+ public RedirectView mention(@RequestParam @NotEmpty(message = "Username may not be empty") String username) {
var uri = webfingerService.discoverAccountURI(username, MediaType.valueOf(Context.ACTIVITY_MEDIA_TYPE));
- if (!uri.toASCIIString().isEmpty()) {
- var context = activityPubService.get(uri).orElseThrow(HttpNotFoundException::new);
- return new RedirectView(context.getUrl());
- }
- throw new HttpNotFoundException();
+ var context = activityPubService.get(uri).orElseThrow(HttpNotFoundException::new);
+ return new RedirectView(context.getUrl());
}
}
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index a6c840bf..d6f0b6ae 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -2715,6 +2715,7 @@ public class ServerTests {
mockMvc.perform(get("/mention?username=vt@juick.com")).andExpect(
redirectedUrl("https://juick.com/vt/")
);
+ mockMvc.perform(get("/mention?username=")).andExpect(status().isBadRequest());
}
@Test