fix: 保留新建用户选择的部门

- 创建账号时保留表单提交的部门归属

- 补充跨部门创建回归测试
This commit is contained in:
2026-07-31 16:44:02 +08:00
parent 12080ceedb
commit 527336bfc9
3 changed files with 131 additions and 3 deletions

View File

@@ -0,0 +1,100 @@
package tech.easyflow.admin.controller.system;
import com.alibaba.fastjson2.JSONObject;
import com.mybatisflex.core.query.QueryWrapper;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;
import org.testng.annotations.Test;
import tech.easyflow.auth.service.AuthCredentialKeyService;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.entity.LoginAccount;
import tech.easyflow.common.satoken.util.SaTokenUtil;
import tech.easyflow.system.entity.SysAccount;
import tech.easyflow.system.service.SysAccountService;
import java.math.BigInteger;
import java.util.Map;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
/**
* {@link SysAccountController} 创建账号测试。
*/
public class SysAccountControllerTest {
/**
* 验证创建用户时保留表单选择的部门,而不是替换为操作者部门。
*/
@Test
public void saveShouldKeepSubmittedDepartment() {
BigInteger selectedDeptId = BigInteger.valueOf(200);
BigInteger operatorDeptId = BigInteger.valueOf(100);
BigInteger tenantId = BigInteger.valueOf(300);
BigInteger operatorId = BigInteger.valueOf(400);
SysAccountService accountService = mock(SysAccountService.class);
AuthCredentialKeyService credentialKeyService = mock(AuthCredentialKeyService.class);
SysAccountController controller = new SysAccountController(accountService, credentialKeyService);
SysAccount entity = createAccount(selectedDeptId);
LoginAccount loginAccount = createLoginAccount(operatorId, tenantId, operatorDeptId);
when(accountService.count(any(QueryWrapper.class))).thenReturn(0L);
when(accountService.save(any(SysAccount.class))).thenAnswer(invocation -> {
invocation.getArgument(0, SysAccount.class).setId(BigInteger.valueOf(500));
return true;
});
when(credentialKeyService.decryptPayload(any()))
.thenReturn(JSONObject.parseObject("{\"password\":\"Valid123!\"}"));
try (MockedStatic<SaTokenUtil> saToken = mockStatic(SaTokenUtil.class)) {
saToken.when(SaTokenUtil::getLoginAccount).thenReturn(loginAccount);
Result<?> result = controller.save(entity);
assertEquals(result.getErrorCode(), 0);
}
ArgumentCaptor<SysAccount> accountCaptor = ArgumentCaptor.forClass(SysAccount.class);
verify(accountService).save(accountCaptor.capture());
SysAccount savedAccount = accountCaptor.getValue();
assertEquals(savedAccount.getDeptId(), selectedDeptId);
assertEquals(savedAccount.getTenantId(), tenantId);
assertEquals(savedAccount.getCreatedBy(), operatorId);
assertEquals(savedAccount.getModifiedBy(), operatorId);
}
/**
* 构造待创建账号。
*
* @param deptId 表单选择的部门 ID
* @return 待创建账号
*/
private SysAccount createAccount(BigInteger deptId) {
SysAccount account = new SysAccount();
account.setDeptId(deptId);
account.setLoginName("department_test_user");
account.setNickname("部门测试用户");
account.setPasswordCredential(Map.of("keyId", "test-key"));
return account;
}
/**
* 构造当前登录账号。
*
* @param id 操作人 ID
* @param tenantId 租户 ID
* @param deptId 操作人部门 ID
* @return 当前登录账号
*/
private LoginAccount createLoginAccount(BigInteger id, BigInteger tenantId, BigInteger deptId) {
LoginAccount account = new LoginAccount();
account.setId(id);
account.setTenantId(tenantId);
account.setDeptId(deptId);
return account;
}
}