test(公司tab): 补分组 wrapper 用例,反向钉改绑到参数绑定本身
评审两处咬不住的地方:分组侧 list() 那条 created_by_id=? OR user_id=? 被公司条件
平铺时,MySQL 的 AND 优先会让前半个 OR 整块逃出筛选(多放行、接口照样 200),而这
一Shape 全仓没有用例捕;读入口的反向钉只断 contains("company_id"),而 @Operation
的描述里就写着这个词,删掉绑定也不会红。
顺带删掉没人调的 list(Long) 重载,并把"空成员集"两支落到 id=-1 哨兵上(IN () 是
MySQL 语法错,退化成不加条件则是把全量放出来)。
This commit is contained in:
+1
-5
@@ -48,10 +48,6 @@ public class ShopManageGroupService {
|
||||
return list(null, null);
|
||||
}
|
||||
|
||||
public List<ShopManageGroupItemVo> list(Long createdById) {
|
||||
return list(createdById, null);
|
||||
}
|
||||
|
||||
public List<ShopManageGroupItemVo> list(Long createdById, List<Long> companyMemberIds) {
|
||||
LambdaQueryWrapper<ShopManageGroupEntity> qw = new LambdaQueryWrapper<ShopManageGroupEntity>()
|
||||
.orderByAsc(ShopManageGroupEntity::getId);
|
||||
@@ -65,7 +61,7 @@ public class ShopManageGroupService {
|
||||
if (companyMemberIds.isEmpty()) {
|
||||
qw.eq(ShopManageGroupEntity::getId, -1L);
|
||||
} else {
|
||||
// 公司条件同样自成一组(G2 护栏钉的是 AND (…) 这个形状;平铺 IN 与它在语义上等价)。
|
||||
// 同样自成一组:上面那支 and() 里已经混着 OR,这条若平铺下去,AND 优先只会作用在 OR 的右半边。
|
||||
qw.and(w -> w.in(ShopManageGroupEntity::getCreatedById, companyMemberIds));
|
||||
}
|
||||
}
|
||||
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
package com.nanri.aiimage.modules.shopkey.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.mapper.ShopManageGroupMapper;
|
||||
import com.nanri.aiimage.common.model.entity.ShopManageGroupEntity;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Task 11 分组侧(计划 Step 1 点名的新建文件):{@code ShopManageGroupService.list} 叠公司条件时,
|
||||
* 既有那条 {@code created_by_id = ? OR user_id = ?} 必须整块留在括号里。
|
||||
*
|
||||
* <p>这是本任务唯一一处「错了不报错、只是多放行」的形状:MP 的 normal 段按顺序拼 AND/OR,而 MySQL 里
|
||||
* AND 优先 ⇒ 平铺成 {@code created_by_id = ? OR user_id = ? AND created_by_id IN (...)} 时,第一个
|
||||
* {@code created_by_id = ?} 整块逃出公司筛选,别家的分组照样列出来,接口还是 200。</p>
|
||||
*
|
||||
* <p>断言口径与 {@code ShopManageServiceTest} 同一条:<b>结构</b>(括号、OR、IN)从
|
||||
* {@code getSqlSegment()} 读,<b>值</b>从 {@code getParamNameValuePairs()} 读——MP 在 segment 里只放
|
||||
* {@code #{ew.paramNameValuePairs.MPGENVALn}} 占位,写 {@code segment.contains("11")} 是恒假断言。
|
||||
* 反向钉(没给成员集不许叠条件)与它的正样本写在同一支用例里,两支护一支持平。</p>
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ShopManageGroupServiceTest {
|
||||
|
||||
@Mock
|
||||
private ShopManageGroupMapper groupMapper;
|
||||
|
||||
/** 只给 groupMapper 打桩;其余构造参数留 null——本文件三支用例都只走 list() 的第一段查询。 */
|
||||
@InjectMocks
|
||||
private ShopManageGroupService groupService;
|
||||
|
||||
/** getSqlSegment() 解析 lambda 靠 TableInfo 缓存,单测没有 Spring 容器建它,不初始化就抛 "can not find lambda cache"。 */
|
||||
@BeforeEach
|
||||
void initTableInfo() {
|
||||
TableInfoHelper.initTableInfo(
|
||||
new MapperBuilderAssistant(new MybatisConfiguration(), ""), ShopManageGroupEntity.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private List<LambdaQueryWrapper<ShopManageGroupEntity>> captureAll(int expectedInvocations) {
|
||||
ArgumentCaptor<LambdaQueryWrapper> captor = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
||||
verify(groupMapper, times(expectedInvocations)).selectList(captor.capture());
|
||||
// captor 是 raw 的(ArgumentCaptor 对泛型参数只能按原始类型捕),这里过 raw List 而不是逐个 cast。
|
||||
return (List) captor.getAllValues();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("分组:list 叠公司必须把既有那条平铺 OR 整个套进括号(否则 created_by_id=? 那半边逃出筛选)")
|
||||
void groupListWrapsTheOrBeforeNarrowing() {
|
||||
when(groupMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
groupService.list(5L, List.of(11L));
|
||||
|
||||
LambdaQueryWrapper<ShopManageGroupEntity> q = captureAll(1).get(0);
|
||||
String segment = q.getSqlSegment();
|
||||
assertThat(segment).as("OR 组必须被 and() 包住").contains("(created_by_id").contains(" OR ");
|
||||
assertThat(segment.lastIndexOf("AND ("))
|
||||
.as("公司条件必须自成一组,不能与 OR 平铺")
|
||||
.isGreaterThan(segment.indexOf("created_by_id"));
|
||||
assertThat(q.getParamNameValuePairs().values()).contains(11L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("分组:成员集为空 ⇒ 走 id = -1 恒空,绝不渲染 IN ()(MySQL 语法错)")
|
||||
void groupListEmptyMemberSetUsesSentinel() {
|
||||
when(groupMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
groupService.list(5L, List.of());
|
||||
|
||||
LambdaQueryWrapper<ShopManageGroupEntity> q = captureAll(1).get(0);
|
||||
assertThat(q.getSqlSegment()).doesNotContain("IN (");
|
||||
assertThat(q.getParamNameValuePairs().values())
|
||||
.as("空集必须落到恒不命中的哨兵 id 上,而不是退化成「不加条件」= 看全量")
|
||||
.contains(-1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("分组:只有给了成员集才叠公司条件——正反两向写在同一支用例里,反向钉不许独自成立")
|
||||
void companyLegAppearsOnlyWhenMemberSetGiven() {
|
||||
when(groupMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
groupService.list(null, null);
|
||||
groupService.list(null, List.of(11L, 12L));
|
||||
|
||||
List<LambdaQueryWrapper<ShopManageGroupEntity>> both = captureAll(2);
|
||||
LambdaQueryWrapper<ShopManageGroupEntity> unfiltered = both.get(0);
|
||||
LambdaQueryWrapper<ShopManageGroupEntity> narrowed = both.get(1);
|
||||
assertThat(unfiltered.getSqlSegment()).doesNotContain("IN (");
|
||||
assertThat(unfiltered.getParamNameValuePairs().values()).doesNotContain(11L, 12L);
|
||||
// 正样本:上一支的 doesNotContain 只有在这一支真的会渲染 IN 时才有意义。
|
||||
assertThat(narrowed.getSqlSegment()).contains("IN (");
|
||||
assertThat(narrowed.getParamNameValuePairs().values()).contains(11L, 12L);
|
||||
}
|
||||
}
|
||||
+25
-5
@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.ShopManageGroupEntity;
|
||||
import com.nanri.aiimage.common.security.ShopCredentialCryptoService;
|
||||
import com.nanri.aiimage.modules.shopkey.mapper.ShopManageMapper;
|
||||
import com.nanri.aiimage.modules.shopkey.model.entity.ShopManageEntity;
|
||||
@@ -50,8 +49,6 @@ class ShopManageServiceTest {
|
||||
void initTableInfo() {
|
||||
TableInfoHelper.initTableInfo(
|
||||
new MapperBuilderAssistant(new MybatisConfiguration(), ""), ShopManageEntity.class);
|
||||
TableInfoHelper.initTableInfo(
|
||||
new MapperBuilderAssistant(new MybatisConfiguration(), ""), ShopManageGroupEntity.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -89,6 +86,22 @@ class ShopManageServiceTest {
|
||||
assertThat(captor.getValue().getParamNameValuePairs().values()).contains(11L, 12L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("读:超管选了一家没有成员的公司 ⇒ 走 id = -1 恒空,不许退化成不加条件")
|
||||
void superAdminPageWithEmptyMemberSetStaysEmpty() {
|
||||
when(shopManageMapper.selectCount(any())).thenReturn(0L);
|
||||
|
||||
service.page(1L, 15L, null, null, 1L, true, List.of());
|
||||
|
||||
ArgumentCaptor<LambdaQueryWrapper<ShopManageEntity>> captor =
|
||||
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
||||
verify(shopManageMapper).selectCount(captor.capture());
|
||||
assertThat(captor.getValue().getSqlSegment())
|
||||
.as("空集渲染 IN () 是 MySQL 语法错;退化成不加条件则是把全量放出来")
|
||||
.doesNotContain("IN (");
|
||||
assertThat(captor.getValue().getParamNameValuePairs().values()).contains(-1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("凭据:非超管读「创建人不认识、分组也不认识」的行 ⇒ 403(§6.1 列表筛了按 id 照读)")
|
||||
void credentialByIdRejectsRowOutsideBothVisibleSets() {
|
||||
@@ -113,7 +126,7 @@ class ShopManageServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("凭据:超管不查可见集(今天不筛、tab 也不是安全边界;短路写了就红在 NPE 上)")
|
||||
@DisplayName("凭据:超管不查可见集(今天不筛、tab 也不是安全边界;删掉短路就红在 403)")
|
||||
void credentialByIdStaysUnfilteredForSuperAdmin() {
|
||||
when(shopManageMapper.selectById(79L)).thenReturn(shop(79L, 999L, 7L));
|
||||
|
||||
@@ -139,6 +152,13 @@ class ShopManageServiceTest {
|
||||
.doesNotContain("company_id")
|
||||
.doesNotContain("companyId");
|
||||
}
|
||||
assertThat(SourceScan.methodBody(text, "page")).contains("company_id");
|
||||
// 取样自检必须钉「绑定」本身:methodBody 含注解,而 @Operation 的文案里就写着 company_id,
|
||||
// 只断 contains("company_id") 会在参数被删掉后照样绿——删掉绑定后 G2 的 A/B 两支也都不再触发。
|
||||
assertThat(SourceScan.methodBody(text, "page"))
|
||||
.as("page 的 company_id 绑定一旦消失,这一整页的 tab 就成了装饰品,且没有任何尺子会红")
|
||||
.contains("@RequestParam(name = \"company_id\"");
|
||||
assertThat(SourceScan.methodBody(text, "listGroups"))
|
||||
.as("listGroups 同样钉在自己的注解上,不是文件里另一处的字面量")
|
||||
.contains("@RequestParam(name = \"company_id\"");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user