diff --git a/src/test/java/com/cardiff/HealthCareTestApplication.java b/src/test/java/com/cardiff/HealthCareTestApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..2c9a2947958474f6c147013a0f287c2f6f9a3011 --- /dev/null +++ b/src/test/java/com/cardiff/HealthCareTestApplication.java @@ -0,0 +1,15 @@ +package com.cardiff; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; + +@SpringBootApplication +@EnableWebSecurity +public class HealthCareTestApplication { + + public static void main(String[] args) { + SpringApplication.run(HealthCareTestApplication.class, args); + } + +} diff --git a/src/test/java/com/cardiff/wylTest/HttpTest.java b/src/test/java/com/cardiff/wylTest/HttpTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6338d1124f16e4dec51dcfb44519c57c8ea95496 --- /dev/null +++ b/src/test/java/com/cardiff/wylTest/HttpTest.java @@ -0,0 +1,56 @@ +package com.cardiff.wylTest; + +import com.cardiff.client_project.utils.Result; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; + +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class HttpTest { + @Value(value="${local.server.port}") + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void testFaultPage() throws Exception { + assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/fault.html", + String.class)).contains("You do not have sufficient access rights, the page will be redirected to the login page within"); + } + + + @Test + public void testLoginPage() throws Exception { + assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/login.html", + String.class)).contains("Verification code"); + } + + @Test + public void testLogin() throws Exception { + String loginUrl = "http://localhost:" + port + "/superAdmin/sign"; + + + Map<String, String> loginData = new HashMap<>(); + loginData.put("name", "2649783657@qq.com"); + loginData.put("password", "admin"); + loginData.put("role","super"); + loginData.put("status","1"); + + + + ResponseEntity<Result> loginResponse = this.restTemplate.postForEntity(loginUrl, loginData, Result.class); + assertThat(loginResponse.getStatusCode().value()).isEqualTo(200); + + + } + +} diff --git a/src/test/java/com/cardiff/wylTest/LightweightTest.java b/src/test/java/com/cardiff/wylTest/LightweightTest.java new file mode 100644 index 0000000000000000000000000000000000000000..ae292039007505176a334df4daaa6dde64fdb792 --- /dev/null +++ b/src/test/java/com/cardiff/wylTest/LightweightTest.java @@ -0,0 +1,101 @@ +package com.cardiff.wylTest; + +import com.cardiff.client_project.HealthCareApplication; +import com.cardiff.client_project.config.WebSecurityConfig; +import com.cardiff.client_project.controller.admin.SuperAdminController; +import com.cardiff.client_project.pojo.dto.SelectDTO; +import com.cardiff.client_project.pojo.dto.SignUserDTO; +import com.cardiff.client_project.service.SuperAdminService; +import com.cardiff.client_project.utils.Result; +import org.junit.internal.Classes; +import org.junit.jupiter.api.Named; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Arrays; + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import com.fasterxml.jackson.databind.ObjectMapper; + +import javax.naming.Name; + +@ExtendWith(SpringExtension.class) +@WebMvcTest(SuperAdminController.class) +public class LightweightTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private SuperAdminService superAdminService; + + @Autowired + private ObjectMapper objectMapper; // Jackson 用于 JSON åºåˆ—化 + + /** + * Testing the user registration interface + * private String name; + * private String password; + * private String email; + * private String phone; + * private String address; + * private String type; + * private String role; + * private int status; + */ + @WithMockUser(username = "2649783657@qq.com",password = "admin",roles = "super") + @Test + public void testSignIn() throws Exception { + SignUserDTO signUserDTO = new SignUserDTO(); + signUserDTO.setName("57@qq.com"); + signUserDTO.setPassword("admin"); + signUserDTO.setEmail("57657@qq.com"); + signUserDTO.setPhone("2649783657@qq.com"); + signUserDTO.setAddress("test"); + signUserDTO.setType("admin"); + signUserDTO.setRole("admin"); + signUserDTO.setStatus(1); + + + Result mockResult = Result.error("User registered error"); + + when(superAdminService.insertUserInform(any(SignUserDTO.class))).thenReturn(mockResult); + + mockMvc.perform(post("/superAdmin/sign") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(signUserDTO))) + .andExpect(status().is(403)); + + } + + /** + * Test the paging query interface + */ + @WithMockUser(username = "2649783657@qq.com",password = "admin") + @Test + public void testPageSelect() throws Exception { + Result mockResult = Result.success("Page select success"); + + when(superAdminService.selectPage("admin", 10, 1)).thenReturn(mockResult); + + mockMvc.perform(get("/superAdmin/pageSelect") + .param("type", "admin") + .param("pageSize", "10") + .param("pageNumber", "1")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.msg").value("Page select success")); + + } +}