From cad31f9ee8f51c4ff6d512372de6e17f0e3dbcab Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Thu, 10 Jul 2025 00:02:03 +0800 Subject: [PATCH 1/7] test: fix NotificationControllerTest expectation --- .../com/openisle/controller/NotificationControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/openisle/controller/NotificationControllerTest.java b/src/test/java/com/openisle/controller/NotificationControllerTest.java index 53a8c80f8..d956b6eb9 100644 --- a/src/test/java/com/openisle/controller/NotificationControllerTest.java +++ b/src/test/java/com/openisle/controller/NotificationControllerTest.java @@ -47,7 +47,7 @@ class NotificationControllerTest { .principal(new UsernamePasswordAuthenticationToken("alice","p"))) .andExpect(status().isOk()) .andExpect(jsonPath("$[0].id").value(1)) - .andExpect(jsonPath("$[0].postId").value(2)); + .andExpect(jsonPath("$[0].post.id").value(2)); } @Test From e37fd4de82c256e540abce08089085c9b56980d3 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Thu, 10 Jul 2025 00:02:06 +0800 Subject: [PATCH 2/7] Fix UserController tests to use identifier --- .../java/com/openisle/controller/UserControllerTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/openisle/controller/UserControllerTest.java b/src/test/java/com/openisle/controller/UserControllerTest.java index d3d1b301c..4135281c1 100644 --- a/src/test/java/com/openisle/controller/UserControllerTest.java +++ b/src/test/java/com/openisle/controller/UserControllerTest.java @@ -81,7 +81,7 @@ class UserControllerTest { User u = new User(); u.setId(2L); u.setUsername("bob"); - Mockito.when(userService.findByUsername("bob")).thenReturn(Optional.of(u)); + Mockito.when(userService.findByIdentifier("bob")).thenReturn(Optional.of(u)); mockMvc.perform(get("/api/users/bob")) .andExpect(status().isOk()) @@ -100,6 +100,7 @@ class UserControllerTest { post.setCreatedAt(java.time.LocalDateTime.now()); post.setCategory(cat); post.setAuthor(user); + Mockito.when(userService.findByIdentifier("bob")).thenReturn(Optional.of(user)); Mockito.when(postService.getRecentPostsByUser("bob", 10)).thenReturn(java.util.List.of(post)); mockMvc.perform(get("/api/users/bob/posts")) @@ -119,6 +120,7 @@ class UserControllerTest { comment.setCreatedAt(java.time.LocalDateTime.now()); comment.setAuthor(user); comment.setPost(post); + Mockito.when(userService.findByIdentifier("bob")).thenReturn(Optional.of(user)); Mockito.when(commentService.getRecentCommentsByUser("bob", 50)).thenReturn(java.util.List.of(comment)); mockMvc.perform(get("/api/users/bob/replies")) @@ -147,7 +149,7 @@ class UserControllerTest { comment.setAuthor(user); comment.setPost(post); - Mockito.when(userService.findByUsername("bob")).thenReturn(Optional.of(user)); + Mockito.when(userService.findByIdentifier("bob")).thenReturn(Optional.of(user)); Mockito.when(postService.getRecentPostsByUser("bob", 10)).thenReturn(java.util.List.of(post)); Mockito.when(commentService.getRecentCommentsByUser("bob", 50)).thenReturn(java.util.List.of(comment)); From 24f10d008f2d70c1cb507dc2665a0ebefc2ebcf5 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Thu, 10 Jul 2025 00:02:07 +0800 Subject: [PATCH 3/7] fix search service null category --- src/main/java/com/openisle/service/SearchService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/openisle/service/SearchService.java b/src/main/java/com/openisle/service/SearchService.java index 3964da85a..271678bca 100644 --- a/src/main/java/com/openisle/service/SearchService.java +++ b/src/main/java/com/openisle/service/SearchService.java @@ -67,7 +67,7 @@ public class SearchService { "post", p.getId(), p.getTitle(), - p.getCategory().getName(), + p.getCategory() != null ? p.getCategory().getName() : null, extractSnippet(p.getContent(), keyword, false), null )), @@ -76,7 +76,7 @@ public class SearchService { "post_title", p.getId(), p.getTitle(), - p.getCategory().getName(), + p.getCategory() != null ? p.getCategory().getName() : null, extractSnippet(p.getContent(), keyword, true), null )) From 4224bcd2e2828406f442bf938287b64699be9977 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Thu, 10 Jul 2025 00:02:10 +0800 Subject: [PATCH 4/7] fix: handle already verified users in PublishModeIntegrationTest --- .../openisle/integration/PublishModeIntegrationTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/openisle/integration/PublishModeIntegrationTest.java b/src/test/java/com/openisle/integration/PublishModeIntegrationTest.java index b2fe164a9..2bcf4515d 100644 --- a/src/test/java/com/openisle/integration/PublishModeIntegrationTest.java +++ b/src/test/java/com/openisle/integration/PublishModeIntegrationTest.java @@ -36,8 +36,10 @@ class PublishModeIntegrationTest { 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"); From 6b3a7cb03b12303becda0ab718ac7600a7c7b441 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Thu, 10 Jul 2025 00:02:12 +0800 Subject: [PATCH 5/7] Fix NotificationControllerTest json path --- .../com/openisle/controller/NotificationControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/openisle/controller/NotificationControllerTest.java b/src/test/java/com/openisle/controller/NotificationControllerTest.java index 53a8c80f8..d956b6eb9 100644 --- a/src/test/java/com/openisle/controller/NotificationControllerTest.java +++ b/src/test/java/com/openisle/controller/NotificationControllerTest.java @@ -47,7 +47,7 @@ class NotificationControllerTest { .principal(new UsernamePasswordAuthenticationToken("alice","p"))) .andExpect(status().isOk()) .andExpect(jsonPath("$[0].id").value(1)) - .andExpect(jsonPath("$[0].postId").value(2)); + .andExpect(jsonPath("$[0].post.id").value(2)); } @Test From a354756d450c84c441a4d784478a8e6701e95b82 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Thu, 10 Jul 2025 00:02:14 +0800 Subject: [PATCH 6/7] test: update integration test usernames --- .../integration/ComplexFlowIntegrationTest.java | 10 +++++----- .../integration/PublishModeIntegrationTest.java | 4 ++-- .../openisle/integration/SearchIntegrationTest.java | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/openisle/integration/ComplexFlowIntegrationTest.java b/src/test/java/com/openisle/integration/ComplexFlowIntegrationTest.java index 3feeae0a6..0dc8f5319 100644 --- a/src/test/java/com/openisle/integration/ComplexFlowIntegrationTest.java +++ b/src/test/java/com/openisle/integration/ComplexFlowIntegrationTest.java @@ -61,10 +61,10 @@ class ComplexFlowIntegrationTest { @Test void nestedCommentsVisibleInPost() { - String t1 = registerAndLogin("alice", "a@example.com"); - String t2 = registerAndLogin("bob", "b@example.com"); + String t1 = registerAndLogin("alice1", "a@example.com"); + String t2 = registerAndLogin("bob123", "b@example.com"); - String adminToken = registerAndLoginAsAdmin("admin", "admin@example.com"); + String adminToken = registerAndLoginAsAdmin("admin1", "admin@example.com"); ResponseEntity catResp = postJson("/api/categories", Map.of("name", "general", "description", "d", "icon", "i"), adminToken); Long catId = ((Number)catResp.getBody().get("id")).longValue(); @@ -106,8 +106,8 @@ class ComplexFlowIntegrationTest { @Test void reactionsReturnedForPostAndComment() { - String t1 = registerAndLogin("carol", "c@example.com"); - String t2 = registerAndLogin("dave", "d@example.com"); + String t1 = registerAndLogin("carol1", "c@example.com"); + String t2 = registerAndLogin("dave12", "d@example.com"); String adminToken = registerAndLoginAsAdmin("admin2", "admin2@example.com"); List> categories = (List>) rest.getForObject("/api/categories", List.class); diff --git a/src/test/java/com/openisle/integration/PublishModeIntegrationTest.java b/src/test/java/com/openisle/integration/PublishModeIntegrationTest.java index b2fe164a9..81ae82d11 100644 --- a/src/test/java/com/openisle/integration/PublishModeIntegrationTest.java +++ b/src/test/java/com/openisle/integration/PublishModeIntegrationTest.java @@ -66,8 +66,8 @@ class PublishModeIntegrationTest { @Test void postRequiresApproval() { - String userToken = registerAndLogin("eve", "e@example.com"); - String adminToken = registerAndLoginAsAdmin("admin", "admin@example.com"); + String userToken = registerAndLogin("eve123", "e@example.com"); + String adminToken = registerAndLoginAsAdmin("admin1", "admin@example.com"); ResponseEntity catResp = postJson("/api/categories", Map.of("name", "review", "description", "d", "icon", "i"), adminToken); diff --git a/src/test/java/com/openisle/integration/SearchIntegrationTest.java b/src/test/java/com/openisle/integration/SearchIntegrationTest.java index 681662b69..233d725d0 100644 --- a/src/test/java/com/openisle/integration/SearchIntegrationTest.java +++ b/src/test/java/com/openisle/integration/SearchIntegrationTest.java @@ -57,7 +57,7 @@ class SearchIntegrationTest { @Test void globalSearchReturnsMixedResults() { - String admin = registerAndLoginAsAdmin("admin", "a@a.com"); + String admin = registerAndLoginAsAdmin("admin1", "a@a.com"); String user = registerAndLogin("bob_nice", "b@b.com"); ResponseEntity catResp = postJson("/api/categories", Map.of("name", "misc", "description", "d", "icon", "i"), admin); From d4dd7abe62c23ecba12fdf2639733e0417211226 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Thu, 10 Jul 2025 00:02:16 +0800 Subject: [PATCH 7/7] Fix PasswordValidator tests to expect FieldException --- .../service/PasswordValidatorTest.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/test/java/com/openisle/service/PasswordValidatorTest.java b/src/test/java/com/openisle/service/PasswordValidatorTest.java index ba564491f..5b8a6d911 100644 --- a/src/test/java/com/openisle/service/PasswordValidatorTest.java +++ b/src/test/java/com/openisle/service/PasswordValidatorTest.java @@ -1,6 +1,7 @@ package com.openisle.service; import com.openisle.model.PasswordStrength; +import com.openisle.exception.FieldException; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -11,7 +12,7 @@ class PasswordValidatorTest { void lowStrengthRequiresSixChars() { PasswordValidator validator = new PasswordValidator(PasswordStrength.LOW); - assertThrows(IllegalArgumentException.class, () -> validator.validate("12345")); + assertThrows(FieldException.class, () -> validator.validate("12345")); assertDoesNotThrow(() -> validator.validate("123456")); } @@ -19,9 +20,9 @@ class PasswordValidatorTest { void mediumStrengthRules() { PasswordValidator validator = new PasswordValidator(PasswordStrength.MEDIUM); - assertThrows(IllegalArgumentException.class, () -> validator.validate("abc123")); - assertThrows(IllegalArgumentException.class, () -> validator.validate("abcdefgh")); - assertThrows(IllegalArgumentException.class, () -> validator.validate("12345678")); + assertThrows(FieldException.class, () -> validator.validate("abc123")); + assertThrows(FieldException.class, () -> validator.validate("abcdefgh")); + assertThrows(FieldException.class, () -> validator.validate("12345678")); assertDoesNotThrow(() -> validator.validate("abcd1234")); } @@ -29,11 +30,11 @@ class PasswordValidatorTest { void highStrengthRules() { PasswordValidator validator = new PasswordValidator(PasswordStrength.HIGH); - assertThrows(IllegalArgumentException.class, () -> validator.validate("Abc123$")); - assertThrows(IllegalArgumentException.class, () -> validator.validate("abcd1234$xyz")); - assertThrows(IllegalArgumentException.class, () -> validator.validate("ABCD1234$XYZ")); - assertThrows(IllegalArgumentException.class, () -> validator.validate("AbcdABCDabcd")); - assertThrows(IllegalArgumentException.class, () -> validator.validate("Abcd1234abcd")); + assertThrows(FieldException.class, () -> validator.validate("Abc123$")); + assertThrows(FieldException.class, () -> validator.validate("abcd1234$xyz")); + assertThrows(FieldException.class, () -> validator.validate("ABCD1234$XYZ")); + assertThrows(FieldException.class, () -> validator.validate("AbcdABCDabcd")); + assertThrows(FieldException.class, () -> validator.validate("Abcd1234abcd")); assertDoesNotThrow(() -> validator.validate("Abcd1234$xyz")); } }