package com.example.rzgb; import com.example.rzgb.common.Json; import java.util.*; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.TestPropertySource; import org.springframework.test.web.servlet.*; import static org.assertj.core.api.Assertions.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; /** 使用隔离测试数据库验证原型手动添加入口的持久化、重试、输入校验和附件归属。 */ @SpringBootTest @AutoConfigureMockMvc @ActiveProfiles("test") @TestPropertySource(properties="spring.datasource.url=jdbc:h2:mem:manual-watch;MODE=MySQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1") class ManualWatchTests { @Autowired MockMvc mvc; @Autowired Json json; @Autowired JdbcTemplate jdbc; private Map request() { return new LinkedHashMap<>(Map.of("commandId",UUID.randomUUID().toString(),"title","手动项目验收", "url","https://example.com/notice/manual","purchaser","用户填写采购方","region","浙江","budgetWan",123.45)); } private ResultActions create(Map body)throws Exception { return mvc.perform(post("/api/watch/manual").with(user("admin")).with(csrf()) .contentType("application/json").content(json.write(body))); } @Test void savesOnceAndRejectsChangedRetryWithoutInventingDatesOrScores()throws Exception { var body=request(); String response=create(body).andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); String id=json.read(response).path("projectId").asText(); assertThat(json.read(create(body).andExpect(status().isOk()).andReturn().getResponse().getContentAsString()).path("projectId").asText()).isEqualTo(id); assertThat(jdbc.queryForObject("SELECT COUNT(*) FROM watch_record WHERE project_id=?",Integer.class,id)).isEqualTo(1); var raw=json.read(jdbc.queryForObject("SELECT document FROM project WHERE id=?",String.class,id)); assertThat(raw.path("money_wan").decimalValue()).isEqualByComparingTo("123.45"); assertThat(raw.has("pub_time")).isFalse(); assertThat(raw.has("score")).isFalse(); body.put("title","变化后的标题"); create(body).andExpect(status().isConflict()); String watch=mvc.perform(get("/api/watch").with(user("admin"))).andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); assertThat(watch).contains(id,"用户手动添加,尚未经公告原文交叉核验"); } @Test void rejectsMissingSourceUnsafeUrlNegativeBudgetAndMissingCsrf()throws Exception { var body=request(); body.put("url",""); create(body).andExpect(status().isBadRequest()); body.put("url","javascript:alert(1)"); create(body).andExpect(status().isBadRequest()); body.put("url","https://example.com"); body.put("budgetWan",-1); create(body).andExpect(status().isBadRequest()); mvc.perform(post("/api/watch/manual").with(user("admin")).contentType("application/json").content(json.write(request()))) .andExpect(status().isForbidden()); } @Test void acceptsOwnedFileAndRefusesAnotherAccountsFileOrManualProject()throws Exception { var upload=mvc.perform(multipart("/api/files").file(new MockMultipartFile("file","manual.txt","text/plain","采购内容:水质监测设备".getBytes(java.nio.charset.StandardCharsets.UTF_8))) .param("purpose","BID_PROJECT").with(user("admin")).with(csrf())).andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); String file=json.read(upload).path("id").asText(); var body=request();body.put("url","");body.put("fileId",file); String id=json.read(create(body).andExpect(status().isOk()).andReturn().getResponse().getContentAsString()).path("projectId").asText(); String username="manual-"+UUID.randomUUID(); jdbc.update("INSERT INTO app_user(id,username,password_hash) VALUES(?,?,?)",UUID.randomUUID().toString(),username,"unused-test-password"); body.put("commandId",UUID.randomUUID().toString()); mvc.perform(post("/api/watch/manual").with(user(username)).with(csrf()).contentType("application/json").content(json.write(body))).andExpect(status().isNotFound()); mvc.perform(post("/api/watch/"+id).with(user(username)).with(csrf())).andExpect(status().isNotFound()); mvc.perform(get("/api/watch").with(user("admin"))).andExpect(status().isOk()).andExpect(content().string(org.hamcrest.Matchers.containsString(file))); } }