Skip to content

Commit c95e20d

Browse files
authored
Merge pull request #2883 from TechnologyEnhancedLearning/Develop/feature/TD-4793-unit-tests-to-deactivate-adminaccount-ifexists
TD-4793-Unit tests added for deactivate delegate controller
2 parents 297c56e + 2fca155 commit c95e20d

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using DigitalLearningSolutions.Web.Controllers.TrackingSystem.Delegates;
2+
using DigitalLearningSolutions.Web.Services;
3+
using DigitalLearningSolutions.Web.Tests.ControllerHelpers;
4+
using DigitalLearningSolutions.Web.Tests.TestHelpers;
5+
using DigitalLearningSolutions.Web.Helpers;
6+
using FakeItEasy;
7+
using FluentAssertions;
8+
using FluentAssertions.AspNetCore.Mvc;
9+
using NUnit.Framework;
10+
using DigitalLearningSolutions.Web.ViewModels.TrackingSystem.Delegates.DeactivateDelegate;
11+
using DigitalLearningSolutions.Data.Models.User;
12+
using System.Collections.Generic;
13+
14+
namespace DigitalLearningSolutions.Web.Tests.Controllers.TrackingSystem.Delegates
15+
{
16+
public class DeactivateDelegateControllerTests
17+
{
18+
private const int DelegateId = 1;
19+
private DeactivateDelegateController controller = null!;
20+
private IUserService userService = null!;
21+
22+
[SetUp]
23+
public void SetUp()
24+
{
25+
userService = A.Fake<IUserService>();
26+
27+
controller = new DeactivateDelegateController(userService)
28+
.WithDefaultContext()
29+
.WithMockUser(true);
30+
}
31+
32+
[Test]
33+
public void Index_returns_not_found_with_null_delegate()
34+
{
35+
// Given
36+
A.CallTo(() => userService.CheckDelegateIsActive(DelegateId)).Returns(null);
37+
38+
// When
39+
var result = controller.Index(DelegateId);
40+
41+
// Then
42+
result.Should()
43+
.BeRedirectToActionResult()
44+
.WithControllerName("LearningSolutions")
45+
.WithActionName("StatusCode")
46+
.WithRouteValue("code", 410);
47+
}
48+
49+
[Test]
50+
public void Index_returns_view_when_service_returns_valid_delegate()
51+
{
52+
// Given
53+
A.CallTo(() => userService.CheckDelegateIsActive(DelegateId)).Returns(DelegateId);
54+
int? centreId = controller.User.GetCentreId();
55+
56+
var delegateEntity = UserTestHelper.GetDefaultDelegateEntity(DelegateId, centreId.Value);
57+
A.CallTo(() => userService.GetDelegateById(DelegateId)).Returns(delegateEntity);
58+
59+
var userEntity = new UserEntity(
60+
UserTestHelper.GetDefaultUserAccount(),
61+
new List<AdminAccount> { UserTestHelper.GetDefaultAdminAccount(centreId.Value) },
62+
new List<DelegateAccount> { UserTestHelper.GetDefaultDelegateAccount(centreId.Value) }
63+
);
64+
65+
A.CallTo(() => userService.GetUserById(delegateEntity.DelegateAccount.UserId)).Returns(userEntity);
66+
67+
// When
68+
var result = controller.Index(DelegateId);
69+
70+
// Then
71+
result.Should().BeViewResult().WithDefaultViewName();
72+
result.Should().BeViewResult().ModelAs<DeactivateDelegateAccountViewModel>().DelegateId.Should().Be(DelegateId);
73+
result.Should().BeViewResult().ModelAs<DeactivateDelegateAccountViewModel>().Name.Should().Be(delegateEntity.UserAccount.FirstName + " " + delegateEntity.UserAccount.LastName);
74+
result.Should().BeViewResult().ModelAs<DeactivateDelegateAccountViewModel>().Email.Should().Be(delegateEntity.UserAccount.PrimaryEmail);
75+
result.Should().BeViewResult().ModelAs<DeactivateDelegateAccountViewModel>().UserId.Should().Be(delegateEntity.UserAccount.Id);
76+
}
77+
78+
[Test]
79+
public void Index_post_returns_error_when_no_option_selected_to_deactivate()
80+
{
81+
// Given
82+
var formData = new DeactivateDelegateAccountViewModel
83+
{
84+
DelegateId = 1,
85+
Name = "Firstname Test",
86+
Roles = new List<string>(),
87+
Email = "[email protected]",
88+
UserId = 2,
89+
Deactivate = null
90+
};
91+
92+
controller.ModelState.AddModelError("key", "Invalid for testing.");
93+
94+
// When
95+
var result = controller.Index(formData);
96+
97+
// Then
98+
result.Should().BeViewResult().ModelAs<DeactivateDelegateAccountViewModel>().DelegateId.Should().Be(DelegateId);
99+
Assert.IsFalse(controller.ModelState.IsValid);
100+
}
101+
102+
103+
[Test]
104+
public void Index_post_returns_view_for_deactivate_delegate_only()
105+
{
106+
// Given
107+
int? centreId = controller.User.GetCentreId();
108+
var formData = new DeactivateDelegateAccountViewModel
109+
{
110+
DelegateId = 1,
111+
Name = "Firstname Test",
112+
Roles = new List<string>(),
113+
Email = "[email protected]",
114+
UserId = 2,
115+
Deactivate = true
116+
};
117+
118+
// When
119+
var result = controller.Index(formData);
120+
121+
// Then
122+
A.CallTo(() => userService.DeactivateDelegateUser(DelegateId)).MustHaveHappenedOnceExactly();
123+
A.CallTo(() => userService.DeactivateAdminAccount(formData.UserId, centreId.Value)).MustNotHaveHappened();
124+
125+
result.Should().BeRedirectToActionResult()
126+
.WithControllerName("ViewDelegate")
127+
.WithActionName("Index")
128+
.WithRouteValue("delegateId", formData.DelegateId);
129+
130+
}
131+
132+
[Test]
133+
public void Index_post_returns_view_for_deactivate_delegate_and_admin()
134+
{
135+
// Given
136+
int? centreId = controller.User.GetCentreId();
137+
var formData = new DeactivateDelegateAccountViewModel
138+
{
139+
DelegateId = 1,
140+
Name = "Firstname Test",
141+
Roles = new List<string>(),
142+
Email = "[email protected]",
143+
UserId = 2,
144+
Deactivate = false
145+
};
146+
147+
// When
148+
var result = controller.Index(formData);
149+
150+
// Then
151+
A.CallTo(() => userService.DeactivateDelegateUser(DelegateId)).MustHaveHappenedOnceExactly();
152+
A.CallTo(() => userService.DeactivateAdminAccount(formData.UserId, centreId.Value)).MustHaveHappenedOnceExactly();
153+
154+
result.Should().BeRedirectToActionResult()
155+
.WithControllerName("ViewDelegate")
156+
.WithActionName("Index")
157+
.WithRouteValue("delegateId", formData.DelegateId);
158+
}
159+
160+
}
161+
}

0 commit comments

Comments
 (0)