test: 测试配置内容

This commit is contained in:
bunny 2025-03-25 13:45:15 +08:00
parent 46df9001d4
commit 560ba612d7
6 changed files with 250 additions and 12 deletions

View File

@ -7,11 +7,9 @@ import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
@Getter
@Setter
@Accessors(chain = true)
@Schema(name = "WebConfiguration对象", title = "前端配置选项", description = "前端配置选项")
public class WebConfigurationDto {

View File

@ -63,13 +63,11 @@
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
</dependency>
<!-- &lt;!&ndash; spring-security &ndash;&gt; -->
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-security</artifactId> -->
<!-- </dependency> -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.30.0</version>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
@ -167,6 +165,15 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -10,7 +10,6 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
@Tag(name = "系统配置", description = "系统配置相关接口")
@RestController
@ -35,9 +34,9 @@ public class ConfigurationController {
@Operation(summary = "更新web配置文件", description = "更新web配置文件")
@PutMapping("updateWebConfiguration")
public Mono<Result<String>> updateWebConfiguration(@Valid @RequestBody WebConfigurationDto dto) {
public Result<Object> updateWebConfiguration(@Valid @RequestBody WebConfigurationDto dto) {
configurationService.updateWebConfiguration(dto);
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS));
return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
}
}

View File

@ -0,0 +1,26 @@
package cn.bunny.services.controller;
import org.junit.jupiter.api.Test;
class DeptControllerTest {
@Test
void getDeptList() {
}
@Test
void getAllDeptList() {
}
@Test
void addDept() {
}
@Test
void updateDept() {
}
@Test
void deleteDept() {
}
}

View File

@ -0,0 +1,10 @@
package cn.bunny.services.controller;
import org.junit.jupiter.api.BeforeEach;
class EmailTemplateControllerTest {
@BeforeEach
void setUp() {
}
}

View File

@ -0,0 +1,198 @@
package cn.bunny.services.controller.configuration;
import cn.bunny.dao.dto.system.configuration.WebConfigurationDto;
import cn.bunny.dao.entity.configuration.WebConfiguration;
import cn.bunny.dao.entity.system.AdminUser;
import cn.bunny.dao.vo.result.Result;
import cn.bunny.dao.vo.system.user.LoginVo;
import cn.bunny.services.mapper.UserMapper;
import cn.bunny.services.utils.UserUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@WebAppConfiguration
// @AutoConfigureMockMvc
class ConfigurationControllerTest {
private static final String prefix = "/api/config";
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private UserUtil userUtil;
@Autowired
private UserMapper userMapper;
private String token;
private MockMvc mockMvc;
private ChromeDriver chromeDriver;
@BeforeEach
void setUpMockMvc() {
AdminUser adminUser = userMapper.selectOne(Wrappers.<AdminUser>lambdaQuery().eq(AdminUser::getUsername, "Administrator"));
adminUser.setPassword("admin123");
LoginVo loginVo = userUtil.buildLoginUserVo(adminUser, 7);
token = loginVo.getToken();
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();
chromeDriver = new ChromeDriver();
}
@AfterEach
void tearDown() {
chromeDriver.quit();
}
@Test
// @WithMockUser(username = "Administrator", password = "admin123", roles = "admin")
// @WithUserDetails("Administrator")
void webConfig() throws Exception {
mockMvc.perform(MockMvcRequestBuilders
.get(prefix + "/noAuth/webConfig")
.header("token", token))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(result -> {
MockHttpServletResponse response = result.getResponse();
String contentAsString = response.getContentAsString();
WebConfiguration webConfiguration = JSON.parseObject(contentAsString, WebConfiguration.class);
if (!webConfiguration.getTitle().equals("BunnyAdmin")) {
throw new Exception();
}
System.out.println(webConfiguration);
});
}
@Test
void getWebConfig() throws Exception {
mockMvc.perform(MockMvcRequestBuilders
.request(HttpMethod.GET, prefix + "/getWebConfig")
.header("token", token))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(result -> {
MockHttpServletResponse response = result.getResponse();
String contentAsString = response.getContentAsString();
Result<WebConfiguration> webConfigurationResult = JSON.parseObject(contentAsString, new TypeReference<>() {
});
if (!webConfigurationResult.getCode().equals(200)) {
throw new Exception();
}
if (!webConfigurationResult.getData().getShowModel().equals("smart")) {
throw new Exception();
}
System.out.println(contentAsString);
});
}
@Test
void updateWebConfiguration() throws Exception {
AtomicReference<WebConfigurationDto> webConfigurationDto = new AtomicReference<>();
String testTitle = "修改的之";
// 获取原本的配置信息
mockMvc.perform(MockMvcRequestBuilders
.request(HttpMethod.GET, prefix + "/getWebConfig")
.header("token", token))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(result -> {
MockHttpServletResponse response = result.getResponse();
String contentAsString = response.getContentAsString();
System.out.println(contentAsString);
Result<WebConfiguration> webConfigurationResult = JSON.parseObject(contentAsString, new TypeReference<>() {
});
WebConfigurationDto dto = new WebConfigurationDto();
BeanUtils.copyProperties(webConfigurationResult.getData(), dto);
webConfigurationDto.set(dto);
});
// 修改原本的测试内容
webConfigurationDto.get().setTitle(testTitle);
// 测试修改方法
mockMvc.perform(MockMvcRequestBuilders.put(prefix + "/updateWebConfiguration")
.contentType(MediaType.APPLICATION_JSON)
.content(JSON.toJSONString(webConfigurationDto.get()))
.header("token", token))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(result -> {
String contentAsString = result.getResponse().getContentAsString();
System.out.println(contentAsString);
Result<String> stringResult = JSON.parseObject(contentAsString, new TypeReference<>() {
});
if (!stringResult.getCode().equals(200)) {
throw new Exception();
}
});
// 验证是否修改成功
mockMvc.perform(MockMvcRequestBuilders
.request(HttpMethod.GET, prefix + "/getWebConfig")
.header("token", token))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(result -> {
MockHttpServletResponse response = result.getResponse();
String contentAsString = response.getContentAsString();
Result<WebConfiguration> webConfigurationResult = JSON.parseObject(contentAsString, new TypeReference<>() {
});
if (!webConfigurationResult.getCode().equals(200)) {
throw new Exception();
}
if (!webConfigurationResult.getData().getTitle().equals(testTitle)) {
throw new Exception();
}
System.out.println(contentAsString);
});
}
@Test
void openChrome() throws InterruptedException {
chromeDriver.get("http://localhost:7000/");
TimeUnit.MINUTES.sleep(100);
chromeDriver.manage().timeouts().implicitlyWait(Duration.of(1000L, TimeUnit.SECONDS.toChronoUnit()));
}
}