Skip to content

Commit

Permalink
Merge branch 'main' into feature/dp-875-Informal-consortium-add-addit…
Browse files Browse the repository at this point in the history
…ional-organisations
  • Loading branch information
dpatel017 authored Jan 10, 2025
2 parents 9667c46 + 9562bbb commit 039a936
Show file tree
Hide file tree
Showing 31 changed files with 542 additions and 88 deletions.
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,23 @@ up: compose.override.yml ## Start Docker containers
.PHONY: up

verify-up: compose.override.yml ## Verify if all Docker containers have run
@docker compose ps -a --format json | jq --exit-status 'select(.ExitCode != 0 or (.Health != "healthy" and .Health != ""))' && exit 1 || echo "All services up"
@timeout=60; \
interval=5; \
while [ $$timeout -gt 0 ]; do \
if docker compose ps -a --format json | jq --exit-status 'select(.ExitCode != 0 or (.Health != "healthy" and .Health != ""))' > /dev/null; then \
echo "Waiting for services to be healthy..."; \
sleep $$interval; \
timeout=$$(($$timeout - $$interval)); \
else \
echo "All services up"; \
exit 0; \
fi; \
done; \
echo "Services did not become healthy in time"; \
exit 1
.PHONY: verify-up


down: ## Destroy Docker containers
@docker compose down
.PHONY: down
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using AutoMapper;
using CO.CDP.Organisation.WebApi.Model;
using CO.CDP.Organisation.WebApi.Tests.AutoMapper;
using CO.CDP.Organisation.WebApi.UseCase;
using CO.CDP.OrganisationInformation;
using CO.CDP.OrganisationInformation.Persistence;
using FluentAssertions;
using Moq;
using Persistence = CO.CDP.OrganisationInformation.Persistence;
using Person = CO.CDP.OrganisationInformation.Persistence.Person;

namespace CO.CDP.Organisation.WebApi.Tests.UseCase;

public class GetLatestMouUseCaseTest(AutoMapperFixture mapperFixture)
: IClassFixture<AutoMapperFixture>
{
private readonly Mock<IOrganisationRepository> _organisationRepository = new();
private GetLatestMouUseCase _useCase => new GetLatestMouUseCase(_organisationRepository.Object, mapperFixture.Mapper);

[Fact]
public async Task Execute_ShouldReturnMappedMou_WhenLatestMouExists()
{
var latestMouEntity = new Persistence.Mou
{
Id = 1,
Guid = Guid.NewGuid(),
FilePath = "/path/to/mou.pdf",
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
};

var mappedMou = new CO.CDP.Organisation.WebApi.Model.Mou
{
Id = latestMouEntity.Guid,
FilePath = latestMouEntity.FilePath,
CreatedOn = latestMouEntity.CreatedOn
};

_organisationRepository
.Setup(repo => repo.GetLatestMou())
.ReturnsAsync(latestMouEntity);

var result = await _useCase.Execute();

result.Should().BeEquivalentTo(mappedMou);

_organisationRepository.Verify(repo => repo.GetLatestMou(), Times.Once);
}

[Fact]
public async Task Execute_ShouldThrowUnknownMouException_WhenLatestMouIsNull()
{
_organisationRepository
.Setup(repo => repo.GetLatestMou())
.ReturnsAsync((Persistence.Mou)null!);

Func<Task> act = async () => await _useCase.Execute();

await act.Should().ThrowAsync<UnknownMouException>()
.WithMessage("No MOU found.");

_organisationRepository.Verify(repo => repo.GetLatestMou(), Times.Once);
}

public static Person FakePerson(
Guid? guid = null,
string? userUrn = null,
string firstname = "Jon",
string lastname = "doe",
string? email = null,
string phone = "07925123123",
List<string>? scopes = null,
Tenant? tenant = null,
List<(Persistence.Organisation, List<string>)>? organisationsWithScope = null
)
{
scopes = scopes ?? [];
var personGuid = guid ?? Guid.NewGuid();
var person = new Person
{
Guid = personGuid,
UserUrn = userUrn ?? $"urn:fdc:gov.uk:2022:{Guid.NewGuid()}",
FirstName = firstname,
LastName = lastname,
Email = email ?? $"jon{personGuid}@example.com",
Phone = phone,
Scopes = scopes
};
if (tenant != null)
{
person.Tenants.Add(tenant);
}

foreach (var organisationWithScope in organisationsWithScope ?? [])
{
person.PersonOrganisations.Add(
new OrganisationPerson
{
Person = person,
Organisation = organisationWithScope.Item1,
Scopes = organisationWithScope.Item2
}
);
}

return person;
}

private static Persistence.Organisation FakeOrganisation(bool? withBuyerInfo = true)
{
Persistence.Organisation org = new()
{
Id = 1,
Guid = Guid.NewGuid(),
Name = "FakeOrg",
Tenant = new Tenant
{
Guid = Guid.NewGuid(),
Name = "Tenant 101"
},
ContactPoints =
[
new Persistence.Organisation.ContactPoint
{
Email = "contact@test.org"
}
],
Type = OrganisationType.Organisation
};

if (withBuyerInfo == true)
{
var devolvedRegulations = new List<DevolvedRegulation>();
devolvedRegulations.Add(DevolvedRegulation.NorthernIreland);

org.BuyerInfo = new Persistence.Organisation.BuyerInformation
{
BuyerType = "FakeBuyerType",
DevolvedRegulations = devolvedRegulations,
};
}

return org;
}
}
147 changes: 147 additions & 0 deletions Services/CO.CDP.Organisation.WebApi.Tests/UseCase/GetMouUseCaseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using AutoMapper;
using CO.CDP.Organisation.WebApi.Model;
using CO.CDP.Organisation.WebApi.Tests.AutoMapper;
using CO.CDP.Organisation.WebApi.UseCase;
using CO.CDP.OrganisationInformation;
using CO.CDP.OrganisationInformation.Persistence;
using FluentAssertions;
using Moq;
using Persistence = CO.CDP.OrganisationInformation.Persistence;
using Person = CO.CDP.OrganisationInformation.Persistence.Person;

