Commit c41d712d9994f0334b784fb4e15e8a0044a275a7
Committed by
Gitee
!25 Auth 单元测试
Merge pull request !25 from neilz/feature/ut-auth
Showing
2 changed files
with
440 additions
and
124 deletions
src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java
| 1 | -package cn.iocoder.dashboard.modules.system.service.auth; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.dashboard.BaseDbUnitTest; | ||
| 4 | -import cn.iocoder.dashboard.common.enums.CommonStatusEnum; | ||
| 5 | -import cn.iocoder.dashboard.framework.security.core.LoginUser; | ||
| 6 | -import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; | ||
| 7 | -import cn.iocoder.dashboard.modules.system.service.auth.impl.SysAuthServiceImpl; | ||
| 8 | -import cn.iocoder.dashboard.modules.system.service.common.SysCaptchaService; | ||
| 9 | -import cn.iocoder.dashboard.modules.system.service.logger.SysLoginLogService; | ||
| 10 | -import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; | ||
| 11 | -import cn.iocoder.dashboard.modules.system.service.user.SysUserService; | ||
| 12 | -import cn.iocoder.dashboard.util.AssertUtils; | ||
| 13 | -import org.junit.jupiter.api.Test; | ||
| 14 | -import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 15 | -import org.springframework.context.annotation.Import; | ||
| 16 | -import org.springframework.security.authentication.AuthenticationManager; | ||
| 17 | -import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
| 18 | - | ||
| 19 | -import javax.annotation.Resource; | ||
| 20 | -import java.util.Set; | ||
| 21 | - | ||
| 22 | -import static cn.iocoder.dashboard.util.RandomUtils.*; | ||
| 23 | -import static java.util.Collections.singleton; | ||
| 24 | -import static org.junit.jupiter.api.Assertions.*; | ||
| 25 | -import static org.mockito.ArgumentMatchers.eq; | ||
| 26 | -import static org.mockito.Mockito.when; | ||
| 27 | - | ||
| 28 | -/** | ||
| 29 | - * {@link SysAuthServiceImpl} 的单元测试 | ||
| 30 | - * | ||
| 31 | - * @author 芋道源码 | ||
| 32 | - */ | ||
| 33 | -@Import(SysAuthServiceImpl.class) | ||
| 34 | -public class SysAuthServiceImplTest extends BaseDbUnitTest { | ||
| 35 | - | ||
| 36 | - @Resource | ||
| 37 | - private SysAuthServiceImpl authService; | ||
| 38 | - | ||
| 39 | - @MockBean | ||
| 40 | - private SysUserService userService; | ||
| 41 | - @MockBean | ||
| 42 | - private SysPermissionService permissionService; | ||
| 43 | - @MockBean | ||
| 44 | - private AuthenticationManager authenticationManager; | ||
| 45 | - @MockBean | ||
| 46 | - private SysCaptchaService captchaService; | ||
| 47 | - @MockBean | ||
| 48 | - private SysLoginLogService loginLogService; | ||
| 49 | - @MockBean | ||
| 50 | - private SysUserSessionService userSessionService; | ||
| 51 | - | ||
| 52 | - @Test | ||
| 53 | - public void testLoadUserByUsername_success() { | ||
| 54 | - // 准备参数 | ||
| 55 | - String username = randomString(); | ||
| 56 | - // mock 方法 | ||
| 57 | - SysUserDO user = randomUserDO(o -> o.setUsername(username)); | ||
| 58 | - when(userService.getUserByUsername(eq(username))).thenReturn(user); | ||
| 59 | - | ||
| 60 | - // 调用 | ||
| 61 | - LoginUser loginUser = (LoginUser) authService.loadUserByUsername(username); | ||
| 62 | - // 校验 | ||
| 63 | - AssertUtils.assertPojoEquals(user, loginUser, "updateTime"); | ||
| 64 | - assertNull(loginUser.getRoleIds()); // 此时不会加载角色,所以是空的 | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - @Test | ||
| 68 | - public void testLoadUserByUsername_userNotFound() { | ||
| 69 | - // 准备参数 | ||
| 70 | - String username = randomString(); | ||
| 71 | - // mock 方法 | ||
| 72 | - | ||
| 73 | - // 调用, 并断言异常 | ||
| 74 | - assertThrows(UsernameNotFoundException.class, // 抛出 UsernameNotFoundException 异常 | ||
| 75 | - () -> authService.loadUserByUsername(username), | ||
| 76 | - username); // 异常提示为 username | ||
| 77 | - } | ||
| 78 | - | ||
| 79 | - @Test | ||
| 80 | - public void testMockLogin_success() { | ||
| 81 | - // 准备参数 | ||
| 82 | - Long userId = randomLongId(); | ||
| 83 | - // mock 方法 01 | ||
| 84 | - SysUserDO user = randomUserDO(o -> o.setId(userId)); | ||
| 85 | - when(userService.getUser(eq(userId))).thenReturn(user); | ||
| 86 | - // mock 方法 02 | ||
| 87 | - Set<Long> roleIds = randomSet(Long.class); | ||
| 88 | - when(permissionService.getUserRoleIds(eq(userId), eq(singleton(CommonStatusEnum.ENABLE.getStatus())))) | ||
| 89 | - .thenReturn(roleIds); | ||
| 90 | - | ||
| 91 | - // 调用 | ||
| 92 | - LoginUser loginUser = authService.mockLogin(userId); | ||
| 93 | - // 断言 | ||
| 94 | - AssertUtils.assertPojoEquals(user, loginUser, "updateTime"); | ||
| 95 | - assertEquals(roleIds, loginUser.getRoleIds()); | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - @Test | ||
| 99 | - public void testMockLogin_userNotFound() { | ||
| 100 | - // 准备参数 | ||
| 101 | - Long userId = randomLongId(); | ||
| 102 | - // mock 方法 | ||
| 103 | - | ||
| 104 | - // 调用, 并断言异常 | ||
| 105 | - assertThrows(UsernameNotFoundException.class, // 抛出 UsernameNotFoundException 异常 | ||
| 106 | - () -> authService.mockLogin(userId), | ||
| 107 | - String.valueOf(userId)); // 异常提示为 userId | ||
| 108 | - } | ||
| 109 | - | ||
| 110 | -} | 1 | +package cn.iocoder.dashboard.modules.system.service.auth; |
| 2 | + | ||
| 3 | +import cn.iocoder.dashboard.BaseDbUnitTest; | ||
| 4 | +import cn.iocoder.dashboard.common.enums.CommonStatusEnum; | ||
| 5 | +import cn.iocoder.dashboard.framework.security.core.LoginUser; | ||
| 6 | +import cn.iocoder.dashboard.modules.system.controller.auth.vo.auth.SysAuthLoginReqVO; | ||
| 7 | +import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; | ||
| 8 | +import cn.iocoder.dashboard.modules.system.enums.logger.SysLoginLogTypeEnum; | ||
| 9 | +import cn.iocoder.dashboard.modules.system.enums.logger.SysLoginResultEnum; | ||
| 10 | +import cn.iocoder.dashboard.modules.system.service.auth.impl.SysAuthServiceImpl; | ||
| 11 | +import cn.iocoder.dashboard.modules.system.service.common.SysCaptchaService; | ||
| 12 | +import cn.iocoder.dashboard.modules.system.service.logger.SysLoginLogService; | ||
| 13 | +import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; | ||
| 14 | +import cn.iocoder.dashboard.modules.system.service.user.SysUserService; | ||
| 15 | +import cn.iocoder.dashboard.util.AssertUtils; | ||
| 16 | +import org.junit.jupiter.api.Test; | ||
| 17 | +import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 18 | +import org.springframework.context.annotation.Import; | ||
| 19 | +import org.springframework.security.authentication.AuthenticationManager; | ||
| 20 | +import org.springframework.security.authentication.BadCredentialsException; | ||
| 21 | +import org.springframework.security.authentication.DisabledException; | ||
| 22 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| 23 | +import org.springframework.security.core.Authentication; | ||
| 24 | +import org.springframework.security.core.AuthenticationException; | ||
| 25 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
| 26 | + | ||
| 27 | +import javax.annotation.Resource; | ||
| 28 | +import java.util.Set; | ||
| 29 | + | ||
| 30 | +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; | ||
| 31 | +import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; | ||
| 32 | +import static cn.iocoder.dashboard.util.RandomUtils.*; | ||
| 33 | +import static java.util.Collections.singleton; | ||
| 34 | +import static org.junit.jupiter.api.Assertions.*; | ||
| 35 | +import static org.mockito.ArgumentMatchers.eq; | ||
| 36 | +import static org.mockito.Mockito.*; | ||
| 37 | + | ||
| 38 | +/** | ||
| 39 | + * {@link SysAuthServiceImpl} 的单元测试 | ||
| 40 | + * | ||
| 41 | + * @author 芋道源码 | ||
| 42 | + */ | ||
| 43 | +@Import(SysAuthServiceImpl.class) | ||
| 44 | +public class SysAuthServiceImplTest extends BaseDbUnitTest { | ||
| 45 | + | ||
| 46 | + @Resource | ||
| 47 | + private SysAuthServiceImpl authService; | ||
| 48 | + | ||
| 49 | + @MockBean | ||
| 50 | + private SysUserService userService; | ||
| 51 | + @MockBean | ||
| 52 | + private SysPermissionService permissionService; | ||
| 53 | + @MockBean | ||
| 54 | + private AuthenticationManager authenticationManager; | ||
| 55 | + @MockBean | ||
| 56 | + private Authentication authentication; | ||
| 57 | + @MockBean | ||
| 58 | + private SysCaptchaService captchaService; | ||
| 59 | + @MockBean | ||
| 60 | + private SysLoginLogService loginLogService; | ||
| 61 | + @MockBean | ||
| 62 | + private SysUserSessionService userSessionService; | ||
| 63 | + | ||
| 64 | + @Test | ||
| 65 | + public void testLoadUserByUsername_success() { | ||
| 66 | + // 准备参数 | ||
| 67 | + String username = randomString(); | ||
| 68 | + // mock 方法 | ||
| 69 | + SysUserDO user = randomUserDO(o -> o.setUsername(username)); | ||
| 70 | + when(userService.getUserByUsername(eq(username))).thenReturn(user); | ||
| 71 | + | ||
| 72 | + // 调用 | ||
| 73 | + LoginUser loginUser = (LoginUser) authService.loadUserByUsername(username); | ||
| 74 | + // 校验 | ||
| 75 | + AssertUtils.assertPojoEquals(user, loginUser, "updateTime"); | ||
| 76 | + assertNull(loginUser.getRoleIds()); // 此时不会加载角色,所以是空的 | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + @Test | ||
| 80 | + public void testLoadUserByUsername_userNotFound() { | ||
| 81 | + // 准备参数 | ||
| 82 | + String username = randomString(); | ||
| 83 | + // mock 方法 | ||
| 84 | + | ||
| 85 | + // 调用, 并断言异常 | ||
| 86 | + assertThrows(UsernameNotFoundException.class, // 抛出 UsernameNotFoundException 异常 | ||
| 87 | + () -> authService.loadUserByUsername(username), | ||
| 88 | + username); // 异常提示为 username | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + @Test | ||
| 92 | + public void testMockLogin_success() { | ||
| 93 | + // 准备参数 | ||
| 94 | + Long userId = randomLongId(); | ||
| 95 | + // mock 方法 01 | ||
| 96 | + SysUserDO user = randomUserDO(o -> o.setId(userId)); | ||
| 97 | + when(userService.getUser(eq(userId))).thenReturn(user); | ||
| 98 | + // mock 方法 02 | ||
| 99 | + Set<Long> roleIds = randomSet(Long.class); | ||
| 100 | + when(permissionService.getUserRoleIds(eq(userId), eq(singleton(CommonStatusEnum.ENABLE.getStatus())))) | ||
| 101 | + .thenReturn(roleIds); | ||
| 102 | + | ||
| 103 | + // 调用 | ||
| 104 | + LoginUser loginUser = authService.mockLogin(userId); | ||
| 105 | + // 断言 | ||
| 106 | + AssertUtils.assertPojoEquals(user, loginUser, "updateTime"); | ||
| 107 | + assertEquals(roleIds, loginUser.getRoleIds()); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + @Test | ||
| 111 | + public void testMockLogin_userNotFound() { | ||
| 112 | + // 准备参数 | ||
| 113 | + Long userId = randomLongId(); | ||
| 114 | + // mock 方法 | ||
| 115 | + | ||
| 116 | + // 调用, 并断言异常 | ||
| 117 | + assertThrows(UsernameNotFoundException.class, // 抛出 UsernameNotFoundException 异常 | ||
| 118 | + () -> authService.mockLogin(userId), | ||
| 119 | + String.valueOf(userId)); // 异常提示为 userId | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + @Test | ||
| 123 | + public void testLogin_captchaNotFound() { | ||
| 124 | + // 准备参数 | ||
| 125 | + SysAuthLoginReqVO reqVO = randomPojo(SysAuthLoginReqVO.class); | ||
| 126 | + String userIp = randomString(); | ||
| 127 | + String userAgent = randomString(); | ||
| 128 | + // 调用, 并断言异常 | ||
| 129 | + assertServiceException(() -> authService.login(reqVO, userIp, userAgent), AUTH_LOGIN_CAPTCHA_NOT_FOUND); | ||
| 130 | + // 校验调用参数 | ||
| 131 | + verify(loginLogService, times(1)).createLoginLog( | ||
| 132 | + argThat(o -> o.getLogType().equals(SysLoginLogTypeEnum.LOGIN_USERNAME.getType()) | ||
| 133 | + && o.getResult().equals(SysLoginResultEnum.CAPTCHA_NOT_FOUND.getResult())) | ||
| 134 | + ); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + @Test | ||
| 138 | + public void testLogin_captchaCodeError() { | ||
| 139 | + // 准备参数 | ||
| 140 | + String userIp = randomString(); | ||
| 141 | + String userAgent = randomString(); | ||
| 142 | + String code = randomString(); | ||
| 143 | + SysAuthLoginReqVO reqVO = randomPojo(SysAuthLoginReqVO.class); | ||
| 144 | + // mock 验证码不正确 | ||
| 145 | + when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(code); | ||
| 146 | + // 调用, 并断言异常 | ||
| 147 | + assertServiceException(() -> authService.login(reqVO, userIp, userAgent), AUTH_LOGIN_CAPTCHA_CODE_ERROR); | ||
| 148 | + // 校验调用参数 | ||
| 149 | + verify(loginLogService, times(1)).createLoginLog( | ||
| 150 | + argThat(o -> o.getLogType().equals(SysLoginLogTypeEnum.LOGIN_USERNAME.getType()) | ||
| 151 | + && o.getResult().equals(SysLoginResultEnum.CAPTCHA_CODE_ERROR.getResult())) | ||
| 152 | + ); | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + @Test | ||
| 156 | + public void testLogin_badCredentials() { | ||
| 157 | + // 准备参数 | ||
| 158 | + String userIp = randomString(); | ||
| 159 | + String userAgent = randomString(); | ||
| 160 | + SysAuthLoginReqVO reqVO = randomPojo(SysAuthLoginReqVO.class); | ||
| 161 | + // mock 验证码正确 | ||
| 162 | + when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(reqVO.getCode()); | ||
| 163 | + // mock 抛出异常 | ||
| 164 | + when(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(reqVO.getUsername(), reqVO.getPassword()))) | ||
| 165 | + .thenThrow(new BadCredentialsException("测试账号或密码不正确")); | ||
| 166 | + // 调用, 并断言异常 | ||
| 167 | + assertServiceException(() -> authService.login(reqVO, userIp, userAgent), AUTH_LOGIN_BAD_CREDENTIALS); | ||
| 168 | + // 校验调用参数 | ||
| 169 | + verify(captchaService, times(1)).deleteCaptchaCode(reqVO.getUuid()); | ||
| 170 | + verify(loginLogService, times(1)).createLoginLog( | ||
| 171 | + argThat(o -> o.getLogType().equals(SysLoginLogTypeEnum.LOGIN_USERNAME.getType()) | ||
| 172 | + && o.getResult().equals(SysLoginResultEnum.BAD_CREDENTIALS.getResult())) | ||
| 173 | + ); | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + @Test | ||
| 177 | + public void testLogin_userDisabled() { | ||
| 178 | + // 准备参数 | ||
| 179 | + String userIp = randomString(); | ||
| 180 | + String userAgent = randomString(); | ||
| 181 | + SysAuthLoginReqVO reqVO = randomPojo(SysAuthLoginReqVO.class); | ||
| 182 | + // mock 验证码正确 | ||
| 183 | + when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(reqVO.getCode()); | ||
| 184 | + // mock 抛出异常 | ||
| 185 | + when(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(reqVO.getUsername(), reqVO.getPassword()))) | ||
| 186 | + .thenThrow(new DisabledException("测试用户被禁用")); | ||
| 187 | + // 调用, 并断言异常 | ||
| 188 | + assertServiceException(() -> authService.login(reqVO, userIp, userAgent), AUTH_LOGIN_USER_DISABLED); | ||
| 189 | + // 校验调用参数 | ||
| 190 | + verify(captchaService, times(1)).deleteCaptchaCode(reqVO.getUuid()); | ||
| 191 | + verify(loginLogService, times(1)).createLoginLog( | ||
| 192 | + argThat(o -> o.getLogType().equals(SysLoginLogTypeEnum.LOGIN_USERNAME.getType()) | ||
| 193 | + && o.getResult().equals(SysLoginResultEnum.USER_DISABLED.getResult())) | ||
| 194 | + ); | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + @Test | ||
| 198 | + public void testLogin_unknownError() { | ||
| 199 | + // 准备参数 | ||
| 200 | + String userIp = randomString(); | ||
| 201 | + String userAgent = randomString(); | ||
| 202 | + SysAuthLoginReqVO reqVO = randomPojo(SysAuthLoginReqVO.class); | ||
| 203 | + // mock 验证码正确 | ||
| 204 | + when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(reqVO.getCode()); | ||
| 205 | + // mock 抛出异常 | ||
| 206 | + when(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(reqVO.getUsername(), reqVO.getPassword()))) | ||
| 207 | + .thenThrow(new AuthenticationException("测试未知异常") {}); | ||
| 208 | + // 调用, 并断言异常 | ||
| 209 | + assertServiceException(() -> authService.login(reqVO, userIp, userAgent), AUTH_LOGIN_FAIL_UNKNOWN); | ||
| 210 | + // 校验调用参数 | ||
| 211 | + verify(captchaService, times(1)).deleteCaptchaCode(reqVO.getUuid()); | ||
| 212 | + verify(loginLogService, times(1)).createLoginLog( | ||
| 213 | + argThat(o -> o.getLogType().equals(SysLoginLogTypeEnum.LOGIN_USERNAME.getType()) | ||
| 214 | + && o.getResult().equals(SysLoginResultEnum.UNKNOWN_ERROR.getResult())) | ||
| 215 | + ); | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + @Test | ||
| 219 | + public void testLogin_success() { | ||
| 220 | + // 准备参数 | ||
| 221 | + String userIp = randomString(); | ||
| 222 | + String userAgent = randomString(); | ||
| 223 | + Long userId = randomLongId(); | ||
| 224 | + Set<Long> userRoleIds = randomSet(Long.class); | ||
| 225 | + String sessionId = randomString(); | ||
| 226 | + SysAuthLoginReqVO reqVO = randomPojo(SysAuthLoginReqVO.class); | ||
| 227 | + LoginUser loginUser = randomPojo(LoginUser.class, o -> { | ||
| 228 | + o.setId(userId); | ||
| 229 | + o.setRoleIds(userRoleIds); | ||
| 230 | + }); | ||
| 231 | + // mock 验证码正确 | ||
| 232 | + when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(reqVO.getCode()); | ||
| 233 | + // mock authentication | ||
| 234 | + when(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(reqVO.getUsername(), reqVO.getPassword()))) | ||
| 235 | + .thenReturn(authentication); | ||
| 236 | + when(authentication.getPrincipal()).thenReturn(loginUser); | ||
| 237 | + // mock 获得 User 拥有的角色编号数组 | ||
| 238 | + when(permissionService.getUserRoleIds(userId, singleton(CommonStatusEnum.ENABLE.getStatus()))).thenReturn(userRoleIds); | ||
| 239 | + // mock 缓存登陆用户到 Redis | ||
| 240 | + when(userSessionService.createUserSession(loginUser, userIp, userAgent)).thenReturn(sessionId); | ||
| 241 | + // 调用, 并断言异常 | ||
| 242 | + String login = authService.login(reqVO, userIp, userAgent); | ||
| 243 | + assertEquals(sessionId, login); | ||
| 244 | + // 校验调用参数 | ||
| 245 | + verify(captchaService, times(1)).deleteCaptchaCode(reqVO.getUuid()); | ||
| 246 | + verify(loginLogService, times(1)).createLoginLog( | ||
| 247 | + argThat(o -> o.getLogType().equals(SysLoginLogTypeEnum.LOGIN_USERNAME.getType()) | ||
| 248 | + && o.getResult().equals(SysLoginResultEnum.SUCCESS.getResult())) | ||
| 249 | + ); | ||
| 250 | + } | ||
| 251 | + | ||
| 252 | + @Test | ||
| 253 | + public void testLogout_success() { | ||
| 254 | + // 准备参数 | ||
| 255 | + String token = randomString(); | ||
| 256 | + LoginUser loginUser = randomPojo(LoginUser.class); | ||
| 257 | + // mock | ||
| 258 | + when(userSessionService.getLoginUser(token)).thenReturn(loginUser); | ||
| 259 | + // 调用 | ||
| 260 | + authService.logout(token); | ||
| 261 | + // 校验调用参数 | ||
| 262 | + verify(userSessionService, times(1)).deleteUserSession(token); | ||
| 263 | + verify(loginLogService, times(1)).createLoginLog( | ||
| 264 | + argThat(o -> o.getLogType().equals(SysLoginLogTypeEnum.LOGOUT_SELF.getType()) | ||
| 265 | + && o.getResult().equals(SysLoginResultEnum.SUCCESS.getResult())) | ||
| 266 | + ); | ||
| 267 | + } | ||
| 268 | + | ||
| 269 | +} |
src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysUserSessionServiceImplTest.java
| 1 | package cn.iocoder.dashboard.modules.system.service.auth; | 1 | package cn.iocoder.dashboard.modules.system.service.auth; |
| 2 | 2 | ||
| 3 | +import static cn.hutool.core.util.RandomUtil.randomEle; | ||
| 4 | +import static cn.iocoder.dashboard.modules.system.dal.redis.SysRedisKeyConstants.LOGIN_USER; | ||
| 5 | +import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | ||
| 6 | +import static cn.iocoder.dashboard.util.RandomUtils.randomDate; | ||
| 7 | +import static cn.iocoder.dashboard.util.RandomUtils.randomLongId; | ||
| 8 | +import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; | ||
| 9 | +import static cn.iocoder.dashboard.util.RandomUtils.randomString; | ||
| 10 | +import static cn.iocoder.dashboard.util.date.DateUtils.addTime; | ||
| 11 | +import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 12 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| 13 | +import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| 14 | +import static org.junit.jupiter.api.Assertions.assertNull; | ||
| 15 | +import static org.mockito.Mockito.when; | ||
| 16 | + | ||
| 17 | +import java.time.Duration; | ||
| 18 | +import java.util.Date; | ||
| 19 | +import java.util.List; | ||
| 20 | +import java.util.stream.Collectors; | ||
| 21 | +import java.util.stream.Stream; | ||
| 22 | + | ||
| 23 | +import javax.annotation.Resource; | ||
| 24 | + | ||
| 25 | +import org.junit.jupiter.api.Test; | ||
| 26 | +import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 27 | +import org.springframework.context.annotation.Import; | ||
| 28 | +import org.springframework.data.redis.core.StringRedisTemplate; | ||
| 29 | + | ||
| 3 | import cn.hutool.core.date.DateUtil; | 30 | import cn.hutool.core.date.DateUtil; |
| 4 | import cn.iocoder.dashboard.BaseDbAndRedisUnitTest; | 31 | import cn.iocoder.dashboard.BaseDbAndRedisUnitTest; |
| 32 | +import cn.iocoder.dashboard.common.enums.CommonStatusEnum; | ||
| 33 | +import cn.iocoder.dashboard.common.pojo.PageResult; | ||
| 5 | import cn.iocoder.dashboard.framework.security.config.SecurityProperties; | 34 | import cn.iocoder.dashboard.framework.security.config.SecurityProperties; |
| 35 | +import cn.iocoder.dashboard.framework.security.core.LoginUser; | ||
| 36 | +import cn.iocoder.dashboard.modules.infra.controller.job.vo.job.InfJobPageReqVO; | ||
| 37 | +import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobDO; | ||
| 38 | +import cn.iocoder.dashboard.modules.infra.enums.config.InfConfigTypeEnum; | ||
| 39 | +import cn.iocoder.dashboard.modules.infra.enums.job.InfJobStatusEnum; | ||
| 40 | +import cn.iocoder.dashboard.modules.system.controller.auth.vo.session.SysUserSessionPageReqVO; | ||
| 6 | import cn.iocoder.dashboard.modules.system.dal.dataobject.auth.SysUserSessionDO; | 41 | import cn.iocoder.dashboard.modules.system.dal.dataobject.auth.SysUserSessionDO; |
| 42 | +import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; | ||
| 7 | import cn.iocoder.dashboard.modules.system.dal.mysql.auth.SysUserSessionMapper; | 43 | import cn.iocoder.dashboard.modules.system.dal.mysql.auth.SysUserSessionMapper; |
| 44 | +import cn.iocoder.dashboard.modules.system.dal.mysql.user.SysUserMapper; | ||
| 8 | import cn.iocoder.dashboard.modules.system.dal.redis.auth.SysLoginUserRedisDAO; | 45 | import cn.iocoder.dashboard.modules.system.dal.redis.auth.SysLoginUserRedisDAO; |
| 46 | +import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum; | ||
| 9 | import cn.iocoder.dashboard.modules.system.service.auth.impl.SysUserSessionServiceImpl; | 47 | import cn.iocoder.dashboard.modules.system.service.auth.impl.SysUserSessionServiceImpl; |
| 10 | import cn.iocoder.dashboard.modules.system.service.dept.impl.SysDeptServiceImpl; | 48 | import cn.iocoder.dashboard.modules.system.service.dept.impl.SysDeptServiceImpl; |
| 11 | import cn.iocoder.dashboard.modules.system.service.logger.impl.SysLoginLogServiceImpl; | 49 | import cn.iocoder.dashboard.modules.system.service.logger.impl.SysLoginLogServiceImpl; |
| 12 | import cn.iocoder.dashboard.modules.system.service.user.SysUserServiceImpl; | 50 | import cn.iocoder.dashboard.modules.system.service.user.SysUserServiceImpl; |
| 13 | import cn.iocoder.dashboard.util.AssertUtils; | 51 | import cn.iocoder.dashboard.util.AssertUtils; |
| 14 | import cn.iocoder.dashboard.util.RandomUtils; | 52 | import cn.iocoder.dashboard.util.RandomUtils; |
| 15 | -import org.junit.jupiter.api.Test; | ||
| 16 | -import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 17 | -import org.springframework.context.annotation.Import; | ||
| 18 | - | ||
| 19 | -import javax.annotation.Resource; | ||
| 20 | -import java.util.Date; | ||
| 21 | -import java.util.List; | ||
| 22 | -import java.util.stream.Collectors; | ||
| 23 | -import java.util.stream.Stream; | ||
| 24 | - | ||
| 25 | -import static org.junit.jupiter.api.Assertions.assertEquals; | 53 | +import cn.iocoder.dashboard.util.json.JsonUtils; |
| 54 | +import cn.iocoder.dashboard.util.object.ObjectUtils; | ||
| 26 | 55 | ||
| 27 | /** | 56 | /** |
| 28 | * SysUserSessionServiceImpl Tester. | 57 | * SysUserSessionServiceImpl Tester. |
| @@ -31,13 +60,17 @@ import static org.junit.jupiter.api.Assertions.assertEquals; | @@ -31,13 +60,17 @@ import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 31 | * @version 1.0 | 60 | * @version 1.0 |
| 32 | * @since <pre>3月 8, 2021</pre> | 61 | * @since <pre>3月 8, 2021</pre> |
| 33 | */ | 62 | */ |
| 34 | -@Import(SysUserSessionServiceImpl.class) | 63 | +@Import({SysUserSessionServiceImpl.class, SysLoginUserRedisDAO.class}) |
| 35 | public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest { | 64 | public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest { |
| 36 | 65 | ||
| 37 | @Resource | 66 | @Resource |
| 38 | private SysUserSessionServiceImpl sysUserSessionService; | 67 | private SysUserSessionServiceImpl sysUserSessionService; |
| 39 | @Resource | 68 | @Resource |
| 40 | private SysUserSessionMapper sysUserSessionMapper; | 69 | private SysUserSessionMapper sysUserSessionMapper; |
| 70 | + @Resource | ||
| 71 | + private SysLoginUserRedisDAO sysLoginUserRedisDAO; | ||
| 72 | + @Resource | ||
| 73 | + private SysUserMapper sysUserMapper; | ||
| 41 | 74 | ||
| 42 | @MockBean | 75 | @MockBean |
| 43 | private SecurityProperties securityProperties; | 76 | private SecurityProperties securityProperties; |
| @@ -47,8 +80,132 @@ public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest { | @@ -47,8 +80,132 @@ public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest { | ||
| 47 | private SysUserServiceImpl sysUserService; | 80 | private SysUserServiceImpl sysUserService; |
| 48 | @MockBean | 81 | @MockBean |
| 49 | private SysLoginLogServiceImpl sysLoginLogService; | 82 | private SysLoginLogServiceImpl sysLoginLogService; |
| 50 | - @MockBean | ||
| 51 | - private SysLoginUserRedisDAO sysLoginUserRedisDAO; | 83 | + |
| 84 | + @Test | ||
| 85 | + public void testCreateUserSession_success() { | ||
| 86 | + // 准备参数 | ||
| 87 | + String userIp = randomString(); | ||
| 88 | + String userAgent = randomString(); | ||
| 89 | + LoginUser loginUser = randomPojo(LoginUser.class); | ||
| 90 | + // mock | ||
| 91 | + when(securityProperties.getSessionTimeout()).thenReturn(Duration.ofDays(1)); | ||
| 92 | + // 调用 | ||
| 93 | + String sessionId = sysUserSessionService.createUserSession(loginUser, userIp, userAgent); | ||
| 94 | + // 校验记录的属性是否正确 | ||
| 95 | + SysUserSessionDO sysUserSessionDO = sysUserSessionMapper.selectById(sessionId); | ||
| 96 | + assertEquals(sysUserSessionDO.getId(), sessionId); | ||
| 97 | + assertEquals(sysUserSessionDO.getUserId(), loginUser.getId()); | ||
| 98 | + assertEquals(sysUserSessionDO.getUserIp(), userIp); | ||
| 99 | + assertEquals(sysUserSessionDO.getUserAgent(), userAgent); | ||
| 100 | + assertEquals(sysUserSessionDO.getUsername(), loginUser.getUsername()); | ||
| 101 | + LoginUser redisLoginUser = sysLoginUserRedisDAO.get(sessionId); | ||
| 102 | + AssertUtils.assertPojoEquals(redisLoginUser, loginUser, "username","password"); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + @Test | ||
| 106 | + public void testCreateRefreshUserSession_success() { | ||
| 107 | + // 准备参数 | ||
| 108 | + String sessionId = randomString(); | ||
| 109 | + String userIp = randomString(); | ||
| 110 | + String userAgent = randomString(); | ||
| 111 | + Long timeLong = randomLongId(); | ||
| 112 | + String userName = randomString(); | ||
| 113 | + Date date = randomDate(); | ||
| 114 | + LoginUser loginUser = randomPojo(LoginUser.class); | ||
| 115 | + // mock | ||
| 116 | + when(securityProperties.getSessionTimeout()).thenReturn(Duration.ofDays(1)); | ||
| 117 | + loginUser.setUpdateTime(date); | ||
| 118 | + sysLoginUserRedisDAO.set(sessionId, loginUser); | ||
| 119 | + SysUserSessionDO userSession = SysUserSessionDO.builder().id(sessionId) | ||
| 120 | + .userId(loginUser.getId()).userIp(userIp).userAgent(userAgent).username(userName) | ||
| 121 | + .sessionTimeout(addTime(Duration.ofMillis(timeLong))) | ||
| 122 | + .build(); | ||
| 123 | + sysUserSessionMapper.insert(userSession); | ||
| 124 | + SysUserSessionDO insertDO = sysUserSessionMapper.selectById(sessionId); | ||
| 125 | + // 调用 | ||
| 126 | + sysUserSessionService.refreshUserSession(sessionId, loginUser); | ||
| 127 | + // 校验记录 redis | ||
| 128 | + LoginUser redisLoginUser = sysLoginUserRedisDAO.get(sessionId); | ||
| 129 | + assertNotEquals(redisLoginUser.getUpdateTime(), date); | ||
| 130 | + // 校验记录 SysUserSessionDO | ||
| 131 | + SysUserSessionDO updateDO = sysUserSessionMapper.selectById(sessionId); | ||
| 132 | + assertEquals(updateDO.getUsername(), loginUser.getUsername()); | ||
| 133 | + assertNotEquals(updateDO.getUpdateTime(), insertDO.getUpdateTime()); | ||
| 134 | + assertNotEquals(updateDO.getSessionTimeout(), addTime(Duration.ofMillis(timeLong))); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + @Test | ||
| 138 | + public void testDeleteUserSession_success() { | ||
| 139 | + // 准备参数 | ||
| 140 | + String sessionId = randomString(); | ||
| 141 | + String userIp = randomString(); | ||
| 142 | + String userAgent = randomString(); | ||
| 143 | + Long timeLong = randomLongId(); | ||
| 144 | + LoginUser loginUser = randomPojo(LoginUser.class); | ||
| 145 | + // mock 存入 Redis | ||
| 146 | + when(securityProperties.getSessionTimeout()).thenReturn(Duration.ofDays(1)); | ||
| 147 | + sysLoginUserRedisDAO.set(sessionId, loginUser); | ||
| 148 | + // mock 存入 db | ||
| 149 | + SysUserSessionDO userSession = SysUserSessionDO.builder().id(sessionId) | ||
| 150 | + .userId(loginUser.getId()).userIp(userIp).userAgent(userAgent).username(loginUser.getUsername()) | ||
| 151 | + .sessionTimeout(addTime(Duration.ofMillis(timeLong))) | ||
| 152 | + .build(); | ||
| 153 | + sysUserSessionMapper.insert(userSession); | ||
| 154 | + // 校验数据存在 | ||
| 155 | + assertNotNull(sysLoginUserRedisDAO.get(sessionId)); | ||
| 156 | + assertNotNull(sysUserSessionMapper.selectById(sessionId)); | ||
| 157 | + // 调用 | ||
| 158 | + sysUserSessionService.deleteUserSession(sessionId); | ||
| 159 | + // 校验数据不存在了 | ||
| 160 | + assertNull(sysLoginUserRedisDAO.get(sessionId)); | ||
| 161 | + assertNull(sysUserSessionMapper.selectById(sessionId)); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + @Test | ||
| 165 | + public void testGetUserSessionPage_success() { | ||
| 166 | + // mock 数据 | ||
| 167 | + String userIp = randomString(); | ||
| 168 | + SysUserDO dbUser1 = randomPojo(SysUserDO.class, o -> { | ||
| 169 | + o.setUsername("testUsername1"); | ||
| 170 | + o.setSex(randomEle(SysSexEnum.values()).getSEX()); | ||
| 171 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 172 | + }); | ||
| 173 | + SysUserDO dbUser2 = randomPojo(SysUserDO.class, o -> { | ||
| 174 | + o.setUsername("testUsername2"); | ||
| 175 | + o.setSex(randomEle(SysSexEnum.values()).getSEX()); | ||
| 176 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 177 | + }); | ||
| 178 | + SysUserSessionDO dbSession = randomPojo(SysUserSessionDO.class, o -> { | ||
| 179 | + o.setUserId(dbUser1.getId()); | ||
| 180 | + o.setUserIp(userIp); | ||
| 181 | + }); | ||
| 182 | + sysUserMapper.insert(dbUser1); | ||
| 183 | + sysUserMapper.insert(dbUser2); | ||
| 184 | + sysUserSessionMapper.insert(dbSession); | ||
| 185 | + sysUserSessionMapper.insert(ObjectUtils.clone(dbSession, o -> { | ||
| 186 | + o.setId(randomString()); | ||
| 187 | + o.setUserId(dbUser2.getId()); | ||
| 188 | + })); | ||
| 189 | + // 测试 userId 不匹配 | ||
| 190 | + sysUserSessionMapper.insert(ObjectUtils.clone(dbSession, o -> { | ||
| 191 | + o.setId(randomString()); | ||
| 192 | + o.setUserId(123456l); | ||
| 193 | + })); | ||
| 194 | + // 测试 userIp 不匹配 | ||
| 195 | + sysUserSessionMapper.insert(ObjectUtils.clone(dbSession, o -> { | ||
| 196 | + o.setId(randomString()); | ||
| 197 | + o.setUserIp("testUserIp"); | ||
| 198 | + })); | ||
| 199 | + // 准备参数 | ||
| 200 | + SysUserSessionPageReqVO reqVo = new SysUserSessionPageReqVO(); | ||
| 201 | + reqVo.setUserIp(userIp); | ||
| 202 | + // 调用 | ||
| 203 | + PageResult<SysUserSessionDO> pageResult = sysUserSessionService.getUserSessionPage(reqVo); | ||
| 204 | + // 断言 | ||
| 205 | + assertEquals(3, pageResult.getTotal()); | ||
| 206 | + assertEquals(3, pageResult.getList().size()); | ||
| 207 | + assertPojoEquals(dbSession, pageResult.getList().get(0)); | ||
| 208 | + } | ||
| 52 | 209 | ||
| 53 | @Test | 210 | @Test |
| 54 | public void testClearSessionTimeout_success() throws Exception { | 211 | public void testClearSessionTimeout_success() throws Exception { |