Skip to content

Commit 8062c30

Browse files
authored
Merge pull request #2677 from TechnologyEnhancedLearning/Develop/feature/TD-3902-ClaimAccountController-refactor
TD-3902- ClaimAccountController - refactor
2 parents c75419f + 0b46af1 commit 8062c30

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

DigitalLearningSolutions.Web.Tests/Controllers/Register/ClaimAccountControllerTests.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
{
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
5-
using DigitalLearningSolutions.Data.DataServices.UserDataService;
6-
using DigitalLearningSolutions.Data.Models.User;
5+
using DigitalLearningSolutions.Data.Models.User;
76
using DigitalLearningSolutions.Web.Controllers.Register;
87
using DigitalLearningSolutions.Web.Extensions;
98
using DigitalLearningSolutions.Web.Services;
@@ -28,7 +27,6 @@ public class ClaimAccountControllerTests
2827
private const string DefaultCandidateNumber = "CN777";
2928
private const string Password = "password";
3029
private IUserService userService = null!;
31-
private IUserDataService userDataService = null!;
3230
private IClaimAccountService claimAccountService = null!;
3331
private IConfiguration config = null!;
3432
private IEmailVerificationService emailVerificationService = null!;
@@ -39,10 +37,9 @@ public class ClaimAccountControllerTests
3937
public void Setup()
4038
{
4139
userService = A.Fake<IUserService>();
42-
userDataService = A.Fake<IUserDataService>();
4340
claimAccountService = A.Fake<IClaimAccountService>();
44-
config= A.Fake<IConfiguration>();
45-
emailVerificationService= A.Fake<IEmailVerificationService>();
41+
config = A.Fake<IConfiguration>();
42+
emailVerificationService = A.Fake<IEmailVerificationService>();
4643
controller = GetClaimAccountController();
4744
controllerWithLoggedInUser = GetClaimAccountController().WithMockUser(
4845
true,
@@ -190,7 +187,7 @@ public async Task
190187
// Given
191188
var model = GivenClaimAccountViewModel(wasPasswordSetByAdmin: true);
192189

193-
A.CallTo(() => userDataService.PrimaryEmailIsInUse(model.Email)).Returns(false);
190+
A.CallTo(() => userService.PrimaryEmailIsInUse(model.Email)).Returns(false);
194191

195192
// When
196193
var result = await controller.CompleteRegistrationWithoutPassword(
@@ -228,7 +225,7 @@ public async Task CompleteRegistrationWithoutPassword_with_email_in_use_returns_
228225
// Given
229226
var model = GivenClaimAccountViewModel(wasPasswordSetByAdmin: true);
230227

231-
A.CallTo(() => userDataService.PrimaryEmailIsInUse(model.Email)).Returns(true);
228+
A.CallTo(() => userService.PrimaryEmailIsInUse(model.Email)).Returns(true);
232229

233230
// When
234231
var result = await controller.CompleteRegistrationWithoutPassword(
@@ -337,7 +334,7 @@ public async Task
337334
var model = GivenClaimAccountViewModel(wasPasswordSetByAdmin: false);
338335
var passwordFormData = new ConfirmPasswordViewModel { Password = Password };
339336

340-
A.CallTo(() => userDataService.PrimaryEmailIsInUse(model.Email)).Returns(false);
337+
A.CallTo(() => userService.PrimaryEmailIsInUse(model.Email)).Returns(false);
341338

342339
// When
343340
var result = await controller.CompleteRegistration(
@@ -376,7 +373,7 @@ public async Task CompleteRegistrationPost_with_email_in_use_returns_NotFound()
376373
// Given
377374
var model = GivenClaimAccountViewModel(wasPasswordSetByAdmin: false);
378375

379-
A.CallTo(() => userDataService.PrimaryEmailIsInUse(model.Email)).Returns(true);
376+
A.CallTo(() => userService.PrimaryEmailIsInUse(model.Email)).Returns(true);
380377

381378
// When
382379
var result = await controller.CompleteRegistration(
@@ -501,7 +498,7 @@ public async Task CompleteRegistrationPost_with_invalid_model_display_with_valid
501498

502499
controller.ModelState.AddModelError("ConfirmPassword", "Required");
503500

504-
A.CallTo(() => userDataService.PrimaryEmailIsInUse(model.Email)).Returns(false);
501+
A.CallTo(() => userService.PrimaryEmailIsInUse(model.Email)).Returns(false);
505502

506503
// When
507504
var result = await controller.CompleteRegistration(
@@ -814,7 +811,7 @@ private ClaimAccountViewModel GivenClaimAccountViewModel(
814811
};
815812

816813
A.CallTo(
817-
() => userDataService.GetUserIdAndCentreForCentreEmailRegistrationConfirmationHashPair(
814+
() => userService.GetUserIdAndCentreForCentreEmailRegistrationConfirmationHashPair(
818815
email,
819816
registrationConfirmationHash
820817
)
@@ -863,7 +860,7 @@ private void GivenLoggedInUserWithAdminAccountAtCentre(int userId, int centreIdF
863860
private void GivenEmailAndCodeDoNotMatchAUserToBeClaimed(string email, string code)
864861
{
865862
A.CallTo(
866-
() => userDataService.GetUserIdAndCentreForCentreEmailRegistrationConfirmationHashPair(
863+
() => userService.GetUserIdAndCentreForCentreEmailRegistrationConfirmationHashPair(
867864
email,
868865
code
869866
)
@@ -891,7 +888,7 @@ private void ACallToLinkAccountMustNotHaveHappened()
891888

892889
private ClaimAccountController GetClaimAccountController()
893890
{
894-
return new ClaimAccountController(userService, userDataService, claimAccountService,config,emailVerificationService)
891+
return new ClaimAccountController(userService, claimAccountService, config, emailVerificationService)
895892
.WithDefaultContext()
896893
.WithMockTempData();
897894
}

DigitalLearningSolutions.Web/Controllers/Register/ClaimAccountController.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace DigitalLearningSolutions.Web.Controllers.Register
22
{
3-
using DigitalLearningSolutions.Data.DataServices.UserDataService;
43
using DigitalLearningSolutions.Data.Utilities;
54
using DigitalLearningSolutions.Web.Attributes;
65
using DigitalLearningSolutions.Web.Extensions;
@@ -21,21 +20,18 @@
2120
public class ClaimAccountController : Controller
2221
{
2322
private readonly IUserService userService;
24-
private readonly IUserDataService userDataService;
2523
private readonly IClaimAccountService claimAccountService;
2624
private readonly IConfiguration config;
2725
private readonly IEmailVerificationService emailVerificationService;
2826

2927
public ClaimAccountController(
3028
IUserService userService,
31-
IUserDataService userDataService,
3229
IClaimAccountService claimAccountService,
3330
IConfiguration config,
3431
IEmailVerificationService emailVerificationService
3532
)
3633
{
3734
this.userService = userService;
38-
this.userDataService = userDataService;
3935
this.claimAccountService = claimAccountService;
4036
this.config = config;
4137
this.emailVerificationService = emailVerificationService;
@@ -135,7 +131,7 @@ private async Task<IActionResult> CompleteRegistrationPost(
135131
string? password = null
136132
)
137133
{
138-
if (userDataService.PrimaryEmailIsInUse(model.Email))
134+
if (userService.PrimaryEmailIsInUse(model.Email))
139135
{
140136
return NotFound();
141137
}
@@ -164,7 +160,7 @@ await claimAccountService.ConvertTemporaryUserToConfirmedUser(
164160

165161
var userEntity = userService.GetUserById(model.UserId);
166162
IClockUtility clockUtility = new ClockUtility();
167-
userDataService.SetPrimaryEmailVerified(userEntity!.UserAccount.Id, model.Email, clockUtility.UtcNow);
163+
userService.SetPrimaryEmailVerified(userEntity!.UserAccount.Id, model.Email, clockUtility.UtcNow);
168164

169165
return RedirectToAction("Confirmation");
170166
}
@@ -259,7 +255,7 @@ public IActionResult AdminAccountAlreadyExists(string email, string centreName)
259255
}
260256

261257
var (userId, centreId, centreName) =
262-
userDataService.GetUserIdAndCentreForCentreEmailRegistrationConfirmationHashPair(email, code);
258+
userService.GetUserIdAndCentreForCentreEmailRegistrationConfirmationHashPair(email, code);
263259

264260
if (userId == null)
265261
{

DigitalLearningSolutions.Web/Services/UserService.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ int userId
122122
);
123123
bool PrimaryEmailIsInUseByOtherUser(string email, int userId);
124124
IEnumerable<UserCentreDetails> GetCentreDetailsForUser(int userId);
125+
bool PrimaryEmailIsInUse(string email);
126+
void SetPrimaryEmailVerified(int userId, string email, DateTime verifiedDateTime);
127+
(int? userId, int? centreId, string? centreName) GetUserIdAndCentreForCentreEmailRegistrationConfirmationHashPair(
128+
string centreSpecificEmail,
129+
string registrationConfirmationHash
130+
);
125131

126132
}
127133

@@ -687,7 +693,7 @@ public void ReactivateAdmin(int adminId)
687693

688694
public bool CentreSpecificEmailIsInUseAtCentreByOtherUser(string email, int centreId, int userId)
689695
{
690-
return userDataService.CentreSpecificEmailIsInUseAtCentreByOtherUser(email, centreId, userId );
696+
return userDataService.CentreSpecificEmailIsInUseAtCentreByOtherUser(email, centreId, userId);
691697
}
692698
public bool PrimaryEmailIsInUseByOtherUser(string email, int userId)
693699
{
@@ -698,5 +704,20 @@ public IEnumerable<UserCentreDetails> GetCentreDetailsForUser(int userId)
698704
{
699705
return userDataService.GetCentreDetailsForUser(userId);
700706
}
707+
708+
public bool PrimaryEmailIsInUse(string email)
709+
{
710+
return userDataService.PrimaryEmailIsInUse(email);
711+
}
712+
713+
public void SetPrimaryEmailVerified(int userId, string email, DateTime verifiedDateTime)
714+
{
715+
userDataService.SetPrimaryEmailVerified(userId, email, verifiedDateTime);
716+
}
717+
718+
public (int? userId, int? centreId, string? centreName) GetUserIdAndCentreForCentreEmailRegistrationConfirmationHashPair(string centreSpecificEmail, string registrationConfirmationHash)
719+
{
720+
return userDataService.GetUserIdAndCentreForCentreEmailRegistrationConfirmationHashPair(centreSpecificEmail, registrationConfirmationHash);
721+
}
701722
}
702723
}

0 commit comments

Comments
 (0)