From c0546aa53cbb88f7eb64ffcedac1d3c8e5ebaf90 Mon Sep 17 00:00:00 2001 From: Auldrin Possa Date: Fri, 11 Oct 2024 15:46:44 +0100 Subject: [PATCH 1/2] TD-4793-unit tests added for deactivate delegate controller --- .../DeactivateDelegateControllerTests.cs | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Delegates/DeactivateDelegateControllerTests.cs diff --git a/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Delegates/DeactivateDelegateControllerTests.cs b/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Delegates/DeactivateDelegateControllerTests.cs new file mode 100644 index 0000000000..aecc9c9b87 --- /dev/null +++ b/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Delegates/DeactivateDelegateControllerTests.cs @@ -0,0 +1,152 @@ +using DigitalLearningSolutions.Web.Controllers.TrackingSystem.Delegates; +using DigitalLearningSolutions.Web.Services; +using DigitalLearningSolutions.Web.Tests.ControllerHelpers; +using DigitalLearningSolutions.Web.Tests.TestHelpers; +using DigitalLearningSolutions.Web.Helpers; +using FakeItEasy; +using FluentAssertions; +using FluentAssertions.AspNetCore.Mvc; +using NUnit.Framework; +using DigitalLearningSolutions.Web.ViewModels.TrackingSystem.Delegates.DeactivateDelegate; +using DigitalLearningSolutions.Data.Models.User; +using System.Collections.Generic; + +namespace DigitalLearningSolutions.Web.Tests.Controllers.TrackingSystem.Delegates +{ + public class DeactivateDelegateControllerTests + { + private const int DelegateId = 1; + private DeactivateDelegateController controller = null!; + private IUserService userService = null!; + + [SetUp] + public void SetUp() + { + userService = A.Fake(); + + controller = new DeactivateDelegateController(userService) + .WithDefaultContext() + .WithMockUser(true); + } + + [Test] + public void Index_returns_not_found_with_null_delegate() + { + // Given + A.CallTo(() => userService.CheckDelegateIsActive(DelegateId)).Returns(null); + + // When + var result = controller.Index(DelegateId); + + // Then + result.Should() + .BeRedirectToActionResult() + .WithControllerName("LearningSolutions") + .WithActionName("StatusCode") + .WithRouteValue("code", 410); + } + + [Test] + public void Index_returns_view_when_service_returns_valid_delegate() + { + // Given + A.CallTo(() => userService.CheckDelegateIsActive(DelegateId)).Returns(DelegateId); + int? centreId = controller.User.GetCentreId(); + + var delegateEntity = UserTestHelper.GetDefaultDelegateEntity(DelegateId, centreId.Value); + A.CallTo(() => userService.GetDelegateById(DelegateId)).Returns(delegateEntity); + + var userEntity = new UserEntity( + UserTestHelper.GetDefaultUserAccount(), + new List { UserTestHelper.GetDefaultAdminAccount(centreId.Value) }, + new List { UserTestHelper.GetDefaultDelegateAccount(centreId.Value) } + ); + + A.CallTo(() => userService.GetUserById(delegateEntity.DelegateAccount.UserId)).Returns(userEntity); + + // When + var result = controller.Index(DelegateId); + + // Then + result.Should().BeViewResult().WithDefaultViewName(); + result.Should().BeViewResult().ModelAs().DelegateId.Should().Be(DelegateId); + result.Should().BeViewResult().ModelAs().Name.Should().Be(delegateEntity.UserAccount.FirstName + " " + delegateEntity.UserAccount.LastName); + result.Should().BeViewResult().ModelAs().Email.Should().Be(delegateEntity.UserAccount.PrimaryEmail); + result.Should().BeViewResult().ModelAs().UserId.Should().Be(delegateEntity.UserAccount.Id); + } + + [Test] + public void Index_post_returns_error_when_no_option_selected_to_deactivate() + { + // Given + var formData = new DeactivateDelegateAccountViewModel + { + DelegateId = 1, + Name = "Firstname Test", + Roles = new List(), + Email = "email@test.com", + UserId = 2, + Deactivate = null + }; + + controller.ModelState.AddModelError("key", "Invalid for testing."); + + // When + var result = controller.Index(formData); + + // Then + result.Should().BeViewResult().ModelAs().DelegateId.Should().Be(DelegateId); + Assert.IsFalse(controller.ModelState.IsValid); + } + + + [Test] + public void Index_post_returns_view_for_deactivate_delegate_only() + { + // Given + var formData = new DeactivateDelegateAccountViewModel + { + DelegateId = 1, + Name = "Firstname Test", + Roles = new List(), + Email = "email@test.com", + UserId = 2, + Deactivate = true + }; + + // When + var result = controller.Index(formData); + + // Then + result.Should().BeRedirectToActionResult() + .WithControllerName("ViewDelegate") + .WithActionName("Index") + .WithRouteValue("delegateId", formData.DelegateId); + } + + [Test] + public void Index_post_returns_view_for_deactivate_delegate_and_admin() + { + // Given + var formData = new DeactivateDelegateAccountViewModel + { + DelegateId = 1, + Name = "Firstname Test", + Roles = new List(), + Email = "email@test.com", + UserId = 2, + Deactivate = false + }; + + // When + var result = controller.Index(formData); + + // Then + result.Should().BeRedirectToActionResult() + .WithControllerName("ViewDelegate") + .WithActionName("Index") + .WithRouteValue("delegateId", formData.DelegateId); + } + + } +} From 2fca1551fda4da1d237d334d816a75cf9cc8b3fe Mon Sep 17 00:00:00 2001 From: Auldrin Possa Date: Fri, 11 Oct 2024 20:57:24 +0100 Subject: [PATCH 2/2] test cases updated --- .../Delegates/DeactivateDelegateControllerTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Delegates/DeactivateDelegateControllerTests.cs b/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Delegates/DeactivateDelegateControllerTests.cs index aecc9c9b87..3cd25f78df 100644 --- a/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Delegates/DeactivateDelegateControllerTests.cs +++ b/DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Delegates/DeactivateDelegateControllerTests.cs @@ -104,6 +104,7 @@ public void Index_post_returns_error_when_no_option_selected_to_deactivate() public void Index_post_returns_view_for_deactivate_delegate_only() { // Given + int? centreId = controller.User.GetCentreId(); var formData = new DeactivateDelegateAccountViewModel { DelegateId = 1, @@ -118,16 +119,21 @@ public void Index_post_returns_view_for_deactivate_delegate_only() var result = controller.Index(formData); // Then + A.CallTo(() => userService.DeactivateDelegateUser(DelegateId)).MustHaveHappenedOnceExactly(); + A.CallTo(() => userService.DeactivateAdminAccount(formData.UserId, centreId.Value)).MustNotHaveHappened(); + result.Should().BeRedirectToActionResult() .WithControllerName("ViewDelegate") .WithActionName("Index") .WithRouteValue("delegateId", formData.DelegateId); + } [Test] public void Index_post_returns_view_for_deactivate_delegate_and_admin() { // Given + int? centreId = controller.User.GetCentreId(); var formData = new DeactivateDelegateAccountViewModel { DelegateId = 1, @@ -142,6 +148,9 @@ public void Index_post_returns_view_for_deactivate_delegate_and_admin() var result = controller.Index(formData); // Then + A.CallTo(() => userService.DeactivateDelegateUser(DelegateId)).MustHaveHappenedOnceExactly(); + A.CallTo(() => userService.DeactivateAdminAccount(formData.UserId, centreId.Value)).MustHaveHappenedOnceExactly(); + result.Should().BeRedirectToActionResult() .WithControllerName("ViewDelegate") .WithActionName("Index")