Skip to content

Commit

Permalink
ShouldThrowValidationExceptionIfNoReceiverWasAdded -> FAIL
Browse files Browse the repository at this point in the history
  • Loading branch information
mabroukmahdhi committed Aug 11, 2024
1 parent 59218cd commit 314b9f0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,66 @@ await Assert.ThrowsAsync<FlexiMessageValidationException>(
broker.SendAndSaveCopy(It.IsAny<EmailMessage>()),
Times.Never);
}

[Fact]
public async void ShouldThrowValidationExceptionIfNoReceiverWasAdded()
{
// given
var randomFlexiMessage = CreateRandomFlexiMessage();

randomFlexiMessage.To = null;
randomFlexiMessage.Cc = null;
randomFlexiMessage.Bcc = null;

var inputFlexiMessage = randomFlexiMessage;

var invalidFlexiMessageException =
new InvalidFlexiMessageException(
message: "FlexiMessage has no receiver.");

invalidFlexiMessageException.AddData(
key: nameof(FlexiMessage.To),
values: "To is not set.");

invalidFlexiMessageException.AddData(
key: nameof(FlexiMessage.Cc),
values: "Cc is not set.");

invalidFlexiMessageException.AddData(
key: nameof(FlexiMessage.Bcc),
values: "Bcc is not set.");

var expectedFlexiMessageValidationException =
new FlexiMessageValidationException(
message: "Flexi Message validation error occurred, fix errors and try again.",
innerException: invalidFlexiMessageException);

// when
var sendMessageTask =
this.flexiExchangeService.SendAndSaveCopyAsync(inputFlexiMessage);

var actualFlexiMessageValidationException =
await Assert.ThrowsAsync<FlexiMessageValidationException>(
sendMessageTask.AsTask);
// then

actualFlexiMessageValidationException.Should()
.BeEquivalentTo(expectedFlexiMessageValidationException);

this.exchangeBrokerMock.Verify(broker =>
broker.GetAccessTokenAsync(),
Times.Never);

this.exchangeBrokerMock.Verify(broker =>
broker.CreateExchangeService(
ExchangeVersion.Exchange2013,
It.IsAny<string>(),
It.IsAny<ImpersonatedUserId>()),
Times.Never);

this.exchangeBrokerMock.Verify(broker =>
broker.SendAndSaveCopy(It.IsAny<EmailMessage>()),
Times.Never);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ---------------------------------------
// Copyright (c) 2024 Mabrouk Mahdhi.
// Made with love for the .NET Community
// ---------------------------------------

using Xeptions;

namespace FlexiMail.Models.Foundations.Messages.Exceptions
{
public class InvalidFlexiMessageException : Xeption
{
public InvalidFlexiMessageException(string message)
: base(message)
{ }

public InvalidFlexiMessageException(string message, Xeption innerException)
: base(message, innerException)
{ }
}
}
2 changes: 2 additions & 0 deletions FlexiMail/Services/FlexiExchangeService.Validations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal partial class FlexiExchangeService
private static void ValidFlexiMessage(FlexiMessage flexiMessage)
{
ValidFlexiMessageIsNotNull(flexiMessage);


}

private static void ValidFlexiMessageIsNotNull(FlexiMessage flexiMessage)
Expand Down

0 comments on commit 314b9f0

Please sign in to comment.