refactor(backend): 重命名后端项目为 gym-manage-api,修改包名为 cn.novalon.gym.manage
This commit is contained in:
+32
@@ -0,0 +1,32 @@
|
||||
package cn.novalon.gym.manage.app.config;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.codec.multipart.MultipartHttpMessageReader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class MultipartConfigTest {
|
||||
|
||||
private MultipartConfig multipartConfig;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
multipartConfig = new MultipartConfig();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultipartConfig() {
|
||||
assertThat(multipartConfig).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultipartHttpMessageReader() {
|
||||
MultipartHttpMessageReader reader = multipartConfig.multipartHttpMessageReader();
|
||||
|
||||
assertThat(reader).isNotNull();
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package cn.novalon.gym.manage.app.config;
|
||||
|
||||
import io.github.resilience4j.ratelimiter.RateLimiter;
|
||||
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RateLimitConfigTest {
|
||||
|
||||
@Test
|
||||
void testRateLimiterRegistry() throws Exception {
|
||||
RateLimitConfig rateLimitConfig = new RateLimitConfig();
|
||||
|
||||
setField(rateLimitConfig, "limitForPeriod", 100);
|
||||
setField(rateLimitConfig, "limitRefreshPeriod", Duration.ofSeconds(1));
|
||||
setField(rateLimitConfig, "timeoutDuration", Duration.ZERO);
|
||||
|
||||
RateLimiterRegistry registry = rateLimitConfig.rateLimiterRegistry();
|
||||
|
||||
assertThat(registry).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testApiRateLimiter() throws Exception {
|
||||
RateLimitConfig rateLimitConfig = new RateLimitConfig();
|
||||
|
||||
setField(rateLimitConfig, "limitForPeriod", 100);
|
||||
setField(rateLimitConfig, "limitRefreshPeriod", Duration.ofSeconds(1));
|
||||
setField(rateLimitConfig, "timeoutDuration", Duration.ZERO);
|
||||
|
||||
RateLimiterRegistry registry = rateLimitConfig.rateLimiterRegistry();
|
||||
RateLimiter rateLimiter = rateLimitConfig.apiRateLimiter(registry);
|
||||
|
||||
assertThat(rateLimiter).isNotNull();
|
||||
assertThat(rateLimiter.getName()).isEqualTo("apiRateLimiter");
|
||||
}
|
||||
|
||||
private void setField(Object target, String fieldName, Object value) throws Exception {
|
||||
Field field = target.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
field.set(target, value);
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package cn.novalon.gym.manage.app.integration;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* 数据库初始化验证测试
|
||||
*
|
||||
* 注意:此测试需要完整的数据库初始化,暂时禁用。
|
||||
* TODO: 修复数据库初始化问题
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-04-03
|
||||
*/
|
||||
@Disabled("暂时禁用:数据库初始化问题需要修复")
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles("test")
|
||||
class DatabaseInitTest {
|
||||
|
||||
@Autowired
|
||||
private R2dbcEntityTemplate r2dbcEntityTemplate;
|
||||
|
||||
@Test
|
||||
void testSysUserTableExists() {
|
||||
r2dbcEntityTemplate.getDatabaseClient()
|
||||
.sql("SELECT COUNT(*) FROM sys_user")
|
||||
.fetch()
|
||||
.one()
|
||||
.as(StepVerifier::create)
|
||||
.expectNextCount(1)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOperationLogTableExists() {
|
||||
r2dbcEntityTemplate.getDatabaseClient()
|
||||
.sql("SELECT COUNT(*) FROM operation_log")
|
||||
.fetch()
|
||||
.one()
|
||||
.as(StepVerifier::create)
|
||||
.expectNextCount(1)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAllTablesCreated() {
|
||||
r2dbcEntityTemplate.getDatabaseClient()
|
||||
.sql("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'")
|
||||
.fetch()
|
||||
.all()
|
||||
.map(row -> row.get("TABLE_NAME"))
|
||||
.collectList()
|
||||
.as(StepVerifier::create)
|
||||
.assertNext(tables -> {
|
||||
System.out.println("Created tables: " + tables);
|
||||
assert tables.contains("SYS_USER") : "SYS_USER table not found";
|
||||
assert tables.contains("OPERATION_LOG") : "OPERATION_LOG table not found";
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package cn.novalon.gym.manage.app.integration;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
/**
|
||||
* 手动创建表测试
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-04-03
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles("test")
|
||||
class ManualTableCreationTest {
|
||||
|
||||
@Autowired
|
||||
private R2dbcEntityTemplate r2dbcEntityTemplate;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
r2dbcEntityTemplate.getDatabaseClient()
|
||||
.sql("CREATE TABLE IF NOT EXISTS operation_log (" +
|
||||
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
||||
"username VARCHAR(50), " +
|
||||
"operation VARCHAR(100), " +
|
||||
"method VARCHAR(200), " +
|
||||
"params TEXT, " +
|
||||
"result TEXT, " +
|
||||
"ip VARCHAR(50), " +
|
||||
"duration BIGINT, " +
|
||||
"status VARCHAR(1) DEFAULT '0', " +
|
||||
"error_msg TEXT, " +
|
||||
"create_by VARCHAR(50), " +
|
||||
"update_by VARCHAR(50), " +
|
||||
"created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
|
||||
"updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
|
||||
"deleted_at TIMESTAMP)")
|
||||
.then()
|
||||
.as(StepVerifier::create)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOperationLogTableExists() {
|
||||
r2dbcEntityTemplate.getDatabaseClient()
|
||||
.sql("SELECT COUNT(*) FROM operation_log")
|
||||
.fetch()
|
||||
.one()
|
||||
.as(StepVerifier::create)
|
||||
.expectNextCount(1)
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package cn.novalon.gym.manage.app.integration;
|
||||
|
||||
import cn.novalon.gym.manage.app.ManageApplication;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
/**
|
||||
* 操作日志导出功能集成测试
|
||||
*
|
||||
* 注意:此测试存在超时问题,暂时禁用。
|
||||
* TODO: 修复Excel导出的超时问题
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-04-03
|
||||
*/
|
||||
@Disabled("暂时禁用:Excel导出功能存在超时问题,需要优化")
|
||||
@SpringBootTest(
|
||||
classes = ManageApplication.class,
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
|
||||
)
|
||||
@ActiveProfiles("test")
|
||||
class OperationLogExportIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebTestClient webTestClient;
|
||||
|
||||
@Test
|
||||
@WithMockUser(username = "admin", roles = {"ADMIN"})
|
||||
void testExportOperationLogs_ShouldReturnExcelFile() {
|
||||
webTestClient.get()
|
||||
.uri("/api/logs/operation/export")
|
||||
.accept(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectHeader().contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.expectHeader().valueMatches("Content-Disposition", "attachment; filename=\"operation_logs_.*\\.xlsx\"")
|
||||
.expectBody(byte[].class)
|
||||
.value(bytes -> {
|
||||
assert bytes != null;
|
||||
assert bytes.length > 0;
|
||||
assert bytes[0] == 0x50;
|
||||
assert bytes[1] == 0x4B;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser(username = "admin", roles = {"ADMIN"})
|
||||
void testExportOperationLogsWithKeyword_ShouldReturnFilteredExcel() {
|
||||
webTestClient.get()
|
||||
.uri(uriBuilder -> uriBuilder
|
||||
.path("/api/logs/operation/export")
|
||||
.queryParam("keyword", "test")
|
||||
.build())
|
||||
.accept(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectHeader().contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.expectBody(byte[].class)
|
||||
.value(bytes -> {
|
||||
assert bytes != null;
|
||||
assert bytes.length > 0;
|
||||
});
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
package cn.novalon.gym.manage.app.integration;
|
||||
|
||||
import cn.novalon.gym.manage.sys.core.domain.OperationLog;
|
||||
import cn.novalon.gym.manage.sys.core.service.IOperationLogService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 操作日志集成测试
|
||||
*
|
||||
* 注意:此测试需要完整的Spring上下文,暂时禁用。
|
||||
* TODO: 优化集成测试配置
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-04-03
|
||||
*/
|
||||
@Disabled("暂时禁用:集成测试配置需要优化")
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles("test")
|
||||
class OperationLogIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebTestClient webTestClient;
|
||||
|
||||
@Autowired
|
||||
private IOperationLogService logService;
|
||||
|
||||
@Autowired
|
||||
private R2dbcEntityTemplate r2dbcEntityTemplate;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
webTestClient = webTestClient.mutate()
|
||||
.responseTimeout(Duration.ofSeconds(10))
|
||||
.build();
|
||||
|
||||
r2dbcEntityTemplate.getDatabaseClient()
|
||||
.sql("CREATE TABLE IF NOT EXISTS operation_log (" +
|
||||
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
||||
"username VARCHAR(50), " +
|
||||
"operation VARCHAR(100), " +
|
||||
"method VARCHAR(200), " +
|
||||
"params TEXT, " +
|
||||
"result TEXT, " +
|
||||
"ip VARCHAR(50), " +
|
||||
"duration BIGINT, " +
|
||||
"status VARCHAR(1) DEFAULT '0', " +
|
||||
"error_msg TEXT, " +
|
||||
"create_by VARCHAR(50), " +
|
||||
"update_by VARCHAR(50), " +
|
||||
"created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
|
||||
"updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
|
||||
"deleted_at TIMESTAMP)")
|
||||
.then()
|
||||
.as(StepVerifier::create)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser(username = "test_user", roles = {"admin"})
|
||||
void testCreateUserOperation_ShouldLogOperation() {
|
||||
String userJson = """
|
||||
{
|
||||
"username": "test_integration_user",
|
||||
"password": "Test123!@#",
|
||||
"email": "test@example.com",
|
||||
"phone": "13900139000",
|
||||
"nickname": "集成测试用户"
|
||||
}
|
||||
""";
|
||||
|
||||
webTestClient.post()
|
||||
.uri("/api/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(userJson)
|
||||
.exchange()
|
||||
.expectStatus().isCreated()
|
||||
.expectBody()
|
||||
.jsonPath("$.id").exists()
|
||||
.jsonPath("$.username").isEqualTo("test_integration_user");
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser(username = "test_user", roles = {"admin"})
|
||||
void testDeleteUserOperation_ShouldLogOperation() {
|
||||
String userJson = """
|
||||
{
|
||||
"username": "test_delete_user",
|
||||
"password": "Test123!@#",
|
||||
"email": "delete@example.com",
|
||||
"phone": "13900139001",
|
||||
"nickname": "待删除用户"
|
||||
}
|
||||
""";
|
||||
|
||||
webTestClient.post()
|
||||
.uri("/api/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(userJson)
|
||||
.exchange()
|
||||
.expectStatus().isCreated()
|
||||
.expectBody()
|
||||
.jsonPath("$.id").value(id -> {
|
||||
Long userId = Long.valueOf(id.toString());
|
||||
|
||||
webTestClient.delete()
|
||||
.uri("/api/users/{id}", userId)
|
||||
.exchange()
|
||||
.expectStatus().isNoContent();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser(username = "test_user", roles = {"admin"})
|
||||
void testFailedOperation_ShouldLogError() {
|
||||
String userJson = """
|
||||
{
|
||||
"username": "admin",
|
||||
"password": "Test123!@#",
|
||||
"email": "duplicate@example.com",
|
||||
"phone": "13900139002",
|
||||
"nickname": "重复用户"
|
||||
}
|
||||
""";
|
||||
|
||||
webTestClient.post()
|
||||
.uri("/api/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(userJson)
|
||||
.exchange()
|
||||
.expectStatus().isCreated();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindAllOperationLogs_ShouldReturnLogs() {
|
||||
StepVerifier.create(logService.findAll().take(5))
|
||||
.expectNextCount(0)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCountOperationLogs_ShouldReturnCount() {
|
||||
StepVerifier.create(logService.count())
|
||||
.expectNextCount(1)
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
||||
+224
@@ -0,0 +1,224 @@
|
||||
package cn.novalon.gym.manage.app.integration;
|
||||
|
||||
import cn.novalon.gym.manage.common.util.StatusConstants;
|
||||
import cn.novalon.gym.manage.sys.core.domain.SysUser;
|
||||
import cn.novalon.gym.manage.sys.core.domain.SysRole;
|
||||
import cn.novalon.gym.manage.sys.core.domain.UserRole;
|
||||
import cn.novalon.gym.manage.sys.core.repository.ISysUserRepository;
|
||||
import cn.novalon.gym.manage.sys.core.repository.ISysRoleRepository;
|
||||
import cn.novalon.gym.manage.sys.core.repository.IUserRoleRepository;
|
||||
import cn.novalon.gym.manage.sys.core.service.impl.SysUserService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 用户服务集成测试
|
||||
*
|
||||
* 使用PostgreSQL数据库进行集成测试
|
||||
*
|
||||
* 注意:此测试需要完整的Spring上下文,暂时禁用。
|
||||
* TODO: 优化集成测试配置
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-04-02
|
||||
*/
|
||||
@Disabled("暂时禁用:集成测试配置需要优化")
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
class SysUserServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ISysUserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private ISysRoleRepository roleRepository;
|
||||
|
||||
@Autowired
|
||||
private IUserRoleRepository userRoleRepository;
|
||||
|
||||
@Autowired
|
||||
private R2dbcEntityTemplate r2dbcEntityTemplate;
|
||||
|
||||
@Autowired
|
||||
private SysUserService userService;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
r2dbcEntityTemplate.delete(SysUser.class).all().block();
|
||||
r2dbcEntityTemplate.delete(SysRole.class).all().block();
|
||||
r2dbcEntityTemplate.delete(UserRole.class).all().block();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAndFindUser() {
|
||||
SysUser user = new SysUser();
|
||||
user.setUsername("testuser");
|
||||
user.setPassword("password123");
|
||||
user.setEmail("test@example.com");
|
||||
user.setNickname("Test User");
|
||||
user.setPhone("13800138000");
|
||||
|
||||
StepVerifier.create(userService.createUser(user))
|
||||
.expectNextMatches(createdUser -> {
|
||||
assertNotNull(createdUser.getId());
|
||||
assertEquals("testuser", createdUser.getUsername());
|
||||
assertEquals("test@example.com", createdUser.getEmail());
|
||||
assertTrue(createdUser.getPassword().startsWith("$2"));
|
||||
assertEquals(StatusConstants.ENABLED, createdUser.getStatus());
|
||||
return true;
|
||||
})
|
||||
.verifyComplete();
|
||||
|
||||
StepVerifier.create(userService.findByUsername("testuser"))
|
||||
.expectNextMatches(foundUser -> {
|
||||
assertEquals("testuser", foundUser.getUsername());
|
||||
assertEquals("test@example.com", foundUser.getEmail());
|
||||
return true;
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUser() {
|
||||
SysUser user = new SysUser();
|
||||
user.setUsername("updateuser");
|
||||
user.setPassword("password123");
|
||||
user.setEmail("update@example.com");
|
||||
|
||||
SysUser createdUser = userService.createUser(user).block();
|
||||
assertNotNull(createdUser);
|
||||
|
||||
createdUser.setEmail("updated@example.com");
|
||||
createdUser.setNickname("Updated User");
|
||||
|
||||
StepVerifier.create(userService.updateUser(createdUser))
|
||||
.expectNextMatches(updatedUser -> {
|
||||
assertEquals("updated@example.com", updatedUser.getEmail());
|
||||
assertEquals("Updated User", updatedUser.getNickname());
|
||||
return true;
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteUser() {
|
||||
SysUser user = new SysUser();
|
||||
user.setUsername("deleteuser");
|
||||
user.setPassword("password123");
|
||||
user.setEmail("delete@example.com");
|
||||
|
||||
SysUser createdUser = userService.createUser(user).block();
|
||||
assertNotNull(createdUser);
|
||||
|
||||
StepVerifier.create(userService.deleteUser(createdUser.getId()))
|
||||
.verifyComplete();
|
||||
|
||||
StepVerifier.create(userService.findById(createdUser.getId()))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChangePassword() {
|
||||
SysUser user = new SysUser();
|
||||
user.setUsername("pwduser");
|
||||
user.setPassword("oldPassword");
|
||||
user.setEmail("pwd@example.com");
|
||||
|
||||
SysUser createdUser = userService.createUser(user).block();
|
||||
assertNotNull(createdUser);
|
||||
|
||||
StepVerifier.create(userService.changePassword(createdUser.getId(), "oldPassword", "newPassword"))
|
||||
.expectNextMatches(updatedUser -> {
|
||||
assertNotEquals(createdUser.getPassword(), updatedUser.getPassword());
|
||||
assertTrue(passwordEncoder.matches("newPassword", updatedUser.getPassword()));
|
||||
return true;
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignRolesToUser() {
|
||||
SysRole role1 = new SysRole();
|
||||
role1.setRoleName("Test Role 1");
|
||||
role1.setRoleKey("test_role_1");
|
||||
role1.setStatus(1);
|
||||
|
||||
SysRole role2 = new SysRole();
|
||||
role2.setRoleName("Test Role 2");
|
||||
role2.setRoleKey("test_role_2");
|
||||
role2.setStatus(1);
|
||||
|
||||
SysRole createdRole1 = roleRepository.save(role1).block();
|
||||
SysRole createdRole2 = roleRepository.save(role2).block();
|
||||
assertNotNull(createdRole1);
|
||||
assertNotNull(createdRole2);
|
||||
|
||||
SysUser user = new SysUser();
|
||||
user.setUsername("roleuser");
|
||||
user.setPassword("password123");
|
||||
user.setEmail("role@example.com");
|
||||
|
||||
SysUser createdUser = userService.createUser(user).block();
|
||||
assertNotNull(createdUser);
|
||||
|
||||
StepVerifier.create(userService.assignRolesToUser(createdUser.getId(),
|
||||
Arrays.asList(createdRole1.getId(), createdRole2.getId())))
|
||||
.verifyComplete();
|
||||
|
||||
StepVerifier.create(userRoleRepository.findByUserId(createdUser.getId()).collectList())
|
||||
.expectNextMatches(userRoles -> {
|
||||
assertEquals(2, userRoles.size());
|
||||
return true;
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindAllUsers() {
|
||||
for (int i = 1; i <= 3; i++) {
|
||||
SysUser user = new SysUser();
|
||||
user.setUsername("user" + i);
|
||||
user.setPassword("password" + i);
|
||||
user.setEmail("user" + i + "@example.com");
|
||||
userService.createUser(user).block();
|
||||
}
|
||||
|
||||
StepVerifier.create(userService.findAll(false).collectList())
|
||||
.expectNextMatches(users -> {
|
||||
assertEquals(3, users.size());
|
||||
return true;
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExistsByUsername() {
|
||||
SysUser user = new SysUser();
|
||||
user.setUsername("existinguser");
|
||||
user.setPassword("password123");
|
||||
user.setEmail("existing@example.com");
|
||||
userService.createUser(user).block();
|
||||
|
||||
StepVerifier.create(userService.existsByUsername("existinguser"))
|
||||
.expectNext(true)
|
||||
.verifyComplete();
|
||||
|
||||
StepVerifier.create(userService.existsByUsername("nonexistinguser"))
|
||||
.expectNext(false)
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
spring:
|
||||
r2dbc:
|
||||
url: r2dbc:postgresql://localhost:55432/manage_system
|
||||
username: novalon
|
||||
password: novalon123
|
||||
pool:
|
||||
enabled: true
|
||||
initial-size: 2
|
||||
max-size: 10
|
||||
|
||||
flyway:
|
||||
enabled: true
|
||||
locations: classpath:db/migration
|
||||
baseline-on-migrate: true
|
||||
validate-on-migrate: true
|
||||
|
||||
sql:
|
||||
init:
|
||||
mode: never
|
||||
|
||||
security:
|
||||
enabled: false
|
||||
|
||||
jwt:
|
||||
secret: test-secret-key-for-integration-testing
|
||||
expiration: 86400000
|
||||
|
||||
logging:
|
||||
level:
|
||||
cn.novalon.manage: DEBUG
|
||||
org.springframework.r2dbc: DEBUG
|
||||
@@ -0,0 +1,80 @@
|
||||
-- H2数据库测试数据
|
||||
-- 用于测试环境
|
||||
|
||||
-- 插入测试角色
|
||||
INSERT INTO sys_role (id, role_name, role_key, role_sort, status, create_by, update_by)
|
||||
VALUES
|
||||
(1, '超级管理员', 'admin', 1, 1, 'system', 'system'),
|
||||
(2, '测试管理员', 'test_admin', 2, 1, 'system', 'system'),
|
||||
(3, '普通用户', 'normal_user', 3, 1, 'system', 'system'),
|
||||
(4, '访客', 'guest', 4, 1, 'system', 'system');
|
||||
|
||||
-- 插入测试用户
|
||||
-- BCrypt哈希值对应明文密码: Test@123
|
||||
INSERT INTO sys_user (id, username, password, email, phone, nickname, status, create_by, update_by)
|
||||
VALUES
|
||||
(1, 'admin', '$2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C', 'admin@novalon.com', '13800138000', '超级管理员', 1, 'system', 'system'),
|
||||
(2, 'testadmin', '$2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C', 'testadmin@novalon.com', '13800138001', '测试管理员', 1, 'system', 'system'),
|
||||
(3, 'normaluser', '$2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C', 'normaluser@novalon.com', '13800138002', '普通用户', 1, 'system', 'system'),
|
||||
(4, 'guestuser', '$2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C', 'guestuser@novalon.com', '13800138003', '访客用户', 1, 'system', 'system'),
|
||||
(5, 'disableduser', '$2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C', 'disableduser@novalon.com', '13800138004', '禁用用户', 0, 'system', 'system');
|
||||
|
||||
-- 为用户分配角色
|
||||
INSERT INTO user_role (user_id, role_id, created_by)
|
||||
VALUES
|
||||
(1, 1, 'system'),
|
||||
(2, 2, 'system'),
|
||||
(3, 3, 'system'),
|
||||
(4, 4, 'system');
|
||||
|
||||
-- 插入测试菜单
|
||||
INSERT INTO sys_menu (id, menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, created_by, updated_by)
|
||||
VALUES
|
||||
(1, '系统管理', 0, 1, '/system', 'Layout', 'M', '1', '1', '', 'system', 'system', 'system'),
|
||||
(2, '用户管理', 1, 1, 'user', 'system/user/index', 'C', '1', '1', 'system:user:list', 'user', 'system', 'system'),
|
||||
(3, '角色管理', 1, 2, 'role', 'system/role/index', 'C', '1', '1', 'system:role:list', 'role', 'system', 'system'),
|
||||
(4, '菜单管理', 1, 3, 'menu', 'system/menu/index', 'C', '1', '1', 'system:menu:list', 'menu', 'system', 'system'),
|
||||
(5, '测试菜单', 0, 99, '/test', 'Layout', 'M', '1', '1', '', 'test', 'system', 'system'),
|
||||
(6, '用户测试', 5, 1, 'user-test', 'system/user-test/index', 'C', '1', '1', 'system:user:test', 'user', 'system', 'system');
|
||||
|
||||
-- 插入测试权限
|
||||
INSERT INTO sys_permission (id, permission_name, permission_key, permission_type, parent_id, status, created_by, updated_by)
|
||||
VALUES
|
||||
(1, '系统管理', 'system:manage', 'menu', 0, 1, 'system', 'system'),
|
||||
(2, '用户管理', 'system:user:manage', 'menu', 1, 1, 'system', 'system'),
|
||||
(3, '用户查询', 'system:user:list', 'button', 2, 1, 'system', 'system'),
|
||||
(4, '用户新增', 'system:user:add', 'button', 2, 1, 'system', 'system'),
|
||||
(5, '用户编辑', 'system:user:edit', 'button', 2, 1, 'system', 'system'),
|
||||
(6, '用户删除', 'system:user:delete', 'button', 2, 1, 'system', 'system'),
|
||||
(7, '测试权限', 'test:permission', 'menu', 0, 1, 'system', 'system'),
|
||||
(8, '用户测试权限', 'system:user:test', 'button', 7, 1, 'system', 'system');
|
||||
|
||||
-- 为角色分配权限
|
||||
INSERT INTO sys_role_permission (role_id, permission_id, created_by, updated_by)
|
||||
SELECT 1, id, 'system', 'system' FROM sys_permission
|
||||
UNION ALL
|
||||
SELECT 2, id, 'system', 'system' FROM sys_permission WHERE id IN (7, 8);
|
||||
|
||||
-- 插入字典类型
|
||||
INSERT INTO sys_dict_type (id, dict_name, dict_type, status, remark, created_by, updated_by)
|
||||
VALUES
|
||||
(1, '用户状态', 'user_status', '0', '用户状态列表', 'system', 'system'),
|
||||
(2, '菜单状态', 'menu_status', '0', '菜单状态列表', 'system', 'system'),
|
||||
(3, '角色状态', 'role_status', '0', '角色状态列表', 'system', 'system');
|
||||
|
||||
-- 插入字典数据
|
||||
INSERT INTO sys_dict_data (id, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, created_by, updated_by)
|
||||
VALUES
|
||||
(1, 1, '正常', '1', 'user_status', '', 'primary', 'Y', '0', 'system', 'system'),
|
||||
(2, 2, '停用', '0', 'user_status', '', 'danger', 'N', '0', 'system', 'system'),
|
||||
(3, 1, '正常', '0', 'menu_status', '', 'primary', 'Y', '0', 'system', 'system'),
|
||||
(4, 2, '停用', '1', 'menu_status', '', 'danger', 'N', '0', 'system', 'system'),
|
||||
(5, 1, '正常', '0', 'role_status', '', 'primary', 'Y', '0', 'system', 'system'),
|
||||
(6, 2, '停用', '1', 'role_status', '', 'danger', 'N', '0', 'system', 'system');
|
||||
|
||||
-- 插入系统配置
|
||||
INSERT INTO sys_config (id, config_name, config_key, config_value, config_type, remark, created_by, updated_by)
|
||||
VALUES
|
||||
(1, '用户管理-用户初始密码', 'sys.user.initPassword', '123456', 'Y', '初始化用户密码', 'system', 'system'),
|
||||
(2, '主框架页-默认皮肤样式名称', 'sys.index.skinName', 'skin-blue', 'Y', '默认皮肤', 'system', 'system'),
|
||||
(3, '用户自助-验证码开关', 'sys.account.captchaEnabled', 'true', 'Y', '是否开启验证码功能', 'system', 'system');
|
||||
@@ -0,0 +1,76 @@
|
||||
-- H2数据库Schema for Integration Testing
|
||||
-- 创建用户表
|
||||
CREATE TABLE IF NOT EXISTS sys_user (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(50) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(100),
|
||||
phone VARCHAR(20),
|
||||
nickname VARCHAR(100),
|
||||
role_id BIGINT,
|
||||
status INTEGER DEFAULT 1,
|
||||
create_by VARCHAR(50),
|
||||
update_by VARCHAR(50),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP
|
||||
);
|
||||
|
||||
-- 创建角色表
|
||||
CREATE TABLE IF NOT EXISTS sys_role (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
role_name VARCHAR(100) NOT NULL,
|
||||
role_key VARCHAR(100) NOT NULL UNIQUE,
|
||||
role_sort INTEGER DEFAULT 0,
|
||||
status INTEGER DEFAULT 1,
|
||||
create_by VARCHAR(50),
|
||||
update_by VARCHAR(50),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP
|
||||
);
|
||||
|
||||
-- 创建用户角色关联表
|
||||
CREATE TABLE IF NOT EXISTS user_role (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
role_id BIGINT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
created_by VARCHAR(50),
|
||||
CONSTRAINT fk_user_role_user FOREIGN KEY (user_id) REFERENCES sys_user(id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_user_role_role FOREIGN KEY (role_id) REFERENCES sys_role(id) ON DELETE CASCADE,
|
||||
CONSTRAINT uk_user_role UNIQUE (user_id, role_id)
|
||||
);
|
||||
|
||||
-- 创建索引
|
||||
CREATE INDEX IF NOT EXISTS idx_user_role_user_id ON user_role(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_role_role_id ON user_role(role_id);
|
||||
|
||||
-- 创建审计日志表
|
||||
CREATE TABLE IF NOT EXISTS audit_log (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
entity_type VARCHAR(100) NOT NULL,
|
||||
entity_id BIGINT,
|
||||
operation_type VARCHAR(20) NOT NULL,
|
||||
operator VARCHAR(100),
|
||||
operation_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
before_data CLOB,
|
||||
after_data CLOB,
|
||||
changed_fields CLOB,
|
||||
ip_address VARCHAR(50),
|
||||
user_agent CLOB,
|
||||
description CLOB,
|
||||
create_by VARCHAR(50),
|
||||
update_by VARCHAR(50),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP
|
||||
);
|
||||
|
||||
-- 创建审计日志索引
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_log_entity_type ON audit_log(entity_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_log_entity_id ON audit_log(entity_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_log_operation_type ON audit_log(operation_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_log_operator ON audit_log(operator);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_log_operation_time ON audit_log(operation_time);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_log_entity ON audit_log(entity_type, entity_id);
|
||||
Reference in New Issue
Block a user