feat(notifications): EWS CreateItem envelope builder + response parser
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
using ZB.MOM.WW.ScadaBridge.NotificationService.Ews;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.NotificationService.Tests.Ews;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for the pure EWS response parser. The parser reports shape only
|
||||
/// (success / error / SOAP fault / unparseable) plus the response code — the
|
||||
/// transient-vs-permanent judgement belongs to the sender's classifier.
|
||||
/// </summary>
|
||||
public class EwsResponseParserTests
|
||||
{
|
||||
private const string SuccessBody = """
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<s:Body>
|
||||
<m:CreateItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
|
||||
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
|
||||
<m:ResponseMessages>
|
||||
<m:CreateItemResponseMessage ResponseClass="Success">
|
||||
<m:ResponseCode>NoError</m:ResponseCode>
|
||||
<m:Items><t:Message><t:ItemId Id="AAA=" ChangeKey="CQ=="/></t:Message></m:Items>
|
||||
</m:CreateItemResponseMessage>
|
||||
</m:ResponseMessages>
|
||||
</m:CreateItemResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>
|
||||
""";
|
||||
|
||||
private const string ErrorBody = """
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<s:Body>
|
||||
<m:CreateItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
|
||||
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
|
||||
<m:ResponseMessages>
|
||||
<m:CreateItemResponseMessage ResponseClass="Error">
|
||||
<m:MessageText>server busy</m:MessageText>
|
||||
<m:ResponseCode>ErrorServerBusy</m:ResponseCode>
|
||||
<m:DescriptiveLinkKey>0</m:DescriptiveLinkKey>
|
||||
</m:CreateItemResponseMessage>
|
||||
</m:ResponseMessages>
|
||||
</m:CreateItemResponse>
|
||||
</s:Body>
|
||||
</s:Envelope>
|
||||
""";
|
||||
|
||||
private const string FaultWithResponseCodeBody = """
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<s:Body>
|
||||
<s:Fault>
|
||||
<faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorSchemaValidation</faultcode>
|
||||
<faultstring xml:lang="en-US">The request failed schema validation.</faultstring>
|
||||
<detail>
|
||||
<e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">ErrorSchemaValidation</e:ResponseCode>
|
||||
<e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">The request failed schema validation.</e:Message>
|
||||
</detail>
|
||||
</s:Fault>
|
||||
</s:Body>
|
||||
</s:Envelope>
|
||||
""";
|
||||
|
||||
private const string FaultWithoutResponseCodeBody = """
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<s:Body>
|
||||
<s:Fault>
|
||||
<faultcode>s:Server</faultcode>
|
||||
<faultstring xml:lang="en-US">An internal server error occurred.</faultstring>
|
||||
<detail><e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">boom</e:Message></detail>
|
||||
</s:Fault>
|
||||
</s:Body>
|
||||
</s:Envelope>
|
||||
""";
|
||||
|
||||
[Fact]
|
||||
public void Parse_SuccessResponse_ReturnsSuccessWithNoError()
|
||||
{
|
||||
var result = EwsResponseParser.Parse(SuccessBody);
|
||||
|
||||
Assert.Equal(EwsResponseKind.Success, result.Kind);
|
||||
Assert.Equal("NoError", result.ResponseCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_ErrorResponse_ReturnsErrorWithCodeAndMessage()
|
||||
{
|
||||
var result = EwsResponseParser.Parse(ErrorBody);
|
||||
|
||||
Assert.Equal(EwsResponseKind.Error, result.Kind);
|
||||
Assert.Equal("ErrorServerBusy", result.ResponseCode);
|
||||
Assert.Equal("server busy", result.MessageText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_SoapFaultWithDetailResponseCode_ReturnsFaultWithCode()
|
||||
{
|
||||
var result = EwsResponseParser.Parse(FaultWithResponseCodeBody);
|
||||
|
||||
Assert.Equal(EwsResponseKind.Fault, result.Kind);
|
||||
Assert.Equal("ErrorSchemaValidation", result.ResponseCode);
|
||||
Assert.Equal("The request failed schema validation.", result.MessageText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_SoapFaultWithoutResponseCode_ReturnsFaultWithNullCode()
|
||||
{
|
||||
var result = EwsResponseParser.Parse(FaultWithoutResponseCodeBody);
|
||||
|
||||
Assert.Equal(EwsResponseKind.Fault, result.Kind);
|
||||
Assert.Null(result.ResponseCode);
|
||||
Assert.Equal("An internal server error occurred.", result.MessageText);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("not xml")]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
[InlineData("<foo/>")]
|
||||
public void Parse_GarbageOrUnrelatedPayload_ReturnsUnparseable(string body)
|
||||
{
|
||||
var result = EwsResponseParser.Parse(body);
|
||||
|
||||
Assert.Equal(EwsResponseKind.Unparseable, result.Kind);
|
||||
Assert.Null(result.ResponseCode);
|
||||
Assert.Null(result.MessageText);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
using System.Xml.Linq;
|
||||
using ZB.MOM.WW.ScadaBridge.NotificationService.Ews;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.NotificationService.Tests.Ews;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for the pure EWS <c>CreateItem</c> SOAP envelope builder: the fixed schema
|
||||
/// sequence Exchange validates against, BCC-only recipients, and XML escaping of
|
||||
/// operator-authored subject/body content.
|
||||
/// </summary>
|
||||
public class EwsSoapEnvelopeTests
|
||||
{
|
||||
private static readonly XNamespace Soap = "http://schemas.xmlsoap.org/soap/envelope/";
|
||||
private static readonly XNamespace Types = "http://schemas.microsoft.com/exchange/services/2006/types";
|
||||
private static readonly XNamespace Messages = "http://schemas.microsoft.com/exchange/services/2006/messages";
|
||||
|
||||
private static XDocument Build(
|
||||
string from = "scada@example.com",
|
||||
IReadOnlyList<string>? bcc = null,
|
||||
string subject = "Subject",
|
||||
string body = "Body")
|
||||
=> XDocument.Parse(EwsSoapEnvelope.BuildCreateItem(from, bcc ?? ["one@example.com"], subject, body));
|
||||
|
||||
[Fact]
|
||||
public void BuildCreateItem_SetsSendOnlyDispositionAndExchange2013RequestVersion()
|
||||
{
|
||||
var doc = Build();
|
||||
|
||||
var createItem = doc.Descendants(Messages + "CreateItem").Single();
|
||||
Assert.Equal("SendOnly", (string?)createItem.Attribute("MessageDisposition"));
|
||||
|
||||
var version = doc.Descendants(Types + "RequestServerVersion").Single();
|
||||
Assert.Equal("Exchange2013", (string?)version.Attribute("Version"));
|
||||
Assert.Single(doc.Descendants(Soap + "Header").Single().Elements());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildCreateItem_EscapesMarkupInSubjectAndBody()
|
||||
{
|
||||
const string subject = "Alarm <HIGH> & \"critical\"";
|
||||
const string body = "value < 5 & tag=\"A\" > threshold";
|
||||
|
||||
var doc = Build(subject: subject, body: body);
|
||||
|
||||
// Round-tripping through the parser proves the content was escaped, not concatenated.
|
||||
Assert.Equal(subject, doc.Descendants(Types + "Subject").Single().Value);
|
||||
Assert.Equal(body, doc.Descendants(Types + "Body").Single().Value);
|
||||
Assert.Equal("Text", (string?)doc.Descendants(Types + "Body").Single().Attribute("BodyType"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildCreateItem_PutsEveryRecipientInBccAndNeverEmitsToRecipients()
|
||||
{
|
||||
string[] recipients = ["a@example.com", "b@example.com", "c@example.com"];
|
||||
|
||||
var doc = Build(bcc: recipients);
|
||||
|
||||
var bcc = doc.Descendants(Types + "BccRecipients").Single();
|
||||
var addresses = bcc.Elements(Types + "Mailbox")
|
||||
.Select(m => m.Element(Types + "EmailAddress")!.Value)
|
||||
.ToArray();
|
||||
Assert.Equal(recipients, addresses);
|
||||
|
||||
// Recipient privacy is the whole reason for BCC-only: no To/Cc may leak the list.
|
||||
Assert.DoesNotContain(doc.Descendants(), e => e.Name.LocalName is "ToRecipients" or "CcRecipients");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildCreateItem_EmitsFromMailbox()
|
||||
{
|
||||
var doc = Build(from: "scada-alerts@example.com");
|
||||
|
||||
var from = doc.Descendants(Types + "From").Single();
|
||||
var mailbox = from.Elements(Types + "Mailbox").Single();
|
||||
Assert.Equal("scada-alerts@example.com", mailbox.Element(Types + "EmailAddress")!.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildCreateItem_OrdersMessageChildrenPerEwsSchemaSequence()
|
||||
{
|
||||
var doc = Build();
|
||||
|
||||
// MessageType is an xs:sequence — any other order is ErrorSchemaValidation at the server.
|
||||
var message = doc.Descendants(Types + "Message").Single();
|
||||
Assert.Equal(
|
||||
new[] { "Subject", "Body", "BccRecipients", "From" },
|
||||
message.Elements().Select(e => e.Name.LocalName).ToArray());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildCreateItem_StartsWithUtf8XmlDeclaration()
|
||||
{
|
||||
var xml = EwsSoapEnvelope.BuildCreateItem("a@example.com", ["b@example.com"], "s", "b");
|
||||
|
||||
Assert.StartsWith("<?xml", xml, StringComparison.Ordinal);
|
||||
var declaration = xml[..xml.IndexOf("?>", StringComparison.Ordinal)];
|
||||
Assert.Contains("utf-8", declaration, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user