From d12edef2ce5f96962304aec90306882e0346d99a Mon Sep 17 00:00:00 2001 From: chiguyong Date: Tue, 17 Mar 2026 23:30:41 +0800 Subject: [PATCH] =?UTF-8?q?test(auth):=20=E6=B7=BB=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PasswordServiceTest: 5个测试 - UserServiceTest: 8个测试 - 添加测试依赖: spring-boot-starter-test, h2 --- module-auth/pom.xml | 12 ++ .../pms/auth/service/PasswordServiceTest.java | 46 ++++++ .../pms/auth/service/UserServiceTest.java | 136 ++++++++++++++++++ pom.xml | 14 ++ 4 files changed, 208 insertions(+) create mode 100644 module-auth/src/test/java/com/ether/pms/auth/service/PasswordServiceTest.java create mode 100644 module-auth/src/test/java/com/ether/pms/auth/service/UserServiceTest.java diff --git a/module-auth/pom.xml b/module-auth/pom.xml index 74dc828..6b884c8 100644 --- a/module-auth/pom.xml +++ b/module-auth/pom.xml @@ -85,5 +85,17 @@ org.springframework.boot spring-boot-starter-data-redis + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.h2database + h2 + test + diff --git a/module-auth/src/test/java/com/ether/pms/auth/service/PasswordServiceTest.java b/module-auth/src/test/java/com/ether/pms/auth/service/PasswordServiceTest.java new file mode 100644 index 0000000..e355c55 --- /dev/null +++ b/module-auth/src/test/java/com/ether/pms/auth/service/PasswordServiceTest.java @@ -0,0 +1,46 @@ +package com.ether.pms.auth.service; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +class PasswordServiceTest { + + @InjectMocks + private PasswordService passwordService; + + @Test + void encode_shouldReturnNonNullValue() { + String encoded = passwordService.encode("Test123!"); + assertNotNull(encoded); + assertNotEquals("Test123!", encoded); + } + + @Test + void matches_shouldReturnTrue_forCorrectPassword() { + String encoded = passwordService.encode("Test123!"); + assertTrue(passwordService.matches("Test123!", encoded)); + } + + @Test + void matches_shouldReturnFalse_forWrongPassword() { + String encoded = passwordService.encode("Test123!"); + assertFalse(passwordService.matches("Wrong123!", encoded)); + } + + @Test + void isPasswordWeak_shouldReturnTrue_forCommonWeakPasswords() { + assertTrue(passwordService.isPasswordWeak("password123")); + assertTrue(passwordService.isPasswordWeak("admin123")); + assertTrue(passwordService.isPasswordWeak("qwerty123")); + } + + @Test + void isPasswordWeak_shouldReturnFalse_forStrongPasswords() { + assertFalse(passwordService.isPasswordWeak("Str0ng!Pass")); + } +} diff --git a/module-auth/src/test/java/com/ether/pms/auth/service/UserServiceTest.java b/module-auth/src/test/java/com/ether/pms/auth/service/UserServiceTest.java new file mode 100644 index 0000000..2418b30 --- /dev/null +++ b/module-auth/src/test/java/com/ether/pms/auth/service/UserServiceTest.java @@ -0,0 +1,136 @@ +package com.ether.pms.auth.service; + +import com.ether.pms.auth.entity.User; +import com.ether.pms.auth.repository.RoleRepository; +import com.ether.pms.auth.repository.UserRepository; +import com.ether.pms.common.BusinessException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Optional; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class UserServiceTest { + + @Mock + private UserRepository userRepository; + + @Mock + private RoleRepository roleRepository; + + @Mock + private PasswordService passwordService; + + @InjectMocks + private UserService userService; + + @Test + void findById_shouldReturnUser_whenExists() { + UUID userId = UUID.randomUUID(); + User mockUser = new User(); + mockUser.setId(userId); + mockUser.setUsername("testuser"); + + when(userRepository.findById(userId)).thenReturn(Optional.of(mockUser)); + + User result = userService.findById(userId); + + assertNotNull(result); + assertEquals("testuser", result.getUsername()); + } + + @Test + void findById_shouldThrowException_whenNotExists() { + UUID userId = UUID.randomUUID(); + when(userRepository.findById(userId)).thenReturn(Optional.empty()); + + assertThrows(BusinessException.class, () -> userService.findById(userId)); + } + + @Test + void findByUsername_shouldReturnUser_whenExists() { + User mockUser = new User(); + mockUser.setUsername("testuser"); + + when(userRepository.findByUsername("testuser")).thenReturn(Optional.of(mockUser)); + + User result = userService.findByUsername("testuser"); + + assertNotNull(result); + assertEquals("testuser", result.getUsername()); + } + + @Test + void create_shouldThrowException_whenUsernameExists() { + User newUser = new User(); + newUser.setUsername("existinguser"); + newUser.setPassword("Test123!"); + + when(userRepository.existsByUsername("existinguser")).thenReturn(true); + + assertThrows(BusinessException.class, () -> userService.create(newUser)); + } + + @Test + void create_shouldThrowException_whenPasswordTooWeak() { + User newUser = new User(); + newUser.setUsername("newuser"); + newUser.setPassword("weak"); + + when(userRepository.existsByUsername("newuser")).thenReturn(false); + doThrow(new IllegalArgumentException("密码太弱")) + .when(passwordService).validatePassword("weak"); + + assertThrows(BusinessException.class, () -> userService.create(newUser)); + } + + @Test + void create_shouldEncodePassword_whenValid() { + User newUser = new User(); + newUser.setUsername("newuser"); + newUser.setPassword("Valid123!"); + + when(userRepository.existsByUsername("newuser")).thenReturn(false); + doNothing().when(passwordService).validatePassword("Valid123!"); + when(passwordService.isPasswordWeak("Valid123!")).thenReturn(false); + when(passwordService.encode("Valid123!")).thenReturn("encodedPassword"); + when(userRepository.save(any(User.class))).thenReturn(newUser); + + User result = userService.create(newUser); + + assertNotNull(result); + verify(passwordService).encode("Valid123!"); + } + + @Test + void updatePassword_shouldThrowException_whenOldPasswordWrong() { + UUID userId = UUID.randomUUID(); + User existingUser = new User(); + existingUser.setId(userId); + existingUser.setPassword("encodedOld"); + + when(userRepository.findById(userId)).thenReturn(Optional.of(existingUser)); + when(passwordService.matches("wrongOld", "encodedOld")).thenReturn(false); + + assertThrows(BusinessException.class, + () -> userService.updatePassword(userId, "wrongOld", "New123!")); + } + + @Test + void delete_shouldCallRepository() { + UUID userId = UUID.randomUUID(); + doNothing().when(userRepository).deleteById(userId); + + userService.delete(userId); + + verify(userRepository).deleteById(userId); + } +} diff --git a/pom.xml b/pom.xml index 5944112..511fe03 100644 --- a/pom.xml +++ b/pom.xml @@ -101,6 +101,20 @@ redisson-spring-boot-starter ${redisson.version} + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot.version} + test + + + + com.h2database + h2 + 2.2.224 + test +