Commit e2b76ee0e5f4d9f5edc57c5f22d9dc736daf1fd0
1 parent
42ce5c75
code review 修改密码等的单元测试
Showing
12 changed files
with
25 additions
and
20 deletions
yudao-admin-server/pom.xml
yudao-user-server/pom.xml
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/controller/user/SysUserProfileController.java
| ... | ... | @@ -70,6 +70,7 @@ public class SysUserProfileController { |
| 70 | 70 | @PreAuthenticated |
| 71 | 71 | public CommonResult<Boolean> updateMobile(@RequestBody @Valid MbrUserUpdateMobileReqVO reqVO) { |
| 72 | 72 | // 校验验证码 |
| 73 | + // TODO @宋天:统一到 userService.updateMobile 方法里 | |
| 73 | 74 | smsCodeService.useSmsCode(reqVO.getMobile(),SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene(), reqVO.getCode(),getClientIP()); |
| 74 | 75 | |
| 75 | 76 | userService.updateMobile(getLoginUserId(), reqVO); | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/controller/user/vo/MbrUserUpdateMobileReqVO.java
| ... | ... | @@ -25,9 +25,9 @@ public class MbrUserUpdateMobileReqVO { |
| 25 | 25 | @Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字") |
| 26 | 26 | private String code; |
| 27 | 27 | |
| 28 | - | |
| 29 | 28 | @ApiModelProperty(value = "手机号",required = true,example = "15823654487") |
| 30 | 29 | @NotBlank(message = "手机号不能为空") |
| 30 | + // TODO @宋天:手机校验,直接使用 @Mobile 哈 | |
| 31 | 31 | @Length(min = 8, max = 11, message = "手机号码长度为 8-11 位") |
| 32 | 32 | @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式错误") |
| 33 | 33 | private String mobile; | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/service/user/impl/MbrUserServiceImpl.java
| ... | ... | @@ -126,8 +126,10 @@ public class MbrUserServiceImpl implements MbrUserService { |
| 126 | 126 | // 检测用户是否存在 |
| 127 | 127 | MbrUserDO userDO = checkUserExists(userId); |
| 128 | 128 | // 检测手机与验证码是否匹配 |
| 129 | + // TODO @宋天:修改手机的时候。应该要校验,老手机 + 老手机 code;新手机 + 新手机 code | |
| 129 | 130 | sysAuthService.checkIfMobileMatchCodeAndDeleteCode(userDO.getMobile(),reqVO.getCode()); |
| 130 | 131 | // 更新用户手机 |
| 132 | + // TODO @宋天:更新的时候,单独创建对象。直接全量更新,会可能导致属性覆盖。可以看看打印出来的 SQL 哈 | |
| 131 | 133 | userDO.setMobile(reqVO.getMobile()); |
| 132 | 134 | userMapper.updateById(userDO); |
| 133 | 135 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java
| ... | ... | @@ -98,6 +98,7 @@ public class SysAuthController { |
| 98 | 98 | @ApiOperation(value = "校验验证码是否正确") |
| 99 | 99 | @PreAuthenticated |
| 100 | 100 | public CommonResult<Boolean> checkSmsCode(@RequestBody @Valid SysAuthSmsLoginReqVO reqVO) { |
| 101 | + // TODO @宋天:check 的时候,不应该使用 useSmsCode 哈,这样验证码就直接被使用了。另外,check 开头的方法,更多是校验的逻辑,不会有 update 数据的动作。这点,在方法命名上,也是要注意的 | |
| 101 | 102 | smsCodeService.useSmsCode(reqVO.getMobile(),SysSmsSceneEnum.CHECK_CODE_BY_SMS.getScene(),reqVO.getCode(),getClientIP()); |
| 102 | 103 | return success(true); |
| 103 | 104 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthResetPasswordReqVO.java
| ... | ... | @@ -8,7 +8,6 @@ import lombok.Data; |
| 8 | 8 | import lombok.NoArgsConstructor; |
| 9 | 9 | import org.hibernate.validator.constraints.Length; |
| 10 | 10 | |
| 11 | -import javax.validation.constraints.NotBlank; | |
| 12 | 11 | import javax.validation.constraints.NotEmpty; |
| 13 | 12 | import javax.validation.constraints.Pattern; |
| 14 | 13 | |
| ... | ... | @@ -29,4 +28,5 @@ public class MbrAuthResetPasswordReqVO { |
| 29 | 28 | @Length(min = 4, max = 6, message = "手机验证码长度为 4-6 位") |
| 30 | 29 | @Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字") |
| 31 | 30 | private String code; |
| 31 | + | |
| 32 | 32 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java
| ... | ... | @@ -285,6 +285,7 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 285 | 285 | MbrUserDO userDO = checkOldPassword(userId, reqVO.getOldPassword()); |
| 286 | 286 | |
| 287 | 287 | // 更新用户密码 |
| 288 | + // TODO @宋天:不要更新整个对象哈 | |
| 288 | 289 | userDO.setPassword(passwordEncoder.encode(reqVO.getPassword())); |
| 289 | 290 | userMapper.updateById(userDO); |
| 290 | 291 | } |
| ... | ... | @@ -300,6 +301,8 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 300 | 301 | // TODO @芋艿 这一步没必要检验验证码与手机是否匹配,因为是根据验证码去redis中查找手机号,然后根据手机号查询用户 |
| 301 | 302 | // 也就是说 即便黑客以其他方式将验证码发送到自己手机上,最终还是会根据手机号查询用户然后进行重置密码的操作,不存在安全问题 |
| 302 | 303 | |
| 304 | + // TODO @宋天:这块微信在讨论下哈~~~ | |
| 305 | + | |
| 303 | 306 | // 校验验证码 |
| 304 | 307 | smsCodeService.useSmsCode(userDO.getMobile(), SysSmsSceneEnum.FORGET_MOBILE_BY_SMS.getScene(), reqVO.getCode(),getClientIP()); |
| 305 | 308 | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/impl/SysSmsCodeServiceImpl.java
| ... | ... | @@ -58,10 +58,14 @@ public class SysSmsCodeServiceImpl implements SysSmsCodeService { |
| 58 | 58 | // 创建验证码 |
| 59 | 59 | String code = this.createSmsCode(mobile, scene, createIp); |
| 60 | 60 | // 发送验证码 |
| 61 | + // TODO @宋天:这里可以拓展下 SysSmsSceneEnum,支持设置对应的短信模板编号(不同场景的短信文案是不同的)、是否要校验手机号已经注册。这样 Controller 就可以收口成一个接口了。相当于说,不同场景,不同策略 | |
| 61 | 62 | smsCoreService.sendSingleSmsToMember(mobile, null, SysSmsTemplateCodeConstants.USER_SMS_LOGIN, |
| 62 | 63 | MapUtil.of("code", code)); |
| 63 | 64 | |
| 64 | 65 | // 存储手机号与验证码到redis,用于标记 |
| 66 | + // TODO @宋天:SysSmsCodeDO 表应该足够,无需增加额外的 redis 存储哇 | |
| 67 | + // TODO @宋天:Redis 相关的操作,不要散落到业务层,而是写一个它对应的 RedisDAO。这样,实现业务与技术的解耦 | |
| 68 | + // TODO @宋天:直接使用 code 作为 key,会存在 2 个问题:1)code 可能会冲突,多个手机号之间;2)缺少前缀。例如说 sms_code_${code} | |
| 65 | 69 | stringRedisTemplate.opsForValue().set(code,mobile,CODE_TIME, TimeUnit.MINUTES); |
| 66 | 70 | } |
| 67 | 71 | ... | ... |
yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/member/controller/SysUserProfileControllerTest.java
| ... | ... | @@ -22,6 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. |
| 22 | 22 | * |
| 23 | 23 | * @author 宋天 |
| 24 | 24 | */ |
| 25 | +// TODO @宋天:controller 的单测可以不写哈,因为收益太低了。未来我们做 qa 自动化测试 | |
| 25 | 26 | public class SysUserProfileControllerTest { |
| 26 | 27 | |
| 27 | 28 | private MockMvc mockMvc; |
| ... | ... | @@ -35,7 +36,7 @@ public class SysUserProfileControllerTest { |
| 35 | 36 | @Mock |
| 36 | 37 | private SysSmsCodeService smsCodeService; |
| 37 | 38 | |
| 38 | - @Before | |
| 39 | + @Before // TODO @宋天:使用 junit5 哈 | |
| 39 | 40 | public void setup() { |
| 40 | 41 | // 初始化 |
| 41 | 42 | MockitoAnnotations.openMocks(this); |
| ... | ... | @@ -52,7 +53,7 @@ public class SysUserProfileControllerTest { |
| 52 | 53 | .content("{\"mobile\":\"15819844280\",\"code\":\"123456\"}}")) |
| 53 | 54 | .andExpect(status().isOk()) |
| 54 | 55 | .andDo(MockMvcResultHandlers.print()); |
| 55 | - | |
| 56 | +// TODO @宋天:方法的结尾,不用空行哈 | |
| 56 | 57 | } |
| 57 | 58 | |
| 58 | 59 | ... | ... |
yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/member/service/MbrUserServiceImplTest.java
| ... | ... | @@ -6,7 +6,6 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 7 | 7 | import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; |
| 8 | 8 | import cn.iocoder.yudao.userserver.BaseDbAndRedisUnitTest; |
| 9 | -import cn.iocoder.yudao.userserver.BaseDbUnitTest; | |
| 10 | 9 | import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserInfoRespVO; |
| 11 | 10 | import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserUpdateMobileReqVO; |
| 12 | 11 | import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper; |
| ... | ... | @@ -15,7 +14,6 @@ import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSend |
| 15 | 14 | import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; |
| 16 | 15 | import cn.iocoder.yudao.userserver.modules.system.service.auth.impl.SysAuthServiceImpl; |
| 17 | 16 | import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; |
| 18 | -import cn.iocoder.yudao.userserver.modules.system.service.sms.impl.SysSmsCodeServiceImpl; | |
| 19 | 17 | import org.junit.jupiter.api.Test; |
| 20 | 18 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 21 | 19 | import org.springframework.context.annotation.Import; |
| ... | ... | @@ -23,14 +21,16 @@ import org.springframework.data.redis.core.StringRedisTemplate; |
| 23 | 21 | import org.springframework.security.crypto.password.PasswordEncoder; |
| 24 | 22 | |
| 25 | 23 | import javax.annotation.Resource; |
| 26 | -import java.io.*; | |
| 24 | +import java.io.ByteArrayInputStream; | |
| 27 | 25 | import java.util.function.Consumer; |
| 28 | 26 | |
| 29 | 27 | import static cn.hutool.core.util.RandomUtil.*; |
| 30 | -import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 31 | 28 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 32 | 29 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 33 | 31 | import static org.mockito.Mockito.*; |
| 32 | + | |
| 33 | +// TODO @芋艿:单测的 review,等逻辑都达成一致后 | |
| 34 | 34 | /** |
| 35 | 35 | * {@link MbrUserServiceImpl} 的单元测试类 |
| 36 | 36 | * | ... | ... |
yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/system/service/SysAuthServiceTest.java
| 1 | 1 | package cn.iocoder.yudao.userserver.modules.system.service; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.util.ArrayUtil; | |
| 4 | 3 | import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; |
| 5 | -import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; | |
| 6 | 4 | import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; |
| 7 | 5 | import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService; |
| 8 | 6 | import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; |
| ... | ... | @@ -10,11 +8,8 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 10 | 8 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 11 | 9 | import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; |
| 12 | 10 | import cn.iocoder.yudao.userserver.BaseDbAndRedisUnitTest; |
| 13 | -import cn.iocoder.yudao.userserver.BaseDbUnitTest; | |
| 14 | -import cn.iocoder.yudao.userserver.config.RedisTestConfiguration; | |
| 15 | 11 | import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper; |
| 16 | 12 | import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; |
| 17 | -import cn.iocoder.yudao.userserver.modules.member.service.user.impl.MbrUserServiceImpl; | |
| 18 | 13 | import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.MbrAuthResetPasswordReqVO; |
| 19 | 14 | import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.MbrAuthUpdatePasswordReqVO; |
| 20 | 15 | import cn.iocoder.yudao.userserver.modules.system.service.auth.SysAuthService; |
| ... | ... | @@ -28,17 +23,17 @@ import org.springframework.security.authentication.AuthenticationManager; |
| 28 | 23 | import org.springframework.security.crypto.password.PasswordEncoder; |
| 29 | 24 | |
| 30 | 25 | import javax.annotation.Resource; |
| 31 | - | |
| 32 | 26 | import java.util.concurrent.TimeUnit; |
| 33 | 27 | import java.util.function.Consumer; |
| 34 | 28 | |
| 35 | 29 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 36 | 30 | import static cn.hutool.core.util.RandomUtil.randomNumbers; |
| 37 | -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | |
| 31 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | |
| 32 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; | |
| 38 | 33 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 39 | -import static org.mockito.Mockito.*; | |
| 40 | - | |
| 34 | +import static org.mockito.Mockito.when; | |
| 41 | 35 | |
| 36 | +// TODO @芋艿:单测的 review,等逻辑都达成一致后 | |
| 42 | 37 | /** |
| 43 | 38 | * {@link SysAuthService} 的单元测试类 |
| 44 | 39 | * |
| ... | ... | @@ -68,7 +63,6 @@ public class SysAuthServiceTest extends BaseDbAndRedisUnitTest { |
| 68 | 63 | @Resource |
| 69 | 64 | private SysAuthServiceImpl authService; |
| 70 | 65 | |
| 71 | - | |
| 72 | 66 | @Test |
| 73 | 67 | public void testUpdatePassword_success(){ |
| 74 | 68 | // 准备参数 | ... | ... |