Merge pull request #20 from nagisa77/codex/fix-category-creation-in-test

Fix category creation in integration tests
This commit is contained in:
Tim
2025-06-30 23:15:45 +08:00
committed by GitHub

View File

@@ -1,9 +1,8 @@
package com.openisle.integration;
import com.openisle.model.User;
import com.openisle.model.Category;
import com.openisle.model.Role;
import com.openisle.repository.UserRepository;
import com.openisle.repository.CategoryRepository;
import com.openisle.service.EmailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -26,9 +25,6 @@ class ComplexFlowIntegrationTest {
@Autowired
private UserRepository users;
@Autowired
private CategoryRepository categories;
@MockBean
private EmailService emailService;
@@ -45,6 +41,14 @@ class ComplexFlowIntegrationTest {
return (String) resp.getBody().get("token");
}
private String registerAndLoginAsAdmin(String username, String email) {
String token = registerAndLogin(username, email);
User u = users.findByUsername(username).orElseThrow();
u.setRole(Role.ADMIN);
users.save(u);
return token;
}
private ResponseEntity<Map> postJson(String url, Map<?,?> body, String token) {
HttpHeaders h = new HttpHeaders();
h.setContentType(MediaType.APPLICATION_JSON);
@@ -57,12 +61,13 @@ class ComplexFlowIntegrationTest {
String t1 = registerAndLogin("alice", "a@example.com");
String t2 = registerAndLogin("bob", "b@example.com");
Category cat = new Category();
cat.setName("general");
cat = categories.save(cat);
String adminToken = registerAndLoginAsAdmin("admin", "admin@example.com");
ResponseEntity<Map> catResp = postJson("/api/categories",
Map.of("name", "general"), adminToken);
Long catId = ((Number)catResp.getBody().get("id")).longValue();
ResponseEntity<Map> postResp = postJson("/api/posts",
Map.of("title", "Hello", "content", "World", "categoryId", cat.getId()), t1);
Map.of("title", "Hello", "content", "World", "categoryId", catId), t1);
Long postId = ((Number)postResp.getBody().get("id")).longValue();
ResponseEntity<Map> c1Resp = postJson("/api/posts/" + postId + "/comments",
@@ -96,12 +101,13 @@ class ComplexFlowIntegrationTest {
String t1 = registerAndLogin("carol", "c@example.com");
String t2 = registerAndLogin("dave", "d@example.com");
Category cat = new Category();
cat.setName("general");
cat = categories.save(cat);
String adminToken = registerAndLoginAsAdmin("admin2", "admin2@example.com");
ResponseEntity<Map> catResp = postJson("/api/categories",
Map.of("name", "general"), adminToken);
Long catId = ((Number)catResp.getBody().get("id")).longValue();
ResponseEntity<Map> postResp = postJson("/api/posts",
Map.of("title", "React", "content", "Test", "categoryId", cat.getId()), t1);
Map.of("title", "React", "content", "Test", "categoryId", catId), t1);
Long postId = ((Number)postResp.getBody().get("id")).longValue();
postJson("/api/posts/" + postId + "/reactions",