diff --git a/src/Altinn.Notifications.Core/Models/NotificationTemplate/EmailTemplate.cs b/src/Altinn.Notifications.Core/Models/NotificationTemplate/EmailTemplate.cs index 0f3353d1..dbee9a59 100644 --- a/src/Altinn.Notifications.Core/Models/NotificationTemplate/EmailTemplate.cs +++ b/src/Altinn.Notifications.Core/Models/NotificationTemplate/EmailTemplate.cs @@ -33,9 +33,9 @@ public class EmailTemplate : INotificationTemplate /// /// Initializes a new instance of the class. /// - 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; diff --git a/src/Altinn.Notifications/Models/EmailNotificationOrderRequestExt.cs b/src/Altinn.Notifications/Models/EmailNotificationOrderRequestExt.cs index dbfcf7a5..51ebe5e2 100644 --- a/src/Altinn.Notifications/Models/EmailNotificationOrderRequestExt.cs +++ b/src/Altinn.Notifications/Models/EmailNotificationOrderRequestExt.cs @@ -17,7 +17,7 @@ public class EmailNotificationOrderRequestExt /// Gets or sets the from address to use as sender of the email /// [JsonPropertyName("fromAddress")] - public string FromAddress { get; set; } = string.Empty; + public string? FromAddress { get; set; } /// /// Gets or sets the subject of the email diff --git a/src/Altinn.Notifications/Validators/EmailNotificationOrderRequestValidator.cs b/src/Altinn.Notifications/Validators/EmailNotificationOrderRequestValidator.cs index 5750fc84..f1250f51 100644 --- a/src/Altinn.Notifications/Validators/EmailNotificationOrderRequestValidator.cs +++ b/src/Altinn.Notifications/Validators/EmailNotificationOrderRequestValidator.cs @@ -26,6 +26,5 @@ public EmailNotificationOrderRequestValidator() RuleFor(order => order.Body).NotEmpty(); RuleFor(order => order.Subject).NotEmpty(); - RuleFor(order => order.FromAddress).NotEmpty(); } } diff --git a/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsOrderController/EmailNotificationOrdersControllerTests.cs b/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsOrderController/EmailNotificationOrdersControllerTests.cs index 06419348..cd78e612 100644 --- a/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsOrderController/EmailNotificationOrdersControllerTests.cs +++ b/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsOrderController/EmailNotificationOrdersControllerTests.cs @@ -181,8 +181,19 @@ public async Task Post_ServiceReturnsError_ServerError() public async Task Post_ServiceReturnsOrder_Accepted() { // Arrange + string expectedFromAddress = "sender@domain.com"; + Mock serviceMock = new(); serviceMock.Setup(s => s.RegisterEmailNotificationOrder(It.IsAny())) + .Callback(orderRequest => + { + var emailTemplate = orderRequest.Templates + .OfType() + .FirstOrDefault(); + + Assert.NotNull(emailTemplate); + Assert.Equal(expectedFromAddress, emailTemplate.FromAddress); + }) .ReturnsAsync((_order, null)); HttpClient client = GetTestClient(orderService: serviceMock.Object); @@ -207,6 +218,54 @@ public async Task Post_ServiceReturnsOrder_Accepted() serviceMock.VerifyAll(); } + [Fact] + public async Task Post_OrderWithoutFromAddress_StringEmptyUsedAsServiceInput_Accepted() + { + // Arrange + Mock serviceMock = new(); + + serviceMock.Setup(s => s.RegisterEmailNotificationOrder(It.IsAny())) + .Callback(orderRequest => + { + var emailTemplate = orderRequest.Templates + .OfType() + .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() { new RecipientExt() { EmailAddress = "recipient1@domain.com" }, new RecipientExt() { EmailAddress = "recipient2@domain.com" } }, + 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? validator = null, IEmailNotificationOrderService? orderService = null) { if (validator == null) diff --git a/test/Altinn.Notifications.Tests/Notifications/TestingValidators/EmailNotificationOrderRequestValidatorTests.cs b/test/Altinn.Notifications.Tests/Notifications/TestingValidators/EmailNotificationOrderRequestValidatorTests.cs index 9a8e83b2..e1e8186c 100644 --- a/test/Altinn.Notifications.Tests/Notifications/TestingValidators/EmailNotificationOrderRequestValidatorTests.cs +++ b/test/Altinn.Notifications.Tests/Notifications/TestingValidators/EmailNotificationOrderRequestValidatorTests.cs @@ -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() { new RecipientExt() { Id = "16069412345", EmailAddress = "recipient2@domain.com" } }, - 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() {