Skip to content

Commit d7d4b94

Browse files
committed
Added a condition for duplicate centre in edit centre details and added a corresponding web test.
1 parent 9cc8974 commit d7d4b94

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

DigitalLearningSolutions.Web.Tests/Controllers/SuperAdmin/CentresControllerTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,47 @@ public void EditCentreDetails_updates_centre_and_redirects_with_successful_save(
9191
.MustHaveHappenedOnceExactly();
9292
}
9393

94+
[Test]
95+
public void EditCentreDetails_VerifiesCentresList_Results_DuplicateCentre_error()
96+
{
97+
// Given
98+
IEnumerable<(int, string)> centresList = new List<(int, string)> { (374, "##HEE Demo Centre##") };
99+
A.CallTo(() => centresDataService.GetAllCentres(false)).Returns(centresList);
100+
var model = new EditCentreDetailsSuperAdminViewModel
101+
{
102+
CentreId = 374,
103+
CentreName = "##HEE Demo Centre##",
104+
CentreTypeId = 1,
105+
CentreType = "NHS Organisation",
106+
RegionName = "National",
107+
CentreEmail = "[email protected]",
108+
IpPrefix = "12.33.4",
109+
ShowOnMap = true,
110+
RegionId = 13
111+
};
112+
113+
// When
114+
var result = controller.EditCentreDetails(model);
115+
116+
// Then
117+
result.Should().BeViewResult();
118+
controller.ModelState.IsValid.Should().BeFalse();
119+
var centreNameErrors = controller.ModelState["CentreName"].Errors;
120+
centreNameErrors.Should().NotBeEmpty();
121+
centreNameErrors.Should().Contain(error => error.ErrorMessage ==
122+
"The centre name you have entered already exists, please enter a different centre name");
123+
124+
A.CallTo(() => centresDataService.UpdateCentreDetailsForSuperAdmin(
125+
model.CentreId,
126+
model.CentreName,
127+
model.CentreTypeId,
128+
model.RegionId,
129+
model.CentreEmail,
130+
model.IpPrefix,
131+
model.ShowOnMap))
132+
.MustNotHaveHappened();
133+
}
134+
94135
[Test]
95136
public void AddCentre_adds_centre_and_redirects_with_successful_save()
96137
{

DigitalLearningSolutions.Web/Controllers/SuperAdmin/Centres/CentresController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ public IActionResult EditCentreDetails(int centreId = 0)
227227
[Route("SuperAdmin/Centres/{centreId=0:int}/EditCentreDetails")]
228228
public IActionResult EditCentreDetails(EditCentreDetailsSuperAdminViewModel model)
229229
{
230+
bool isCentreNamePresent = centresDataService.GetAllCentres().Any(center => center.Item2 == model.CentreName);
231+
if (isCentreNamePresent)
232+
{
233+
ModelState.AddModelError("CentreName", CommonValidationErrorMessages.CentreNameAlreadyExist);
234+
}
235+
230236
if (!ModelState.IsValid)
231237
{
232238
var regions = regionDataService.GetRegionsAlphabetical().ToList();

0 commit comments

Comments
 (0)