101 lines
3.4 KiB
C#
101 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using StellaOps.Notify.Engine;
|
|
using StellaOps.Notify.Models;
|
|
using Xunit;
|
|
|
|
namespace StellaOps.Notify.Connectors.Email.Tests;
|
|
|
|
public sealed class EmailChannelHealthProviderTests
|
|
{
|
|
private static readonly EmailChannelHealthProvider Provider = new();
|
|
|
|
[Fact]
|
|
public async Task CheckAsync_ReturnsHealthy()
|
|
{
|
|
var channel = CreateChannel(enabled: true, target: "ops@example.com");
|
|
|
|
var context = new ChannelHealthContext(
|
|
channel.TenantId,
|
|
channel,
|
|
channel.Config.Target!,
|
|
new DateTimeOffset(2025, 10, 20, 15, 0, 0, TimeSpan.Zero),
|
|
"trace-email-001");
|
|
|
|
var result = await Provider.CheckAsync(context, CancellationToken.None);
|
|
|
|
Assert.Equal(ChannelHealthStatus.Healthy, result.Status);
|
|
Assert.Equal("true", result.Metadata["email.channel.enabled"]);
|
|
Assert.Equal("true", result.Metadata["email.validation.targetPresent"]);
|
|
Assert.Equal("ops@example.com", result.Metadata["email.target"]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CheckAsync_ReturnsDegradedWhenDisabled()
|
|
{
|
|
var channel = CreateChannel(enabled: false, target: "ops@example.com");
|
|
|
|
var context = new ChannelHealthContext(
|
|
channel.TenantId,
|
|
channel,
|
|
channel.Config.Target!,
|
|
DateTimeOffset.UtcNow,
|
|
"trace-email-002");
|
|
|
|
var result = await Provider.CheckAsync(context, CancellationToken.None);
|
|
|
|
Assert.Equal(ChannelHealthStatus.Degraded, result.Status);
|
|
Assert.Equal("false", result.Metadata["email.channel.enabled"]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CheckAsync_ReturnsUnhealthyWhenTargetMissing()
|
|
{
|
|
var channel = NotifyChannel.Create(
|
|
channelId: "channel-email-ops",
|
|
tenantId: "tenant-sec",
|
|
name: "email:ops",
|
|
type: NotifyChannelType.Email,
|
|
config: NotifyChannelConfig.Create(
|
|
secretRef: "ref://notify/channels/email/ops",
|
|
target: null,
|
|
properties: new Dictionary<string, string>
|
|
{
|
|
["smtpHost"] = "smtp.ops.example.com"
|
|
}),
|
|
enabled: true);
|
|
|
|
var context = new ChannelHealthContext(
|
|
channel.TenantId,
|
|
channel,
|
|
channel.Name,
|
|
DateTimeOffset.UtcNow,
|
|
"trace-email-003");
|
|
|
|
var result = await Provider.CheckAsync(context, CancellationToken.None);
|
|
|
|
Assert.Equal(ChannelHealthStatus.Unhealthy, result.Status);
|
|
Assert.Equal("false", result.Metadata["email.validation.targetPresent"]);
|
|
}
|
|
|
|
private static NotifyChannel CreateChannel(bool enabled, string? target)
|
|
{
|
|
return NotifyChannel.Create(
|
|
channelId: "channel-email-ops",
|
|
tenantId: "tenant-sec",
|
|
name: "email:ops",
|
|
type: NotifyChannelType.Email,
|
|
config: NotifyChannelConfig.Create(
|
|
secretRef: "ref://notify/channels/email/ops",
|
|
target: target,
|
|
properties: new Dictionary<string, string>
|
|
{
|
|
["smtpHost"] = "smtp.ops.example.com",
|
|
["password"] = "super-secret"
|
|
}),
|
|
enabled: enabled);
|
|
}
|
|
}
|