From a8024b2eb9d3ca36433f546a524712584179df2d Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Tue, 1 Jul 2025 21:37:30 +0800 Subject: [PATCH] test: mock captcha service and fix auth helper --- .../java/com/openisle/controller/AuthControllerTest.java | 3 +++ .../java/com/openisle/controller/CommentControllerTest.java | 3 +++ .../java/com/openisle/controller/PostControllerTest.java | 3 +++ .../openisle/integration/ComplexFlowIntegrationTest.java | 6 ++++-- .../com/openisle/integration/SearchIntegrationTest.java | 6 ++++-- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/openisle/controller/AuthControllerTest.java b/src/test/java/com/openisle/controller/AuthControllerTest.java index 8f0a5cd04..a4b557fa3 100644 --- a/src/test/java/com/openisle/controller/AuthControllerTest.java +++ b/src/test/java/com/openisle/controller/AuthControllerTest.java @@ -4,6 +4,7 @@ import com.openisle.model.User; import com.openisle.service.EmailSender; import com.openisle.service.JwtService; import com.openisle.service.UserService; +import com.openisle.service.CaptchaService; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; @@ -34,6 +35,8 @@ class AuthControllerTest { private JwtService jwtService; @MockBean private EmailSender emailService; + @MockBean + private CaptchaService captchaService; @Test void registerSendsEmail() throws Exception { diff --git a/src/test/java/com/openisle/controller/CommentControllerTest.java b/src/test/java/com/openisle/controller/CommentControllerTest.java index 228b84543..dd6ac95b3 100644 --- a/src/test/java/com/openisle/controller/CommentControllerTest.java +++ b/src/test/java/com/openisle/controller/CommentControllerTest.java @@ -4,6 +4,7 @@ import com.openisle.model.Comment; import com.openisle.model.Post; import com.openisle.model.User; import com.openisle.service.CommentService; +import com.openisle.service.CaptchaService; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; @@ -30,6 +31,8 @@ class CommentControllerTest { @MockBean private CommentService commentService; + @MockBean + private CaptchaService captchaService; private Comment createComment(Long id, String content, String authorName) { User user = new User(); diff --git a/src/test/java/com/openisle/controller/PostControllerTest.java b/src/test/java/com/openisle/controller/PostControllerTest.java index 8824987b3..e030ee8e5 100644 --- a/src/test/java/com/openisle/controller/PostControllerTest.java +++ b/src/test/java/com/openisle/controller/PostControllerTest.java @@ -6,6 +6,7 @@ import com.openisle.model.Category; import com.openisle.service.PostService; import com.openisle.service.CommentService; import com.openisle.service.ReactionService; +import com.openisle.service.CaptchaService; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; @@ -37,6 +38,8 @@ class PostControllerTest { private CommentService commentService; @MockBean private ReactionService reactionService; + @MockBean + private CaptchaService captchaService; @Test void createAndGetPost() throws Exception { diff --git a/src/test/java/com/openisle/integration/ComplexFlowIntegrationTest.java b/src/test/java/com/openisle/integration/ComplexFlowIntegrationTest.java index baa40aea2..c13cc2c12 100644 --- a/src/test/java/com/openisle/integration/ComplexFlowIntegrationTest.java +++ b/src/test/java/com/openisle/integration/ComplexFlowIntegrationTest.java @@ -35,8 +35,10 @@ class ComplexFlowIntegrationTest { rest.postForEntity("/api/auth/register", new HttpEntity<>( Map.of("username", username, "email", email, "password", "pass123"), h), Map.class); User u = users.findByUsername(username).orElseThrow(); - rest.postForEntity("/api/auth/verify", new HttpEntity<>( - Map.of("username", username, "code", u.getVerificationCode()), h), Map.class); + if (u.getVerificationCode() != null) { + rest.postForEntity("/api/auth/verify", new HttpEntity<>( + Map.of("username", username, "code", u.getVerificationCode()), h), Map.class); + } ResponseEntity resp = rest.postForEntity("/api/auth/login", new HttpEntity<>( Map.of("username", username, "password", "pass123"), h), Map.class); return (String) resp.getBody().get("token"); diff --git a/src/test/java/com/openisle/integration/SearchIntegrationTest.java b/src/test/java/com/openisle/integration/SearchIntegrationTest.java index 7761f9cf0..646e0314c 100644 --- a/src/test/java/com/openisle/integration/SearchIntegrationTest.java +++ b/src/test/java/com/openisle/integration/SearchIntegrationTest.java @@ -31,8 +31,10 @@ class SearchIntegrationTest { rest.postForEntity("/api/auth/register", new HttpEntity<>( Map.of("username", username, "email", email, "password", "pass123"), h), Map.class); User u = users.findByUsername(username).orElseThrow(); - rest.postForEntity("/api/auth/verify", new HttpEntity<>( - Map.of("username", username, "code", u.getVerificationCode()), h), Map.class); + if (u.getVerificationCode() != null) { + rest.postForEntity("/api/auth/verify", new HttpEntity<>( + Map.of("username", username, "code", u.getVerificationCode()), h), Map.class); + } ResponseEntity resp = rest.postForEntity("/api/auth/login", new HttpEntity<>( Map.of("username", username, "password", "pass123"), h), Map.class); return (String) resp.getBody().get("token");