package com.baiyue.integration.security; import com.baiyue.common.core.exception.BizException; import org.junit.jupiter.api.Test; import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; class InternalServiceTokenGuardTest { private static final String VALID_TOKEN = "integration-service-test-token-32-bytes"; @Test void rejectsMissingEmptyAndShortTokens() { assertInvalidConfiguration(null); assertInvalidConfiguration(""); assertInvalidConfiguration(" "); assertInvalidConfiguration("too-short"); } @Test void rejectsKnownInsecurePlaceholder() { assertInvalidConfiguration("change-me-to-an-internal-token-at-least-32-bytes-long"); } @Test void acceptsConfiguredTokenWithAtLeastThirtyTwoBytes() { InternalServiceTokenGuard guard = guardWithToken(VALID_TOKEN); assertThatCode(guard::initializeToken).doesNotThrowAnyException(); assertThatCode(() -> guard.verify(VALID_TOKEN)).doesNotThrowAnyException(); assertThatThrownBy(() -> guard.verify("different-internal-service-token")) .isInstanceOf(BizException.class); } private static void assertInvalidConfiguration(String token) { InternalServiceTokenGuard guard = guardWithToken(token); assertThatThrownBy(guard::initializeToken) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("INTERNAL_SERVICE_TOKEN"); } private static InternalServiceTokenGuard guardWithToken(String token) { InternalServiceTokenGuard guard = new InternalServiceTokenGuard(); ReflectionTestUtils.setField(guard, "token", token); return guard; } }