Skip to content

Commit

Permalink
Merge pull request #131 from SkillsFundingAgency/MF-255-non-eoi-add-a…
Browse files Browse the repository at this point in the history
…pprentice

MF-255 Non-EOI select apprentice
  • Loading branch information
SijiOdun authored Aug 29, 2019
2 parents 8a1f617 + 555f26e commit 8aeefbf
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 360 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Reflection;
using AutoFixture;
using AutoFixture.Kernel;
using AutoFixture.NUnit3;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace SFA.DAS.Reservations.Web.UnitTests.Customisations
{
[AttributeUsage(AttributeTargets.Parameter)]
public class ArrangeActionContextAttribute : CustomizeAttribute
{
public override ICustomization GetCustomization(ParameterInfo parameter)
{
if (parameter == null)
{
throw new ArgumentNullException(nameof(parameter));
}

if (parameter.ParameterType != typeof(ActionExecutingContext))
{
throw new ArgumentException(nameof(parameter));
}

return new ArrangeActionContextCustomisation();
}
}

public class ArrangeActionContextCustomisation : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new ActionExecutingContextBuilder());
fixture.Customize<ActionExecutingContext>(composer => composer
.Without(context => context.Result));
}
}

public class ActionExecutingContextBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
if (request is ParameterInfo paramInfo
&& paramInfo.ParameterType == typeof(object)
&& paramInfo.Name == "controller")
{
return context.Create<Controller>();
}

return new NoSpecimen();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading;
using MediatR;
Expand All @@ -10,12 +9,7 @@
using System.Threading.Tasks;
using AutoFixture.NUnit3;
using Microsoft.Extensions.Options;
using SFA.DAS.Common.Domain.Types;
using SFA.DAS.Encoding;
using SFA.DAS.Reservations.Application.Employers.Queries.GetLegalEntities;
using SFA.DAS.Reservations.Application.FundingRules.Queries.GetNextUnreadGlobalFundingRule;
using SFA.DAS.Reservations.Domain.Employers;
using SFA.DAS.Reservations.Domain.Interfaces;
using SFA.DAS.Reservations.Domain.Rules;
using SFA.DAS.Reservations.Domain.Rules.Api;
using SFA.DAS.Reservations.Infrastructure.Configuration;
Expand All @@ -30,11 +24,7 @@ public class WhenViewingTheIndexPage

