-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exclusions - Adding an exclusion - authority website (#675)
* Exclusions - Adding an exclusion - authority website * Added unit test * PR comment fix * PR comment update * PR comment update * pr comments resolved
- Loading branch information
1 parent
c12e7bb
commit ff6adec
Showing
18 changed files
with
2,312 additions
and
131 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
Frontend/CO.CDP.OrganisationApp.Tests/Pages/Forms/FormElementUrlInputModelTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
using CO.CDP.OrganisationApp.Models; | ||
using CO.CDP.OrganisationApp.Pages.Forms; | ||
using FluentAssertions; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace CO.CDP.OrganisationApp.Tests.Pages.Forms; | ||
|
||
public class FormElementUrlInputModelTests | ||
{ | ||
[Fact] | ||
public void GetAnswer_ShouldReturnNull_WhenIsNotRequiredAndHasNoValue() | ||
{ | ||
var model = new FormElementUrlInputModel | ||
{ | ||
IsRequired = false, | ||
HasValue = false | ||
}; | ||
|
||
var result = model.GetAnswer(); | ||
|
||
result.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void GetAnswer_ShouldReturnAnswer_WhenTextInputIsProvided() | ||
{ | ||
var model = new FormElementUrlInputModel | ||
{ | ||
IsRequired = true, | ||
TextInput = "https://example.com", | ||
HasValue = true | ||
}; | ||
|
||
var result = model.GetAnswer(); | ||
|
||
result.Should().NotBeNull(); | ||
result?.TextValue.Should().Be("https://example.com"); | ||
} | ||
|
||
[Fact] | ||
public void SetAnswer_ShouldSetTextInputAndHasValue_WhenAnswerIsProvided() | ||
{ | ||
var model = new FormElementUrlInputModel(); | ||
var answer = new FormAnswer { TextValue = "https://example.com" }; | ||
|
||
model.SetAnswer(answer); | ||
|
||
model.TextInput.Should().Be("https://example.com"); | ||
model.HasValue.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void SetAnswer_ShouldSetHasValueToFalse_WhenAnswerIsNullAndRedirectFromCheckYourAnswerPage() | ||
{ | ||
var model = new FormElementUrlInputModel(); | ||
model.Initialize(new FormQuestion { IsRequired = false }, true); | ||
|
||
model.SetAnswer(null); | ||
|
||
model.HasValue.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldReturnError_WhenHasValueIsNullAndIsNotRequired() | ||
{ | ||
var model = new FormElementUrlInputModel | ||
{ | ||
IsRequired = false, | ||
HasValue = null | ||
}; | ||
|
||
var validationResults = new List<ValidationResult>(); | ||
var context = new ValidationContext(model); | ||
Validator.TryValidateObject(model, context, validationResults, true); | ||
|
||
validationResults.Should().ContainSingle(); | ||
validationResults[0].ErrorMessage.Should().Be("Select an option"); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldReturnError_WhenTextInputIsMissingAndIsRequired() | ||
{ | ||
var model = new FormElementUrlInputModel | ||
{ | ||
IsRequired = true, | ||
HasValue = true, | ||
TextInput = null | ||
}; | ||
|
||
var validationResults = new List<ValidationResult>(); | ||
var context = new ValidationContext(model); | ||
Validator.TryValidateObject(model, context, validationResults, true); | ||
|
||
validationResults.Should().ContainSingle(); | ||
validationResults[0].ErrorMessage.Should().Be("Enter a website address"); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldReturnError_WhenTextInputIsInvalidUrl() | ||
{ | ||
var model = new FormElementUrlInputModel | ||
{ | ||
IsRequired = true, | ||
HasValue = true, | ||
TextInput = "invalid-url" | ||
}; | ||
|
||
var validationResults = new List<ValidationResult>(); | ||
var context = new ValidationContext(model); | ||
Validator.TryValidateObject(model, context, validationResults, true); | ||
|
||
validationResults.Should().ContainSingle(); | ||
validationResults[0].ErrorMessage.Should().Be("Enter a valid website address in the correct format"); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldPass_WhenValidUrlIsProvidedAndIsRequired() | ||
{ | ||
var model = new FormElementUrlInputModel | ||
{ | ||
IsRequired = true, | ||
HasValue = true, | ||
TextInput = "https://example.com" | ||
}; | ||
|
||
var validationResults = new List<ValidationResult>(); | ||
var context = new ValidationContext(model); | ||
var isValid = Validator.TryValidateObject(model, context, validationResults, true); | ||
|
||
isValid.Should().BeTrue(); | ||
validationResults.Should().BeEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,5 +78,6 @@ public enum FormQuestionType | |
Date, | ||
CheckBox, | ||
Address, | ||
MultiLine | ||
MultiLine, | ||
Url | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Frontend/CO.CDP.OrganisationApp/Pages/Forms/FormElementUrlInputModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using CO.CDP.OrganisationApp.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace CO.CDP.OrganisationApp.Pages.Forms; | ||
|
||
public class FormElementUrlInputModel : FormElementModel, IValidatableObject | ||
{ | ||
[BindProperty] | ||
public string? TextInput { get; set; } | ||
|
||
[BindProperty] | ||
public bool? HasValue { get; set; } | ||
|
||
|
||
public override FormAnswer? GetAnswer() | ||
{ | ||
if (IsRequired == false && HasValue == false) | ||
{ | ||
return null; | ||
} | ||
|
||
return string.IsNullOrWhiteSpace(TextInput) ? null : new FormAnswer { TextValue = TextInput }; | ||
} | ||
|
||
public override void SetAnswer(FormAnswer? answer) | ||
{ | ||
if (answer?.TextValue != null) | ||
{ | ||
TextInput = answer.TextValue; | ||
HasValue = true; | ||
} | ||
else if (RedirectFromCheckYourAnswerPage && IsRequired == false) | ||
{ | ||
HasValue = false; | ||
} | ||
} | ||
|
||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
{ | ||
var validateTextField = IsRequired; | ||
|
||
if (IsRequired == false) | ||
{ | ||
if (HasValue == null) | ||
{ | ||
yield return new ValidationResult("Select an option", [nameof(HasValue)]); | ||
} | ||
else if (HasValue == true) | ||
{ | ||
validateTextField = true; | ||
} | ||
} | ||
|
||
if (validateTextField) | ||
{ | ||
if (string.IsNullOrWhiteSpace(TextInput)) | ||
{ | ||
yield return new ValidationResult("Enter a website address", [nameof(TextInput)]); | ||
} | ||
else if (Uri.TryCreate(TextInput, UriKind.Absolute, out var _) == false) | ||
{ | ||
yield return new ValidationResult("Enter a valid website address in the correct format", [nameof(TextInput)]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.