|
@@ -0,0 +1,107 @@
|
|
|
|
|
+package com.ruoyi.app.stall;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
+import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
+import com.ruoyi.common.utils.MessageUtils;
|
|
|
|
|
+import com.ruoyi.system.domain.InfoUser;
|
|
|
|
|
+import com.ruoyi.system.domain.PosStore;
|
|
|
|
|
+import com.ruoyi.system.service.IInfoUserService;
|
|
|
|
|
+import com.ruoyi.system.service.IPosStoreService;
|
|
|
|
|
+import com.ruoyi.system.service.MerchantStoreAccessService;
|
|
|
|
|
+import com.ruoyi.system.utils.JwtUtil;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.AfterEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
|
|
+import org.mockito.MockedStatic;
|
|
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
+
|
|
|
|
|
+class StallListAccessTest {
|
|
|
|
|
+ private final IInfoUserService users = mock(IInfoUserService.class);
|
|
|
|
|
+ private final IPosStoreService stores = mock(IPosStoreService.class);
|
|
|
|
|
+ private final MerchantStoreAccessService access = mock(MerchantStoreAccessService.class);
|
|
|
|
|
+ private final StallController controller = new StallController();
|
|
|
|
|
+ private final InfoUser user = new InfoUser();
|
|
|
|
|
+ private String token;
|
|
|
|
|
+ private MockedStatic<MessageUtils> messages;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() throws Exception {
|
|
|
|
|
+ messages = mockStatic(MessageUtils.class);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "infoUserService", users);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "posStoreService", stores);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "merchantStoreAccessService", access);
|
|
|
|
|
+ user.setUserId(50L);
|
|
|
|
|
+ user.setUserType("5");
|
|
|
|
|
+ user.setStoreId(999L);
|
|
|
|
|
+ when(users.selectInfoUserByUserId(50L)).thenReturn(user);
|
|
|
|
|
+ token = JwtUtil.setToken("50", "subaccount");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @AfterEach
|
|
|
|
|
+ void tearDown() {
|
|
|
|
|
+ messages.close();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
|
|
+ void subaccountQueriesOnlyCurrentAuthorizedStores() {
|
|
|
|
|
+ when(access.getAccessibleStoreIds(50L)).thenReturn(Set.of(8L, 9L));
|
|
|
|
|
+ PosStore store = new PosStore();
|
|
|
|
|
+ store.setId(8);
|
|
|
|
|
+ when(stores.list(any(QueryWrapper.class))).thenReturn(List.of(store));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(List.of(store), controller.getStallList(token).get(AjaxResult.DATA_TAG));
|
|
|
|
|
+
|
|
|
|
|
+ ArgumentCaptor<QueryWrapper> query = ArgumentCaptor.forClass(QueryWrapper.class);
|
|
|
|
|
+ verify(stores).list(query.capture());
|
|
|
|
|
+ assertTrue(query.getValue().getSqlSegment().contains("id IN"));
|
|
|
|
|
+ assertEquals(Set.of(8L, 9L), Set.copyOf(query.getValue().getParamNameValuePairs().values()));
|
|
|
|
|
+ verify(stores, never()).selectPosStoreList(any());
|
|
|
|
|
+ verify(stores, never()).selectPosStoreById(any());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void emptyAuthorizationReturnsEmptyWithoutQueryingStores() {
|
|
|
|
|
+ when(access.getAccessibleStoreIds(50L)).thenReturn(Set.of());
|
|
|
|
|
+ assertEquals(List.of(), controller.getStallList(token).get(AjaxResult.DATA_TAG));
|
|
|
|
|
+ verifyNoInteractions(stores);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void unavailableAccountCannotFallBackToOwnerQuery() {
|
|
|
|
|
+ when(access.getAccessibleStoreIds(50L)).thenThrow(new ServiceException("unavailable"));
|
|
|
|
|
+ assertThrows(ServiceException.class, () -> controller.getStallList(token));
|
|
|
|
|
+ verifyNoInteractions(stores);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void ownerAndNightMarketKeepExistingQuery() {
|
|
|
|
|
+ for (String type : List.of("1", "3")) {
|
|
|
|
|
+ user.setUserType(type);
|
|
|
|
|
+ controller.getStallList(token);
|
|
|
|
|
+ }
|
|
|
|
|
+ ArgumentCaptor<PosStore> query = ArgumentCaptor.forClass(PosStore.class);
|
|
|
|
|
+ verify(stores, times(2)).selectPosStoreList(query.capture());
|
|
|
|
|
+ query.getAllValues().forEach(value -> assertEquals(50L, value.getUserId()));
|
|
|
|
|
+ verifyNoInteractions(access);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void stallOwnerKeepsBoundStoreQuery() {
|
|
|
|
|
+ user.setUserType("4");
|
|
|
|
|
+ PosStore store = new PosStore();
|
|
|
|
|
+ store.setId(999);
|
|
|
|
|
+ when(stores.selectPosStoreById(999L)).thenReturn(store);
|
|
|
|
|
+ assertEquals(List.of(store), controller.getStallList(token).get(AjaxResult.DATA_TAG));
|
|
|
|
|
+ verifyNoInteractions(access);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|