|
|
|
|
@ -6,13 +6,18 @@ import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
|
import org.springframework.test.web.servlet.request.RequestPostProcessor;
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
|
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
|
|
|
|
|
|
@ -86,6 +91,87 @@ class TeacherExperimentReportControllerTest {
|
|
|
|
|
mockMvc.perform(get("/api/v1/experiment-reports/teacher")).andExpect(status().isUnauthorized());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void scoresUnscoredReportAndPersistsComment() throws Exception {
|
|
|
|
|
String body = "{\"id\":\"r-b\",\"score\":92.50,\"comment\":\"报告结构完整,分析到位\"}";
|
|
|
|
|
mockMvc.perform(post("/api/v1/experiment-reports/teacher/score")
|
|
|
|
|
.contentType(MediaType.APPLICATION_JSON).content(body).with(teacher()))
|
|
|
|
|
.andExpect(status().isOk())
|
|
|
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
|
|
|
.andExpect(jsonPath("$.data.id").value("r-b"))
|
|
|
|
|
.andExpect(jsonPath("$.data.teacherScore").value(92.50))
|
|
|
|
|
.andExpect(jsonPath("$.data.teacherComment").value("报告结构完整,分析到位"));
|
|
|
|
|
|
|
|
|
|
BigDecimal dbScore = jdbc.queryForObject(
|
|
|
|
|
"SELECT teacher_score FROM experiment_report WHERE id='r-b'", BigDecimal.class);
|
|
|
|
|
String dbComment = jdbc.queryForObject(
|
|
|
|
|
"SELECT teacher_comment FROM experiment_report WHERE id='r-b'", String.class);
|
|
|
|
|
assertEquals(0, new BigDecimal("92.50").compareTo(dbScore));
|
|
|
|
|
assertEquals("报告结构完整,分析到位", dbComment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void overwritesExistingScore() throws Exception {
|
|
|
|
|
// r-a 种子已带 teacher_score=85.00,评分应覆盖而非拦截
|
|
|
|
|
String body = "{\"id\":\"r-a\",\"score\":78.00,\"comment\":\"重评\"}";
|
|
|
|
|
mockMvc.perform(post("/api/v1/experiment-reports/teacher/score")
|
|
|
|
|
.contentType(MediaType.APPLICATION_JSON).content(body).with(teacher()))
|
|
|
|
|
.andExpect(status().isOk())
|
|
|
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
|
|
|
.andExpect(jsonPath("$.data.teacherScore").value(78.00))
|
|
|
|
|
.andExpect(jsonPath("$.data.teacherComment").value("重评"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void returns404WhenReportMissing() throws Exception {
|
|
|
|
|
String body = "{\"id\":\"r-missing\",\"score\":90.00}";
|
|
|
|
|
mockMvc.perform(post("/api/v1/experiment-reports/teacher/score")
|
|
|
|
|
.contentType(MediaType.APPLICATION_JSON).content(body).with(teacher()))
|
|
|
|
|
.andExpect(status().isNotFound())
|
|
|
|
|
.andExpect(jsonPath("$.code").value(404));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void returns403WhenReportBelongsToOtherSchool() throws Exception {
|
|
|
|
|
jdbc.update("INSERT INTO experiment_report (id,report_name,file_url,file_size,upload_time,teacher_score,"
|
|
|
|
|
+ "user_id,school_id,class_id,created_at,updated_at,deleted) "
|
|
|
|
|
+ "VALUES ('r-x','报告X.pdf','/x.pdf',1024,CURRENT_TIMESTAMP,NULL,?,?,?,"
|
|
|
|
|
+ "CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,FALSE)", STUDENT_B, "9999", "2072");
|
|
|
|
|
String body = "{\"id\":\"r-x\",\"score\":90.00}";
|
|
|
|
|
mockMvc.perform(post("/api/v1/experiment-reports/teacher/score")
|
|
|
|
|
.contentType(MediaType.APPLICATION_JSON).content(body).with(teacher()))
|
|
|
|
|
.andExpect(status().isForbidden())
|
|
|
|
|
.andExpect(jsonPath("$.code").value(403));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void rejectsScoreOutOfRange() throws Exception {
|
|
|
|
|
String body = "{\"id\":\"r-b\",\"score\":150.00}";
|
|
|
|
|
mockMvc.perform(post("/api/v1/experiment-reports/teacher/score")
|
|
|
|
|
.contentType(MediaType.APPLICATION_JSON).content(body).with(teacher()))
|
|
|
|
|
.andExpect(status().isBadRequest())
|
|
|
|
|
.andExpect(jsonPath("$.code").value(400));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void rejectsCommentTooLong() throws Exception {
|
|
|
|
|
String longComment = new String(new char[501]).replace('\0', 'x');
|
|
|
|
|
String body = "{\"id\":\"r-b\",\"score\":90.00,\"comment\":\"" + longComment + "\"}";
|
|
|
|
|
mockMvc.perform(post("/api/v1/experiment-reports/teacher/score")
|
|
|
|
|
.contentType(MediaType.APPLICATION_JSON).content(body).with(teacher()))
|
|
|
|
|
.andExpect(status().isBadRequest())
|
|
|
|
|
.andExpect(jsonPath("$.code").value(400));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void rejectsBlankId() throws Exception {
|
|
|
|
|
String body = "{\"id\":\"\",\"score\":90.00}";
|
|
|
|
|
mockMvc.perform(post("/api/v1/experiment-reports/teacher/score")
|
|
|
|
|
.contentType(MediaType.APPLICATION_JSON).content(body).with(teacher()))
|
|
|
|
|
.andExpect(status().isBadRequest())
|
|
|
|
|
.andExpect(jsonPath("$.code").value(400));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RequestPostProcessor teacher() {
|
|
|
|
|
return jwt().jwt(token -> token.subject(TEACHER).claim("userId", TEACHER)
|
|
|
|
|
.claim("preferred_username", "tea-1"));
|
|
|
|
|
|