feat(lastleg): manage three delivery document categories

This commit is contained in:
2026-09-09 11:22:47 +08:00
parent f37e9ce555
commit 465d9dfc10
26 changed files with 967 additions and 4 deletions
@@ -0,0 +1,39 @@
package com.baiyue.lastleg.delivery.controller;
import com.baiyue.common.security.AuthenticatedUser;
import com.baiyue.lastleg.delivery.dto.DeliveryDocumentDtos.*;
import com.baiyue.lastleg.delivery.service.DeliveryDocumentService;
import com.baiyue.lastleg.security.OverseasPermissions;
import org.junit.jupiter.api.*;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.util.ReflectionTestUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.LocalDateTime;
import java.util.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
class DeliveryDocumentControllerTest {
@AfterEach void clear() { SecurityContextHolder.clearContext(); }
@Test void readonlyUserGetsNoWriteActionsInCurrentReplayOrHistory() throws Exception { verifyActions(false); }
@Test void currentWritePermissionRetainsManagementAction() throws Exception { verifyActions(true); }
private void verifyActions(boolean writer) throws Exception {
var service=mock(DeliveryDocumentService.class);
var controller=new DeliveryDocumentController(); ReflectionTestUtils.setField(controller,"service",service);
var user=new AuthenticatedUser(9L,0L,null,null,"reader","PLATFORM","WEB",Set.of());
var authorities=writer?List.of(new SimpleGrantedAuthority(OverseasPermissions.DELIVERY_DOCUMENTS)):
List.of(new SimpleGrantedAuthority(OverseasPermissions.DELIVERY_LIST));
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(user,null,authorities));
var snapshot=new Detail("DL-1","100",1,null,0,3,List.of(),List.of("MANAGE_DOCUMENTS"),true);
when(service.detail("100","DL-1")).thenReturn(snapshot);
when(service.history("100","DL-1")).thenReturn(List.of(new History(1,"9",LocalDateTime.now(),snapshot)));
var request=new Save(0L,"key",null,List.of());
when(service.save("100","DL-1","9",request)).thenReturn(snapshot);
var json=new ObjectMapper().findAndRegisterModules();
assertThat(json.valueToTree(controller.detail(user,"DL-1","100")).at("/data/allowedActions").size()).isEqualTo(writer?1:0);
assertThat(json.valueToTree(controller.save(user,"DL-1","100",request)).at("/data/allowedActions").size()).isEqualTo(writer?1:0);
assertThat(json.valueToTree(controller.history(user,"DL-1","100")).at("/data/0/snapshot/allowedActions").size()).isEqualTo(writer?1:0);
}
}