test: 添加StatsHandler和SysAuthHandler单元测试
- StatsHandler: 测试系统统计数据查询功能 - SysAuthHandler: 测试用户登录、注册、登出等认证功能 - 覆盖成功场景、失败场景和边界条件 - 使用Mockito模拟Service层依赖 - 使用StepVerifier验证响应式流
This commit is contained in:
+236
@@ -0,0 +1,236 @@
|
||||
package cn.novalon.manage.sys.handler.auth;
|
||||
|
||||
import cn.novalon.manage.sys.dto.request.LoginRequest;
|
||||
import cn.novalon.manage.sys.dto.request.UserRegisterRequest;
|
||||
import cn.novalon.manage.sys.dto.response.AuthResponse;
|
||||
import cn.novalon.manage.sys.security.JwtTokenProvider;
|
||||
import cn.novalon.manage.sys.core.domain.SysUser;
|
||||
import cn.novalon.manage.sys.core.service.ISysUserService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.reactive.function.server.MockServerRequest;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.web.bind.support.WebExchangeBindException;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SysAuthHandlerTest {
|
||||
|
||||
@Mock
|
||||
private ISysUserService userService;
|
||||
|
||||
@Mock
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Mock
|
||||
private JwtTokenProvider jwtTokenProvider;
|
||||
|
||||
private SysAuthHandler authHandler;
|
||||
private SysUser testUser;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
authHandler = new SysAuthHandler(userService, passwordEncoder, jwtTokenProvider);
|
||||
|
||||
testUser = new SysUser();
|
||||
testUser.setId(1L);
|
||||
testUser.setUsername("testuser");
|
||||
testUser.setPassword("encoded_password");
|
||||
testUser.setEmail("test@example.com");
|
||||
testUser.setStatus(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogin_Success() {
|
||||
LoginRequest loginRequest = new LoginRequest();
|
||||
loginRequest.setUsername("testuser");
|
||||
loginRequest.setPassword("password123");
|
||||
|
||||
when(userService.findByUsername("testuser")).thenReturn(Mono.just(testUser));
|
||||
when(passwordEncoder.matches("password123", "encoded_password")).thenReturn(true);
|
||||
when(jwtTokenProvider.generateToken("testuser", 1L)).thenReturn("test_token");
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(loginRequest));
|
||||
Mono<ServerResponse> response = authHandler.login(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).findByUsername("testuser");
|
||||
verify(passwordEncoder).matches("password123", "encoded_password");
|
||||
verify(jwtTokenProvider).generateToken("testuser", 1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogin_EmptyUsername() {
|
||||
LoginRequest loginRequest = new LoginRequest();
|
||||
loginRequest.setUsername("");
|
||||
loginRequest.setPassword("password123");
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(loginRequest));
|
||||
Mono<ServerResponse> response = authHandler.login(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.BAD_REQUEST)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogin_EmptyPassword() {
|
||||
LoginRequest loginRequest = new LoginRequest();
|
||||
loginRequest.setUsername("testuser");
|
||||
loginRequest.setPassword("");
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(loginRequest));
|
||||
Mono<ServerResponse> response = authHandler.login(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.BAD_REQUEST)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogin_UserNotFound() {
|
||||
LoginRequest loginRequest = new LoginRequest();
|
||||
loginRequest.setUsername("unknown");
|
||||
loginRequest.setPassword("password123");
|
||||
|
||||
when(userService.findByUsername("unknown")).thenReturn(Mono.empty());
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(loginRequest));
|
||||
Mono<ServerResponse> response = authHandler.login(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.UNAUTHORIZED)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).findByUsername("unknown");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogin_WrongPassword() {
|
||||
LoginRequest loginRequest = new LoginRequest();
|
||||
loginRequest.setUsername("testuser");
|
||||
loginRequest.setPassword("wrongpassword");
|
||||
|
||||
when(userService.findByUsername("testuser")).thenReturn(Mono.just(testUser));
|
||||
when(passwordEncoder.matches("wrongpassword", "encoded_password")).thenReturn(false);
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(loginRequest));
|
||||
Mono<ServerResponse> response = authHandler.login(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.UNAUTHORIZED)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).findByUsername("testuser");
|
||||
verify(passwordEncoder).matches("wrongpassword", "encoded_password");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogin_UserDisabled() {
|
||||
testUser.setStatus(0);
|
||||
|
||||
LoginRequest loginRequest = new LoginRequest();
|
||||
loginRequest.setUsername("testuser");
|
||||
loginRequest.setPassword("password123");
|
||||
|
||||
when(userService.findByUsername("testuser")).thenReturn(Mono.just(testUser));
|
||||
when(passwordEncoder.matches("password123", "encoded_password")).thenReturn(true);
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(loginRequest));
|
||||
Mono<ServerResponse> response = authHandler.login(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.UNAUTHORIZED)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).findByUsername("testuser");
|
||||
verify(passwordEncoder).matches("password123", "encoded_password");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRegister_Success() {
|
||||
UserRegisterRequest registerRequest = new UserRegisterRequest();
|
||||
registerRequest.setUsername("newuser");
|
||||
registerRequest.setPassword("password123");
|
||||
registerRequest.setEmail("new@example.com");
|
||||
|
||||
when(userService.findByUsername("newuser")).thenReturn(Mono.empty());
|
||||
when(passwordEncoder.encode("password123")).thenReturn("encoded_password");
|
||||
when(userService.createUser(any(SysUser.class))).thenReturn(Mono.just(testUser));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(registerRequest));
|
||||
Mono<ServerResponse> response = authHandler.register(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.CREATED)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).findByUsername("newuser");
|
||||
verify(passwordEncoder).encode("password123");
|
||||
verify(userService).createUser(any(SysUser.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRegister_UsernameExists() {
|
||||
UserRegisterRequest registerRequest = new UserRegisterRequest();
|
||||
registerRequest.setUsername("testuser");
|
||||
registerRequest.setPassword("password123");
|
||||
registerRequest.setEmail("new@example.com");
|
||||
|
||||
when(userService.findByUsername("testuser")).thenReturn(Mono.just(testUser));
|
||||
when(passwordEncoder.encode("password123")).thenReturn("encoded_password");
|
||||
when(userService.createUser(any(SysUser.class))).thenReturn(Mono.just(testUser));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(registerRequest));
|
||||
Mono<ServerResponse> response = authHandler.register(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectErrorMatches(ex -> ex.getMessage().contains("用户名已存在"))
|
||||
.verify();
|
||||
|
||||
verify(userService).findByUsername("testuser");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogout() {
|
||||
ServerRequest request = MockServerRequest.builder().build();
|
||||
Mono<ServerResponse> response = authHandler.logout(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package cn.novalon.manage.sys.handler.stats;
|
||||
|
||||
import cn.novalon.manage.sys.core.service.ISysUserService;
|
||||
import cn.novalon.manage.sys.core.service.ISysRoleService;
|
||||
import cn.novalon.manage.sys.core.service.IOperationLogService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.reactive.function.server.MockServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class StatsHandlerTest {
|
||||
|
||||
@Mock
|
||||
private ISysUserService userService;
|
||||
|
||||
@Mock
|
||||
private ISysRoleService roleService;
|
||||
|
||||
@Mock
|
||||
private IOperationLogService operationLogService;
|
||||
|
||||
private StatsHandler statsHandler;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
statsHandler = new StatsHandler(userService, roleService, operationLogService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetOverview() {
|
||||
when(userService.count()).thenReturn(Mono.just(100L));
|
||||
when(roleService.count()).thenReturn(Mono.just(10L));
|
||||
when(operationLogService.count()).thenReturn(Mono.just(1000L));
|
||||
when(operationLogService.countToday()).thenReturn(Mono.just(50L));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder().build();
|
||||
Mono<ServerResponse> response = statsHandler.getOverview(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).count();
|
||||
verify(roleService).count();
|
||||
verify(operationLogService).count();
|
||||
verify(operationLogService).countToday();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user