feat(公司隔离): 加 CompanyScopeSupport 与二期实施计划
取不到公司一律抛 BusinessException,不返回 null:null 进实体会被 MyBatis-Plus 跳过该列,落库时由 V145 的列 DEFAULT=1 静默兜住,第二家公司开通后每个漏传都 会归错公司且不报错。同时把 18 号实施计划入库(docs/plans 已不再 gitignore)。
This commit is contained in:
+76
@@ -0,0 +1,76 @@
|
||||
package com.nanri.aiimage.common.service;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 二期数据隔离的唯一取公司出口(设计文档 §3.1)。
|
||||
*
|
||||
* <p>这里最重要的不是"能取到公司",而是<b>取不到时必须炸</b>:漏传 company_id 一旦退回 null,
|
||||
* 下游就落进 V145 的列 DEFAULT=1,第二家公司开通后每个漏传都会静默归到厦门米椿花(§6.3 / §11.2)。</p>
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class CompanyScopeSupportTest {
|
||||
|
||||
@Mock AdminUserMapper adminUserMapper;
|
||||
@InjectMocks CompanyScopeSupport companyScope;
|
||||
|
||||
@Test
|
||||
void resolvesCompanyIdFromUserIdThroughUsersTable() {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(7L);
|
||||
user.setCompanyId(3L);
|
||||
when(adminUserMapper.selectById(7L)).thenReturn(user);
|
||||
|
||||
assertThat(companyScope.requireCompanyId(7L)).isEqualTo(3L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullUserIdFailsFastInsteadOfReturningNull() {
|
||||
assertThatThrownBy(() -> companyScope.requireCompanyId((Long) null))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("归属账号");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownUserFailsFast() {
|
||||
when(adminUserMapper.selectById(99L)).thenReturn(null);
|
||||
|
||||
assertThatThrownBy(() -> companyScope.requireCompanyId(99L))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("归属账号不存在");
|
||||
}
|
||||
|
||||
/** V144 之后 users.company_id 已无 DEFAULT:手工 SQL 或旧 dump 重建都可能留下 NULL。 */
|
||||
@Test
|
||||
void userWithoutCompanyFailsFastWith403() {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(8L);
|
||||
user.setCompanyId(null);
|
||||
when(adminUserMapper.selectById(8L)).thenReturn(user);
|
||||
|
||||
assertThatThrownBy(() -> companyScope.requireCompanyId(8L))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("没有所属公司");
|
||||
}
|
||||
|
||||
@Test
|
||||
void assertSameCompanyRejectsForeignAndMissingRowCompany() {
|
||||
companyScope.assertSameCompany(1L, 1L, "无权操作其他公司的数据");
|
||||
|
||||
assertThatThrownBy(() -> companyScope.assertSameCompany(1L, 2L, "无权操作其他公司的数据"))
|
||||
.isInstanceOf(BusinessException.class).hasMessageContaining("其他公司");
|
||||
assertThatThrownBy(() -> companyScope.assertSameCompany(1L, null, "无权操作其他公司的数据"))
|
||||
.isInstanceOf(BusinessException.class).hasMessageContaining("其他公司");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user