generated from SkillsFundingAgency/das-github-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSP-1392: POST Permissions Endpoint (#25)
* CSP-1392: POST Permissions endpoint and relevant unit test coverage.
- Loading branch information
1 parent
29b9b8f
commit cf23603
Showing
60 changed files
with
1,386 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...PR.Api.UnitTests/Controllers/PermissionsControllerTests/PermissionsControllerPostTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using AutoFixture.NUnit3; | ||
using FluentAssertions; | ||
using FluentValidation.Results; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Moq; | ||
using SFA.DAS.PR.Api.Common; | ||
using SFA.DAS.PR.Api.Controllers; | ||
using SFA.DAS.PR.Application.Mediatr.Responses; | ||
using SFA.DAS.PR.Application.Permissions.Commands.PostPermissions; | ||
using SFA.DAS.Testing.AutoFixture; | ||
|
||
namespace SFA.DAS.PR.Api.UnitTests.Controllers.PermissionsControllerTests; | ||
|
||
public class PermissionsControllerPostTests | ||
{ | ||
[Test] | ||
[MoqAutoData] | ||
public async Task PostPermission_InvokesQueryHandler( | ||
[Frozen] Mock<IMediator> mediatorMock, | ||
[Greedy] PermissionsController sut, | ||
PostPermissionsCommand command, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
await sut.PostPermission(command, cancellationToken); | ||
|
||
mediatorMock.Verify(m => | ||
m.Send(It.Is<PostPermissionsCommand>(q => | ||
q.AccountLegalEntityId == command.AccountLegalEntityId && | ||
q.Ukprn == command.Ukprn && | ||
q.Operations == command.Operations && | ||
q.UserRef == command.UserRef | ||
), | ||
cancellationToken) | ||
); | ||
} | ||
|
||
[Test, MoqAutoData] | ||
public async Task PostPermission_HandlerReturnsData_ReturnsOkResponse( | ||
[Frozen] Mock<IMediator> mediatorMock, | ||
[Greedy] PermissionsController sut, | ||
PostPermissionsCommand command, | ||
PostPermissionsCommandResult postPermissionsCommandResult, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
var response = new ValidatedResponse<PostPermissionsCommandResult>(postPermissionsCommandResult); | ||
|
||
mediatorMock.Setup(m => m.Send( | ||
It.Is<PostPermissionsCommand>(q => | ||
q.AccountLegalEntityId == command.AccountLegalEntityId && | ||
q.Ukprn == command.Ukprn && | ||
q.Operations == command.Operations && | ||
q.UserRef == command.UserRef | ||
), | ||
cancellationToken) | ||
).ReturnsAsync(response); | ||
|
||
var result = await sut.PostPermission(command, cancellationToken); | ||
result.As<OkObjectResult>().Should().NotBeNull(); | ||
result.As<OkObjectResult>().Value.Should().Be(postPermissionsCommandResult); | ||
} | ||
|
||
[Test, MoqAutoData] | ||
public async Task PostPermission_HandlerReturnsData_ReturnsBadRequest( | ||
[Frozen] Mock<IMediator> mediatorMock, | ||
[Greedy] PermissionsController sut, | ||
PostPermissionsCommand command, | ||
IList<ValidationFailure> validationErrors, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
var response = new ValidatedResponse<PostPermissionsCommandResult>(validationErrors); | ||
|
||
mediatorMock.Setup(m => m.Send( | ||
It.Is<PostPermissionsCommand>(q => | ||
q.AccountLegalEntityId == command.AccountLegalEntityId && | ||
q.Ukprn == command.Ukprn && | ||
q.Operations == command.Operations && | ||
q.UserRef == command.UserRef | ||
), | ||
cancellationToken) | ||
).ReturnsAsync(response); | ||
|
||
var result = await sut.PostPermission(command, cancellationToken); | ||
result.As<BadRequestObjectResult>().Should().NotBeNull(); | ||
result.As<BadRequestObjectResult>().Value.As<List<ValidationError>>().Count.Should().Be(validationErrors.Count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/SFA.DAS.PR.Application.UnitTests/Common/Validators/AccountLegalEntityValidatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using FluentValidation; | ||
using Moq; | ||
using SFA.DAS.PR.Application.Common.Validators; | ||
using SFA.DAS.PR.Domain.Interfaces; | ||
|
||
namespace SFA.DAS.PR.Application.UnitTests.Common.Validators; | ||
|
||
public class AccountLegalEntityValidatorTests | ||
{ | ||
public class TestAccountLegalEntityIdEntity : IAccountLegalEntityIdEntity | ||
{ | ||
public long AccountLegalEntityId { get; set; } | ||
} | ||
|
||
[Test] | ||
public async Task ValidateAccountLegalEntityExists_ValidEntity_ReturnsValid() | ||
{ | ||
var entity = new TestAccountLegalEntityIdEntity { AccountLegalEntityId = 1 }; | ||
|
||
var mockRepository = new Mock<IAccountLegalEntityReadRepository>(); | ||
mockRepository.Setup(r => r.AccountLegalEntityExists(It.IsAny<long>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(true); | ||
|
||
var validator = new InlineValidator<TestAccountLegalEntityIdEntity>(); | ||
validator.RuleFor(x => x.AccountLegalEntityId) | ||
.ValidateAccountLegalEntityExists(mockRepository.Object); | ||
|
||
var validationResult = await validator.ValidateAsync(entity); | ||
|
||
Assert.That(validationResult.IsValid, Is.True); | ||
} | ||
|
||
[Test] | ||
public async Task ValidateAccountLegalEntity_Null_AccountLegalEntityId_ReturnsInvalid() | ||
{ | ||
var entity = new TestAccountLegalEntityIdEntity { AccountLegalEntityId = 0 }; | ||
|
||
var mockRepository = new Mock<IAccountLegalEntityReadRepository>(); | ||
mockRepository.Setup(r => r.AccountLegalEntityExists(It.IsAny<long>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(true); | ||
|
||
var validator = new InlineValidator<TestAccountLegalEntityIdEntity>(); | ||
validator.RuleFor(x => x.AccountLegalEntityId) | ||
.ValidateAccountLegalEntityExists(mockRepository.Object); | ||
|
||
var validationResult = await validator.ValidateAsync(entity); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(validationResult.IsValid, Is.False); | ||
Assert.That(validationResult.Errors[0].ErrorMessage, Is.EqualTo(AccountLegalEntityValidator.AccountLegalEntityIdValidationMessage)); | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task ValidateAccountLegalEntity_AccountLegalEntityExists_ReturnsInvalid() | ||
{ | ||
var entity = new TestAccountLegalEntityIdEntity { AccountLegalEntityId = 1 }; | ||
|
||
var mockRepository = new Mock<IAccountLegalEntityReadRepository>(); | ||
mockRepository.Setup(r => r.AccountLegalEntityExists(It.IsAny<long>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(false); | ||
|
||
var validator = new InlineValidator<TestAccountLegalEntityIdEntity>(); | ||
validator.RuleFor(x => x.AccountLegalEntityId) | ||
.ValidateAccountLegalEntityExists(mockRepository.Object); | ||
|
||
var validationResult = await validator.ValidateAsync(entity); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(validationResult.IsValid, Is.False); | ||
Assert.That(validationResult.Errors[0].ErrorMessage, Is.EqualTo(AccountLegalEntityValidator.AccountLegalEntityExistValidationMessage)); | ||
}); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/SFA.DAS.PR.Application.UnitTests/Common/Validators/UkprnFormatValidatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using FluentValidation; | ||
using SFA.DAS.PR.Application.Common.Validators; | ||
using SFA.DAS.PR.Domain.Interfaces; | ||
|
||
namespace SFA.DAS.PR.Application.UnitTests.Common.Validators; | ||
|
||
public class UkprnFormatValidatorTests | ||
{ | ||
public class TestUkprnEntity : IUkprnEntity | ||
{ | ||
public long? Ukprn { get; set; } | ||
} | ||
|
||
[Test] | ||
public async Task ValidateUkrpn_Ukprn_ReturnsValid() | ||
{ | ||
var entity = new TestUkprnEntity { Ukprn = 10000003 }; | ||
|
||
var validator = new InlineValidator<TestUkprnEntity>(); | ||
validator.RuleFor(x => x.Ukprn) | ||
.CheckUkprnFormat(); | ||
|
||
var validationResult = await validator.ValidateAsync(entity); | ||
|
||
Assert.That(validationResult.IsValid, Is.True); | ||
} | ||
|
||
[Test] | ||
public async Task ValidateUkrpn_Ukprn_ReturnsInvalid() | ||
{ | ||
var entity = new TestUkprnEntity { Ukprn = 1 }; | ||
|
||
var validator = new InlineValidator<TestUkprnEntity>(); | ||
validator.RuleFor(x => x.Ukprn) | ||
.CheckUkprnFormat(); | ||
|
||
var validationResult = await validator.ValidateAsync(entity); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(validationResult.IsValid, Is.False); | ||
Assert.That(validationResult.Errors[0].ErrorMessage, Is.EqualTo(UkprnFormatValidator.UkprnFormatValidationMessage)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.