费用提交
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
package com.baiyue.finance;
|
||||
|
||||
import com.baiyue.common.security.AuthenticatedUser;
|
||||
import com.baiyue.finance.controller.PlatformFinanceWalletController;
|
||||
import com.baiyue.finance.dto.FinanceWalletDtos.WalletRefundRequest;
|
||||
import com.baiyue.finance.service.FinanceWalletService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Set;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/** 平台退款入口鉴权、必填原因和稳定幂等引用的回归。 */
|
||||
class PlatformFinanceWalletRefundControllerTest {
|
||||
private final FinanceWalletService service = mock(FinanceWalletService.class);
|
||||
private final PlatformFinanceWalletController controller = new PlatformFinanceWalletController(service);
|
||||
private final WalletRefundRequest request = new WalletRefundRequest(new BigDecimal("12.00"), "退回多收费用");
|
||||
|
||||
/** 客户即使携带同名权限也不能借平台入口操作他人资金。 */
|
||||
@Test void nonPlatformPrincipalsAreDeniedBeforeWalletAccess() {
|
||||
for (var user : new AuthenticatedUser[]{null, user("CLIENT", 9L), user("PLATFORM", 9L)})
|
||||
assertThatThrownBy(() -> controller.refund(user, 9, 1, "key", request))
|
||||
.isInstanceOf(org.springframework.web.server.ResponseStatusException.class);
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
|
||||
/** 同一客户请求键生成同一退款引用,实际防重与金额上限仍由资金事务执行。 */
|
||||
@Test void authorizedCallsUseStableReferenceAndExistingAdjustPermission() throws Exception {
|
||||
controller.refund(user("PLATFORM", 0L), 9, 1, " key ", request);
|
||||
controller.refund(user("PLATFORM", 0L), 9, 1, "key", request);
|
||||
var reference = org.mockito.ArgumentCaptor.forClass(String.class);
|
||||
verify(service, times(2)).refundConsumption(eq(9L), eq(1L), reference.capture(), eq("key"),
|
||||
eq(request.amount()), eq(7L), isNull(), eq(request.reason()));
|
||||
assertThat(reference.getAllValues()).allSatisfy(value -> assertThat(value).startsWith("MANUAL_REFUND:"));
|
||||
assertThat(reference.getAllValues().get(0)).isEqualTo(reference.getAllValues().get(1));
|
||||
assertThat(PlatformFinanceWalletController.class.getMethod("refund", AuthenticatedUser.class,
|
||||
long.class, long.class, String.class, WalletRefundRequest.class).getAnnotation(PreAuthorize.class).value())
|
||||
.isEqualTo("hasAuthority('platform:finance:customer-account:adjust')");
|
||||
}
|
||||
|
||||
/** 空说明或非法幂等键在触达资金前失败。 */
|
||||
@Test void missingReasonAndIdempotencyAreRejected() {
|
||||
assertThatThrownBy(() -> controller.refund(user("PLATFORM", 0L), 9, 1, " ", request)).isInstanceOf(IllegalArgumentException.class);
|
||||
assertThatThrownBy(() -> controller.refund(user("PLATFORM", 0L), 9, 1, "key",
|
||||
new WalletRefundRequest(BigDecimal.ONE, " "))).isInstanceOf(IllegalArgumentException.class);
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
|
||||
private AuthenticatedUser user(String app, long tenant) {
|
||||
return new AuthenticatedUser(7L, tenant, 0L, null, "finance", app, "WEB",
|
||||
Set.of("platform:finance:customer-account:adjust"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user