From 1b7f3db4f10d3132f358060916c0543199c80ae4 Mon Sep 17 00:00:00 2001 From: Kola samuel Date: Mon, 2 Aug 2021 16:57:45 +0100 Subject: [PATCH] Add User Info to Request --- ...PostingReviewApprenticeshipUpdatesTests.cs | 22 +++++++++++++++---- ...enPostingViewApprenticeshipUpdatesTests.cs | 15 +++++++++++-- .../Controllers/ApprenticeController.cs | 14 +++++++----- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingReviewApprenticeshipUpdatesTests.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingReviewApprenticeshipUpdatesTests.cs index 99279a213..243f81a3b 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingReviewApprenticeshipUpdatesTests.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingReviewApprenticeshipUpdatesTests.cs @@ -1,9 +1,12 @@ -using Microsoft.AspNetCore.Http; +using AutoFixture; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Moq; using NUnit.Framework; using SFA.DAS.CommitmentsV2.Api.Types.Requests; +using SFA.DAS.CommitmentsV2.Types; +using SFA.DAS.EmployerCommitmentsV2.Web.Authentication; using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice.Edit; using System.Threading; using System.Threading.Tasks; @@ -54,20 +57,31 @@ public class WhenPostingReviewApprenticeshipUpdatesTestsFixture : ApprenticeCont private const string ChangesRejectedMessage = "Changes rejected"; public ReviewApprenticeshipUpdatesViewModel ViewModel { get; set; } + + public UserInfo UserInfo; + public Mock AuthenticationService { get; } + public WhenPostingReviewApprenticeshipUpdatesTestsFixture() : base () { ViewModel = new ReviewApprenticeshipUpdatesViewModel { ApprenticeshipId = 1, AccountId = 1 }; _controller.TempData = new TempDataDictionary( Mock.Of(), Mock.Of()); + + var autoFixture = new Fixture(); + UserInfo = autoFixture.Create(); + AuthenticationService = new Mock(); + AuthenticationService.Setup(x => x.UserInfo).Returns(UserInfo); } public async Task ReviewApprenticeshipUpdates() { - return await _controller.ReviewApprenticeshipUpdates(ViewModel); + return await _controller.ReviewApprenticeshipUpdates(AuthenticationService.Object, ViewModel); } internal void VerifyAcceptApprenticeshipUpdatesApiIsCalled() { - _mockCommitmentsApiClient.Verify(x => x.AcceptApprenticeshipUpdates(ViewModel.ApprenticeshipId, It.IsAny(), It.IsAny()), Times.Once()); + _mockCommitmentsApiClient + .Verify(x => x.AcceptApprenticeshipUpdates(ViewModel.ApprenticeshipId, It.Is(o => o.UserInfo != null), It.IsAny()), Times.Once()); + Assert.IsTrue(_controller.TempData.Values.Contains(ChangesApprovedMessage)); Assert.IsTrue(_controller.TempData.ContainsKey(FlashMessageBodyTempDataKey)); Assert.IsTrue(_controller.TempData.ContainsKey(FlashMessageLevelTempDataKey)); @@ -76,7 +90,7 @@ internal void VerifyAcceptApprenticeshipUpdatesApiIsCalled() internal void VerifyRejectApprenticeshipUpdatesApiIsCalled() { - _mockCommitmentsApiClient.Verify(x => x.RejectApprenticeshipUpdates(ViewModel.ApprenticeshipId, It.IsAny(), It.IsAny()), Times.Once()); + _mockCommitmentsApiClient.Verify(x => x.RejectApprenticeshipUpdates(ViewModel.ApprenticeshipId, It.Is(o => o.UserInfo != null), It.IsAny()), Times.Once()); Assert.IsTrue(_controller.TempData.Values.Contains(ChangesRejectedMessage)); Assert.IsTrue(_controller.TempData.ContainsKey(FlashMessageBodyTempDataKey)); Assert.IsTrue(_controller.TempData.ContainsKey(FlashMessageLevelTempDataKey)); diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingViewApprenticeshipUpdatesTests.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingViewApprenticeshipUpdatesTests.cs index 02d64cbb9..2eed396e3 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingViewApprenticeshipUpdatesTests.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingViewApprenticeshipUpdatesTests.cs @@ -4,9 +4,12 @@ using Moq; using NUnit.Framework; using SFA.DAS.CommitmentsV2.Api.Types.Requests; +using SFA.DAS.EmployerCommitmentsV2.Web.Authentication; using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice.Edit; using System.Threading; using System.Threading.Tasks; +using AutoFixture; +using SFA.DAS.CommitmentsV2.Types; namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests { @@ -48,21 +51,29 @@ public async Task VerifyIsRedirectedToApprenticeshipDetails() public class WhenPostingViewApprenticeshipUpdatesTestsFixture : ApprenticeControllerTestFixtureBase { + public Mock AuthenticationService { get; } + public UserInfo UserInfo; + public ViewApprenticeshipUpdatesViewModel ViewModel { get; set; } public WhenPostingViewApprenticeshipUpdatesTestsFixture() : base () { ViewModel = new ViewApprenticeshipUpdatesViewModel { ApprenticeshipId = 1, AccountId = 1 }; _controller.TempData = new TempDataDictionary( Mock.Of(), Mock.Of()); + + var autoFixture = new Fixture(); + UserInfo = autoFixture.Create(); + AuthenticationService = new Mock(); + AuthenticationService.Setup(x => x.UserInfo).Returns(UserInfo); } public async Task ViewApprenticeshipUpdates() { - return await _controller.ViewApprenticeshipUpdates(ViewModel); + return await _controller.ViewApprenticeshipUpdates(AuthenticationService.Object, ViewModel); } internal void Verify_UndoApprenticeshipUpdatesApi_Is_Called() { - _mockCommitmentsApiClient.Verify(x => x.UndoApprenticeshipUpdates(ViewModel.ApprenticeshipId, It.IsAny(), It.IsAny()), Times.Once()); + _mockCommitmentsApiClient.Verify(x => x.UndoApprenticeshipUpdates(ViewModel.ApprenticeshipId, It.Is(o => o.UserInfo != null), It.IsAny()), Times.Once()); } internal void Verify_UndoApprenticeshipUpdatesApi_IsNot_Called() diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ApprenticeController.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ApprenticeController.cs index 7d99c07f7..0b48e34d6 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ApprenticeController.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ApprenticeController.cs @@ -17,6 +17,7 @@ using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice.Edit; using SFA.DAS.EmployerCommitmentsV2.Web.RouteValues; using SFA.DAS.EmployerUrlHelper; +using SFA.DAS.EmployerCommitmentsV2.Web.Authentication; using EditEndDateRequest = SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice.EditEndDateRequest; namespace SFA.DAS.EmployerCommitmentsV2.Web.Controllers @@ -655,14 +656,15 @@ public async Task ReviewApprenticeshipUpdates(ReviewApprenticeshi [HttpPost] [Route("{apprenticeshipHashedId}/changes/review")] - public async Task ReviewApprenticeshipUpdates(ReviewApprenticeshipUpdatesViewModel viewModel) + public async Task ReviewApprenticeshipUpdates([FromServices] IAuthenticationService authenticationService, ReviewApprenticeshipUpdatesViewModel viewModel) { if (viewModel.ApproveChanges.Value) { var request = new AcceptApprenticeshipUpdatesRequest { ApprenticeshipId = viewModel.ApprenticeshipId, - AccountId = viewModel.AccountId + AccountId = viewModel.AccountId, + UserInfo = authenticationService.UserInfo }; await _commitmentsApiClient.AcceptApprenticeshipUpdates(viewModel.ApprenticeshipId, request); @@ -673,7 +675,8 @@ public async Task ReviewApprenticeshipUpdates(ReviewApprenticeshi var request = new RejectApprenticeshipUpdatesRequest { ApprenticeshipId = viewModel.ApprenticeshipId, - AccountId = viewModel.AccountId + AccountId = viewModel.AccountId, + UserInfo = authenticationService.UserInfo }; await _commitmentsApiClient.RejectApprenticeshipUpdates(viewModel.ApprenticeshipId, request); @@ -695,14 +698,15 @@ public async Task ViewApprenticeshipUpdates(ViewApprenticeshipUpd [HttpPost] [Route("{apprenticeshipHashedId}/changes/view")] - public async Task ViewApprenticeshipUpdates(ViewApprenticeshipUpdatesViewModel viewModel) + public async Task ViewApprenticeshipUpdates([FromServices] IAuthenticationService authenticationService, ViewApprenticeshipUpdatesViewModel viewModel) { if (viewModel.UndoChanges.Value) { var request = new UndoApprenticeshipUpdatesRequest { ApprenticeshipId = viewModel.ApprenticeshipId, - AccountId = viewModel.AccountId + AccountId = viewModel.AccountId, + UserInfo = authenticationService.UserInfo }; await _commitmentsApiClient.UndoApprenticeshipUpdates(viewModel.ApprenticeshipId, request);