package run.halo.operationaudit.alert; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; import reactor.core.publisher.Mono; import run.halo.app.extension.ReactiveExtensionClient; import run.halo.app.extension.Secret; import run.halo.app.plugin.ReactiveSettingFetcher; /** * 告警设置 TTL 缓存测试(与审计设置同款模式):命中缓存、失败/缺省回退默认(告警关闭); * Webhook 地址 Secret 解析:未配置引用 / Secret 不存在 / 缺字段 / 空白值均 fail closed * (错误信号,绝不发送),字段值去除首尾空白。 */ class AlertSettingProviderTest { private final ReactiveSettingFetcher fetcher = mock(ReactiveSettingFetcher.class); private final ReactiveExtensionClient client = mock(ReactiveExtensionClient.class); private final AlertSettingProvider provider = new AlertSettingProvider(fetcher, client); private static AlertSettings settings(String secretName) { return new AlertSettings(true, secretName, 5, 5); } private static Secret secret(String url) { var secret = new Secret(); secret.setStringData(Map.of(AlertSettingProvider.WEBHOOK_URL_FIELD, url)); return secret; } @Test void shouldCacheWithinTtl() { var fetchCount = new AtomicInteger(0); when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class))) .thenAnswer(inv -> { fetchCount.incrementAndGet(); return Mono.just(new AlertSettings(true, "webhook-secret", 3, 2)); }); var first = provider.get().block(); var second = provider.get().block(); assertThat(first).extracting(AlertSettings::enabled, AlertSettings::webhookSecretName, AlertSettings::thresholdCount, AlertSettings::windowMinutes) .containsExactly(true, "webhook-secret", 3, 2); assertThat(second).isSameAs(first); assertThat(fetchCount.get()).isEqualTo(1); } @Test void shouldFallbackToDefaultsWhenFetchFails() { when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class))) .thenReturn(Mono.error(new IllegalStateException("configmap missing"))); var settings = provider.get().block(); assertThat(settings).isEqualTo(AlertSettings.defaults()); assertThat(settings.enabledOrDefault()).isFalse(); assertThat(settings.webhookSecretNameOrDefault()).isNull(); } @Test void shouldFallbackToDefaultsWhenConfigMissing() { when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class))) .thenReturn(Mono.empty()); assertThat(provider.get().block()).isEqualTo(AlertSettings.defaults()); } @Test void shouldRefetchAfterExpiry() { when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class))) .thenReturn(Mono.just(AlertSettings.defaults())); provider.get().block(); // 手动使缓存过期(TTL 为 60s,等待必然不现实,直接构造过期条目) provider.cache = new AlertSettingProvider.CacheEntry(AlertSettings.defaults(), 0); provider.get().block(); verify(fetcher, times(2)).fetch(any(), any()); } // ===== Webhook 地址 Secret 解析(fail closed) ===== @Test void shouldResolveWebhookUrlFromSecret() { when(client.fetch(eq(Secret.class), eq("webhook-secret"))) .thenReturn(Mono.just(secret(" https://hooks.example.com/x "))); assertThat(provider.webhookUrl(settings("webhook-secret")).block()) .isEqualTo("https://hooks.example.com/x"); // 便捷方法:先读设置再解析 when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class))) .thenReturn(Mono.just(settings("webhook-secret"))); assertThat(provider.webhookUrl().block()).isEqualTo("https://hooks.example.com/x"); } @Test void shouldFailClosedWhenSecretRefMissing() { assertThatThrownBy(() -> provider.webhookUrl(settings(" ")).block()) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("未配置"); } @Test void shouldFailClosedWhenSecretMissing() { when(client.fetch(eq(Secret.class), eq("gone"))).thenReturn(Mono.empty()); assertThatThrownBy(() -> provider.webhookUrl(settings("gone")).block()) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("不存在"); } @Test void shouldFailClosedWhenSecretFieldMissing() { var secret = new Secret(); secret.setStringData(Map.of("other", "value")); when(client.fetch(eq(Secret.class), eq("webhook-secret"))).thenReturn(Mono.just(secret)); assertThatThrownBy(() -> provider.webhookUrl(settings("webhook-secret")).block()) .isInstanceOf(IllegalStateException.class) .hasMessageContaining(AlertSettingProvider.WEBHOOK_URL_FIELD); } @Test void shouldFailClosedWhenSecretFieldBlank() { when(client.fetch(eq(Secret.class), eq("webhook-secret"))) .thenReturn(Mono.just(secret(" "))); assertThatThrownBy(() -> provider.webhookUrl(settings("webhook-secret")).block()) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("缺少"); } }