Skip to content

Commit c0546aa

Browse files
committed
TD-4793-unit tests added for deactivate delegate controller
1 parent a8682c5 commit c0546aa

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
var formData = new DeactivateDelegateAccountViewModel
108+
{
109+
DelegateId = 1,
110+
Name = "Firstname Test",
111+
Roles = new List<string>(),
112+
Email = "[email protected]",
113+
UserId = 2,
114+
Deactivate = true
115+
};
116+
117+
// When
118+
var result = controller.Index(formData);
119+
120+
// Then
121+
result.Should().BeRedirectToActionResult()
122+
.WithControllerName("ViewDelegate")
123+
.WithActionName("Index")
124+
.WithRouteValue("delegateId", formData.DelegateId);
125+
}
126+
127+
[Test]
128+
public void Index_post_returns_view_for_deactivate_delegate_and_admin()
129+
{
130+
// Given
131+
var formData = new DeactivateDelegateAccountViewModel
132+
{
133+
DelegateId = 1,
134+
Name = "Firstname Test",
135+
Roles = new List<string>(),
136+
Email = "[email protected]",
137+
UserId = 2,
138+
Deactivate = false
139+
};
140+
141+
// When
142+
var result = controller.Index(formData);
143+
144+
// Then
145+
result.Should().BeRedirectToActionResult()
146+
.WithControllerName("ViewDelegate")
147+
.WithActionName("Index")
148+
.WithRouteValue("delegateId", formData.DelegateId);
149+
}
150+
151+
}
152+
}

0 commit comments

Comments
 (0)