2026-07-30 18:01:36 +08:00
|
|
|
package com.baiyue.eventbridge.security;
|
|
|
|
|
|
2026-07-31 16:53:07 +08:00
|
|
|
import org.junit.jupiter.api.DisplayName;
|
2026-07-30 18:01:36 +08:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.springframework.web.server.ResponseStatusException;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
|
|
|
2026-07-31 16:53:07 +08:00
|
|
|
@DisplayName("内部服务令牌校验")
|
2026-07-30 18:01:36 +08:00
|
|
|
class InternalServiceTokenGuardTest {
|
|
|
|
|
@Test
|
2026-07-31 16:53:07 +08:00
|
|
|
void validTokenAllowsAccess() {
|
2026-07-30 18:01:36 +08:00
|
|
|
InternalServiceTokenGuard guard = new InternalServiceTokenGuard("a-secure-internal-token");
|
|
|
|
|
assertThatCode(() -> guard.verify("a-secure-internal-token")).doesNotThrowAnyException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2026-07-31 16:53:07 +08:00
|
|
|
void unconfiguredTokenDisablesReceiveEndpoint() {
|
2026-07-30 18:01:36 +08:00
|
|
|
InternalServiceTokenGuard guard = new InternalServiceTokenGuard("");
|
|
|
|
|
assertThatThrownBy(() -> guard.verify("anything"))
|
|
|
|
|
.isInstanceOf(ResponseStatusException.class)
|
|
|
|
|
.hasMessageContaining("接口已关闭");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2026-07-31 16:53:07 +08:00
|
|
|
void invalidTokenIsRejected() {
|
2026-07-30 18:01:36 +08:00
|
|
|
InternalServiceTokenGuard guard = new InternalServiceTokenGuard("expected");
|
|
|
|
|
assertThatThrownBy(() -> guard.verify("wrong"))
|
|
|
|
|
.isInstanceOf(ResponseStatusException.class)
|
|
|
|
|
.hasMessageContaining("令牌无效");
|
|
|
|
|
}
|
|
|
|
|
}
|