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 )) 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 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)); diff --git a/src/test/java/com/openisle/integration/PublishModeIntegrationTest.java b/src/test/java/com/openisle/integration/PublishModeIntegrationTest.java index 81ae82d11..d7755f6c0 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"); 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")); } }