Commit eea41dba0a92695674eaf8f14e50f1f65600efb3
1 parent
e27b394b
feat(usr): user create error branches REQ-USR-001
Showing
2 changed files
with
87 additions
and
2 deletions
backend/src/main/java/com/xly/erp/module/usr/service/impl/UserServiceImpl.java
| @@ -57,6 +57,23 @@ public class UserServiceImpl implements UserService { | @@ -57,6 +57,23 @@ public class UserServiceImpl implements UserService { | ||
| 57 | 57 | ||
| 58 | @Override | 58 | @Override |
| 59 | public Map<String, Object> create(CreateUserDTO dto) { | 59 | public Map<String, Object> create(CreateUserDTO dto) { |
| 60 | + if (!USER_TYPES.contains(dto.getSUserType())) { | ||
| 61 | + throw new BizException(40001, "sUserType: 取值非法"); | ||
| 62 | + } | ||
| 63 | + if (!LANGUAGES.contains(dto.getSLanguage())) { | ||
| 64 | + throw new BizException(40001, "sLanguage: 取值非法"); | ||
| 65 | + } | ||
| 66 | + if (dto.getIStaffId() != null && !staffMapper.existsActiveById(dto.getIStaffId())) { | ||
| 67 | + throw new BizException(40022, "职员不存在或已删除"); | ||
| 68 | + } | ||
| 69 | + List<Integer> ids = dto.getPermissionCategoryIds(); | ||
| 70 | + if (ids != null && !ids.isEmpty()) { | ||
| 71 | + int found = permissionCategoryMapper.countActiveByIds(ids); | ||
| 72 | + if (found != ids.size()) { | ||
| 73 | + throw new BizException(40023, "权限分类含无效 id"); | ||
| 74 | + } | ||
| 75 | + } | ||
| 76 | + | ||
| 60 | User entity = new User(); | 77 | User entity = new User(); |
| 61 | entity.setSBrandsId(tenant.getBrandsId()); | 78 | entity.setSBrandsId(tenant.getBrandsId()); |
| 62 | entity.setSSubsidiaryId(tenant.getSubsidiaryId()); | 79 | entity.setSSubsidiaryId(tenant.getSubsidiaryId()); |
| @@ -73,9 +90,12 @@ public class UserServiceImpl implements UserService { | @@ -73,9 +90,12 @@ public class UserServiceImpl implements UserService { | ||
| 73 | entity.setSCreatedBy(createdBy); | 90 | entity.setSCreatedBy(createdBy); |
| 74 | entity.setBDeleted(false); | 91 | entity.setBDeleted(false); |
| 75 | 92 | ||
| 76 | - userMapper.insert(entity); | 93 | + try { |
| 94 | + userMapper.insert(entity); | ||
| 95 | + } catch (DuplicateKeyException e) { | ||
| 96 | + throw new BizException(40020, "用户号或用户名已存在"); | ||
| 97 | + } | ||
| 77 | 98 | ||
| 78 | - List<Integer> ids = dto.getPermissionCategoryIds(); | ||
| 79 | if (ids != null && !ids.isEmpty()) { | 99 | if (ids != null && !ids.isEmpty()) { |
| 80 | for (Integer cid : ids) { | 100 | for (Integer cid : ids) { |
| 81 | UserPermission rel = new UserPermission(); | 101 | UserPermission rel = new UserPermission(); |
backend/src/test/java/com/xly/erp/module/usr/service/UserServiceImplTest.java
| @@ -115,6 +115,71 @@ class UserServiceImplTest { | @@ -115,6 +115,71 @@ class UserServiceImplTest { | ||
| 115 | verify(userPermissionMapper, never()).insert(any(UserPermission.class)); | 115 | verify(userPermissionMapper, never()).insert(any(UserPermission.class)); |
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | + @Test | ||
| 119 | + void createWithInvalidUserType_throws40001() { | ||
| 120 | + CreateUserDTO dto = baseDto(); | ||
| 121 | + dto.setSUserType("火星"); | ||
| 122 | + assertThatThrownBy(() -> service.create(dto)) | ||
| 123 | + .isInstanceOf(BizException.class) | ||
| 124 | + .hasFieldOrPropertyWithValue("code", 40001); | ||
| 125 | + verify(userMapper, never()).insert(any(User.class)); | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + @Test | ||
| 129 | + void createWithInvalidLanguage_throws40001() { | ||
| 130 | + CreateUserDTO dto = baseDto(); | ||
| 131 | + dto.setSLanguage("ja"); | ||
| 132 | + assertThatThrownBy(() -> service.create(dto)) | ||
| 133 | + .isInstanceOf(BizException.class) | ||
| 134 | + .hasFieldOrPropertyWithValue("code", 40001); | ||
| 135 | + verify(userMapper, never()).insert(any(User.class)); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + @Test | ||
| 139 | + void createWithStaffNotFound_throws40022() { | ||
| 140 | + when(staffMapper.existsActiveById(99)).thenReturn(false); | ||
| 141 | + CreateUserDTO dto = baseDto(); | ||
| 142 | + dto.setIStaffId(99); | ||
| 143 | + assertThatThrownBy(() -> service.create(dto)) | ||
| 144 | + .isInstanceOf(BizException.class) | ||
| 145 | + .hasFieldOrPropertyWithValue("code", 40022); | ||
| 146 | + verify(userMapper, never()).insert(any(User.class)); | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + @Test | ||
| 150 | + void createWithSomeInvalidPermissionIds_throws40023() { | ||
| 151 | + when(permissionCategoryMapper.countActiveByIds(List.of(1, 2, 3))).thenReturn(2); | ||
| 152 | + CreateUserDTO dto = baseDto(); | ||
| 153 | + dto.setPermissionCategoryIds(List.of(1, 2, 3)); | ||
| 154 | + assertThatThrownBy(() -> service.create(dto)) | ||
| 155 | + .isInstanceOf(BizException.class) | ||
| 156 | + .hasFieldOrPropertyWithValue("code", 40023); | ||
| 157 | + verify(userMapper, never()).insert(any(User.class)); | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + @Test | ||
| 161 | + void createWithDuplicateUserNo_throws40020() { | ||
| 162 | + when(userMapper.insert(any(User.class))) | ||
| 163 | + .thenThrow(new DuplicateKeyException("uk_user_no")); | ||
| 164 | + CreateUserDTO dto = baseDto(); | ||
| 165 | + assertThatThrownBy(() -> service.create(dto)) | ||
| 166 | + .isInstanceOf(BizException.class) | ||
| 167 | + .hasFieldOrPropertyWithValue("code", 40020); | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + @Test | ||
| 171 | + void createUsesAuthenticatedUserNoAsCreatedBy() { | ||
| 172 | + SecurityContextHolder.getContext().setAuthentication( | ||
| 173 | + new UsernamePasswordAuthenticationToken("ALICE", null, Collections.emptyList())); | ||
| 174 | + CreateUserDTO dto = baseDto(); | ||
| 175 | + | ||
| 176 | + service.create(dto); | ||
| 177 | + | ||
| 178 | + ArgumentCaptor<User> cap = ArgumentCaptor.forClass(User.class); | ||
| 179 | + verify(userMapper).insert(cap.capture()); | ||
| 180 | + assertThat(cap.getValue().getSCreatedBy()).isEqualTo("ALICE"); | ||
| 181 | + } | ||
| 182 | + | ||
| 118 | private CreateUserDTO baseDto() { | 183 | private CreateUserDTO baseDto() { |
| 119 | CreateUserDTO dto = new CreateUserDTO(); | 184 | CreateUserDTO dto = new CreateUserDTO(); |
| 120 | dto.setSUserNo("u001"); | 185 | dto.setSUserNo("u001"); |