fix(商品类目): 非超管读侧钉本公司 + 写侧补行归属闸门
10-06 逐节点复验实测:公司 2 的 admin 读 /api/admin/product-categories 仍得 412 行, 其中 394 行是公司 1 的自建类目。根因是这一页走的是第二道门 readCompanyId —— 它「非超管返回 null(不筛)」是为品牌库「靠分组可见的行」刻意设计的, 而 biz_product_category 连账号列都没有,null 在这里就是把整棵树交给每一家。 - DataReadScope 新增第三道门 readCompanyIdOrOwn:超管按 tab,非超管(含 company_super_admin)一律 requireCompanyId(operator),带别家 tab 也不越权、也不返回空 (类目树没有「空树」这个可用视图,返回空会连自建根都挂不上)。 - 写侧同批补闸门:读收窄而写不收窄=藏起来的行照样能改名/删除,那是静默删除不是隔离。 update/delete 收 writeCompanyId(operator),与行自己的 company_id 比(不与 tab 比: 内置行归哨兵 0,任何公司账号都不该改得动它);判定复用 CompanyScopeSupport .assertSameCompany,不再各处比一次。 - 品牌库那扇门与其三条「非超管=null」用例原样保留 —— 那是另一张表的正确口径。 - 护栏:G2 把第三道门接进 SCOPE_DOORS/SCOPE_CALLEE_NAMES,并自带能红/能绿两向探针 (只认 readScope 的自检等于第二、三道门没人看守);新增 14 例,后端全量 3841 例 0 红。
This commit is contained in:
+40
-7
@@ -47,24 +47,24 @@ class ProductCategoryControllerCompanyScopeTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("列表:tab 的公司 id 交给 readCompanyId,出口给什么 service 就用什么")
|
||||
@DisplayName("列表:tab 的公司 id 交给 readCompanyIdOrOwn,出口给什么 service 就用什么")
|
||||
void listPassesTheTabValueThroughTheDoor() {
|
||||
HttpServletRequest request = login();
|
||||
when(adminAuthSupport.requireAdminOrInternal(request)).thenReturn(user());
|
||||
when(dataReadScope.readCompanyId(any(AdminUserEntity.class), eq(COMPANY_ID))).thenReturn(COMPANY_ID);
|
||||
when(dataReadScope.readCompanyIdOrOwn(any(AdminUserEntity.class), eq(COMPANY_ID))).thenReturn(COMPANY_ID);
|
||||
|
||||
controller().list(request, "面霜", COMPANY_ID);
|
||||
|
||||
verify(dataReadScope).readCompanyId(any(AdminUserEntity.class), eq(COMPANY_ID));
|
||||
verify(dataReadScope).readCompanyIdOrOwn(any(AdminUserEntity.class), eq(COMPANY_ID));
|
||||
verify(productCategoryService).list("面霜", COMPANY_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("列表:非超管由出口判成\"不筛\",controller 不许自己补一个公司 id 进去")
|
||||
void listUsesTheDorsVerdictForNonSuperAdmin() {
|
||||
void listUsesTheDoorsVerdictForNonSuperAdmin() {
|
||||
HttpServletRequest request = login();
|
||||
when(adminAuthSupport.requireAdminOrInternal(request)).thenReturn(user());
|
||||
when(dataReadScope.readCompanyId(any(AdminUserEntity.class), eq(COMPANY_ID))).thenReturn(null);
|
||||
when(dataReadScope.readCompanyIdOrOwn(any(AdminUserEntity.class), eq(COMPANY_ID))).thenReturn(null);
|
||||
|
||||
controller().list(request, "面霜", COMPANY_ID);
|
||||
|
||||
@@ -76,7 +76,7 @@ class ProductCategoryControllerCompanyScopeTest {
|
||||
void everyReadEndpointGoesThroughTheDoor() {
|
||||
HttpServletRequest request = login();
|
||||
when(adminAuthSupport.requireAdminOrInternal(request)).thenReturn(user());
|
||||
when(dataReadScope.readCompanyId(any(AdminUserEntity.class), eq(COMPANY_ID))).thenReturn(COMPANY_ID);
|
||||
when(dataReadScope.readCompanyIdOrOwn(any(AdminUserEntity.class), eq(COMPANY_ID))).thenReturn(COMPANY_ID);
|
||||
// export 的返回要真喂给 ResponseEntity.contentLength(bytes.length):mock 给 null 会 NPE 在断言之前的装配里。
|
||||
when(productCategoryService.export("面霜", COMPANY_ID)).thenReturn(new byte[0]);
|
||||
ProductCategoryController controller = controller();
|
||||
@@ -89,7 +89,7 @@ class ProductCategoryControllerCompanyScopeTest {
|
||||
verify(productCategoryService).search("面霜", 1L, 20L, COMPANY_ID);
|
||||
verify(productCategoryService).export("面霜", COMPANY_ID);
|
||||
verify(dataReadScope, times(3))
|
||||
.readCompanyId(any(AdminUserEntity.class), eq(COMPANY_ID));
|
||||
.readCompanyIdOrOwn(any(AdminUserEntity.class), eq(COMPANY_ID));
|
||||
}
|
||||
|
||||
/** 写侧口径(一期定案):归属由后端从操作者会话推导,请求体不带 company_id,controller 也不读 tab。 */
|
||||
@@ -110,6 +110,39 @@ class ProductCategoryControllerCompanyScopeTest {
|
||||
verifyNoInteractions(dataReadScope);
|
||||
}
|
||||
|
||||
/** 写侧闸门(10-06 逐节点复验后补):读侧已经收窄,写侧不收窄="藏起来的行照样能删"。 */
|
||||
@Test
|
||||
@DisplayName("更新/删除:把 writeCompanyId 的判定原样交给 service,controller 不自己补公司 id")
|
||||
void writeEndpointsPassTheRowOwnershipGate() {
|
||||
HttpServletRequest request = login();
|
||||
when(adminAuthSupport.requireAdminOrInternal(request)).thenReturn(user());
|
||||
when(dataReadScope.writeCompanyId(any(AdminUserEntity.class))).thenReturn(COMPANY_ID);
|
||||
ProductCategorySaveRequest body = new ProductCategorySaveRequest();
|
||||
body.setName("面霜改名");
|
||||
ProductCategoryController controller = controller();
|
||||
|
||||
controller.update(request, 300L, body);
|
||||
controller.delete(request, 300L);
|
||||
|
||||
verify(productCategoryService).update(300L, body, COMPANY_ID);
|
||||
verify(productCategoryService).delete(300L, COMPANY_ID);
|
||||
}
|
||||
|
||||
/** 反向钉:闸门给 null(平台超管)时必须原样传 null——顺手补一个公司 id 会把超管锁死在本公司。 */
|
||||
@Test
|
||||
@DisplayName("更新:闸门判超管(null)时把 null 传下去,不补任何公司 id")
|
||||
void writeGateKeepsTheSuperAdminVerdict() {
|
||||
HttpServletRequest request = login();
|
||||
when(adminAuthSupport.requireAdminOrInternal(request)).thenReturn(user());
|
||||
when(dataReadScope.writeCompanyId(any(AdminUserEntity.class))).thenReturn(null);
|
||||
ProductCategorySaveRequest body = new ProductCategorySaveRequest();
|
||||
body.setName("内置类目改名");
|
||||
|
||||
controller().update(request, 10L, body);
|
||||
|
||||
verify(productCategoryService).update(10L, body, null);
|
||||
}
|
||||
|
||||
private MockHttpServletRequest login() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("X-Internal-Token", "irrelevant-here");
|
||||
|
||||
+108
-1
@@ -29,9 +29,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
@@ -230,7 +232,7 @@ class ProductCategoryCompanyScopeTest {
|
||||
when(productCategoryMapper.selectCount(any())).thenReturn(0L);
|
||||
when(productCategoryMapper.updateById(any(ProductCategoryEntity.class))).thenReturn(1);
|
||||
|
||||
service.update(300L, saveRequest("面霜改名", BUILTIN_PARENT_ID));
|
||||
service.update(300L, saveRequest("面霜改名", BUILTIN_PARENT_ID), COMPANY_ID);
|
||||
|
||||
LambdaQueryWrapper<ProductCategoryEntity> sibling = countWrappers().get(0);
|
||||
assertTrue(sibling.getSqlSegment().contains("company_id IN"), sibling.getSqlSegment());
|
||||
@@ -240,6 +242,111 @@ class ProductCategoryCompanyScopeTest {
|
||||
"把行挪到可见集外的节点下面会让它从原本看得见它的 tab 里消失:" + params);
|
||||
}
|
||||
|
||||
// ---- 写侧行归属闸门(10-06 逐节点复验后补)----
|
||||
// 读侧已经把这一页收窄到本公司,写侧不收窄="藏起来的行照样能改名/删除",那是静默删除不是隔离。
|
||||
// assertSameCompany 自己的判定由 CompanyScopeSupportTest 钉(本仓唯一一份),这里只钉"service 有没有
|
||||
// 把这一刀落在正确的两个值上、被拒之后还写不写库"——所以桩按真方法的语义给:只有配对不对才抛。
|
||||
|
||||
@Test
|
||||
@DisplayName("更新别家公司的行:闸门 403 且 updateById 一次都不跑")
|
||||
void updateRejectsARowOwnedByAnotherCompany() {
|
||||
Long foreignCompanyId = 3L;
|
||||
when(productCategoryMapper.selectById(300L)).thenReturn(row(300L, null, "别家的根", foreignCompanyId));
|
||||
stubSameCompanyJudgment();
|
||||
|
||||
BusinessException exception = assertThrows(BusinessException.class,
|
||||
() -> service.update(300L, saveRequest("改名", null), COMPANY_ID));
|
||||
|
||||
assertEquals(403, exception.getCode());
|
||||
verify(productCategoryMapper, never()).updateById(any(ProductCategoryEntity.class));
|
||||
// 断言闸门拿到的是<b>行自己的</b>归属而不是 tab 之外的什么值:喂错第二个实参,这一刀形同没落。
|
||||
verify(companyScopeSupport).assertSameCompany(eq(COMPANY_ID), eq(foreignCompanyId), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("更新内置节点(哨兵 0):公司账号一律拒,改了它所有公司的树与货源归类同一天跟着变")
|
||||
void updateRejectsTheSharedBuiltinRow() {
|
||||
when(productCategoryMapper.selectById(BUILTIN_PARENT_ID))
|
||||
.thenReturn(row(BUILTIN_PARENT_ID, null, "护肤品", BUILTIN));
|
||||
stubSameCompanyJudgment();
|
||||
|
||||
BusinessException exception = assertThrows(BusinessException.class,
|
||||
() -> service.update(BUILTIN_PARENT_ID, saveRequest("护肤品改名", null), COMPANY_ID));
|
||||
|
||||
assertEquals(403, exception.getCode());
|
||||
verify(productCategoryMapper, never()).updateById(any(ProductCategoryEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("更新:本公司自己的行放行(闸门不该把这一页的正常编辑也拦死)")
|
||||
void updateAcceptsTheRowsOwnCompany() {
|
||||
when(productCategoryMapper.selectById(300L)).thenReturn(row(300L, null, "面霜", COMPANY_ID));
|
||||
when(productCategoryMapper.selectCount(any())).thenReturn(0L);
|
||||
when(productCategoryMapper.updateById(any(ProductCategoryEntity.class))).thenReturn(1);
|
||||
stubSameCompanyJudgment();
|
||||
|
||||
service.update(300L, saveRequest("面霜改名", null), COMPANY_ID);
|
||||
|
||||
verify(productCategoryMapper).updateById(any(ProductCategoryEntity.class));
|
||||
}
|
||||
|
||||
/** 反向钉(多隔离那一向):闸门判 null = 平台超管不限,顺手补一个公司 id 会把超管锁死在本公司。 */
|
||||
@Test
|
||||
@DisplayName("更新:超管口径(null)不调用行归属闸门,内置节点照样能改")
|
||||
void superAdminUpdateSkipsTheRowGate() {
|
||||
when(productCategoryMapper.selectById(BUILTIN_PARENT_ID))
|
||||
.thenReturn(row(BUILTIN_PARENT_ID, null, "护肤品", BUILTIN));
|
||||
when(productCategoryMapper.selectCount(any())).thenReturn(0L);
|
||||
when(productCategoryMapper.updateById(any(ProductCategoryEntity.class))).thenReturn(1);
|
||||
|
||||
service.update(BUILTIN_PARENT_ID, saveRequest("护肤品改名", null), null);
|
||||
|
||||
verify(productCategoryMapper).updateById(any(ProductCategoryEntity.class));
|
||||
verifyNoInteractions(companyScopeSupport);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("删除:闸门在数子级之前,别家的行连「有几个子节点」都不去查")
|
||||
void deleteRejectsARowOwnedByAnotherCompany() {
|
||||
Long foreignCompanyId = 3L;
|
||||
when(productCategoryMapper.selectById(400L)).thenReturn(row(400L, null, "别家的叶子", foreignCompanyId));
|
||||
stubSameCompanyJudgment();
|
||||
|
||||
BusinessException exception = assertThrows(BusinessException.class,
|
||||
() -> service.delete(400L, COMPANY_ID));
|
||||
|
||||
assertEquals(403, exception.getCode());
|
||||
verify(productCategoryMapper, never()).deleteById(anyLong());
|
||||
verify(productCategoryMapper, never()).selectCount(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("删除:本公司自己的行放行并真删")
|
||||
void deleteAcceptsTheRowsOwnCompany() {
|
||||
when(productCategoryMapper.selectById(401L)).thenReturn(row(401L, null, "自建叶子", COMPANY_ID));
|
||||
when(productCategoryMapper.selectCount(any())).thenReturn(0L);
|
||||
stubSameCompanyJudgment();
|
||||
|
||||
service.delete(401L, COMPANY_ID);
|
||||
|
||||
verify(productCategoryMapper).deleteById(401L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把 {@code assertSameCompany} 按<b>真方法的语义</b>桩上(第一参 ≠ 第二参才抛 403)。
|
||||
* 不这么写的话 Mockito 对 void 方法默认"什么都不做",三条拒绝用例会因为错误的理由恒绿。
|
||||
*/
|
||||
private void stubSameCompanyJudgment() {
|
||||
org.mockito.Mockito.doAnswer(call -> {
|
||||
Long operatorCompanyId = call.getArgument(0);
|
||||
Long rowCompanyId = call.getArgument(1);
|
||||
if (rowCompanyId == null || !rowCompanyId.equals(operatorCompanyId)) {
|
||||
throw new BusinessException(403, call.getArgument(2, String.class));
|
||||
}
|
||||
return null;
|
||||
}).when(companyScopeSupport).assertSameCompany(any(), any(), any());
|
||||
}
|
||||
|
||||
private ProductCategorySaveRequest saveRequest(String name, Long parentId) {
|
||||
ProductCategorySaveRequest request = new ProductCategorySaveRequest();
|
||||
request.setName(name);
|
||||
|
||||
Reference in New Issue
Block a user