格式修改
This commit is contained in:
+15
-3
@@ -2,22 +2,34 @@ package com.baiyue.eventbridge.security;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DisplayName("内部服务令牌校验")
|
||||
@SpringJUnitConfig(InternalServiceTokenGuard.class)
|
||||
@TestPropertySource(properties = "baiyue.security.internal-service-token=a-secure-internal-token")
|
||||
class InternalServiceTokenGuardTest {
|
||||
@Resource private InternalServiceTokenGuard guard;
|
||||
|
||||
@AfterEach
|
||||
void resetToken() {
|
||||
ReflectionTestUtils.setField(guard, "expectedToken", "a-secure-internal-token");
|
||||
}
|
||||
@Test
|
||||
void validTokenAllowsAccess() {
|
||||
InternalServiceTokenGuard guard = new InternalServiceTokenGuard("a-secure-internal-token");
|
||||
assertThatCode(() -> guard.verify("a-secure-internal-token")).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void unconfiguredTokenDisablesReceiveEndpoint() {
|
||||
InternalServiceTokenGuard guard = new InternalServiceTokenGuard("");
|
||||
ReflectionTestUtils.setField(guard, "expectedToken", "");
|
||||
assertThatThrownBy(() -> guard.verify("anything"))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasMessageContaining("接口已关闭");
|
||||
@@ -25,7 +37,7 @@ class InternalServiceTokenGuardTest {
|
||||
|
||||
@Test
|
||||
void invalidTokenIsRejected() {
|
||||
InternalServiceTokenGuard guard = new InternalServiceTokenGuard("expected");
|
||||
ReflectionTestUtils.setField(guard, "expectedToken", "expected");
|
||||
assertThatThrownBy(() -> guard.verify("wrong"))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasMessageContaining("令牌无效");
|
||||
|
||||
+31
-5
@@ -6,9 +6,17 @@ import com.baiyue.common.event.EventPayloadHash;
|
||||
import com.baiyue.common.event.RegionCode;
|
||||
import com.baiyue.eventbridge.domain.BridgeInbox;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -22,10 +30,28 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@DisplayName("跨区域事件收件箱")
|
||||
@SpringJUnitConfig(classes = {EventInboxService.class, EventInboxServiceTest.TestConfig.class})
|
||||
@TestPropertySource(properties = "baiyue.region=OVERSEAS")
|
||||
class EventInboxServiceTest {
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final InboxStore store = mock(InboxStore.class);
|
||||
private final EventInboxService service = new EventInboxService(store, objectMapper, "OVERSEAS");
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
@MockBean
|
||||
private InboxStore store;
|
||||
@Resource
|
||||
private EventInboxService service;
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class TestConfig {
|
||||
@Bean
|
||||
ObjectMapper objectMapper() {
|
||||
return new ObjectMapper();
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void resetRegion() {
|
||||
ReflectionTestUtils.setField(service, "localRegion", "OVERSEAS");
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstEventPersistsAndReturnsReceived() {
|
||||
@@ -66,10 +92,10 @@ class EventInboxServiceTest {
|
||||
|
||||
@Test
|
||||
void targetRegionDifferentFromCurrentDeploymentIsRejected() {
|
||||
EventInboxService cnService = new EventInboxService(store, objectMapper, "CN");
|
||||
ReflectionTestUtils.setField(service, "localRegion", "CN");
|
||||
EventEnvelope envelope = withHash(envelope("sha256:placeholder"));
|
||||
|
||||
assertThatThrownBy(() -> cnService.receive(envelope))
|
||||
assertThatThrownBy(() -> service.receive(envelope))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("目标区域");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user