Skip to content

Commit

Permalink
Don't redirect on invalid model state, just log.
Browse files Browse the repository at this point in the history
Allow for .NET treatment of POSTed empty string.
  • Loading branch information
RobertGHippo committed Jul 17, 2024
1 parent d95c5e1 commit 614f757
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ public async Task<IActionResult> Get()
}

[HttpPost]
public IActionResult Refine(string refineSearch)
public IActionResult Refine(string? refineSearch)
{
if (!ModelState.IsValid)
{
logger.LogError("Invalid model state");
return RedirectToAction("Index", "Error");
logger.LogError($"Invalid model state in {nameof(QualificationDetailsController)} POST");
}

userJourneyCookieService.SetQualificationNameSearchCriteria(refineSearch);
userJourneyCookieService.SetQualificationNameSearchCriteria(refineSearch ?? string.Empty);

return RedirectToAction("Get");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,63 @@ public void Refine_SaveQualificationName_RedirectsToGet()
actionResult.ActionName.Should().Be("Get");
mockUserJourneyCookieService.Verify(x => x.SetQualificationNameSearchCriteria("Test"), Times.Once);
}

[TestMethod]
public void Refine_NullParam_RedirectsToGet()
{
var mockLogger = new Mock<ILogger<QualificationDetailsController>>();
var mockContentService = new Mock<IContentService>();
var mockContentFilterService = new Mock<IContentFilterService>();
var mockInsetTextRenderer = new Mock<IGovUkInsetTextRenderer>();
var mockHtmlRenderer = new Mock<IHtmlRenderer>();
var mockUserJourneyCookieService = new Mock<IUserJourneyCookieService>();

var controller =
new QualificationDetailsController(mockLogger.Object, mockContentService.Object,
mockContentFilterService.Object, mockInsetTextRenderer.Object,
mockHtmlRenderer.Object, mockUserJourneyCookieService.Object)
{
ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext()
}
};

var result = controller.Refine(null);

result.Should().BeOfType<RedirectToActionResult>();

var actionResult = (RedirectToActionResult)result;

actionResult.ActionName.Should().Be("Get");
mockUserJourneyCookieService.Verify(x => x.SetQualificationNameSearchCriteria(string.Empty), Times.Once);
}

[TestMethod]
public void Refine_InvalidModel_LogsError()
{
var mockLogger = new Mock<ILogger<QualificationDetailsController>>();
var mockContentService = new Mock<IContentService>();
var mockContentFilterService = new Mock<IContentFilterService>();
var mockInsetTextRenderer = new Mock<IGovUkInsetTextRenderer>();
var mockHtmlRenderer = new Mock<IHtmlRenderer>();
var mockUserJourneyCookieService = new Mock<IUserJourneyCookieService>();

var controller =
new QualificationDetailsController(mockLogger.Object, mockContentService.Object,
mockContentFilterService.Object, mockInsetTextRenderer.Object,
mockHtmlRenderer.Object, mockUserJourneyCookieService.Object)
{
ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext()
}
};

controller.ModelState.AddModelError("Key", "Error message");

controller.Refine(null);

mockLogger.VerifyError($"Invalid model state in {nameof(QualificationDetailsController)} POST");
}
}

0 comments on commit 614f757

Please sign in to comment.