Skip to content

Commit

Permalink
Merge branch 'main' into feature/247-object-for-return
Browse files Browse the repository at this point in the history
  • Loading branch information
acn-sbuad committed Sep 22, 2023
2 parents d4dd6d1 + 2927d5a commit 0c06525
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public class EmailTemplate : INotificationTemplate
/// <summary>
/// Initializes a new instance of the <see cref="EmailTemplate"/> class.
/// </summary>
public EmailTemplate(string fromAddress, string subject, string body, EmailContentType contentType)
public EmailTemplate(string? fromAddress, string subject, string body, EmailContentType contentType)
{
FromAddress = fromAddress;
FromAddress = fromAddress ?? string.Empty;
Subject = subject;
Body = body;
ContentType = contentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class EmailNotificationOrderRequestExt
/// Gets or sets the from address to use as sender of the email
/// </summary>
[JsonPropertyName("fromAddress")]
public string FromAddress { get; set; } = string.Empty;
public string? FromAddress { get; set; }

/// <summary>
/// Gets or sets the subject of the email
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ public EmailNotificationOrderRequestValidator()

RuleFor(order => order.Body).NotEmpty();
RuleFor(order => order.Subject).NotEmpty();
RuleFor(order => order.FromAddress).NotEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,19 @@ public async Task Post_ServiceReturnsError_ServerError()
public async Task Post_ServiceReturnsOrder_Accepted()
{
// Arrange
string expectedFromAddress = "[email protected]";

Mock<IEmailNotificationOrderService> serviceMock = new();
serviceMock.Setup(s => s.RegisterEmailNotificationOrder(It.IsAny<NotificationOrderRequest>()))
.Callback<NotificationOrderRequest>(orderRequest =>
{
var emailTemplate = orderRequest.Templates
.OfType<EmailTemplate>()
.FirstOrDefault();

Assert.NotNull(emailTemplate);
Assert.Equal(expectedFromAddress, emailTemplate.FromAddress);
})
.ReturnsAsync((_order, null));

HttpClient client = GetTestClient(orderService: serviceMock.Object);
Expand All @@ -207,6 +218,54 @@ public async Task Post_ServiceReturnsOrder_Accepted()
serviceMock.VerifyAll();
}

[Fact]
public async Task Post_OrderWithoutFromAddress_StringEmptyUsedAsServiceInput_Accepted()
{
// Arrange
Mock<IEmailNotificationOrderService> serviceMock = new();

serviceMock.Setup(s => s.RegisterEmailNotificationOrder(It.IsAny<NotificationOrderRequest>()))
.Callback<NotificationOrderRequest>(orderRequest =>
{
var emailTemplate = orderRequest.Templates
.OfType<EmailTemplate>()
.FirstOrDefault();

Assert.NotNull(emailTemplate);
Assert.Empty(emailTemplate.FromAddress);
})
.ReturnsAsync((_order, null));

HttpClient client = GetTestClient(orderService: serviceMock.Object);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", PrincipalUtil.GetOrgToken("ttd"));

EmailNotificationOrderRequestExt request = new()
{
Body = "email-body",
ContentType = EmailContentType.Html,
Recipients = new List<RecipientExt>() { new RecipientExt() { EmailAddress = "[email protected]" }, new RecipientExt() { EmailAddress = "[email protected]" } },
SendersReference = "senders-reference",
RequestedSendTime = DateTime.UtcNow,
Subject = "email-subject",
};

HttpRequestMessage httpRequestMessage = new(HttpMethod.Post, _basePath)
{
Content = new StringContent(request.Serialize(), Encoding.UTF8, "application/json")
};

// Act
HttpResponseMessage response = await client.SendAsync(httpRequestMessage);
string actualOrderId = await response.Content.ReadAsStringAsync();

// Assert
Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
Assert.Equal("http://localhost:5090/notifications/api/v1/orders/" + _order.Id, response.Headers?.Location?.ToString());
Assert.Equal($"{_order.Id}", actualOrderId);

serviceMock.VerifyAll();
}

private HttpClient GetTestClient(IValidator<EmailNotificationOrderRequestExt>? validator = null, IEmailNotificationOrderService? orderService = null)
{
if (validator == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,6 @@ public void Validate_SendTimePassed_ReturnsFalse()
Assert.Contains(actual.Errors, a => a.ErrorMessage.Equals("Send time must be in the future. Leave blank to send immediately."));
}

[Fact]
public void Validate_FromAddressMissing_ReturnsFalse()
{
var order = new EmailNotificationOrderRequestExt()
{
Subject = "This is an email subject",
Recipients = new List<RecipientExt>() { new RecipientExt() { Id = "16069412345", EmailAddress = "[email protected]" } },
Body = "This is an email body"
};

var actual = _validator.Validate(order);
Assert.False(actual.IsValid);
Assert.Contains(actual.Errors, a => a.ErrorMessage.Equals("'From Address' must not be empty."));
}

[Fact]
public void Validate_SubjectMissing_ReturnsFalse()
{
Expand Down

0 comments on commit 0c06525

Please sign in to comment.