Skip to content

Commit

Permalink
Merge pull request #70 from SkillsFundingAgency/CV-456-Read-Only-View…
Browse files Browse the repository at this point in the history
…-Apprentice

Cv 456 read only view apprentice
  • Loading branch information
subhamano authored Nov 11, 2019
2 parents e386ede + d18122c commit 7ccaa84
Show file tree
Hide file tree
Showing 20 changed files with 566 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Moq;
Expand All @@ -19,51 +20,60 @@ namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.DraftApprentic
{
[TestFixture]
[Parallelizable(ParallelScope.Children)]
public class EditDraftApprenticeshipTests
public class DetailsTests
{
[Test]
public async Task GetEditDraftApprenticeship_ValidModel_ShouldReturnViewModel()
public async Task GetDetails_ShouldReturnViewModel()
{
var fixtures = new EditDraftApprenticeshipTestsFixture().WithDraftApprenticeship().WithCohort();
var fixtures = new DetailsTestFixture().WithDraftApprenticeship().WithCohort();

var result = await fixtures.Sut.EditDraftApprenticeship(fixtures.EditDraftApprenticeshipRequest);

var model = result.VerifyReturnsViewModel().WithModel<EditDraftApprenticeshipViewModel>();
Assert.AreSame(model, fixtures.EditDraftApprenticeshipViewModel);
var result = await fixtures.Sut.Details(fixtures.DetailsRequest);
var viewResult = result.VerifyReturnsViewModel();
Assert.AreEqual(fixtures.ViewModel.Object, viewResult.Model);
}

[Test]
public async Task GetEditDraftApprenticeship_ValidModelButCohortIsWithProvider_ShouldRedirectUserToViewDetails()
public async Task GetDetails_Cohort_With_Employer_ShouldReturnEditPage()
{
var fixtures = new EditDraftApprenticeshipTestsFixture().WithDraftApprenticeship();
fixtures.WithOtherParty();
fixtures.WithCohort(new CohortDetails{CohortId = fixtures.CohortId}).WithViewApprenticeLink("XYZ");
var fixtures = new DetailsTestFixture()
.WithDraftApprenticeship()
.WithCohortWithEmployer();

var result = await fixtures.Sut.EditDraftApprenticeship(fixtures.EditDraftApprenticeshipRequest);
var result = await fixtures.Sut.Details(fixtures.DetailsRequest);
var viewResult = result.VerifyReturnsViewModel();
Assert.AreEqual("Edit", viewResult.ViewName);
}

var redirect = result.VerifyReturnsRedirect();
Assert.AreEqual("XYZ", redirect.Url);
fixtures.LinkGeneratorMock.Verify(x=>x.CommitmentsLink($"accounts/{fixtures.AccountHashedId}/apprentices/{fixtures.CohortReference}/apprenticeships/{fixtures.DraftApprenticeshipHashedId}/view"));
[Test]
public async Task GetDetails_Cohort_With_OtherParty_ShouldReturnViewPage()
{
var fixtures = new DetailsTestFixture()
.WithDraftApprenticeship()
.WithCohortWithOtherParty();

var result = await fixtures.Sut.Details(fixtures.DetailsRequest);
var viewResult = result.VerifyReturnsViewModel();
Assert.AreEqual("View", viewResult.ViewName);
}

[Test]
public async Task PostEditDraftApprenticeship_WithValidModel_ShouldSaveDraftApprenticeshipAndRedirectToCohortPage()
public async Task PostDetails_WithValidModel_ShouldSaveDraftApprenticeshipAndRedirectToCohortPage()
{
var fixtures = new EditDraftApprenticeshipTestsFixture()
var fixtures = new DetailsTestFixture()
.WithCohort()
.WithCohortLink("cohortPage");

var result = await fixtures.Sut.EditDraftApprenticeship(new EditDraftApprenticeshipViewModel{ AccountHashedId = fixtures.AccountHashedId, CohortId = fixtures.CohortId, CohortReference = fixtures.CohortReference, DraftApprenticeshipId = fixtures.DraftApprenticeshipId});
var result = await fixtures.Sut.EditDraftApprenticeship(new EditDraftApprenticeshipViewModel { AccountHashedId = fixtures.AccountHashedId, CohortId = fixtures.CohortId, CohortReference = fixtures.CohortReference, DraftApprenticeshipId = fixtures.DraftApprenticeshipId });

fixtures.CommitmentsServiceMock.Verify(cs => cs.UpdateDraftApprenticeship(fixtures.CohortId, fixtures.DraftApprenticeshipId, It.IsAny<UpdateDraftApprenticeshipRequest>()), Times.Once);
var redirect = result.VerifyReturnsRedirect();
Assert.AreEqual("cohortPage", redirect.Url);
}

[Test]
public async Task PostEditDraftApprenticeship_WithValidModel__WithEnhancedApproval_ShouldSaveDraftApprenticeshipAndRedirectToCohortDetailsV2Page()
public async Task PostDetails_WithValidModel__WithEnhancedApproval_ShouldSaveDraftApprenticeshipAndRedirectToCohortDetailsV2Page()
{
var fixtures = new EditDraftApprenticeshipTestsFixture()
var fixtures = new DetailsTestFixture()
.WithEnhancedApproval()
.WithCohort();

Expand All @@ -75,37 +85,42 @@ public async Task PostEditDraftApprenticeship_WithValidModel__WithEnhancedApprov
}
}

public class EditDraftApprenticeshipTestsFixture
public class DetailsTestFixture
{
public EditDraftApprenticeshipTestsFixture()
public DetailsTestFixture()
{
CommitmentsServiceMock = new Mock<ICommitmentsService>();
LinkGeneratorMock = new Mock<ILinkGenerator>();
EditDraftApprenticeshipViewModel = new EditDraftApprenticeshipViewModel
{
CohortId = CohortId
};
ViewModel = new Mock<IDraftApprenticeshipViewModel>();

EditDraftApprenticeshipRequest = new EditDraftApprenticeshipRequest
DetailsRequest = new DetailsRequest
{
AccountHashedId = AccountHashedId, CohortId = CohortId, CohortReference = CohortReference,
DraftApprenticeshipId = DraftApprenticeshipId, DraftApprenticeshipHashedId = DraftApprenticeshipHashedId
AccountHashedId = AccountHashedId,
CohortId = CohortId,
CohortReference = CohortReference,
DraftApprenticeshipId = DraftApprenticeshipId,
DraftApprenticeshipHashedId = DraftApprenticeshipHashedId
};
CohortDetails = new CohortDetails
{
CohortId = CohortId, HashedCohortId = CohortReference, IsFundedByTransfer = false,
ProviderName = "ProviderName", WithParty = Party.Employer
CohortId = CohortId,
HashedCohortId = CohortReference,
IsFundedByTransfer = false,
ProviderName = "ProviderName",
WithParty = Party.Employer
};
EditDraftApprenticeshipDetails = new EditDraftApprenticeshipDetails
{
CohortId = CohortId, CohortReference = CohortReference, DraftApprenticeshipId = DraftApprenticeshipId,
CohortId = CohortId,
CohortReference = CohortReference,
DraftApprenticeshipId = DraftApprenticeshipId,
DraftApprenticeshipHashedId = DraftApprenticeshipHashedId
};
ApiErrors = new List<ErrorDetail>{new ErrorDetail("Field1", "Message1")};
ApiErrors = new List<ErrorDetail> { new ErrorDetail("Field1", "Message1") };

ModelMapperMock = new Mock<IModelMapper>();
ModelMapperMock.Setup(x => x.Map<EditDraftApprenticeshipViewModel>(It.IsAny<EditDraftApprenticeshipRequest>()))
.ReturnsAsync(EditDraftApprenticeshipViewModel);
ModelMapperMock.Setup(x => x.Map<IDraftApprenticeshipViewModel>(It.IsAny<DetailsRequest>()))
.ReturnsAsync(ViewModel.Object);

AuthorizationServiceMock = new Mock<IAuthorizationService>();
AuthorizationServiceMock.Setup(x => x.IsAuthorized(EmployerFeature.EnhancedApproval)).Returns(false);
Expand All @@ -130,10 +145,10 @@ public EditDraftApprenticeshipTestsFixture()
public EditDraftApprenticeshipDetails EditDraftApprenticeshipDetails { get; private set; }
public DraftApprenticeshipController Sut { get; private set; }
public List<ErrorDetail> ApiErrors { get; private set; }
public EditDraftApprenticeshipRequest EditDraftApprenticeshipRequest;
public EditDraftApprenticeshipViewModel EditDraftApprenticeshipViewModel;
public DetailsRequest DetailsRequest;
public Mock<IDraftApprenticeshipViewModel> ViewModel;

public EditDraftApprenticeshipTestsFixture WithCohortLink(string url)
public DetailsTestFixture WithCohortLink(string url)
{
LinkGeneratorMock
.Setup(lg => lg.CommitmentsLink(It.IsAny<string>()))
Expand All @@ -142,7 +157,21 @@ public EditDraftApprenticeshipTestsFixture WithCohortLink(string url)
return this;
}

public EditDraftApprenticeshipTestsFixture WithViewApprenticeLink(string url)
public DetailsTestFixture WithCohortWithOtherParty()
{
ModelMapperMock.Setup(x => x.Map<IDraftApprenticeshipViewModel>(It.IsAny<DetailsRequest>()))
.ReturnsAsync(new ViewDraftApprenticeshipViewModel());
return this;
}

public DetailsTestFixture WithCohortWithEmployer()
{
ModelMapperMock.Setup(x => x.Map<IDraftApprenticeshipViewModel>(It.IsAny<DetailsRequest>()))
.ReturnsAsync(new EditDraftApprenticeshipViewModel());
return this;
}

public DetailsTestFixture WithViewApprenticeLink(string url)
{
LinkGeneratorMock
.Setup(lg => lg.CommitmentsLink(It.IsAny<string>()))
Expand All @@ -151,7 +180,7 @@ public EditDraftApprenticeshipTestsFixture WithViewApprenticeLink(string url)
return this;
}

public EditDraftApprenticeshipTestsFixture WithDraftApprenticeship(EditDraftApprenticeshipDetails details = null)
public DetailsTestFixture WithDraftApprenticeship(EditDraftApprenticeshipDetails details = null)
{
var returnValue = details ?? EditDraftApprenticeshipDetails;

Expand All @@ -162,7 +191,7 @@ public EditDraftApprenticeshipTestsFixture WithDraftApprenticeship(EditDraftAppr
return this;
}

public EditDraftApprenticeshipTestsFixture WithUpdateDraftApprenticeshipDomainError()
public DetailsTestFixture WithUpdateDraftApprenticeshipDomainError()
{
CommitmentsServiceMock
.Setup(cs => cs.UpdateDraftApprenticeship(It.IsAny<long>(), It.IsAny<long>(), It.IsAny<UpdateDraftApprenticeshipRequest>()))
Expand All @@ -171,7 +200,7 @@ public EditDraftApprenticeshipTestsFixture WithUpdateDraftApprenticeshipDomainEr
return this;
}

public EditDraftApprenticeshipTestsFixture WithCohort(CohortDetails cohortDetails = null)
public DetailsTestFixture WithCohort(CohortDetails cohortDetails = null)
{
var returnValue = cohortDetails ?? CohortDetails;

Expand All @@ -181,9 +210,9 @@ public EditDraftApprenticeshipTestsFixture WithCohort(CohortDetails cohortDetail

return this;
}
public EditDraftApprenticeshipTestsFixture WithTransferCohort()
public DetailsTestFixture WithTransferCohort()
{
var returnValue = new CohortDetails { CohortId = CohortId, HashedCohortId = CohortReference, IsFundedByTransfer = true, WithParty = Party.Employer};
var returnValue = new CohortDetails { CohortId = CohortId, HashedCohortId = CohortReference, IsFundedByTransfer = true, WithParty = Party.Employer };

CommitmentsServiceMock
.Setup(cs => cs.GetCohortDetail(It.IsAny<long>()))
Expand All @@ -192,19 +221,19 @@ public EditDraftApprenticeshipTestsFixture WithTransferCohort()
return this;
}

public EditDraftApprenticeshipTestsFixture WithEnhancedApproval()
public DetailsTestFixture WithEnhancedApproval()
{
AuthorizationServiceMock.Setup(x => x.IsAuthorized(EmployerFeature.EnhancedApproval)).Returns(true);
return this;
}

public EditDraftApprenticeshipTestsFixture WithModelStateError()
public DetailsTestFixture WithModelStateError()
{
Sut.ModelState.AddModelError("AKey", "Some Error");
return this;
}

public EditDraftApprenticeshipTestsFixture WithOtherParty()
public DetailsTestFixture WithOtherParty()
{
ModelMapperMock.Setup(x => x.Map<EditDraftApprenticeshipViewModel>(It.IsAny<EditDraftApprenticeshipRequest>()))
.Throws(new CohortEmployerUpdateDeniedException("Cohort With Other Party"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using SFA.DAS.CommitmentsV2.Api.Client;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.EmployerCommitmentsV2.Web.Exceptions;
using SFA.DAS.EmployerCommitmentsV2.Web.Mappers.DraftApprenticeship;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.DraftApprenticeship;
using SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Extensions;
Expand Down Expand Up @@ -60,9 +59,10 @@ public async Task Arrange()
_trainingProgrammeApiClient = new TrainingProgrammeApiClientMock();

_source = autoFixture.Create<EditDraftApprenticeshipRequest>();
_source.Cohort = _cohort;
_mapper = new EditDraftApprenticeshipViewModelMapper(_commitmentsApiClient.Object, _encodingService.Object, _trainingProgrammeApiClient.Object);

_result = await _mapper.Map(TestHelper.Clone(_source));
_result = await _mapper.Map(TestHelper.Clone(_source)) as EditDraftApprenticeshipViewModel;
}

[Test]
Expand All @@ -80,7 +80,7 @@ public void DraftApprenticeshipHashedIdIsMappedCorrectly()
[Test]
public void CohortIdIsMappedCorrectly()
{
Assert.AreEqual(_source.CohortId, _result.CohortId);
Assert.AreEqual(_source.Request.CohortId, _result.CohortId);
}

[Test]
Expand Down Expand Up @@ -152,7 +152,7 @@ public void OriginatorReferenceIsMappedCorrectly()
[Test]
public void AccountHashedIdIsMappedCorrectly()
{
Assert.AreEqual(_source.AccountHashedId, _result.AccountHashedId);
Assert.AreEqual(_source.Request.AccountHashedId, _result.AccountHashedId);
}

[Test]
Expand All @@ -167,19 +167,12 @@ public async Task CoursesAreMappedCorrectly(long? transferSenderId, bool fundedB
{
_cohort.TransferSenderId = transferSenderId;

_result = await _mapper.Map(_source);
_result = await _mapper.Map(_source) as EditDraftApprenticeshipViewModel;

Assert.AreEqual(fundedByTransfer
? _trainingProgrammeApiClient.Standards
: _trainingProgrammeApiClient.All,
_result.Courses);
}

[Test]
public void ThrowsWhenCohortNotWithEditingParty()
{
_cohort.WithParty = Party.Provider;
Assert.ThrowsAsync<CohortEmployerUpdateDeniedException>(() => _mapper.Map(_source));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Shared.Interfaces;
using SFA.DAS.CommitmentsV2.Api.Client;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.EmployerCommitmentsV2.Web.Mappers.DraftApprenticeship;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.DraftApprenticeship;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Mappers.DraftApprenticeship
{
[TestFixture]
public class IDraftApprenticeshipDetailsViewModelMapperTests
{
private IDraftApprenticeshipDetailsViewModelMapper _mapper;
private Mock<ICommitmentsApiClient> _apiClient;
private Mock<IModelMapper> _modelMapper;
private GetCohortResponse _cohort;
private DetailsRequest _request;

[SetUp]
public void Arrange()
{
var autoFixture = new Fixture();
_cohort = autoFixture.Create<GetCohortResponse>();

_request = autoFixture.Create<DetailsRequest>();

_apiClient = new Mock<ICommitmentsApiClient>();

_apiClient.Setup(x => x.GetCohort(It.IsAny<long>(), It.IsAny<CancellationToken>())).ReturnsAsync(_cohort);

_modelMapper = new Mock<IModelMapper>();

_modelMapper.Setup(x => x.Map<IDraftApprenticeshipViewModel>(It.IsAny<EditDraftApprenticeshipRequest>()))
.ReturnsAsync(new EditDraftApprenticeshipViewModel());

_modelMapper.Setup(x => x.Map<IDraftApprenticeshipViewModel>(It.IsAny<ViewDraftApprenticeshipRequest>()))
.ReturnsAsync(new ViewDraftApprenticeshipViewModel());

_mapper = new IDraftApprenticeshipDetailsViewModelMapper(_apiClient.Object, _modelMapper.Object);
}

[TestCase(Party.Employer, typeof(EditDraftApprenticeshipRequest))]
[TestCase(Party.Provider, typeof(ViewDraftApprenticeshipRequest))]
[TestCase(Party.TransferSender, typeof(ViewDraftApprenticeshipRequest))]
public async Task When_Mapping_The_Mapping_Request_Is_Passed_On_To_The_Appropriate_Mapper(Party withParty, Type expectedMappingRequestType)
{
_cohort.WithParty = withParty;
await _mapper.Map(_request);

_modelMapper.Verify(
x => x.Map<IDraftApprenticeshipViewModel>(It.Is<IDraftApprenticeshipRequest>(
r => r.GetType() == expectedMappingRequestType
&& r.Request == _request
&& r.Cohort == _cohort)),
Times.Once);
}
}
}
Loading

0 comments on commit 7ccaa84

Please sign in to comment.