Store notification delivery settings in database

This commit is contained in:
biss committed 2026-11-21 19:45:06 +08:00
1 parent ce8e7021a9
commit 0231c468a4
14 files changed
+13255 -13

No files matched your search

@@ -130,6 +130,38 @@ public sealed class NotificationsControllerTests
Assert.Contains("尚未配置", delivery.LastError);
}
[Fact]
public async Task Disabled_email_leaves_existing_pending_delivery_untouched()
{
await using var fixture = await NotificationFixture.CreateAsync();
var notification = new Notification { UserId = fixture.Sender.Id, Title = "待发送", Content = "内容", ExternalDeliveryPlannedAt = DateTime.UtcNow };
fixture.Db.Notifications.Add(notification);
fixture.Db.NotificationDeliveries.Add(new NotificationDelivery { Notification = notification, UserId = fixture.Sender.Id,
Channel = NotificationChannel.Email, TargetKey = "queued", NextAttemptAt = DateTime.UtcNow.AddMinutes(-1) });
fixture.Db.NotificationSystemSettings.Add(new Jiaowu.Api.Domain.System.NotificationSystemSetting {
Id = NotificationSettings.SettingId, EmailEnabled = false, EmailFromAddress = "", EmailFromName = "", MaxAttempts = 5 });
await fixture.Db.SaveChangesAsync();
fixture.Db.ChangeTracker.Clear();
using var services = new ServiceCollection().AddSingleton(fixture.Db).BuildServiceProvider();
var worker = new NotificationDeliveryWorker(services.GetRequiredService<IServiceScopeFactory>(),
Options.Create(new NotificationDeliveryOptions { Email = new EmailDeliveryOptions { Enabled = true } }),
NullLogger<NotificationDeliveryWorker>.Instance);
await worker.RunOnceAsync(CancellationToken.None);
var delivery = await fixture.Db.NotificationDeliveries.SingleAsync();
Assert.Equal(NotificationDeliveryState.Pending, delivery.State);
Assert.Equal(0, delivery.AttemptCount);
}
[Fact]
public async Task Enabling_email_requires_configured_smtp_and_valid_sender()
{
await using var fixture = await NotificationFixture.CreateAsync();
var settings = new NotificationSettings(fixture.Db, Options.Create(new NotificationDeliveryOptions()));
var controller = new NotificationSettingsController(fixture.Db, settings);
Assert.IsType<BadRequestObjectResult>(await controller.Put(new(true, "sender@example.com", "教务处", 5), CancellationToken.None));
Assert.Empty(await fixture.Db.NotificationSystemSettings.ToListAsync());
}
[Fact]
public async Task Delivery_worker_keeps_external_channels_opt_in()
{