Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions DigitalLearningSolutions.Data/DataServices/CentresDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,16 @@ FROM Centres
return ipPrefixes ?? new string[0];
}

public (bool autoRegistered, string? autoRegisterManagerEmail) GetCentreAutoRegisterValues(int centreId)
{
return connection.QueryFirstOrDefault<(bool, string?)>(
@"SELECT AutoRegistered, AutoRegisterManagerEmail
FROM Centres
WHERE CentreID = @centreId",
new { centreId }
);
}

public (bool autoRegistered, string? autoRegisterManagerEmail) GetCentreAutoRegisterValues(int centreId)
{
return connection.QueryFirstOrDefault<(bool, string?)>(
@"SELECT AutoRegistered, AutoRegisterManagerEmail
FROM Centres
WHERE CentreID = @centreId",
new { centreId }
);
}
public IEnumerable<CentreRanking> GetCentreRanks(
DateTime dateSince,
int? regionId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace DigitalLearningSolutions.Web.Tests.Controllers.Register
{
using System.Collections.Generic;
using DigitalLearningSolutions.Data.DataServices;
using DigitalLearningSolutions.Data.Models.User;
using DigitalLearningSolutions.Data.Services;
Expand Down Expand Up @@ -29,6 +30,7 @@ public void Setup()
centresDataService = A.Fake<ICentresDataService>();
cryptoService = A.Fake<ICryptoService>();
jobGroupsDataService = A.Fake<IJobGroupsDataService>();
registrationService = A.Fake<IRegistrationService>();
userDataService = A.Fake<IUserDataService>();
controller = new RegisterAdminController(
centresDataService,
Expand All @@ -42,7 +44,7 @@ public void Setup()
}

[Test]
public void IndexGet_with_no_centreId_param_shows_error()
public void IndexGet_with_no_centreId_param_shows_notfound_error()
{
// When
var result = controller.Index();
Expand All @@ -52,7 +54,7 @@ public void IndexGet_with_no_centreId_param_shows_error()
}

[Test]
public void IndexGet_with_invalid_centreId_param_shows_error()
public void IndexGet_with_invalid_centreId_param_shows_notfound_error()
{
// Given
const int centreId = 7;
Expand All @@ -67,53 +69,81 @@ public void IndexGet_with_invalid_centreId_param_shows_error()
}

[Test]
public void IndexGet_with_centre_autoregistered_true_shows_error()
public void IndexGet_with_centre_autoregistered_true_shows_notfound_error()
{
// Given
const int centreId = 7;
A.CallTo(() => centresDataService.GetCentreName(centreId)).Returns("My centre");
A.CallTo(() => centresDataService.GetCentreAutoRegisterValues(centreId)).Returns((true, "email@email"));
A.CallTo(() => userDataService.GetAdminUsersByCentreId(centreId)).Returns(new List<AdminUser>());

// When
var result = controller.Index(centreId);

// Then
A.CallTo(() => centresDataService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
A.CallTo(() => centresDataService.GetCentreAutoRegisterValues(centreId)).MustHaveHappened(1, Times.Exactly);
A.CallTo(() => userDataService.GetAdminUsersByCentreId(centreId)).MustHaveHappened(1, Times.Exactly);
result.Should().BeNotFoundResult();
}

[Test]
public void IndexGet_with_centre_autoregisteremail_null_shows_error()
public void IndexGet_with_centre_autoregisteremail_null_shows_notfound_error()
{
// Given
const int centreId = 7;
A.CallTo(() => centresDataService.GetCentreName(centreId)).Returns("Some centre");
A.CallTo(() => centresDataService.GetCentreAutoRegisterValues(centreId)).Returns((false, null));
A.CallTo(() => userDataService.GetAdminUsersByCentreId(centreId)).Returns(new List<AdminUser>());

// When
var result = controller.Index(centreId);

// Then
A.CallTo(() => centresDataService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
A.CallTo(() => centresDataService.GetCentreAutoRegisterValues(centreId)).MustHaveHappened(1, Times.Exactly);
A.CallTo(() => userDataService.GetAdminUsersByCentreId(centreId)).MustHaveHappened(1, Times.Exactly);
result.Should().BeNotFoundResult();
}

[Test]
public void IndexGet_with_centre_autoregisteremail_empty_shows_error()
public void IndexGet_with_centre_autoregisteremail_empty_shows_notfound_error()
{
// Given
const int centreId = 7;
A.CallTo(() => centresDataService.GetCentreName(centreId)).Returns("Some centre");
A.CallTo(() => centresDataService.GetCentreAutoRegisterValues(centreId)).Returns((false, string.Empty));
A.CallTo(() => userDataService.GetAdminUsersByCentreId(centreId)).Returns(new List<AdminUser>());

// When
var result = controller.Index(centreId);

// Then
A.CallTo(() => centresDataService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
A.CallTo(() => centresDataService.GetCentreAutoRegisterValues(centreId)).MustHaveHappened(1, Times.Exactly);
A.CallTo(() => userDataService.GetAdminUsersByCentreId(centreId)).MustHaveHappened(1, Times.Exactly);
result.Should().BeNotFoundResult();
}

[Test]
public void IndexGet_with_centre_with_active_centre_manager_shows_notfound_error()
{
// Given
const int centreId = 7;
A.CallTo(() => centresDataService.GetCentreName(centreId)).Returns("Some centre");
A.CallTo(() => centresDataService.GetCentreAutoRegisterValues(centreId)).Returns((false, "email@email"));

var centreManagerAdmin = new AdminUser { CentreId = centreId, IsCentreManager = true };
A.CallTo(() => userDataService.GetAdminUsersByCentreId(centreId))
.Returns(new List<AdminUser> { centreManagerAdmin });

// When
var result = controller.Index(centreId);

// Then
A.CallTo(() => centresDataService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
A.CallTo(() => centresDataService.GetCentreAutoRegisterValues(centreId)).MustHaveHappened(1, Times.Exactly);
A.CallTo(() => userDataService.GetAdminUsersByCentreId(centreId)).MustHaveHappened(1, Times.Exactly);
result.Should().BeNotFoundResult();
}

Expand All @@ -124,13 +154,15 @@ public void IndexGet_with_allowed_admin_registration_sets_data_correctly()
const int centreId = 7;
A.CallTo(() => centresDataService.GetCentreName(centreId)).Returns("Some centre");
A.CallTo(() => centresDataService.GetCentreAutoRegisterValues(centreId)).Returns((false, "email@email"));
A.CallTo(() => userDataService.GetAdminUsersByCentreId(centreId)).Returns(new List<AdminUser>());

// When
var result = controller.Index(centreId);

// Then
A.CallTo(() => centresDataService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
A.CallTo(() => centresDataService.GetCentreAutoRegisterValues(centreId)).MustHaveHappened(1, Times.Exactly);
A.CallTo(() => userDataService.GetAdminUsersByCentreId(centreId)).MustHaveHappened(1, Times.Exactly);
var data = controller.TempData.Peek<RegistrationData>()!;
data.Centre.Should().Be(centreId);
result.Should().BeRedirectToActionResult().WithActionName("PersonalInformation");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace DigitalLearningSolutions.Web.Controllers
{
using System;
using System.Linq;
using DigitalLearningSolutions.Data.DataServices;
using DigitalLearningSolutions.Data.Services;
using DigitalLearningSolutions.Web.Extensions;
Expand Down Expand Up @@ -168,7 +169,7 @@ public IActionResult Summary(SummaryViewModel model)
{
return new StatusCodeResult(500);
}

var registrationModel = RegistrationMappingHelper.MapToRegistrationModel(data);
registrationService.RegisterCentreManager(registrationModel);

Expand All @@ -188,8 +189,10 @@ private bool IsRegisterAdminAllowed(int centreId)
return false;
}

var adminUsers = userDataService.GetAdminUsersByCentreId(centreId);
var hasCentreManagerAdmin = adminUsers.Any(user => user.IsCentreManager);
var (autoRegistered, autoRegisterManagerEmail) = centresDataService.GetCentreAutoRegisterValues(centreId);
return !autoRegistered && !string.IsNullOrWhiteSpace(autoRegisterManagerEmail);
return !hasCentreManagerAdmin && !autoRegistered && !string.IsNullOrWhiteSpace(autoRegisterManagerEmail);
}

private void SetAdminRegistrationData(int centreId)
Expand Down