feat(sms): Transport recipient PhoneNumber + SmsConfig round-trip (S10)

This commit is contained in:
Joseph Doherty
2026-06-19 10:39:12 -04:00
parent f0c69aad83
commit cdfd0ffbd2
6 changed files with 438 additions and 10 deletions
@@ -200,4 +200,130 @@ public sealed class BundleDtoSerializationTests
Assert.NotNull(content.Instances);
Assert.Empty(content.Instances);
}
// --- S10: SMS recipient PhoneNumber + SmsConfig wire format ---------------
[Fact]
public void LegacyRecipient_MissingPhoneNumber_DeserializesToNull()
{
// A pre-SMS bundle: notification-list recipients carry only Name + Email,
// with no PhoneNumber field present in the JSON text at all.
const string legacyJson = """
{
"TemplateFolders": [],
"Templates": [],
"SharedScripts": [],
"ExternalSystems": [],
"DatabaseConnections": [],
"NotificationLists": [
{
"Name": "alerts",
"Type": "Email",
"Recipients": [ { "Name": "Ops", "EmailAddress": "ops@example.com" } ]
}
],
"SmtpConfigs": [],
"ApiMethods": []
}
""";
var content = JsonSerializer.Deserialize<BundleContentDto>(legacyJson, Options);
Assert.NotNull(content);
var recipient = Assert.Single(Assert.Single(content!.NotificationLists).Recipients);
Assert.Equal("Ops", recipient.Name);
Assert.Equal("ops@example.com", recipient.EmailAddress);
// The backward-compat invariant: an absent PhoneNumber comes back null.
Assert.Null(recipient.PhoneNumber);
}
[Fact]
public void LegacyContent_MissingSmsConfigsArray_DeserializesToEmpty_NotNull()
{
// A pre-SMS bundle has no smsConfigs field; the importer must see an empty
// (never null) collection — same forward-compat invariant as the M8 arrays.
const string legacyJson = """
{
"TemplateFolders": [],
"Templates": [],
"SharedScripts": [],
"ExternalSystems": [],
"DatabaseConnections": [],
"NotificationLists": [],
"SmtpConfigs": [],
"ApiMethods": []
}
""";
var content = JsonSerializer.Deserialize<BundleContentDto>(legacyJson, Options);
Assert.NotNull(content);
Assert.NotNull(content!.SmsConfigs);
Assert.Empty(content.SmsConfigs);
}
[Fact]
public void DefaultConstructed_SmsConfigs_IsEmpty_NotNull()
{
var content = new BundleContentDto(
TemplateFolders: Array.Empty<TemplateFolderDto>(),
Templates: Array.Empty<TemplateDto>(),
SharedScripts: Array.Empty<SharedScriptDto>(),
ExternalSystems: Array.Empty<ExternalSystemDto>(),
DatabaseConnections: Array.Empty<DatabaseConnectionDto>(),
NotificationLists: Array.Empty<NotificationListDto>(),
SmtpConfigs: Array.Empty<SmtpConfigDto>(),
ApiMethods: Array.Empty<ApiMethodDto>());
Assert.NotNull(content.SmsConfigs);
Assert.Empty(content.SmsConfigs);
}
[Fact]
public void SmsConfig_AuthToken_RidesSecretsBlock_NotCleartextPublicFields()
{
// The auth token must travel only inside the SecretsBlock, exactly like the
// SMTP credentials test pattern — it must NOT leak into any public DTO field.
var content = new BundleContentDto(
TemplateFolders: Array.Empty<TemplateFolderDto>(),
Templates: Array.Empty<TemplateDto>(),
SharedScripts: Array.Empty<SharedScriptDto>(),
ExternalSystems: Array.Empty<ExternalSystemDto>(),
DatabaseConnections: Array.Empty<DatabaseConnectionDto>(),
NotificationLists: Array.Empty<NotificationListDto>(),
SmtpConfigs: Array.Empty<SmtpConfigDto>(),
ApiMethods: Array.Empty<ApiMethodDto>())
{
SmsConfigs = new[]
{
new SmsConfigDto(
AccountSid: "AC123",
FromNumber: "+15550001111",
MessagingServiceSid: "MG999",
ApiBaseUrl: "https://api.twilio.com",
ConnectionTimeoutSeconds: 20,
MaxRetries: 7,
RetryDelay: TimeSpan.FromSeconds(45),
Secrets: new SecretsBlock(new Dictionary<string, string>
{
["AuthToken"] = "twilio-super-secret",
})),
},
};
var json = JsonSerializer.Serialize(content, Options);
// The secret appears exactly once — inside the SecretsBlock — and the
// round-trip restores it.
Assert.Contains("twilio-super-secret", json, StringComparison.Ordinal);
Assert.Contains("\"AuthToken\"", json, StringComparison.Ordinal);
var roundTripped = JsonSerializer.Deserialize<BundleContentDto>(json, Options);
Assert.NotNull(roundTripped);
var sms = Assert.Single(roundTripped!.SmsConfigs);
Assert.Equal("AC123", sms.AccountSid);
Assert.Equal("+15550001111", sms.FromNumber);
Assert.NotNull(sms.Secrets);
Assert.Equal("twilio-super-secret", sms.Secrets!.Values["AuthToken"]);
}
}
@@ -618,4 +618,188 @@ public sealed class EntitySerializerTests
Assert.Equal("ns=2;s=Pump1.P", rtBinding.DataSourceReferenceOverride);
Assert.Equal(0, rtBinding.DataConnectionId);
}
// --- S10: SMS recipient PhoneNumber + SmsConfiguration round-trip ---------
[Fact]
public void ToDto_carries_sms_recipient_phone_number()
{
var list = new NotificationList("on-call") { Id = 1, Type = NotificationType.Sms };
list.Recipients.Add(NotificationRecipient.ForSms("Pager", "+15551234567"));
var aggregate = MakeEmptyAggregate() with { NotificationLists = new[] { list } };
var dto = new EntitySerializer().ToBundleContent(aggregate);
var dtoList = Assert.Single(dto.NotificationLists);
Assert.Equal(NotificationType.Sms, dtoList.Type);
var dtoRecipient = Assert.Single(dtoList.Recipients);
Assert.Equal("Pager", dtoRecipient.Name);
Assert.Equal("+15551234567", dtoRecipient.PhoneNumber);
// SMS-only recipient: no email is carried (the old email-only filter would
// have dropped this recipient entirely).
Assert.Null(dtoRecipient.EmailAddress);
}
[Fact]
public void Roundtrip_sms_recipient_preserves_phone_number()
{
var list = new NotificationList("on-call") { Id = 1, Type = NotificationType.Sms };
list.Recipients.Add(NotificationRecipient.ForSms("Pager", "+15551234567"));
var aggregate = MakeEmptyAggregate() with { NotificationLists = new[] { list } };
var sut = new EntitySerializer();
var rt = sut.FromBundleContent(sut.ToBundleContent(aggregate));
var rtList = Assert.Single(rt.NotificationLists);
Assert.Equal(NotificationType.Sms, rtList.Type);
var rtRecipient = Assert.Single(rtList.Recipients);
Assert.Equal("Pager", rtRecipient.Name);
Assert.Equal("+15551234567", rtRecipient.PhoneNumber);
Assert.Null(rtRecipient.EmailAddress);
}
[Fact]
public void Roundtrip_email_recipient_preserves_email_and_leaves_phone_null()
{
var list = new NotificationList("alerts") { Id = 1, Type = NotificationType.Email };
list.Recipients.Add(NotificationRecipient.ForEmail("Ops", "ops@example.com"));
var aggregate = MakeEmptyAggregate() with { NotificationLists = new[] { list } };
var sut = new EntitySerializer();
var dto = sut.ToBundleContent(aggregate);
var rt = sut.FromBundleContent(dto);
var dtoRecipient = Assert.Single(Assert.Single(dto.NotificationLists).Recipients);
Assert.Equal("ops@example.com", dtoRecipient.EmailAddress);
Assert.Null(dtoRecipient.PhoneNumber);
var rtRecipient = Assert.Single(Assert.Single(rt.NotificationLists).Recipients);
Assert.Equal("Ops", rtRecipient.Name);
Assert.Equal("ops@example.com", rtRecipient.EmailAddress);
Assert.Null(rtRecipient.PhoneNumber);
}
[Fact]
public void Roundtrip_recipient_with_both_contacts_preserves_both()
{
var list = new NotificationList("dual") { Id = 1, Type = NotificationType.Sms };
// A recipient carrying BOTH an email and a phone number must round-trip
// both contacts (neither is lost on import).
list.Recipients.Add(new NotificationRecipient("Both", "both@example.com")
{
PhoneNumber = "+15559876543",
});
var aggregate = MakeEmptyAggregate() with { NotificationLists = new[] { list } };
var sut = new EntitySerializer();
var rt = sut.FromBundleContent(sut.ToBundleContent(aggregate));
var rtRecipient = Assert.Single(Assert.Single(rt.NotificationLists).Recipients);
Assert.Equal("both@example.com", rtRecipient.EmailAddress);
Assert.Equal("+15559876543", rtRecipient.PhoneNumber);
}
[Fact]
public void FromDto_recipient_without_phone_number_yields_null_phone()
{
// Backward-compat at the DTO level: a recipient DTO built without a phone
// number (old bundles never carried one) imports with PhoneNumber == null.
var dto = new BundleContentDto(
TemplateFolders: Array.Empty<TemplateFolderDto>(),
Templates: Array.Empty<TemplateDto>(),
SharedScripts: Array.Empty<SharedScriptDto>(),
ExternalSystems: Array.Empty<ExternalSystemDto>(),
DatabaseConnections: Array.Empty<DatabaseConnectionDto>(),
NotificationLists: new[]
{
new NotificationListDto(
"alerts",
NotificationType.Email,
new[] { new NotificationRecipientDto("Ops", "ops@example.com") }),
},
SmtpConfigs: Array.Empty<SmtpConfigDto>(),
ApiMethods: Array.Empty<ApiMethodDto>());
var aggregate = new EntitySerializer().FromBundleContent(dto);
var recipient = Assert.Single(Assert.Single(aggregate.NotificationLists).Recipients);
Assert.Equal("ops@example.com", recipient.EmailAddress);
Assert.Null(recipient.PhoneNumber);
}
[Fact]
public void ToDto_carves_sms_auth_token_into_secrets_block()
{
var sms = new SmsConfiguration("AC123", "+15550001111")
{
Id = 1,
AuthToken = "twilio-super-secret",
MessagingServiceSid = "MG999",
ApiBaseUrl = "https://api.twilio.com",
ConnectionTimeoutSeconds = 20,
MaxRetries = 7,
RetryDelay = TimeSpan.FromSeconds(45),
};
var aggregate = MakeEmptyAggregate() with { SmsConfigurations = new[] { sms } };
var dto = new EntitySerializer().ToBundleContent(aggregate);
var dtoSms = Assert.Single(dto.SmsConfigs);
Assert.Equal("AC123", dtoSms.AccountSid);
Assert.Equal("+15550001111", dtoSms.FromNumber);
Assert.Equal("MG999", dtoSms.MessagingServiceSid);
// The auth token lands in the SecretsBlock — NOT in any public field.
Assert.NotNull(dtoSms.Secrets);
Assert.Equal("twilio-super-secret", dtoSms.Secrets!.Values["AuthToken"]);
Assert.DoesNotContain("twilio-super-secret", dtoSms.AccountSid);
Assert.DoesNotContain("twilio-super-secret", dtoSms.FromNumber);
}
[Fact]
public void Roundtrip_sms_config_restores_auth_token_from_secrets_block()
{
var sms = new SmsConfiguration("AC123", "+15550001111")
{
Id = 1,
AuthToken = "twilio-super-secret",
MessagingServiceSid = "MG999",
ApiBaseUrl = "https://api.twilio.com",
ConnectionTimeoutSeconds = 20,
MaxRetries = 7,
RetryDelay = TimeSpan.FromSeconds(45),
};
var aggregate = MakeEmptyAggregate() with { SmsConfigurations = new[] { sms } };
var sut = new EntitySerializer();
var rt = sut.FromBundleContent(sut.ToBundleContent(aggregate));
var rtSms = Assert.Single(rt.SmsConfigurations);
Assert.Equal("AC123", rtSms.AccountSid);
Assert.Equal("+15550001111", rtSms.FromNumber);
Assert.Equal("MG999", rtSms.MessagingServiceSid);
Assert.Equal("https://api.twilio.com", rtSms.ApiBaseUrl);
Assert.Equal(20, rtSms.ConnectionTimeoutSeconds);
Assert.Equal(7, rtSms.MaxRetries);
Assert.Equal(TimeSpan.FromSeconds(45), rtSms.RetryDelay);
// Secret decrypted back out of the SecretsBlock.
Assert.Equal("twilio-super-secret", rtSms.AuthToken);
}
[Fact]
public void ToDto_omits_sms_secret_key_when_auth_token_is_null()
{
var sms = new SmsConfiguration("AC123", "+15550001111") { Id = 1, AuthToken = null };
var aggregate = MakeEmptyAggregate() with { SmsConfigurations = new[] { sms } };
var dto = new EntitySerializer().ToBundleContent(aggregate);
var dtoSms = Assert.Single(dto.SmsConfigs);
// No token => no SecretsBlock at all (mirrors the SMTP "empty credentials"
// behaviour where the whole block stays null).
Assert.Null(dtoSms.Secrets);
// And it round-trips to a null token without throwing.
var rt = new EntitySerializer().FromBundleContent(dto);
Assert.Null(Assert.Single(rt.SmsConfigurations).AuthToken);
}
}