From 614f757d247600ad815f05bd4fc4d2c91259e5cf Mon Sep 17 00:00:00 2001 From: RobertGHippo Date: Wed, 17 Jul 2024 15:41:42 +0100 Subject: [PATCH] Don't redirect on invalid model state, just log. Allow for .NET treatment of POSTed empty string. --- .../QualificationDetailsController.cs | 7 +-- .../QualificationDetailsControllerTests.cs | 59 +++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/Dfe.EarlyYearsQualification.Web/Controllers/QualificationDetailsController.cs b/src/Dfe.EarlyYearsQualification.Web/Controllers/QualificationDetailsController.cs index 2dff25cc..b6e176ba 100644 --- a/src/Dfe.EarlyYearsQualification.Web/Controllers/QualificationDetailsController.cs +++ b/src/Dfe.EarlyYearsQualification.Web/Controllers/QualificationDetailsController.cs @@ -36,15 +36,14 @@ public async Task 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"); } diff --git a/tests/Dfe.EarlyYearsQualification.UnitTests/Controllers/QualificationDetailsControllerTests.cs b/tests/Dfe.EarlyYearsQualification.UnitTests/Controllers/QualificationDetailsControllerTests.cs index 27805265..f6889be5 100644 --- a/tests/Dfe.EarlyYearsQualification.UnitTests/Controllers/QualificationDetailsControllerTests.cs +++ b/tests/Dfe.EarlyYearsQualification.UnitTests/Controllers/QualificationDetailsControllerTests.cs @@ -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>(); + var mockContentService = new Mock(); + var mockContentFilterService = new Mock(); + var mockInsetTextRenderer = new Mock(); + var mockHtmlRenderer = new Mock(); + var mockUserJourneyCookieService = new Mock(); + + 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(); + + 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>(); + var mockContentService = new Mock(); + var mockContentFilterService = new Mock(); + var mockInsetTextRenderer = new Mock(); + var mockHtmlRenderer = new Mock(); + var mockUserJourneyCookieService = new Mock(); + + 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"); + } } \ No newline at end of file