-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added update organisation join request endpoint, usecase & unit test
- Loading branch information
Showing
8 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
Services/CO.CDP.Organisation.WebApi.Tests/UseCase/UpdateJoinRequestUseCaseTests.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,127 @@ | ||
using CO.CDP.Organisation.WebApi.Model; | ||
using CO.CDP.Organisation.WebApi.UseCase; | ||
using CO.CDP.OrganisationInformation; | ||
using CO.CDP.OrganisationInformation.Persistence; | ||
using FluentAssertions; | ||
using Moq; | ||
|
||
namespace CO.CDP.Organisation.WebApi.Tests.UseCase; | ||
|
||
public class UpdateJoinRequestUseCaseTests | ||
{ | ||
private readonly Mock<IOrganisationRepository> _mockOrganisationRepository; | ||
private readonly Mock<IOrganisationJoinRequestRepository> _mockRequestRepository; | ||
private readonly UpdateJoinRequestUseCase _useCase; | ||
private readonly Guid _organisationId = Guid.NewGuid(); | ||
private readonly Guid _joinRequestId = Guid.NewGuid(); | ||
public UpdateJoinRequestUseCaseTests() | ||
{ | ||
_mockOrganisationRepository = new Mock<IOrganisationRepository>(); | ||
_mockRequestRepository = new Mock<IOrganisationJoinRequestRepository>(); | ||
_useCase = new UpdateJoinRequestUseCase(_mockOrganisationRepository.Object, _mockRequestRepository.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task Execute_ShouldUpdateJoinRequest_WhenOrganisationAndJoinRequestExist() | ||
{ | ||
var updateJoinRequest = new UpdateJoinRequest | ||
{ | ||
ReviewedBy = 1, | ||
status = OrganisationJoinRequestStatus.Accepted, | ||
}; | ||
|
||
var joinRequest = OrganisationJoinRequest; | ||
|
||
_mockOrganisationRepository | ||
.Setup(repo => repo.Find(_organisationId)) | ||
.ReturnsAsync(Organisation); | ||
|
||
_mockRequestRepository | ||
.Setup(repo => repo.Find(_joinRequestId, _organisationId)) | ||
.ReturnsAsync(joinRequest); | ||
|
||
var result = await _useCase.Execute((_organisationId, _joinRequestId, updateJoinRequest)); | ||
|
||
result.Should().BeTrue(); | ||
joinRequest.ReviewedById.Should().Be(updateJoinRequest.ReviewedBy); | ||
joinRequest.Status.Should().Be(updateJoinRequest.status); | ||
joinRequest.ReviewedOn.Should().BeCloseTo(DateTimeOffset.UtcNow, TimeSpan.FromSeconds(1)); | ||
|
||
_mockOrganisationRepository.Verify(repo => repo.Find(_organisationId), Times.Once); | ||
_mockRequestRepository.Verify(repo => repo.Find(_joinRequestId, _organisationId), Times.Once); | ||
_mockRequestRepository.Verify(repo => repo.Save(joinRequest), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async Task Execute_ShouldThrowException_WhenOrganisationNotFound() | ||
{ | ||
var organisationId = Guid.NewGuid(); | ||
var joinRequestId = Guid.NewGuid(); | ||
var updateJoinRequest = new UpdateJoinRequest | ||
{ | ||
ReviewedBy = 1, | ||
status = OrganisationJoinRequestStatus.Accepted, | ||
}; | ||
|
||
_mockOrganisationRepository | ||
.Setup(repo => repo.Find(organisationId)) | ||
.ReturnsAsync((CO.CDP.OrganisationInformation.Persistence.Organisation?)null); | ||
|
||
Func<Task> act = async () => await _useCase.Execute((organisationId, joinRequestId, updateJoinRequest)); | ||
|
||
await act.Should().ThrowAsync<UnknownOrganisationException>() | ||
.WithMessage($"Unknown organisation {organisationId}."); | ||
|
||
_mockOrganisationRepository.Verify(repo => repo.Find(organisationId), Times.Once); | ||
_mockRequestRepository.Verify(repo => repo.Find(It.IsAny<Guid>(), It.IsAny<Guid>()), Times.Never); | ||
_mockRequestRepository.Verify(repo => repo.Save(It.IsAny<CO.CDP.OrganisationInformation.Persistence.OrganisationJoinRequest>()), Times.Never); | ||
} | ||
|
||
[Fact] | ||
public async Task Execute_ShouldThrowException_WhenJoinRequestNotFound() | ||
{ | ||
var organisationId = Guid.NewGuid(); | ||
var joinRequestId = Guid.NewGuid(); | ||
var updateJoinRequest = new UpdateJoinRequest | ||
{ | ||
ReviewedBy = 1, | ||
status = OrganisationJoinRequestStatus.Accepted | ||
}; | ||
|
||
_mockOrganisationRepository | ||
.Setup(repo => repo.Find(organisationId)) | ||
.ReturnsAsync(Organisation); | ||
|
||
_ = _mockRequestRepository | ||
.Setup(repo => repo.Find(joinRequestId, organisationId)) | ||
.ReturnsAsync((CO.CDP.OrganisationInformation.Persistence.OrganisationJoinRequest?)null); | ||
|
||
Func<Task> act = async () => await _useCase.Execute((organisationId, joinRequestId, updateJoinRequest)); | ||
|
||
await act.Should().ThrowAsync<UnknownOrganisationJoinRequestException>() | ||
.WithMessage($"Unknown organisation join request for org id {organisationId} or request id {joinRequestId}."); | ||
|
||
_mockOrganisationRepository.Verify(repo => repo.Find(organisationId), Times.Once); | ||
_mockRequestRepository.Verify(repo => repo.Find(joinRequestId, organisationId), Times.Once); | ||
_mockRequestRepository.Verify(repo => repo.Save(It.IsAny<CO.CDP.OrganisationInformation.Persistence.OrganisationJoinRequest>()), Times.Never); | ||
} | ||
|
||
private CO.CDP.OrganisationInformation.Persistence.Organisation Organisation => | ||
new CO.CDP.OrganisationInformation.Persistence.Organisation | ||
{ | ||
Guid = _organisationId, | ||
Name = "Test", | ||
Tenant = It.IsAny<Tenant>(), | ||
ContactPoints = [new CO.CDP.OrganisationInformation.Persistence.Organisation.ContactPoint { Email = "[email protected]" }], | ||
SupplierInfo = new CO.CDP.OrganisationInformation.Persistence.Organisation.SupplierInformation() | ||
}; | ||
|
||
private CO.CDP.OrganisationInformation.Persistence.OrganisationJoinRequest OrganisationJoinRequest => | ||
new CO.CDP.OrganisationInformation.Persistence.OrganisationJoinRequest | ||
{ | ||
Guid = _joinRequestId, | ||
Status = OrganisationJoinRequestStatus.Accepted, | ||
Organisation = Organisation, | ||
Id = 1 | ||
}; | ||
} |
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
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
27 changes: 27 additions & 0 deletions
27
Services/CO.CDP.Organisation.WebApi/UseCase/UpdateJoinRequestUseCase.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,27 @@ | ||
using CO.CDP.Organisation.WebApi.Model; | ||
using CO.CDP.OrganisationInformation.Persistence; | ||
|
||
namespace CO.CDP.Organisation.WebApi.UseCase; | ||
|
||
public class UpdateJoinRequestUseCase( | ||
IOrganisationRepository organisationRepository, | ||
IOrganisationJoinRequestRepository requestRepository) | ||
: IUseCase<(Guid organisationId, Guid joinRequestId, UpdateJoinRequest updateJoinRequest), bool> | ||
{ | ||
public async Task<bool> Execute((Guid organisationId, Guid joinRequestId, UpdateJoinRequest updateJoinRequest) command) | ||
{ | ||
_ = await organisationRepository.Find(command.organisationId) | ||
?? throw new UnknownOrganisationException($"Unknown organisation {command.organisationId}."); | ||
|
||
var joinRequest = await requestRepository.Find(command.joinRequestId, command.organisationId) | ||
?? throw new UnknownOrganisationJoinRequestException ($"Unknown organisation join request for org id {command.organisationId} or request id {command.joinRequestId}."); | ||
|
||
joinRequest.ReviewedById = command.updateJoinRequest.ReviewedBy; | ||
joinRequest.Status = command.updateJoinRequest.status; | ||
joinRequest.ReviewedOn = DateTimeOffset.UtcNow; | ||
|
||
requestRepository.Save(joinRequest); | ||
|
||
return await Task.FromResult(true); | ||
} | ||
} |
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