This commit is contained in:
2026-08-05 16:26:10 +08:00
parent 06f7f9d2f2
commit 5ba9716e12
155 changed files with 2883 additions and 551 deletions
@@ -0,0 +1,49 @@
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;
}
}