diff --git a/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Centre/SystemNotificationControllerTests.cs b/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Centre/SystemNotificationControllerTests.cs index 405bc336aa..b5763f4982 100644 --- a/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Centre/SystemNotificationControllerTests.cs +++ b/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Centre/SystemNotificationControllerTests.cs @@ -1,8 +1,11 @@ namespace DigitalLearningSolutions.Web.Tests.Controllers.TrackingSystem.Centre { using System; + using System.Collections.Generic; using DigitalLearningSolutions.Data.DataServices; + using DigitalLearningSolutions.Data.Models; using DigitalLearningSolutions.Data.Services; + using DigitalLearningSolutions.Data.Tests.TestHelpers; using DigitalLearningSolutions.Web.Controllers.TrackingSystem.Centre; using DigitalLearningSolutions.Web.Helpers; using DigitalLearningSolutions.Web.Tests.ControllerHelpers; @@ -15,10 +18,10 @@ public class SystemNotificationControllerTests { private readonly IClockService clockService = A.Fake(); + private SystemNotificationsController controller = null!; private HttpRequest httpRequest = null!; private HttpResponse httpResponse = null!; private ISystemNotificationsDataService systemNotificationsDataService = null!; - private SystemNotificationsController controller = null!; [SetUp] public void Setup() @@ -30,24 +33,27 @@ public void Setup() new SystemNotificationsController(systemNotificationsDataService, clockService) .WithMockHttpContext(httpRequest, response: httpResponse) .WithMockUser(true) - .WithMockServices(); + .WithMockServices() + .WithMockTempData(); } [Test] - public void SkipNotifications_sets_cookie_and_redirects_to_dashboard() + public void Index_sets_cookie_when_unacknowledged_notifications_exist() { // Given var testDate = new DateTime(2021, 8, 23); A.CallTo(() => clockService.UtcNow).Returns(testDate); var expectedExpiry = testDate.AddHours(24); + A.CallTo(() => systemNotificationsDataService.GetUnacknowledgedSystemNotifications(A._)) + .Returns(new List { SystemNotificationTestHelper.GetDefaultSystemNotification() }); // When - var result = controller.SkipNotifications(); + var result = controller.Index(); // Then using (new AssertionScope()) { - result.Should().BeRedirectToActionResult().WithControllerName("Dashboard").WithActionName("Index"); + result.Should().BeViewResult().WithDefaultViewName(); A.CallTo( () => httpResponse.Cookies.Append( SystemNotificationCookieHelper.CookieName, @@ -58,20 +64,6 @@ public void SkipNotifications_sets_cookie_and_redirects_to_dashboard() } } - [Test] - public void SkipNotifications_does_not_acknowledge_notifications() - { - // Given - var testDate = new DateTime(2021, 8, 23); - A.CallTo(() => clockService.UtcNow).Returns(testDate); - - // When - controller.SkipNotifications(); - - // Then - A.CallTo(() => systemNotificationsDataService.AcknowledgeNotification(A._, A._)).MustNotHaveHappened(); - } - [Test] public void Post_acknowledges_notification_and_redirects() { @@ -82,53 +74,62 @@ public void Post_acknowledges_notification_and_redirects() using (new AssertionScope()) { A.CallTo(() => systemNotificationsDataService.AcknowledgeNotification(1, 7)).MustHaveHappened(); - result.Should().BeRedirectToActionResult().WithControllerName("SystemNotifications").WithActionName("Index"); + result.Should().BeRedirectToActionResult().WithControllerName("SystemNotifications") + .WithActionName("Index"); } } [Test] - public void Post_deletes_cookie_if_one_exists_for_user() + public void Index_deletes_cookie_if_one_exists_for_user_and_no_unacknowledged_notifications_exist() { // Given A.CallTo(() => httpRequest.Cookies).Returns( ControllerContextHelper.SetUpFakeRequestCookieCollection(SystemNotificationCookieHelper.CookieName, "7") ); + A.CallTo(() => systemNotificationsDataService.GetUnacknowledgedSystemNotifications(A._)) + .Returns(new List()); // When - controller.AcknowledgeNotification(1, 1); + controller.Index(); // Then A.CallTo(() => httpResponse.Cookies.Delete(SystemNotificationCookieHelper.CookieName)).MustHaveHappened(); } [Test] - public void Post_does_not_delete_cookie_if_one_exists_for_someone_other_than_current_user() + public void Index_does_not_delete_cookie_if_one_exists_for_someone_other_than_current_user() { // Given A.CallTo(() => httpRequest.Cookies).Returns( ControllerContextHelper.SetUpFakeRequestCookieCollection(SystemNotificationCookieHelper.CookieName, "8") ); + A.CallTo(() => systemNotificationsDataService.GetUnacknowledgedSystemNotifications(A._)) + .Returns(new List()); // When - controller.AcknowledgeNotification(1, 1); + controller.Index(); // Then - A.CallTo(() => httpResponse.Cookies.Delete(SystemNotificationCookieHelper.CookieName)).MustNotHaveHappened(); + A.CallTo(() => httpResponse.Cookies.Delete(SystemNotificationCookieHelper.CookieName)) + .MustNotHaveHappened(); } [Test] - public void Post_does_not_delete_cookie_if_one_does_not_exist() + public void Index_does_not_delete_cookie_if_one_does_not_exist() { // Given A.CallTo(() => httpRequest.Cookies).Returns( A.Fake() ); + A.CallTo(() => systemNotificationsDataService.GetUnacknowledgedSystemNotifications(A._)) + .Returns(new List()); // When - controller.AcknowledgeNotification(1, 1); + controller.Index(); // Then - A.CallTo(() => httpResponse.Cookies.Delete(SystemNotificationCookieHelper.CookieName)).MustNotHaveHappened(); + A.CallTo(() => httpResponse.Cookies.Delete(SystemNotificationCookieHelper.CookieName)) + .MustNotHaveHappened(); } } } diff --git a/DigitalLearningSolutions.Web/Controllers/TrackingSystem/Centre/SystemNotificationsController.cs b/DigitalLearningSolutions.Web/Controllers/TrackingSystem/Centre/SystemNotificationsController.cs index bcb40cbf64..138217040a 100644 --- a/DigitalLearningSolutions.Web/Controllers/TrackingSystem/Centre/SystemNotificationsController.cs +++ b/DigitalLearningSolutions.Web/Controllers/TrackingSystem/Centre/SystemNotificationsController.cs @@ -33,19 +33,20 @@ public IActionResult Index(int page = 1) var adminId = User.GetAdminId()!.Value; var unacknowledgedNotifications = systemNotificationsDataService.GetUnacknowledgedSystemNotifications(adminId).ToList(); + + if (unacknowledgedNotifications.Count > 0) + { + Response.Cookies.SetSkipSystemNotificationCookie(adminId, clockService.UtcNow); + } + else if (Request.Cookies.HasSkippedNotificationsCookie(adminId)) + { + Response.Cookies.DeleteSkipSystemNotificationCookie(); + } + var model = new SystemNotificationsViewModel(unacknowledgedNotifications, page); return View(model); } - [HttpGet] - [Route("SkipNotifications")] - public IActionResult SkipNotifications() - { - var adminId = User.GetAdminId()!.Value; - Response.Cookies.SetSkipSystemNotificationCookie(adminId, clockService.UtcNow); - return RedirectToAction("Index", "Dashboard"); - } - [HttpPost] [Route("{page:int}")] public IActionResult AcknowledgeNotification(int systemNotificationId, int page) @@ -53,11 +54,6 @@ public IActionResult AcknowledgeNotification(int systemNotificationId, int page) var adminId = User.GetAdminId()!.Value; systemNotificationsDataService.AcknowledgeNotification(systemNotificationId, adminId); - if (Request.Cookies.HasSkippedNotificationsCookie(adminId)) - { - Response.Cookies.DeleteSkipSystemNotificationCookie(); - } - return RedirectToAction("Index", "SystemNotifications", new { page }); } } diff --git a/DigitalLearningSolutions.Web/Views/TrackingSystem/Centre/SystemNotifications/Index.cshtml b/DigitalLearningSolutions.Web/Views/TrackingSystem/Centre/SystemNotifications/Index.cshtml index ab518ab10a..123c8bd7b8 100644 --- a/DigitalLearningSolutions.Web/Views/TrackingSystem/Centre/SystemNotifications/Index.cshtml +++ b/DigitalLearningSolutions.Web/Views/TrackingSystem/Centre/SystemNotifications/Index.cshtml @@ -20,8 +20,8 @@

New system notifications

- @if (Model.UnacknowledgedNotification == null) {