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-1158-Buyers - Change Buyer Type #1267

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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,134 @@
using CO.CDP.Localization;
using CO.CDP.Organisation.WebApiClient;
using CO.CDP.OrganisationApp.Pages.Organisation;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Localization;
using Moq;

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

public class BuyerOrganisationTypeModelTest
{
private readonly Mock<IOrganisationClient> _organisationClientMock;
private readonly BuyerOrganisationTypeModel _model;
private readonly Mock<IStringLocalizer> _stringLocalizerMock;
private readonly Guid _testOrganisationId = Guid.NewGuid();

public BuyerOrganisationTypeModelTest()
{
_organisationClientMock = new Mock<IOrganisationClient>();
_model = new BuyerOrganisationTypeModel(_organisationClientMock.Object)
{
Id = _testOrganisationId
};

_stringLocalizerMock = new Mock<IStringLocalizer>();
}

[Fact]
public void WhenEmptyModelIsPosted_ShouldRaiseValidationErrors()
{
var model = GivenOrganisationTypeModel();

var results = ModelValidationHelper.Validate(model);

results.Count.Should().Be(1);
}

[Fact]
public async Task OnGet_ValidSession_ReturnsOrganisationDetailsAsync()
{
_organisationClientMock.Setup(o => o.GetOrganisationBuyerInformationAsync(_testOrganisationId))
.ReturnsAsync(GivenBuyerInformationClientModel(_testOrganisationId));

await _model.OnGet();

_organisationClientMock.Verify(c => c.GetOrganisationBuyerInformationAsync(_testOrganisationId), Times.Once);
}

[Fact]
public async Task OnGet_WhenOrganisationExists_SetsBuyerOrganisationTypeAndReturnsPage()
{
_organisationClientMock
.Setup(client => client.GetOrganisationBuyerInformationAsync(_testOrganisationId))
.ReturnsAsync(GivenBuyerInformationClientModel(_testOrganisationId));

var result = await _model.OnGet();

_model.BuyerOrganisationType.Should().Be("PublicUndertaking");
result.Should().BeOfType<PageResult>();
}

[Fact]
public async Task OnGet_WhenOrganisationDoesNotExist_RedirectsToPageNotFound()
{
_organisationClientMock
.Setup(client => client.GetOrganisationBuyerInformationAsync(_testOrganisationId))
.ReturnsAsync((BuyerInformation?)null);

var result = await _model.OnGet();

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

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

var result = await _model.OnPost();

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

[Fact]
public async Task OnPost_WhenOrganisationDoesNotExist_RedirectsToPageNotFound()
{
_organisationClientMock
.Setup(client => client.GetOrganisationBuyerInformationAsync(_testOrganisationId))
.ReturnsAsync((BuyerInformation?)null);

var result = await _model.OnPost();

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

[Fact]
public async Task OnPost_WhenUpdateIsSuccessful_RedirectsToOrganisationOverview()
{
_organisationClientMock
.Setup(client => client.GetOrganisationBuyerInformationAsync(_testOrganisationId))
.ReturnsAsync(GivenBuyerInformationClientModel(_testOrganisationId));

var result = await _model.OnPost();
result.Should().NotBeNull();

result.Should().BeOfType<RedirectToPageResult>()
.Which.PageName.Should().Be("OrganisationOverview");
}


private static CO.CDP.Organisation.WebApiClient.Organisation GivenOrganisationClientModel(Guid? id)
{
return new CO.CDP.Organisation.WebApiClient.Organisation(additionalIdentifiers: null, addresses: null, contactPoint: new ContactPoint("Main Contact", "[email protected]", "123456789", null), id: id ?? Guid.NewGuid(), identifier: null, name: null, type: OrganisationType.Organisation, roles: [], details: new Details(approval: null, buyerInformation: null, pendingRoles: [], publicServiceMissionOrganization: null, scale: null, shelteredWorkshop: null, vcse: null));
}

private static CO.CDP.Organisation.WebApiClient.BuyerInformation GivenBuyerInformationClientModel(Guid? id)
{
return new CO.CDP.Organisation.WebApiClient.BuyerInformation(
buyerType: "PublicUndertaking",
devolvedRegulations: [DevolvedRegulation.NorthernIreland, DevolvedRegulation.Wales]);
}

private BuyerOrganisationTypeModel GivenOrganisationTypeModel()
{
return new BuyerOrganisationTypeModel(_organisationClientMock.Object);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
@page "/organisation/{id}/buyerorganisationtype"
@model CO.CDP.OrganisationApp.Pages.Organisation.BuyerOrganisationTypeModel
@using CO.CDP.Localization

@{
ViewData["Title"] = @StaticTextResource.OrganisationRegistration_BuyerOrganisationType_Heading;
var organisationTypeHasError = ((TagBuilder)Html.ValidationMessageFor(m => m.BuyerOrganisationType)).HasInnerHtml;
var otherHasError = ((TagBuilder)Html.ValidationMessageFor(m => m.OtherValue)).HasInnerHtml;

string IsSelected(string? radioValue)
{
return Model.BuyerOrganisationType == radioValue ? "checked='checked'" : string.Empty;
}
}

@section BeforeContent {
<a href="/organisation/@Model.Id" class="govuk-back-link">
@StaticTextResource.Global_Back
</a>
}

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds govuk-body">
<partial name="_ErrorSummary" model="@ModelState" />
<form method="post">
@if (Request.Query.ContainsKey("frm-summary"))
{
<input type="hidden" name="RedirectToSummary" value="true" />
}
<div class="govuk-form-group @(organisationTypeHasError ? "govuk-form-group--error" : "")">
<fieldset class="govuk-fieldset">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--l">
<h1 class="govuk-fieldset__heading">
@ViewData["Title"]
</h1>
</legend>
<p class="govuk-body">@Html.Raw(@StaticTextResource.OrganisationRegistration_BuyerOrganisationType_Hint)</p>

@if (organisationTypeHasError)
{
<p id="organisationType-error" class="govuk-error-message">
<span class="govuk-visually-hidden">@StaticTextResource.Global_Error:</span> @Html.ValidationMessageFor(m => m.BuyerOrganisationType)
</p>
}

<div class="govuk-radios" data-module="govuk-radios">
<div class="govuk-radios__item">
<input class="govuk-radios__input" id="ca-type" name="BuyerOrganisationType"
type="radio" value="CentralGovernment"
aria-describedby="ca-type-item-hint" @IsSelected("CentralGovernment")>
<label class="govuk-label govuk-radios__label" for="ca-type">
@BuyerOrganisationTypeModel.BuyerTypes["CentralGovernment"]
</label>
<div id="ca-type-item-hint" class="govuk-hint govuk-radios__hint">
@Html.Raw(StaticTextResource.OrganisationRegistration_BuyerOrganisationType_CentralGovernment_Hint)
</div>
</div>

<div class="govuk-radios__item">
<input class="govuk-radios__input" id="ca-type-2" name="BuyerOrganisationType"
type="radio" value="RegionalAndLocalGovernment"
aria-describedby="ca-type-2-item-hint" @IsSelected("RegionalAndLocalGovernment")>
<label class="govuk-label govuk-radios__label" for="ca-type-2">
@BuyerOrganisationTypeModel.BuyerTypes["RegionalAndLocalGovernment"]
</label>
<div id="ca-type-2-item-hint" class="govuk-hint govuk-radios__hint">
@Html.Raw(StaticTextResource.OrganisationRegistration_BuyerOrganisationType_RegionalAndLocalGovernment_Hint)
</div>
</div>

<div class="govuk-radios__item">
<input class="govuk-radios__input" id="ca-type-3" name="BuyerOrganisationType"
type="radio" value="PublicUndertaking" aria-describedby="ca-type-3-item-hint" @IsSelected("PublicUndertaking")>
<label class="govuk-label govuk-radios__label" for="ca-type-3">
@BuyerOrganisationTypeModel.BuyerTypes["PublicUndertaking"]
</label>
<div id="ca-type-3-item-hint" class="govuk-hint govuk-radios__hint">
@Html.Raw(StaticTextResource.OrganisationRegistration_BuyerOrganisationType_PublicUndertaking_Hint)
</div>
</div>
<div class="govuk-radios__item">
<input class="govuk-radios__input" id="ca-type-4" name="BuyerOrganisationType"
type="radio" value="PrivateUtility" aria-describedby="ca-type-4-item-hint" @IsSelected("PrivateUtility")>
<label class="govuk-label govuk-radios__label" for="ca-type-4">
@BuyerOrganisationTypeModel.BuyerTypes["PrivateUtility"]
</label>
<div id="ca-type-4-item-hint" class="govuk-hint govuk-radios__hint">
@Html.Raw(StaticTextResource.OrganisationRegistration_BuyerOrganisationType_PrivateUtility_Hint)
</div>
</div>
<div class="govuk-radios__item">
<input class="govuk-radios__input" id="ca-type-5" name="BuyerOrganisationType"
type="radio" value="Other" data-aria-controls="conditional-ca-type-5" @IsSelected("Other")>
<label class="govuk-label govuk-radios__label" for="ca-type-5">
@StaticTextResource.OrganisationRegistration_BuyerOrganisationType_Other_Label
</label>
</div>

<div class="govuk-radios__conditional govuk-radios__conditional--hidden" id="conditional-ca-type-5">
<div class="govuk-form-group @(otherHasError ? "govuk-form-group--error" : "")">
<label class="govuk-label" for="other-buyer-type">
@StaticTextResource.OrganisationRegistration_BuyerOrganisationType_OtherEnterType_Label
</label>
@Html.ValidationMessageFor(m => m.OtherValue, "", new { @class = "govuk-error-message", @id = "other-buyer-type-error" })

<div id="ca-type-5-item-hint" class="govuk-hint">
@StaticTextResource.OrganisationRegistration_BuyerOrganisationType_Other_Hint
</div>

<input autocomplete="off" class="govuk-input @(otherHasError ? "govuk-input--error" : "")"
id="OtherValue" name="OtherValue" type="text" value="@(Model.BuyerOrganisationType == "Other" ? Model.OtherValue : "")">
</div>
</div>
</div>
</fieldset>
</div>
<govuk-button>
@StaticTextResource.Global_Continue
</govuk-button>
</form>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using CO.CDP.Localization;
using CO.CDP.Mvc.Validation;
using CO.CDP.Organisation.WebApiClient;
using CO.CDP.OrganisationApp.Constants;
using CO.CDP.OrganisationApp.Models;
using CO.CDP.OrganisationApp.WebApiClients;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using OrganisationWebApiClient = CO.CDP.Organisation.WebApiClient;

namespace CO.CDP.OrganisationApp.Pages.Organisation;

[Authorize(Policy = OrgScopeRequirement.Editor)]
public class BuyerOrganisationTypeModel(IOrganisationClient organisationClient) : PageModel
{
[BindProperty(SupportsGet = true)]
public Guid Id { get; set; }

[BindProperty]
[Required(ErrorMessage = nameof(StaticTextResource.OrganisationRegistration_BuyerOrganisationType_ValidationErrorMessage))]
public string? BuyerOrganisationType { get; set; }

[BindProperty]
[DisplayName(nameof(StaticTextResource.OrganisationRegistration_BuyerOrganisationType_OtherEnterType_Label))]
[RequiredIf("BuyerOrganisationType", "Other")]
public string? OtherValue { get; set; }

public async Task<IActionResult> OnGet()
{
try
{
var buyerInformation = await organisationClient.GetOrganisationBuyerInformationAsync(Id);
if (buyerInformation == null) return Redirect("/page-not-found");

BuyerOrganisationType = buyerInformation.BuyerType;
if (!string.IsNullOrEmpty(BuyerOrganisationType) && !BuyerTypes.Keys.Contains(BuyerOrganisationType))
{
OtherValue = BuyerOrganisationType;
BuyerOrganisationType = "Other";
}

return Page();
}
catch (ApiException ex) when (ex.StatusCode == 404)
{
return Redirect("/page-not-found");
}
}

public async Task<IActionResult> OnPost()
{
if (!ModelState.IsValid)
{
return Page();
}

var buyerInformation = await organisationClient.GetOrganisationBuyerInformationAsync(Id);
if (buyerInformation == null) return Redirect("/page-not-found");

try
{
var buyerOrganisationType = (BuyerOrganisationType == "Other" ? OtherValue : BuyerOrganisationType);

await organisationClient.UpdateBuyerOrganisationType(Id, buyerOrganisationType: buyerOrganisationType!);
}
catch (ApiException<OrganisationWebApiClient.ProblemDetails> aex)
{
ApiExceptionMapper.MapApiExceptions(aex, ModelState);
return Page();
}
catch (CO.CDP.Organisation.WebApiClient.ApiException ex) when (ex.StatusCode == 404)
{
return Redirect("/page-not-found");
}
return RedirectToPage("OrganisationOverview", new { Id });
}

public static Dictionary<string, string> BuyerTypes => new()
{
{ "CentralGovernment", StaticTextResource.OrganisationRegistration_BuyerOrganisationType_CentralGovernment_Label},
{ "RegionalAndLocalGovernment", StaticTextResource.OrganisationRegistration_BuyerOrganisationType_RegionalAndLocalGovernment_Label},
{ "PublicUndertaking", StaticTextResource.OrganisationRegistration_BuyerOrganisationType_PublicUndertaking_Label},
{ "PrivateUtility", StaticTextResource.OrganisationRegistration_BuyerOrganisationType_PrivateUtility_Label}
};
}
Loading