namespace CO.CDP.Organisation.WebApi.Tests.UseCase;

public class GetMouUseCaseTest(AutoMapperFixture mapperFixture)
: IClassFixture<AutoMapperFixture>
{
private readonly Mock<IOrganisationRepository> _organisationRepository = new();
private GetMouUseCase _useCase => new GetMouUseCase(_organisationRepository.Object, mapperFixture.Mapper);

[Fact]
public async Task Execute_ShouldReturnMappedMou_WhenLatestMouExists()
{
var mouId = Guid.NewGuid();
var mouEntity = new Persistence.Mou
{
Id = 1,
Guid = mouId,
FilePath = "/path/to/mou.pdf",
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
};

var mappedMou = new Model.Mou
{
Id = mouId,
FilePath = mouEntity.FilePath,
CreatedOn = mouEntity.CreatedOn
};

_organisationRepository
.Setup(repo => repo.GetMou(mouId))
.ReturnsAsync(mouEntity);

var result = await _useCase.Execute(mouId);

result.Should().BeEquivalentTo(mappedMou);

_organisationRepository.Verify(repo => repo.GetMou(mouId), Times.Once);
}

[Fact]
public async Task Execute_ShouldThrowUnknownMouException_WhenLatestMouIsNull()
{
var mouId = Guid.NewGuid();
_organisationRepository
.Setup(repo => repo.GetMou(mouId))
.ReturnsAsync((Persistence.Mou)null!);

Func<Task> act = async () => await _useCase.Execute(mouId);

await act.Should().ThrowAsync<UnknownMouException>()
.WithMessage("No MOU found.");

_organisationRepository.Verify(repo => repo.GetMou(mouId), Times.Once);
}

public static Person FakePerson(
Guid? guid = null,
string? userUrn = null,
string firstname = "Jon",
string lastname = "doe",
string? email = null,
string phone = "07925123123",
List<string>? scopes = null,
Tenant? tenant = null,
List<(Persistence.Organisation, List<string>)>? organisationsWithScope = null
)
{
scopes = scopes ?? [];
var personGuid = guid ?? Guid.NewGuid();
var person = new Person
{
Guid = personGuid,
UserUrn = userUrn ?? $"urn:fdc:gov.uk:2022:{Guid.NewGuid()}",
FirstName = firstname,
LastName = lastname,
Email = email ?? $"jon{personGuid}@example.com",
Phone = phone,
Scopes = scopes
};
if (tenant != null)
{
person.Tenants.Add(tenant);
}

foreach (var organisationWithScope in organisationsWithScope ?? [])
{
person.PersonOrganisations.Add(
new OrganisationPerson
{
Person = person,
Organisation = organisationWithScope.Item1,
Scopes = organisationWithScope.Item2
}
);
}

return person;
}

private static Persistence.Organisation FakeOrganisation(bool? withBuyerInfo = true)
{
Persistence.Organisation org = new()
{
Id = 1,
Guid = Guid.NewGuid(),
Name = "FakeOrg",
Tenant = new Tenant
{
Guid = Guid.NewGuid(),
Name = "Tenant 101"
},
ContactPoints =
[
new Persistence.Organisation.ContactPoint
{
Email = "contact@test.org"
}
],
Type = OrganisationType.Organisation
};

if (withBuyerInfo == true)
{
var devolvedRegulations = new List<DevolvedRegulation>();
devolvedRegulations.Add(DevolvedRegulation.NorthernIreland);

org.BuyerInfo = new Persistence.Organisation.BuyerInformation
{
BuyerType = "FakeBuyerType",
DevolvedRegulations = devolvedRegulations,
};
}

return org;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task Execute_ShouldThrowInvalidOperationException_WhenNoMouSignatur

Func<Task> act = async () => await _useCase.Execute(organisation.Guid);

await act.Should().ThrowAsync<InvalidOperationException>()
await act.Should().ThrowAsync<UnknownMouException>()
.WithMessage($"No MOU signatures found for organisation {organisation.Guid}.");
}

Expand Down
Loading

0 comments on commit 039a936

Please sign in to comment.