Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DP-875 - Informal Consortium - Add additional organisations #1118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using CO.CDP.Organisation.WebApiClient;
using CO.CDP.OrganisationApp.Pages.Consortium;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Moq;

namespace CO.CDP.OrganisationApp.Tests.Pages.Consortium;

public class ConsortiumConfirmSupplierModelTests
{
private readonly Mock<ITempDataService> _tempDataServiceMock = new();
private readonly Mock<IOrganisationClient> _organisationClientMock = new();
private static readonly Guid _consortiumId = Guid.NewGuid();
private readonly ConsortiumConfirmSupplierModel _model;
public ConsortiumConfirmSupplierModelTests()
{
_model = new ConsortiumConfirmSupplierModel(
_organisationClientMock.Object,
_tempDataServiceMock.Object)
{
Id = Guid.NewGuid()
};
}

[Fact]
public async Task OnGet_ReturnsPage_WhenOrganisationExists()
{
var organisation = GivenOrganisationClientModel();
var sharecode = new ConsortiumSharecode { SharecodeOrganisationName = "Sharecode Org" };

_organisationClientMock.Setup(x => x.GetOrganisationAsync(_model.Id))
.ReturnsAsync(organisation);

_tempDataServiceMock.Setup(x => x.PeekOrDefault<ConsortiumSharecode>(ConsortiumSharecode.TempDataKey))
.Returns(sharecode);

var result = await _model.OnGet();

result.Should().BeOfType<PageResult>();
_model.ConsortiumName.Should().Be("Test Consortium");
_model.Heading.Should().Be($"Add {sharecode.SharecodeOrganisationName} to the consortium?");
}

[Fact]
public async Task OnGet_RedirectsToPageNotFound_WhenOrganisationDoesNotExist()
{
_organisationClientMock.Setup(x => x.GetOrganisationAsync(_model.Id))
.ReturnsAsync((CO.CDP.Organisation.WebApiClient.Organisation?)null);

var result = await _model.OnGet();

result.Should().BeOfType<RedirectResult>().Which.Url.Should().Be("/page-not-found");
}

[Fact]
public async Task OnPost_AddsOrganisationParty_WhenConfirmSupplierIsTrue()
{
var organisation = GivenOrganisationClientModel();
var sharecode = new ConsortiumSharecode
{
OrganisationPartyId = Guid.NewGuid(),
Sharecode = "TestSharecode"
};

_organisationClientMock.Setup(x => x.GetOrganisationAsync(_model.Id))
.ReturnsAsync(organisation);

_tempDataServiceMock.Setup(x => x.PeekOrDefault<ConsortiumSharecode>(ConsortiumSharecode.TempDataKey))
.Returns(sharecode);

_model.ConfirmSupplier = true;

var result = await _model.OnPost();

result.Should().BeOfType<RedirectToPageResult>().Which.PageName.Should().Be("ConsortiumOverview");
_tempDataServiceMock.Verify(x => x.Remove(ConsortiumSharecode.TempDataKey), Times.Never);
_organisationClientMock.Verify(x => x.AddOrganisationPartyAsync(
_model.Id,
It.Is<AddOrganisationParty>(p =>
p.OrganisationPartyId == sharecode.OrganisationPartyId &&
p.ShareCode == sharecode.Sharecode &&
p.OrganisationRelationship == OrganisationRelationship.Consortium
)
), Times.Once);
}

[Fact]
public async Task OnPost_RemovesTempData_WhenConfirmSupplierIsFalse()
{
var organisation = GivenOrganisationClientModel();
var sharecode = new ConsortiumSharecode { SharecodeOrganisationName = "Sharecode Org" };

_organisationClientMock.Setup(x => x.GetOrganisationAsync(_model.Id))
.ReturnsAsync(organisation);

_tempDataServiceMock.Setup(x => x.PeekOrDefault<ConsortiumSharecode>(ConsortiumSharecode.TempDataKey))
.Returns(sharecode);

_model.ConfirmSupplier = false;

var result = await _model.OnPost();

result.Should().BeOfType<RedirectToPageResult>().Which.PageName.Should().Be("ConsortiumOverview");
_tempDataServiceMock.Verify(x => x.Remove(ConsortiumSharecode.TempDataKey), Times.Once);
}

[Fact]
public async Task OnPost_RedirectsToPageNotFound_WhenOrganisationDoesNotExist()
{
_organisationClientMock.Setup(x => x.GetOrganisationAsync(_model.Id))
.ReturnsAsync((CO.CDP.Organisation.WebApiClient.Organisation?)null);

var result = await _model.OnPost();

result.Should().BeOfType<RedirectResult>().Which.Url.Should().Be("/page-not-found");
}

private static CO.CDP.Organisation.WebApiClient.Organisation GivenOrganisationClientModel()
{
return new CO.CDP.Organisation.WebApiClient.Organisation(additionalIdentifiers: null, addresses: null, contactPoint: null, id: _consortiumId, identifier: null, name: "Test Consortium", type: CDP.Organisation.WebApiClient.OrganisationType.InformalConsortium, roles: [], details: new CO.CDP.Organisation.WebApiClient.Details(approval: null, pendingRoles: []));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using CO.CDP.DataSharing.WebApiClient;
using CO.CDP.Organisation.WebApiClient;
using CO.CDP.OrganisationApp.Pages.Consortium;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Moq;

namespace CO.CDP.OrganisationApp.Tests.Pages.Consortium;

public class ConsortiumEnterSharecodeModelTests
{
private readonly Mock<ITempDataService> _tempDataServiceMock = new();
private readonly Mock<IOrganisationClient> _organisationClientMock = new();
private readonly Mock<IDataSharingClient> _dataSharingClientMock = new();
private static readonly Guid _consortiumId = Guid.NewGuid();
private readonly ConsortiumEnterSharecodeModel _model;
public ConsortiumEnterSharecodeModelTests()
{
_model = new ConsortiumEnterSharecodeModel(
_organisationClientMock.Object,
_dataSharingClientMock.Object,
_tempDataServiceMock.Object)
{
Id = Guid.NewGuid()
};
}

[Fact]
public async Task OnGet_ShouldRedirectToPageNotFound_WhenConsortiumIsNull()
{
_organisationClientMock
.Setup(client => client.GetOrganisationAsync(It.IsAny<Guid>()))
.ReturnsAsync((CO.CDP.Organisation.WebApiClient.Organisation?)null);

var result = await _model.OnGet();

result.Should().BeOfType<RedirectResult>()
.Which.Url.Should().Be("/page-not-found");
}

[Fact]
public async Task OnGet_ShouldReturnPage_WhenConsortiumExists()
{
var organisation = GivenOrganisationClientModel();

_organisationClientMock
.Setup(client => client.GetOrganisationAsync(It.IsAny<Guid>()))
.ReturnsAsync(organisation);

var result = await _model.OnGet();

result.Should().BeOfType<PageResult>();
_model.ConsortiumName.Should().Be(organisation.Name);
}

[Fact]
public async Task OnPost_ShouldReturnPage_WhenModelStateIsInvalid()
{
_model.ModelState.AddModelError("EnterSharecode", "Required");

var result = await _model.OnPost();

result.Should().BeOfType<PageResult>();
}

[Fact]
public async Task OnPost_ShouldReturnPage_WhenSharecodeAlreadyExists()
{
var parties = new OrganisationParties(new List<OrganisationParty>
{
new OrganisationParty(Guid.NewGuid(), "Consortium 1", new OrganisationPartyShareCode(DateTimeOffset.Now, "EXISTING_CODE"))
});

_dataSharingClientMock
.Setup(client => client.GetSharedDataAsync(It.IsAny<string>()))
.ReturnsAsync(GetSupplierInfo());

_organisationClientMock
.Setup(client => client.GetOrganisationPartiesAsync(It.IsAny<Guid>()))
.ReturnsAsync(parties);

_model.EnterSharecode = "EXISTING_CODE";

var result = await _model.OnPost();

result.Should().BeOfType<PageResult>();
_model.ModelState.Should().ContainSingle(m => m.Key == nameof(_model.EnterSharecode));
}

[Fact]
public async Task OnPost_ShouldRedirectToConfirmSupplier_WhenSharecodeIsValid()
{
var parties = new OrganisationParties([]);

_dataSharingClientMock
.Setup(client => client.GetSharedDataAsync(It.IsAny<string>()))
.ReturnsAsync(GetSupplierInfo());

_organisationClientMock
.Setup(client => client.GetOrganisationPartiesAsync(It.IsAny<Guid>()))
.ReturnsAsync(parties);

_model.EnterSharecode = "VALID_CODE";

var result = await _model.OnPost();

result.Should().BeOfType<RedirectToPageResult>()
.Which.PageName.Should().Be("ConsortiumConfirmSupplier");
_tempDataServiceMock.Verify(service => service.Put(ConsortiumSharecode.TempDataKey,
It.Is<ConsortiumSharecode>(sc => sc.Sharecode == "VALID_CODE" && sc.SharecodeOrganisationName == "Test Sharecode Organisation")));
}

private static CO.CDP.Organisation.WebApiClient.Organisation GivenOrganisationClientModel()
{
return new CO.CDP.Organisation.WebApiClient.Organisation(additionalIdentifiers: null, addresses: null, contactPoint: null, id: _consortiumId, identifier: null, name: "Test Consortium", type: CDP.Organisation.WebApiClient.OrganisationType.InformalConsortium, roles: [], details: new CO.CDP.Organisation.WebApiClient.Details(approval: null, pendingRoles: []));
}

private static DataSharing.WebApiClient.SupplierInformation GetSupplierInfo()
{
return new DataSharing.WebApiClient.SupplierInformation
(
id: Guid.NewGuid(),
name: "Test Sharecode Organisation",
associatedPersons: [],
additionalParties: [],
additionalEntities: [],
identifier: null!,
additionalIdentifiers: [],
address: new DataSharing.WebApiClient.Address("GB", "UK", "very local", "WS1", "", "1 st", DataSharing.WebApiClient.AddressType.Registered),
contactPoint: null!,
roles: [],
details: null!,
supplierInformationData: null!
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using CO.CDP.Localization;
using CO.CDP.Organisation.WebApiClient;
using CO.CDP.OrganisationApp.Pages.Consortium;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Moq;

namespace CO.CDP.OrganisationApp.Tests.Pages.Consortium;

public class ConsortiumOverviewModelTests
{
private readonly Mock<ITempDataService> _tempDataServiceMock = new();
private readonly Mock<IFlashMessageService> _flashMessageServiceMock = new();
private readonly Mock<IOrganisationClient> _organisationClientMock = new();
private static readonly Guid _consortiumId = Guid.NewGuid();
private readonly ConsortiumOverviewModel _pageModel;
public ConsortiumOverviewModelTests()
{
_pageModel = new ConsortiumOverviewModel(
_organisationClientMock.Object,
_flashMessageServiceMock.Object,
_tempDataServiceMock.Object
)
{
Id = Guid.NewGuid()
};
}

[Fact]
public async Task OnGet_ReturnsPage_WhenOrganisationExists()
{
var organisation = GivenOrganisationClientModel();
var parties = new OrganisationParties(new List<OrganisationParty>
{
new OrganisationParty(Guid.NewGuid(), "Consortium 1", new OrganisationPartyShareCode(DateTimeOffset.Now, "EXISTING_CODE"))
});

var sharecode = new ConsortiumSharecode { SharecodeOrganisationName = "Sharecode Org" };

_organisationClientMock.Setup(x => x.GetOrganisationAsync(_pageModel.Id)).ReturnsAsync(organisation);
_organisationClientMock.Setup(x => x.GetOrganisationPartiesAsync(_pageModel.Id)).ReturnsAsync(parties);
_tempDataServiceMock.Setup(x => x.Get<ConsortiumSharecode>(ConsortiumSharecode.TempDataKey)).Returns(sharecode);

var result = await _pageModel.OnGet();

result.Should().BeOfType<PageResult>();
_pageModel.OrganisationDetails.Should().BeEquivalentTo(organisation);
_pageModel.Parties.Should().BeEquivalentTo(parties);
_flashMessageServiceMock.Verify(x => x.SetFlashMessage
(FlashMessageType.Success,
string.Format(StaticTextResource.Consortium_ConsortiumOverview_Success_Heading, sharecode.SharecodeOrganisationName),
null, null, null, null), Times.Once);
}

[Fact]
public async Task OnGet_ReturnsPage_WhenPartiesNotFound()
{
var organisation = GivenOrganisationClientModel();

_organisationClientMock.Setup(x => x.GetOrganisationAsync(_pageModel.Id))
.ReturnsAsync(organisation);

_organisationClientMock.Setup(x => x.GetOrganisationPartiesAsync(_pageModel.Id))
.ThrowsAsync(new ApiException("Not Found", 404, null, null, null));

var result = await _pageModel.OnGet();

result.Should().BeOfType<PageResult>();
_pageModel.OrganisationDetails.Should().BeEquivalentTo(organisation);
_pageModel.Parties.Should().BeNull();
}

[Fact]
public async Task OnGet_RedirectsToPageNotFound_WhenOrganisationNotFound()
{
_organisationClientMock.Setup(x => x.GetOrganisationAsync(_pageModel.Id))
.ThrowsAsync(new ApiException("Not Found", 404, null, null, null));

var result = await _pageModel.OnGet();

result.Should().BeOfType<RedirectResult>().Which.Url.Should().Be("/page-not-found");
}

[Fact]
public async Task OnGet_SetsFlashMessage_WhenSharecodeExists()
{
var organisation = GivenOrganisationClientModel();
var parties = new OrganisationParties(new List<OrganisationParty>
{
new OrganisationParty(Guid.NewGuid(), "Consortium 1", new OrganisationPartyShareCode(DateTimeOffset.Now, "EXISTING_CODE"))
});
var sharecode = new ConsortiumSharecode { SharecodeOrganisationName = "Sharecode Org" };

_organisationClientMock.Setup(x => x.GetOrganisationAsync(_pageModel.Id))
.ReturnsAsync(organisation);

_organisationClientMock.Setup(x => x.GetOrganisationPartiesAsync(_pageModel.Id))
.ReturnsAsync(parties);

_tempDataServiceMock.Setup(x => x.Get<ConsortiumSharecode>(ConsortiumSharecode.TempDataKey)).Returns(sharecode);

var result = await _pageModel.OnGet();

result.Should().BeOfType<PageResult>();
_flashMessageServiceMock.Verify(x => x.SetFlashMessage(
FlashMessageType.Success,
string.Format(StaticTextResource.Consortium_ConsortiumOverview_Success_Heading, sharecode.SharecodeOrganisationName),
null, null, null, null), Times.Once);
}

private static CO.CDP.Organisation.WebApiClient.Organisation GivenOrganisationClientModel()
{
return new CO.CDP.Organisation.WebApiClient.Organisation(additionalIdentifiers: null, addresses: null, contactPoint: null, id: _consortiumId, identifier: null, name: "Test Consortium", type: CDP.Organisation.WebApiClient.OrganisationType.InformalConsortium, roles: [], details: new CO.CDP.Organisation.WebApiClient.Details(approval: null, pendingRoles: []));
}
}
Loading
Loading