Skip to content

Commit

Permalink
Validating requestedSendTime to check if it has zone specified or n…
Browse files Browse the repository at this point in the history
…ot (#308)

* Introduced method to check if requestedSendTime is without zone

* Implemented validator with HasTimeZone method

* Implemented the conditional predicate with anonymous function

* Tests added for time zone validation

* Error message updated for the requestedSendTime zone error

* Error message updated in the corrsponding tests for the changes

* Modification to convert the time zone from Local to UTC

* Test added for the modification to convert Local to UTC
  • Loading branch information
khanrn authored Nov 27, 2023
1 parent e3b6dda commit 8cd8ada
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Altinn.Notifications/Mappers/OrderMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static NotificationOrderRequest MapToOrderRequest(this EmailNotificationO
extRequest.SendersReference,
creator,
new List<INotificationTemplate>() { emailTemplate },
extRequest.RequestedSendTime,
extRequest.RequestedSendTime.ToUniversalTime(),
NotificationChannel.Email,
recipients);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public EmailNotificationOrderRequestValidator()
.WithMessage("A valid email address must be provided for all recipients.");

RuleFor(order => order.RequestedSendTime)
.Must(sendTime => sendTime >= DateTime.UtcNow.AddMinutes(-5))
.WithMessage("Send time must be in the future. Leave blank to send immediately.");
.Must(sendTime => sendTime.Kind != DateTimeKind.Unspecified)
.WithMessage("The requested send time value must have specified a time zone.")
.Must(sendTime => sendTime >= DateTime.UtcNow.AddMinutes(-5))
.WithMessage("Send time must be in the future. Leave blank to send immediately.");

RuleFor(order => order.Body).NotEmpty();
RuleFor(order => order.Subject).NotEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,42 @@ public void MapToOrderRequest_RecipientsProvided_AreEquivalent()
Assert.Equivalent(expected, actual, true);
}

[Fact]
public void MapToOrderRequest_SendTimeLocalConvertedToUtc_AreEquivalent()
{
DateTime sendTime = DateTime.Now; // Setting the time in Local time zone

// Arrange
EmailNotificationOrderRequestExt orderRequestExt = new()
{
Body = "email-body",
ContentType = EmailContentTypeExt.Html,
RequestedSendTime = sendTime, // Local time zone
Subject = "email-subject"
};

NotificationOrderRequest expected = new()
{
Creator = new Creator("ttd"),
Templates = new List<INotificationTemplate>()
{
new EmailTemplate(
string.Empty,
"email-subject",
"email-body",
EmailContentType.Html)
},
RequestedSendTime = sendTime.ToUniversalTime(), // Expecting the time in UTC time zone
NotificationChannel = NotificationChannel.Email
};

// Act
var actual = orderRequestExt.MapToOrderRequest("ttd");

// Assert
Assert.Equivalent(expected, actual, true);
}

[Fact]
public void MapToNotificationOrderWithStatusExt_EmailStatusProvided_AreEquivalent()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,55 @@ public void Validate_EmailNotDefinedForRecipient_ReturnFalse()
Assert.Contains(actual.Errors, a => a.ErrorMessage.Equals("A valid email address must be provided for all recipients."));
}

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

var actual = _validator.Validate(order);

Assert.True(actual.IsValid);
}

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

var actual = _validator.Validate(order);

Assert.True(actual.IsValid);
}

[Fact]
public void Validate_SendTimeHasNoZone_ReturnsFalse()
{
var order = new EmailNotificationOrderRequestExt()
{
Subject = "This is an email subject",
Recipients = new List<RecipientExt>() { new RecipientExt() { EmailAddress = "[email protected]" } },
Body = "This is an email body",
RequestedSendTime = new DateTime(2023, 06, 16, 08, 50, 00, DateTimeKind.Unspecified)
};

var actual = _validator.Validate(order);

Assert.False(actual.IsValid);
Assert.Contains(actual.Errors, a => a.ErrorMessage.Equals("The requested send time value must have specified a time zone."));
}

[Fact]
public void Validate_SendTimePassed_ReturnsFalse()
{
Expand Down

0 comments on commit 8cd8ada

Please sign in to comment.