feat(admin): 账号域三页平台分侧+京东上架额度收口(V206/V207)

This commit is contained in:
2026-11-17 03:29:33 +08:00
parent 764eeb2914
commit 5c7a310ee7
58 changed files with 2796 additions and 1455 deletions
@@ -93,7 +93,9 @@ class MigrationInventoryTest {
* V204(aff6c88a)与 V205(任务证据下线,71b19e4f)随 2026-11-17 发版窗越过 202 落库。
* Flyway 未开 out-of-order ⇒ 该路径入库前必须把文件改名到 V206+(RELEASE_LEDGER 2026-11-16
* 22:2x 段的顺序债登记);把新迁移写回 202 会让已过 205 的环境启动期报
* "resolved migration not applied" 失败。与 132/133 同属"只能永久保留、不能回填"。</p>
* "resolved migration not applied" 失败。与 132/133 同属"只能永久保留、不能回填"。
* 2026-11-17 已改号 {@code V207__users_jd_publish_quota.sql} 入库(V206 被同窗
* admin_account_platform 三表平台列占用,account-platform 提交);202 号自此彻底空置。</p>
*
* <p>共同后果:新迁移只能取当前最大值之上的号(详见 docs/migration-inventory.md)。</p>
*/
@@ -0,0 +1,544 @@
package com.nanri.aiimage.modules.admin.service;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.mapper.AdminUserMapper;
import com.nanri.aiimage.common.mapper.ShopManageGroupMapper;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.model.entity.ShopManageGroupEntity;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.AdminPlatformScope;
import com.nanri.aiimage.common.service.CompanyScopeSupport;
import com.nanri.aiimage.modules.admin.model.dto.AdminUserCreateRequest;
import com.nanri.aiimage.modules.admin.model.dto.AdminUserUpdateRequest;
import com.nanri.aiimage.modules.admin.spi.UserSecretCleanupPort;
import com.nanri.aiimage.modules.auth.util.WerkzeugPasswordEncoder;
import com.nanri.aiimage.modules.company.mapper.CompanyMapper;
import com.nanri.aiimage.modules.company.model.dto.CompanySaveRequest;
import com.nanri.aiimage.modules.company.model.entity.CompanyEntity;
import com.nanri.aiimage.modules.company.service.CompanyService;
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
import com.nanri.aiimage.modules.shopkey.mapper.QueryAsinMapper;
import com.nanri.aiimage.modules.shopkey.mapper.ShopManageGroupMemberMapper;
import com.nanri.aiimage.modules.shopkey.mapper.ShopManageMapper;
import com.nanri.aiimage.modules.shopkey.mapper.SkipPriceAsinMapper;
import com.nanri.aiimage.modules.shopkey.model.dto.ShopManageGroupCreateRequest;
import com.nanri.aiimage.modules.shopkey.model.dto.ShopManageGroupUpdateRequest;
import com.nanri.aiimage.modules.shopkey.service.ShopManageGroupService;
import com.nanri.aiimage.modules.shopkey.spi.GroupDeletionGuardPort;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 账号域「所属平台」(V206,2026-11-17 用户口径「亚马逊这边也要有;京东的数据是新的,
* 别把亚马逊的带过来」):users / company / biz_shop_manage_group 三张表各带 platform 列
* (amazon|jd),后管三个管理页按平台 tab 过滤、新建默认当前平台、编辑只显式翻档。
*
* <p>判据钉四条形状:①列表过滤真的进 SQL 条件(打在 wrapper 参数上,不是打在返回值——
* 返回空也可能是别的条件写反);②新建缺省落 'amazon'(与列 DEFAULT 同口径);③编辑只在
* 显式给出时落(PUT 只带 disabled 的既有形状不得把平台冲掉);④非法值 400,不许静默归档。</p>
*/
class AdminAccountPlatformScopeTest {
private AdminUserMapper userMapper;
private AdminAuthSupport authSupport;
private PermissionMenuService permissionService;
private CompanyService companyService;
private AdminUserService userService;
private CompanyMapper companyMapper;
private CompanyService standaloneCompanyService;
private ShopManageGroupMapper groupMapper;
private ShopManageGroupMemberMapper groupMemberMapper;
private CompanyScopeSupport companyScope;
private AdminUserMapper groupAdminUserMapper;
private ShopManageGroupService groupService;
@BeforeEach
@SuppressWarnings({"unchecked", "rawtypes"})
void setUp() {
TableInfoHelper.initTableInfo(
new MapperBuilderAssistant(new MybatisConfiguration(), ""), AdminUserEntity.class);
TableInfoHelper.initTableInfo(
new MapperBuilderAssistant(new MybatisConfiguration(), ""), CompanyEntity.class);
TableInfoHelper.initTableInfo(
new MapperBuilderAssistant(new MybatisConfiguration(), ""), ShopManageGroupEntity.class);
userMapper = mock(AdminUserMapper.class);
authSupport = mock(AdminAuthSupport.class);
permissionService = mock(PermissionMenuService.class);
companyService = mock(CompanyService.class);
userService = new AdminUserService(userMapper, mock(WerkzeugPasswordEncoder.class), authSupport,
permissionService, mock(UserSecretCleanupPort.class), companyService);
when(permissionService.hasAnyAdminMenu(any(), any())).thenReturn(true);
when(userMapper.selectCount(any(LambdaQueryWrapper.class))).thenReturn(0L);
when(userMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of());
companyMapper = mock(CompanyMapper.class);
standaloneCompanyService = new CompanyService(companyMapper, authSupport,
mock(PermissionMenuService.class));
when(companyMapper.selectCount(any(LambdaQueryWrapper.class))).thenReturn(0L);
when(companyMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of());
groupMapper = mock(ShopManageGroupMapper.class);
groupMemberMapper = mock(ShopManageGroupMemberMapper.class);
companyScope = mock(CompanyScopeSupport.class);
groupAdminUserMapper = mock(AdminUserMapper.class);
groupService = new ShopManageGroupService(groupMapper, groupMemberMapper,
List.of(mock(GroupDeletionGuardPort.class)), mock(ShopManageMapper.class),
mock(QueryAsinMapper.class), mock(SkipPriceAsinMapper.class),
groupAdminUserMapper, companyScope);
when(groupMapper.selectCount(any(LambdaQueryWrapper.class))).thenReturn(0L);
when(groupMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of());
when(groupMemberMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of());
when(companyScope.requireCompanyId(any(AdminUserEntity.class))).thenReturn(5L);
}
// ---------------------------------------------------------------- 用户列表过滤
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void userListPlatformFilterBecomesAQueryCondition() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
userService.listUsers(operator, 1, 10, null, null, null, null, "jd");
LambdaQueryWrapper<AdminUserEntity> query = capturedUserCountQuery();
// MP 的参数是惰性入表的:先渲染 SQL 段再断参数值(同 AdminUserListCompanyTest 的断言顺序)
assertThat(query.getSqlSegment()).contains("platform");
assertThat(query.getParamNameValuePairs()).containsValue("jd");
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void userListWithoutPlatformAddsNoCondition() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
// 8 参重载传 null 与旧 7 参口径完全一致:不带平台时查询条件一个都没有
userService.listUsers(operator, 1, 10, null, null, null, null, null);
assertThat(capturedUserCountQuery().getParamNameValuePairs()).isEmpty();
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void userListRejectsIllegalPlatformValue() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
assertThatThrownBy(() -> userService.listUsers(operator, 1, 10, null, null, null, null, "taobao"))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("所属平台");
}
// ---------------------------------------------------------------- 用户新建/编辑落库
@Test
void createUserWithoutPlatformFallsBackToAmazon() {
AdminUserEntity operator = operator(1L, "company_super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("company_super_admin");
AtomicReference<AdminUserEntity> inserted = captureUserInsert();
AdminUserCreateRequest request = new AdminUserCreateRequest();
request.setUsername("京东客服");
request.setPassword("123456");
request.setRole("normal");
userService.createUser(operator, request);
assertThat(inserted.get().getPlatform()).isEqualTo(AdminPlatformScope.AMAZON);
}
@Test
void createUserWithExplicitJdPlatformStoresJd() {
AdminUserEntity operator = operator(1L, "company_super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("company_super_admin");
AtomicReference<AdminUserEntity> inserted = captureUserInsert();
AdminUserCreateRequest request = new AdminUserCreateRequest();
request.setUsername("京东客服");
request.setPassword("123456");
request.setRole("normal");
request.setPlatform("jd");
userService.createUser(operator, request);
assertThat(inserted.get().getPlatform()).isEqualTo(AdminPlatformScope.JD);
}
@Test
void createUserRejectsIllegalPlatformValue() {
AdminUserEntity operator = operator(1L, "company_super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("company_super_admin");
AdminUserCreateRequest request = new AdminUserCreateRequest();
request.setUsername("京东客服");
request.setPassword("123456");
request.setRole("normal");
request.setPlatform("taobao");
assertThatThrownBy(() -> userService.createUser(operator, request))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("所属平台");
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void updateUserWithPlatformOnlyStillAppliesIt() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
when(userMapper.selectById(11L)).thenReturn(target(11L, "admin"));
when(userMapper.update(any(), any(LambdaUpdateWrapper.class))).thenReturn(1);
AdminUserUpdateRequest request = new AdminUserUpdateRequest();
request.setPlatform("jd");
userService.updateUser(operator, 11L, request);
LambdaUpdateWrapper<AdminUserEntity> update = capturedUserUpdate();
assertThat(update.getSqlSet()).contains("platform");
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void disabledOnlyUpdateDoesNotTouchPlatform() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
when(userMapper.selectById(11L)).thenReturn(target(11L, "admin"));
when(userMapper.update(any(), any(LambdaUpdateWrapper.class))).thenReturn(1);
AdminUserUpdateRequest request = new AdminUserUpdateRequest();
request.setDisabled(true);
userService.updateUser(operator, 11L, request);
LambdaUpdateWrapper<AdminUserEntity> update = capturedUserUpdate();
assertThat(update.getSqlSet()).contains("disabled_at");
assertThat(update.getSqlSet()).doesNotContain("platform");
}
@Test
void updateUserRejectsIllegalPlatformValue() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
when(userMapper.selectById(11L)).thenReturn(target(11L, "admin"));
AdminUserUpdateRequest request = new AdminUserUpdateRequest();
request.setPlatform("taobao");
assertThatThrownBy(() -> userService.updateUser(operator, 11L, request))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("所属平台");
}
// ---------------------------------------------------------------- 公司目录过滤与落库
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void companyListPlatformFilterBecomesAQueryCondition() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
standaloneCompanyService.listVisible(operator, "jd");
ArgumentCaptor<LambdaQueryWrapper> captor = ArgumentCaptor.forClass((Class) LambdaQueryWrapper.class);
verify(companyMapper).selectList(captor.capture());
// MP 的参数是惰性入表的:先渲染 SQL 段再断参数值
assertThat(captor.getValue().getSqlSegment()).contains("platform");
assertThat(captor.getValue().getParamNameValuePairs()).containsValue("jd");
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void companyListWithoutPlatformAddsNoCondition() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
standaloneCompanyService.listVisible(operator);
ArgumentCaptor<LambdaQueryWrapper> captor = ArgumentCaptor.forClass((Class) LambdaQueryWrapper.class);
verify(companyMapper).selectList(captor.capture());
assertThat(captor.getValue().getParamNameValuePairs()).isEmpty();
}
@Test
void createCompanyWithoutPlatformFallsBackToAmazon() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
AtomicReference<CompanyEntity> inserted = captureCompanyInsert();
CompanySaveRequest request = new CompanySaveRequest();
request.setCompanyName("京东事业部");
standaloneCompanyService.create(operator, request);
assertThat(inserted.get().getPlatform()).isEqualTo(AdminPlatformScope.AMAZON);
}
@Test
void updateCompanyKeepsPlatformWhenRequestOmitsIt() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
CompanyEntity existing = companyRow(2L, "京东事业部");
existing.setPlatform(AdminPlatformScope.JD);
when(companyMapper.selectById(2L)).thenReturn(existing);
when(companyMapper.updateById(any(CompanyEntity.class))).thenReturn(1);
CompanySaveRequest request = new CompanySaveRequest();
request.setCompanyName("京东事业部");
CompanyEntity saved = standaloneCompanyService.update(operator, 2L, request);
assertThat(saved.getPlatform()).isEqualTo(AdminPlatformScope.JD);
}
@Test
void updateCompanyWithExplicitPlatformReassignsIt() {
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
CompanyEntity existing = companyRow(2L, "老公司");
existing.setPlatform(AdminPlatformScope.AMAZON);
when(companyMapper.selectById(2L)).thenReturn(existing);
when(companyMapper.updateById(any(CompanyEntity.class))).thenReturn(1);
CompanySaveRequest request = new CompanySaveRequest();
request.setCompanyName("老公司");
request.setPlatform("jd");
CompanyEntity saved = standaloneCompanyService.update(operator, 2L, request);
assertThat(saved.getPlatform()).isEqualTo(AdminPlatformScope.JD);
}
// ---------------------------------------------------------------- 分组列表过滤与落库
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void groupListPlatformFilterBecomesAQueryCondition() {
groupService.list(null, null, "jd");
ArgumentCaptor<LambdaQueryWrapper> captor = ArgumentCaptor.forClass((Class) LambdaQueryWrapper.class);
verify(groupMapper).selectList(captor.capture());
// MP 的参数是惰性入表的:先渲染 SQL 段再断参数值
assertThat(captor.getValue().getSqlSegment()).contains("platform");
assertThat(captor.getValue().getParamNameValuePairs()).containsValue("jd");
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void groupListWithoutPlatformAddsNoCondition() {
groupService.list(null, null);
ArgumentCaptor<LambdaQueryWrapper> captor = ArgumentCaptor.forClass((Class) LambdaQueryWrapper.class);
verify(groupMapper).selectList(captor.capture());
assertThat(captor.getValue().getParamNameValuePairs()).isEmpty();
}
@Test
void groupListRejectsIllegalPlatformValue() {
assertThatThrownBy(() -> groupService.list(null, null, "taobao"))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("所属平台");
}
@Test
void createGroupWithExplicitPlatformStoresJd() {
stubGroupInsertCapturing();
AdminUserEntity leader = operator(2L, "admin", 5L);
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(groupAdminUserMapper.selectById(2L)).thenReturn(leader);
when(groupAdminUserMapper.selectById(1L)).thenReturn(operator);
ShopManageGroupCreateRequest request = new ShopManageGroupCreateRequest();
request.setGroupName("京东采集组");
request.setCreatedById(2L);
request.setPlatform("jd");
groupService.create(request, 1L, true);
assertThat(groupInsertCaptor.get().getPlatform()).isEqualTo(AdminPlatformScope.JD);
}
@Test
void createGroupWithoutPlatformFallsBackToAmazon() {
stubGroupInsertCapturing();
AdminUserEntity leader = operator(2L, "admin", 5L);
AdminUserEntity operator = operator(1L, "super_admin", 5L);
when(groupAdminUserMapper.selectById(2L)).thenReturn(leader);
when(groupAdminUserMapper.selectById(1L)).thenReturn(operator);
ShopManageGroupCreateRequest request = new ShopManageGroupCreateRequest();
request.setGroupName("亚马逊采集组");
request.setCreatedById(2L);
groupService.create(request, 1L, true);
assertThat(groupInsertCaptor.get().getPlatform()).isEqualTo(AdminPlatformScope.AMAZON);
}
@Test
void updateGroupKeepsPlatformWhenRequestOmitsIt() {
ShopManageGroupEntity existing = groupRow(9L, "采集组");
existing.setPlatform(AdminPlatformScope.JD);
when(groupMapper.selectById(9L)).thenReturn(existing);
AdminUserEntity leader = operator(2L, "admin", 5L);
when(groupAdminUserMapper.selectById(2L)).thenReturn(leader);
// validateManageAccess(超管档)还要按操作者再查一次公司:漏桩会让 requireCompanyId
// 拿到 null 账号、any(AdminUserEntity.class) 匹配不上 null ⇒ 组长/操作者公司判不一致
when(groupAdminUserMapper.selectById(1L)).thenReturn(operator(1L, "super_admin", 5L));
when(groupMapper.updateById(any(ShopManageGroupEntity.class))).thenReturn(1);
when(groupAdminUserMapper.selectBatchIds(any())).thenReturn(List.of());
ShopManageGroupUpdateRequest request = new ShopManageGroupUpdateRequest();
request.setGroupName("采集组");
groupService.update(9L, 1L, true, request);
ArgumentCaptor<ShopManageGroupEntity> captor = ArgumentCaptor.forClass(ShopManageGroupEntity.class);
verify(groupMapper).updateById(captor.capture());
assertThat(captor.getValue().getPlatform()).isEqualTo(AdminPlatformScope.JD);
}
@Test
void updateGroupWithExplicitPlatformReassignsIt() {
ShopManageGroupEntity existing = groupRow(9L, "采集组");
existing.setPlatform(AdminPlatformScope.AMAZON);
when(groupMapper.selectById(9L)).thenReturn(existing);
AdminUserEntity leader = operator(2L, "admin", 5L);
when(groupAdminUserMapper.selectById(2L)).thenReturn(leader);
// 同上:validateManageAccess(超管档)按操作者再查一次公司
when(groupAdminUserMapper.selectById(1L)).thenReturn(operator(1L, "super_admin", 5L));
when(groupMapper.updateById(any(ShopManageGroupEntity.class))).thenReturn(1);
when(groupAdminUserMapper.selectBatchIds(any())).thenReturn(List.of());
ShopManageGroupUpdateRequest request = new ShopManageGroupUpdateRequest();
request.setGroupName("采集组");
request.setPlatform("jd");
groupService.update(9L, 1L, true, request);
ArgumentCaptor<ShopManageGroupEntity> captor = ArgumentCaptor.forClass(ShopManageGroupEntity.class);
verify(groupMapper).updateById(captor.capture());
assertThat(captor.getValue().getPlatform()).isEqualTo(AdminPlatformScope.JD);
}
// ---------------------------------------------------------------- 取值出口自身
@Test
void normalizeOrCreateTrimsAndDefaultsBlankToAmazon() {
assertThat(AdminPlatformScope.normalizeOrCreate(null)).isEqualTo(AdminPlatformScope.AMAZON);
assertThat(AdminPlatformScope.normalizeOrCreate(" ")).isEqualTo(AdminPlatformScope.AMAZON);
assertThat(AdminPlatformScope.normalizeOrCreate(" jd ")).isEqualTo(AdminPlatformScope.JD);
assertThatThrownBy(() -> AdminPlatformScope.normalizeOrCreate(" taobao "))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("所属平台");
}
// ---------------------------------------------------------------- 夹具
private final AtomicReference<AdminUserEntity> userInsertCaptor = new AtomicReference<>();
private final AtomicReference<CompanyEntity> companyInsertCaptor = new AtomicReference<>();
private final AtomicReference<ShopManageGroupEntity> groupInsertCaptor = new AtomicReference<>();
private AtomicReference<AdminUserEntity> captureUserInsert() {
when(userMapper.insert(any(AdminUserEntity.class))).thenAnswer(inv -> {
AdminUserEntity entity = inv.getArgument(0);
entity.setId(99L);
userInsertCaptor.set(entity);
return 1;
});
return userInsertCaptor;
}
private AtomicReference<CompanyEntity> captureCompanyInsert() {
when(companyMapper.insert(any(CompanyEntity.class))).thenAnswer(inv -> {
CompanyEntity entity = inv.getArgument(0);
entity.setId(99L);
companyInsertCaptor.set(entity);
return 1;
});
return companyInsertCaptor;
}
private void stubGroupInsertCapturing() {
when(groupMapper.insert(any(ShopManageGroupEntity.class))).thenAnswer(inv -> {
ShopManageGroupEntity entity = inv.getArgument(0);
entity.setId(9L);
groupInsertCaptor.set(entity);
return 1;
});
// create() 末尾 toVo(getById(id)) 会按新 id 回读同一行
when(groupMapper.selectById(any())).thenAnswer(inv -> groupInsertCaptor.get());
}
@SuppressWarnings({"unchecked", "rawtypes"})
private LambdaQueryWrapper<AdminUserEntity> capturedUserCountQuery() {
ArgumentCaptor<LambdaQueryWrapper> captor = ArgumentCaptor.forClass((Class) LambdaQueryWrapper.class);
verify(userMapper).selectCount(captor.capture());
return captor.getValue();
}
@SuppressWarnings({"unchecked", "rawtypes"})
private LambdaUpdateWrapper<AdminUserEntity> capturedUserUpdate() {
ArgumentCaptor<LambdaUpdateWrapper> captor = ArgumentCaptor.forClass((Class) LambdaUpdateWrapper.class);
verify(userMapper).update(any(), captor.capture());
return captor.getValue();
}
private AdminUserEntity operator(Long id, String role, Long companyId) {
AdminUserEntity entity = new AdminUserEntity();
entity.setId(id);
entity.setUsername("op" + id);
entity.setRole(role);
entity.setIsAdmin("normal".equals(role) ? 0 : 1);
entity.setCompanyId(companyId);
return entity;
}
private AdminUserEntity target(Long id, String role) {
AdminUserEntity entity = new AdminUserEntity();
entity.setId(id);
entity.setUsername("u" + id);
entity.setRole(role);
entity.setIsAdmin("normal".equals(role) ? 0 : 1);
entity.setCompanyId(5L);
entity.setCreatedById(1L);
return entity;
}
private CompanyEntity companyRow(Long id, String name) {
CompanyEntity entity = new CompanyEntity();
entity.setId(id);
entity.setCompanyName(name);
entity.setStatus(CompanyService.STATUS_ACTIVE);
return entity;
}
private ShopManageGroupEntity groupRow(Long id, String name) {
ShopManageGroupEntity entity = new ShopManageGroupEntity();
entity.setId(id);
entity.setGroupName(name);
entity.setCreatedById(2L);
entity.setCompanyId(5L);
return entity;
}
}
@@ -60,6 +60,25 @@ class AdminUserServiceCompanySuperAdminTest {
when(permissionService.hasAnyAdminMenu(any(), any())).thenReturn(true);
}
@Test
void 京东额度允许零并拒绝负数和普通账号() {
AdminUserEntity operator = user(1L, "super_admin", 1L, null);
AdminUserEntity target = user(10L, "admin", 1L, 1L);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
when(userMapper.selectById(10L)).thenReturn(target);
AdminUserUpdateRequest request = new AdminUserUpdateRequest();
request.setJdPublishQuota(0);
service.updateUser(operator, 10L, request);
verify(userMapper).update(any(), any());
request.setJdPublishQuota(-1);
assertThatThrownBy(() -> service.updateUser(operator, 10L, request))
.isInstanceOf(BusinessException.class).hasMessageContaining("不能为负数");
request.setJdPublishQuota(1);
target.setRole("normal");
assertThatThrownBy(() -> service.updateUser(operator, 10L, request))
.isInstanceOf(BusinessException.class).hasMessageContaining("只能为管理员");
}
// ---------- 建号 ----------
@Test
@@ -57,7 +57,7 @@ class CompanyControllerEndpointTest {
body.setRemark("二期");
when(adminAuthSupport.requireAdminOrInternal(request)).thenReturn(operator);
when(companyService.listVisible(operator)).thenReturn(List.of(company(1L, "厦门米椿花")));
when(companyService.listVisible(operator, null)).thenReturn(List.of(company(1L, "厦门米椿花")));
when(companyService.create(any(), any())).thenReturn(company(2L, "福州数富"));
when(companyService.update(any(), any(), any())).thenReturn(company(2L, "福州数富科技"));
}
@@ -73,13 +73,13 @@ class CompanyControllerEndpointTest {
@Test
@DisplayName("每个端点先鉴权再进 service,顺序不能反")
void everyEndpointAuthenticatesBeforeTouchingTheDirectory() {
controller.listCompanies(request);
controller.listCompanies(request, null);
controller.createCompany(request, body);
controller.updateCompany(request, 2L, body);
InOrder order = inOrder(adminAuthSupport, companyService);
order.verify(adminAuthSupport).requireAdminOrInternal(request);
order.verify(companyService).listVisible(operator);
order.verify(companyService).listVisible(operator, null);
order.verify(adminAuthSupport).requireAdminOrInternal(request);
order.verify(companyService).create(operator, body);
order.verify(adminAuthSupport).requireAdminOrInternal(request);
@@ -108,7 +108,7 @@ class CompanyControllerEndpointTest {
@Test
@DisplayName("列表回包是 CompanyVo 而不是实体:实体字段是 camelCase,前端解析不到公司名")
void listReturnsVoItemsNotEntities() {
ApiResponse<List<CompanyVo>> response = controller.listCompanies(request);
ApiResponse<List<CompanyVo>> response = controller.listCompanies(request, null);
assertThat(response.getData()).hasSize(1);
assertThat(response.getData().get(0).getCompanyName()).isEqualTo("厦门米椿花");
@@ -88,10 +88,12 @@ class JdPublishQuotaServiceTest {
@Test
void 上限为0_整体放行且不查库() {
JdPublishQuotaService service = newService();
service.maxProductsPerAdmin = 0L;
assertThatCode(() -> service.assertWithinQuota(admin(5L), 999_999)).doesNotThrowAnyException();
verifyNoInteractions(jdProductMapper, userDataScopeSupport, adminUserMapper);
AdminUserEntity operator = admin(5L);
operator.setJdPublishQuota(0);
when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L)))
.thenReturn(List.of());
assertThatCode(() -> service.assertWithinQuota(operator, 999_999)).doesNotThrowAnyException();
verifyNoInteractions(jdProductMapper, adminUserMapper);
}
@Test
@@ -135,6 +137,19 @@ class JdPublishQuotaServiceTest {
assertThat(snapshot.unlimited()).isFalse();
}
@Test
void 员工使用所属管理员的独立额度() {
JdPublishQuotaService service = newService();
AdminUserEntity manager = admin(5L);
manager.setJdPublishQuota(12);
when(adminUserMapper.selectById(5L)).thenReturn(manager);
when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L)))
.thenReturn(List.of(5L, 9L));
when(jdProductMapper.countByUserIds(List.of(5L, 9L))).thenReturn(12L);
assertThatThrownBy(() -> service.assertWithinQuota(employee(9L, 5L), 1))
.isInstanceOf(BusinessException.class).hasMessageContaining("12");
}
@Test
void 普通账号读不到主管_回退按自己计() {
JdPublishQuotaService service = newService();