package com.bms.receivable; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import com.bms.receivable.domain.ArBillDetail; import com.bms.receivable.domain.vo.ArBillDetailQuery; import com.bms.receivable.mapper.ArBillDetailMapper; import com.bms.receivable.security.ArBillRowScope; import com.bms.receivable.security.ArBillScopeResolver; import com.bms.receivable.service.impl.ArBillServiceImpl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.parser.defaults.DefaultCountSqlParser; import org.apache.ibatis.builder.xml.XMLMapperBuilder; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.session.Configuration; import org.junit.jupiter.api.Test; import org.springframework.test.util.ReflectionTestUtils; class ArBillDetailSearchTest { @Test void numberSearchUsesExactMatchesAndCannotBypassBillOrPermissionScope() throws Exception { ArBillDetailQuery query = new ArBillDetailQuery(); query.setOrderKeyword("00123' OR 1=1 --"); BoundSql bound = detailSql(query, scope(false, true)); String sql = normalized(bound.getSql()); assertTrue(sql.contains("where ar_bill_id = ?")); assertTrue(sql.contains("and (order_no = ? or tracking_no = ? or transfer_no = ?)")); assertTrue(sql.contains("b.ar_bill_id = ar_bill_detail.ar_bill_id")); assertTrue(sql.contains("c.customer_id = b.customer_id")); assertTrue(sql.contains("su.user_id = ?")); assertFalse(sql.contains("order_no like")); assertFalse(sql.contains(query.getOrderKeyword())); assertEquals(3, bound.getParameterMappings().stream() .filter(parameter -> parameter.getProperty().equals("query.orderKeyword")).count()); } @Test void productSearchAndPaginationCountApplyBothFiltersAndTheSameScope() throws Exception { ArBillDetailQuery query = new ArBillDetailQuery(); query.setOrderKeyword("ORDER001"); query.setProductKeyword("荷兰"); String sql = normalized(detailSql(query, scope(false, true)).getSql()); assertTrue(sql.contains("and (product_code like concat('%', ?, '%') or product_name like concat('%', ?, '%'))")); String count = normalized(new DefaultCountSqlParser().getSmartCountSql(sql, "0")); assertTrue(count.startsWith("select count(0)")); assertTrue(count.contains("ar_bill_id = ?")); assertTrue(count.contains("order_no = ?")); assertTrue(count.contains("tracking_no = ?")); assertTrue(count.contains("transfer_no = ?")); assertTrue(count.contains("product_code like")); assertTrue(count.contains("product_name like")); assertTrue(count.contains("su.user_id = ?")); } @Test void noSearchKeepsExistingQueryAndDeniedScopeRemainsDenied() throws Exception { for (ArBillDetailQuery query : new ArBillDetailQuery[]{null, new ArBillDetailQuery()}) { String sql = normalized(detailSql(query, scope(false, false)).getSql()); assertFalse(sql.contains("order_no = ?")); assertFalse(sql.contains("product_code like")); assertTrue(sql.contains("and 1 = 0")); assertTrue(sql.contains("where ar_bill_id = ?")); assertTrue(sql.endsWith("order by detail_no")); } } @Test void serviceTrimsSearchAndPreservesPaginationForTheScopedDetailQuery() { ArBillServiceImpl service = new ArBillServiceImpl(); ArBillDetailMapper details = mock(ArBillDetailMapper.class); ArBillScopeResolver resolver = mock(ArBillScopeResolver.class); ArBillRowScope scope = mock(ArBillRowScope.class); ReflectionTestUtils.setField(service, "detailMapper", details); ReflectionTestUtils.setField(service, "billScope", resolver); when(resolver.resolve()).thenReturn(scope); List result = List.of(new ArBillDetail()); when(details.selectByBillIdScoped(eq(7L), any(), same(scope))).thenAnswer(invocation -> { ArBillDetailQuery query = invocation.getArgument(1); assertEquals("000123", query.getOrderKeyword()); assertEquals("荷兰", query.getProductKeyword()); assertEquals(3, PageHelper.getLocalPage().getPageNum()); return result; }); ArBillDetailQuery query = new ArBillDetailQuery(); query.setOrderKeyword(" 000123 "); query.setProductKeyword(" 荷兰 "); PageHelper.startPage(3, 10); try { assertSame(result, service.selectArBillDetails(7L, query)); verify(details).selectByBillIdScoped(eq(7L), any(), same(scope)); verifyNoMoreInteractions(details); } finally { PageHelper.clearPage(); } } @Test void wholeBillExportQueryDoesNotInheritDetailSearchFilters() throws Exception { String sql = normalized(configuration().getMappedStatement("com.bms.receivable.mapper.ArBillDetailMapper.selectExportPage") .getBoundSql(Map.of("arBillId", 7L, "lastDetailNo", 0, "pageSize", 2000)).getSql()); assertTrue(sql.contains("where ar_bill_id = ? and detail_no > ?")); assertFalse(sql.contains("order_no = ?")); assertFalse(sql.contains("product_code like")); assertTrue(sql.endsWith("limit ?")); } private BoundSql detailSql(ArBillDetailQuery query, Map scope) throws Exception { Map parameters = new HashMap<>(); parameters.put("arBillId", 7L); parameters.put("query", query); parameters.put("scope", scope); return configuration().getMappedStatement("com.bms.receivable.mapper.ArBillDetailMapper.selectByBillIdScoped") .getBoundSql(parameters); } private Configuration configuration() throws Exception { Configuration configuration = new Configuration(); configuration.getTypeAliasRegistry().registerAlias("ArBillDetail", ArBillDetail.class); for (String resource : List.of("mapper/receivable/ArBillScopeMapper.xml", "mapper/receivable/ArBillDetailMapper.xml")) { try (InputStream input = getClass().getClassLoader().getResourceAsStream(resource)) { new XMLMapperBuilder(input, configuration, resource, configuration.getSqlFragments()).parse(); } } return configuration; } private Map scope(boolean all, boolean self) { return Map.of("all", all, "self", self, "department", false, "userId", 9L, "deptId", 10L); } private String normalized(String sql) { return sql.replaceAll("\\s+", " ").trim().toLowerCase(Locale.ROOT); } }