Skip to content

Commit

Permalink
Prevent a null email address from being submitted (#1693)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Nov 20, 2024
1 parent bf75077 commit 93726c2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public CreateTrnRequestRequestValidator(IClock clock)
});

RuleForEach(r => r.Person.EmailAddresses)
.NotNull()
.WithMessage("Email address cannot be null.")
.EmailAddress()
.MaximumLength(AttributeConstraints.Contact.EMailAddress1MaxLength);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,41 @@ public async Task Post_RequestWithoutEmail_ReturnsOk()
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

[Fact]
public async Task Post_RequestWithNullEmail_ReturnsError()
{
// Arrange
var requestId = Guid.NewGuid().ToString();
var firstName = Faker.Name.First();
var middleName = Faker.Name.Middle();
var lastName = Faker.Name.Last();
var dateOfBirth = new DateOnly(1990, 01, 01);

var requestBody = CreateJsonContent(CreateDummyRequest() with
{
RequestId = requestId,
Person = new()
{
FirstName = firstName,
MiddleName = middleName,
LastName = lastName,
DateOfBirth = dateOfBirth,
EmailAddresses = new[] { (string?)null }!
}
});

var request = new HttpRequestMessage(HttpMethod.Post, "v3/trn-requests")
{
Content = requestBody
};

// Act
var response = await GetHttpClientWithApiKey().SendAsync(request);

// Assert
await AssertEx.JsonResponseHasValidationErrorForPropertyAsync(response, "person.emailAddresses[0]", "Email address cannot be null.");
}

private static CreateTrnRequestRequest CreateDummyRequest() => new()
{
RequestId = Guid.NewGuid().ToString(),
Expand Down

0 comments on commit 93726c2

Please sign in to comment.