[Test, MoqAutoData]
public async Task ThenChecksIfRelatedUnreadRulesExists(
string accountId,
string expectedUserId,
long decodedAccountId,
GetLegalEntitiesResponse getLegalEntitiesResponse,
[Frozen] Mock<IEncodingService> mockEncodingService,
[Frozen] Mock<IMediator> mockMediator,
EmployerReservationsController controller)
{
Expand All @@ -46,21 +36,9 @@ public async Task ThenChecksIfRelatedUnreadRulesExists(
mockMediator.Setup(x =>
x.Send(It.IsAny<GetNextUnreadGlobalFundingRuleQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetNextUnreadGlobalFundingRuleResult());
//arrange eoi
foreach (var accountLegalEntity in getLegalEntitiesResponse.AccountLegalEntities)
{
accountLegalEntity.IsLevy = false;
accountLegalEntity.AgreementType = AgreementType.NonLevyExpressionOfInterest;
}
mockMediator
.Setup(mediator => mediator.Send(It.IsAny<GetLegalEntitiesQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(getLegalEntitiesResponse);
mockEncodingService
.Setup(service => service.Decode(accountId, EncodingType.AccountId))
.Returns(decodedAccountId);

//act
await controller.Index(accountId);
await controller.Index();

//assert
mockMediator.Verify(m => m.Send(It.Is<GetNextUnreadGlobalFundingRuleQuery>(
Expand All @@ -69,11 +47,7 @@ public async Task ThenChecksIfRelatedUnreadRulesExists(

[Test, MoqAutoData]
public async Task ThenRedirectToStartIfNoFundingRulesExist(
string accountId,
string expectedUserId,
long decodedAccountId,
GetLegalEntitiesResponse getLegalEntitiesResponse,
[Frozen] Mock<IEncodingService> mockEncodingService,
[Frozen] Mock<IMediator> mockMediator,
EmployerReservationsController controller)
{
Expand All @@ -84,21 +58,9 @@ public async Task ThenRedirectToStartIfNoFundingRulesExist(
}));
mockMediator.Setup(x => x.Send(It.IsAny<GetNextUnreadGlobalFundingRuleQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync((GetNextUnreadGlobalFundingRuleResult) null);
//arrange eoi
foreach (var accountLegalEntity in getLegalEntitiesResponse.AccountLegalEntities)
{
accountLegalEntity.IsLevy = false;
accountLegalEntity.AgreementType = AgreementType.NonLevyExpressionOfInterest;
}
mockMediator
.Setup(mediator => mediator.Send(It.IsAny<GetLegalEntitiesQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(getLegalEntitiesResponse);
mockEncodingService
.Setup(service => service.Decode(accountId, EncodingType.AccountId))
.Returns(decodedAccountId);

//act
var redirect = await controller.Index(accountId) as RedirectToActionResult;
var redirect = await controller.Index() as RedirectToActionResult;

//assert
Assert.IsNotNull(redirect);
Expand All @@ -107,11 +69,7 @@ public async Task ThenRedirectToStartIfNoFundingRulesExist(

[Test, MoqAutoData]
public async Task ThenRedirectToFundingPausedIfFundingRulesExist(
string accountId,
string expectedUserId,
long decodedAccountId,
GetLegalEntitiesResponse getLegalEntitiesResponse,
[Frozen] Mock<IEncodingService> mockEncodingService,
[Frozen] Mock<IMediator> mockMediator,
[Frozen] Mock<IOptions<ReservationsWebConfiguration>> config,
EmployerReservationsController controller)
Expand All @@ -129,21 +87,9 @@ public async Task ThenRedirectToFundingPausedIfFundingRulesExist(
var result = new GetNextUnreadGlobalFundingRuleResult { Rule = expectedRule };
mockMediator.Setup(x => x.Send(It.IsAny<GetNextUnreadGlobalFundingRuleQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(result);
//arrange eoi
foreach (var accountLegalEntity in getLegalEntitiesResponse.AccountLegalEntities)
{
accountLegalEntity.IsLevy = false;
accountLegalEntity.AgreementType = AgreementType.NonLevyExpressionOfInterest;
}
mockMediator
.Setup(mediator => mediator.Send(It.IsAny<GetLegalEntitiesQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(getLegalEntitiesResponse);
mockEncodingService
.Setup(service => service.Decode(accountId, EncodingType.AccountId))
.Returns(decodedAccountId);

//act
var view = await controller.Index(accountId) as ViewResult;
var view = await controller.Index() as ViewResult;

//assert
Assert.IsNotNull(view);
Expand All @@ -158,11 +104,7 @@ public async Task ThenRedirectToFundingPausedIfFundingRulesExist(

[Test, MoqAutoData]
public async Task ThenRedirectToStartIfNoIdFoundOnNextGlobalFundingRule(
string accountId,
string expectedUserId,
long decodedAccountId,
GetLegalEntitiesResponse getLegalEntitiesResponse,
[Frozen] Mock<IEncodingService> mockEncodingService,
[Frozen] Mock<IMediator> mockMediator,
EmployerReservationsController controller)
{
Expand All @@ -173,21 +115,9 @@ public async Task ThenRedirectToStartIfNoIdFoundOnNextGlobalFundingRule(
}));
mockMediator.Setup(x => x.Send(It.IsAny<GetNextUnreadGlobalFundingRuleQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetNextUnreadGlobalFundingRuleResult {Rule = new GlobalRule{ActiveFrom = DateTime.Now}});
//arrange eoi
foreach (var accountLegalEntity in getLegalEntitiesResponse.AccountLegalEntities)
{
accountLegalEntity.IsLevy = false;
accountLegalEntity.AgreementType = AgreementType.NonLevyExpressionOfInterest;
}
mockMediator
.Setup(mediator => mediator.Send(It.IsAny<GetLegalEntitiesQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(getLegalEntitiesResponse);
mockEncodingService
.Setup(service => service.Decode(accountId, EncodingType.AccountId))
.Returns(decodedAccountId);

//act
var redirect = await controller.Index(accountId) as RedirectToActionResult;
var redirect = await controller.Index() as RedirectToActionResult;

//assert
Assert.IsNotNull(redirect);
Expand All @@ -196,11 +126,7 @@ public async Task ThenRedirectToStartIfNoIdFoundOnNextGlobalFundingRule(

[Test, MoqAutoData]
public async Task ThenRedirectToStartIfNoActiveFromDateFoundOnNextGlobalFundingRule(
string accountId,
string expectedUserId,
long decodedAccountId,
GetLegalEntitiesResponse getLegalEntitiesResponse,
[Frozen] Mock<IEncodingService> mockEncodingService,
[Frozen] Mock<IMediator> mockMediator,
EmployerReservationsController controller)
{
Expand All @@ -211,126 +137,13 @@ public async Task ThenRedirectToStartIfNoActiveFromDateFoundOnNextGlobalFundingR
}));
mockMediator.Setup(x => x.Send(It.IsAny<GetNextUnreadGlobalFundingRuleQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetNextUnreadGlobalFundingRuleResult {Rule = new GlobalRule{Id = 2}});
//arrange eoi
foreach (var accountLegalEntity in getLegalEntitiesResponse.AccountLegalEntities)
{
accountLegalEntity.IsLevy = false;
accountLegalEntity.AgreementType = AgreementType.NonLevyExpressionOfInterest;
}
mockMediator
.Setup(mediator => mediator.Send(It.IsAny<GetLegalEntitiesQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(getLegalEntitiesResponse);
mockEncodingService
.Setup(service => service.Decode(accountId, EncodingType.AccountId))
.Returns(decodedAccountId);

//act
var redirect = await controller.Index(accountId) as RedirectToActionResult;
var redirect = await controller.Index() as RedirectToActionResult;

//assert
Assert.IsNotNull(redirect);
Assert.AreEqual(redirect.ActionName, "Start");
}

[Test, MoqAutoData]
public async Task And_Not_Eoi_And_NonLevy_Then_Returns_NonEoiHolding_View(
string accountId,
string expectedUserId,
string homeLink,
long decodedAccountId,
GetLegalEntitiesResponse getLegalEntitiesResponse,
[Frozen] Mock<IEncodingService> mockEncodingService,
[Frozen] Mock<IExternalUrlHelper> mockUrlHelper,
[Frozen] Mock<IMediator> mockMediator,
EmployerReservationsController controller)
{
//arrange
controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[]
{
new Claim(EmployerClaims.IdamsUserIdClaimTypeIdentifier, expectedUserId)
}));

mockMediator
.Setup(x => x.Send(It.IsAny<GetNextUnreadGlobalFundingRuleQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetNextUnreadGlobalFundingRuleResult {Rule = new GlobalRule{Id = 2}});
//arrange eoi
foreach (var accountLegalEntity in getLegalEntitiesResponse.AccountLegalEntities)
{
accountLegalEntity.IsLevy = false;
accountLegalEntity.AgreementType = AgreementType.Levy;
}
mockMediator
.Setup(mediator => mediator.Send(It.IsAny<GetLegalEntitiesQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(getLegalEntitiesResponse);
mockEncodingService
.Setup(service => service.Decode(accountId, EncodingType.AccountId))
.Returns(decodedAccountId);
mockUrlHelper
.Setup(helper => helper.GenerateUrl(It.Is<UrlParameters>(parameters =>
parameters.Controller == "teams" &&
parameters.SubDomain == "accounts" &&
parameters.Folder == "accounts" &&
parameters.Id == accountId
)))
.Returns(homeLink);

//act
var result = await controller.Index(accountId) as ViewResult;

//assert
Assert.AreEqual("NonEoiHolding", result.ViewName);
var model = result.Model as NonEoiHoldingViewModel;
Assert.AreEqual(homeLink, model.BackLink);
Assert.AreEqual(homeLink, model.HomeLink);
}

[Test, MoqAutoData]
// no agreement - no legal entities returned from api
public async Task And_No_Agreement_Signed_Then_Returns_NonEoiHolding_View(
string accountId,
string expectedUserId,
string homeLink,
long decodedAccountId,
GetLegalEntitiesResponse getLegalEntitiesResponse,
[Frozen] Mock<IEncodingService> mockEncodingService,
[Frozen] Mock<IExternalUrlHelper> mockUrlHelper,
[Frozen] Mock<IMediator> mockMediator,
EmployerReservationsController controller)
{
//arrange
controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[]
{
new Claim(EmployerClaims.IdamsUserIdClaimTypeIdentifier, expectedUserId)
}));

mockMediator
.Setup(x => x.Send(It.IsAny<GetNextUnreadGlobalFundingRuleQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetNextUnreadGlobalFundingRuleResult {Rule = new GlobalRule{Id = 2}});
//arrange eoi
getLegalEntitiesResponse.AccountLegalEntities = new List<AccountLegalEntity>();
mockMediator
.Setup(mediator => mediator.Send(It.IsAny<GetLegalEntitiesQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(getLegalEntitiesResponse);
mockEncodingService
.Setup(service => service.Decode(accountId, EncodingType.AccountId))
.Returns(decodedAccountId);
mockUrlHelper
.Setup(helper => helper.GenerateUrl(It.Is<UrlParameters>(parameters =>
parameters.Controller == "teams" &&
parameters.SubDomain == "accounts" &&
parameters.Folder == "accounts" &&
parameters.Id == accountId
)))
.Returns(homeLink);

//act
var result = await controller.Index(accountId) as ViewResult;

//assert
Assert.AreEqual("NonEoiHolding", result.ViewName);
var model = result.Model as NonEoiHoldingViewModel;
Assert.AreEqual(homeLink, model.BackLink);
Assert.AreEqual(homeLink, model.HomeLink);
}
}
}
Loading

0 comments on commit 8aeefbf

Please sign in to comment.