Skip to content

Commit

Permalink
Merge pull request #799 from cabinetoffice/hotfix/DP-626-fix-permissi…
Browse files Browse the repository at this point in the history
…on-issue-use-lookup

Use identifier and lookup for all join request screens.
  • Loading branch information
rmohammed-goaco authored Oct 22, 2024
2 parents 65212b4 + 52b5f1c commit 25343d4
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public async Task OnPost_WhenModelStateIsValidAndOrganisationAlreadyExists_Shoul
model.HasCompaniesHouseNumber = true;
model.RedirectToSummary = true;
model.CompaniesHouseNumber = "123456";
model.OrganisationId = new Guid();
model.OrganisationIdentifier = "GB-COH:123456789";
model.OrganisationName = "Test company";

GivenRegistrationIsInProgress(model.HasCompaniesHouseNumber, model.CompaniesHouseNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class JoinOrganisationSuccessModelTests
{
private readonly Mock<IOrganisationClient> _organisationClientMock;
private readonly JoinOrganisationSuccessModel _joinOrganisationSuccessModel;
private readonly string _identifier = "GB-COH:123456789";
private readonly Guid _organisationId = Guid.NewGuid();
private readonly Guid _personId = Guid.NewGuid();
private readonly CO.CDP.Organisation.WebApiClient.Organisation _organisation;

public JoinOrganisationSuccessModelTests()
Expand All @@ -25,29 +25,27 @@ public JoinOrganisationSuccessModelTests()
[Fact]
public async Task OnGet_ValidOrganisationId_ReturnsPageResult()
{
var organisationId = Guid.NewGuid();

_organisationClientMock.Setup(client => client.GetOrganisationAsync(organisationId))
_organisationClientMock.Setup(client => client.LookupOrganisationAsync(string.Empty, _identifier))
.ReturnsAsync(_organisation);

var result = await _joinOrganisationSuccessModel.OnGet(organisationId);
var result = await _joinOrganisationSuccessModel.OnGet(_identifier);

result.Should().BeOfType<PageResult>();
_joinOrganisationSuccessModel.OrganisationDetails.Should().Be(_organisation);
_organisationClientMock.Verify(client => client.GetOrganisationAsync(organisationId), Times.Once);
_organisationClientMock.Verify(client => client.LookupOrganisationAsync(string.Empty, _identifier), Times.Once);
}

[Fact]
public async Task OnGet_OrganisationNotFound_ReturnsRedirectToPageNotFound()
{
_organisationClientMock.Setup(client => client.GetOrganisationAsync(_organisationId))
_organisationClientMock.Setup(client => client.LookupOrganisationAsync(string.Empty, _identifier))
.ThrowsAsync(new ApiException("Not Found", 404, "Not Found", null, null));

var result = await _joinOrganisationSuccessModel.OnGet(_organisationId);
var result = await _joinOrganisationSuccessModel.OnGet(_identifier);

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

_organisationClientMock.Verify(client => client.GetOrganisationAsync(_organisationId), Times.Once);
_organisationClientMock.Verify(client => client.LookupOrganisationAsync(string.Empty, _identifier), Times.Once);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class JoinOrganisationModelTests
private readonly Mock<ISession> _sessionMock;
private readonly JoinOrganisationModel _joinOrganisationModel;
private readonly Guid _organisationId = Guid.NewGuid();
private readonly string _identifier = "GB-COH:123456789";
private readonly Guid _personId = Guid.NewGuid();
private readonly CDP.Organisation.WebApiClient.Organisation _organisation;

Expand All @@ -30,69 +31,73 @@ public JoinOrganisationModelTests()
[Fact]
public async Task OnGet_ValidOrganisationId_ReturnsPageResult()
{
_organisationClientMock.Setup(client => client.GetOrganisationAsync(_organisationId))
_organisationClientMock.Setup(client => client.LookupOrganisationAsync(string.Empty, _identifier))
.ReturnsAsync(_organisation);

var result = await _joinOrganisationModel.OnGet(_organisationId);
var result = await _joinOrganisationModel.OnGet(_identifier);

result.Should().BeOfType<PageResult>();
_joinOrganisationModel.OrganisationDetails.Should().Be(_organisation);
_organisationClientMock.Verify(client => client.GetOrganisationAsync(_organisationId), Times.Once);
_organisationClientMock.Verify(client => client.LookupOrganisationAsync(string.Empty, _identifier), Times.Once);
}

[Fact]
public async Task OnGet_OrganisationNotFound_ReturnsRedirectToPageNotFound()
{
_organisationClientMock.Setup(client => client.GetOrganisationAsync(_organisationId))
_organisationClientMock.Setup(client => client.LookupOrganisationAsync(string.Empty, _identifier))
.ThrowsAsync(new ApiException("Not Found", 404, "Not Found", null, null));

var result = await _joinOrganisationModel.OnGet(_organisationId);
var result = await _joinOrganisationModel.OnGet(_identifier);

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

_organisationClientMock.Verify(client => client.GetOrganisationAsync(_organisationId), Times.Once);
_organisationClientMock.Verify(client => client.LookupOrganisationAsync(string.Empty, _identifier), Times.Once);
}

[Fact]
public async Task OnPost_ModelStateInvalid_ReturnsPageResultWithOrganisationDetails()
{
_joinOrganisationModel.ModelState.AddModelError("Join", "Select an option");

_organisationClientMock.Setup(client => client.GetOrganisationAsync(_organisationId))
_organisationClientMock.Setup(client => client.LookupOrganisationAsync(string.Empty, _identifier))
.ReturnsAsync(_organisation);

var result = await _joinOrganisationModel.OnPost(_organisationId);
var result = await _joinOrganisationModel.OnPost(_identifier);

result.Should().BeOfType<PageResult>();
_joinOrganisationModel.OrganisationDetails.Should().Be(_organisation);
_organisationClientMock.Verify(client => client.GetOrganisationAsync(_organisationId), Times.Once);
_organisationClientMock.Verify(client => client.LookupOrganisationAsync(string.Empty, _identifier), Times.Once);
}

[Fact]
public async Task OnPost_UserHasPersonId_JoinIsTrue_CreatesJoinRequestAndRedirectsToSuccessPage()
{
var personId = Guid.NewGuid();
_organisationClientMock.Setup(client => client.LookupOrganisationAsync(string.Empty, _identifier))
.ReturnsAsync(_organisation);

_joinOrganisationModel.Join = true;

var result = await _joinOrganisationModel.OnPost(_organisationId);
var result = await _joinOrganisationModel.OnPost(_identifier);

_organisationClientMock.Verify(client => client.CreateJoinRequestAsync(
_organisationId,
It.Is<CreateOrganisationJoinRequest>(r => r.PersonId == _joinOrganisationModel.UserDetails.PersonId)),
Times.Once);

result.Should().BeOfType<RedirectResult>()
.Which.Url.Should().Be($"/registration/{_organisationId}/join-organisation/success");
.Which.Url.Should().Be($"/registration/{_identifier}/join-organisation/success");
}

[Fact]
public async Task OnPost_UserHasPersonId_JoinIsFalse_RedirectsToCompaniesHouseNumber()
{
var organisationId = Guid.NewGuid();
_organisationClientMock.Setup(client => client.LookupOrganisationAsync(string.Empty, _identifier))
.ReturnsAsync(_organisation);

_joinOrganisationModel.Join = false;

var result = await _joinOrganisationModel.OnPost(organisationId);
var result = await _joinOrganisationModel.OnPost(_identifier);

result.Should().BeOfType<RedirectResult>()
.Which.Url.Should().Be("/registration/has-companies-house-number");
Expand All @@ -103,12 +108,11 @@ public async Task OnPost_UserHasPersonId_JoinIsFalse_RedirectsToCompaniesHouseNu
[Fact]
public async Task OnPost_UserHasNoPersonId_RedirectsToHomePage()
{
var organisationId = Guid.NewGuid();
_joinOrganisationModel.Join = true;
_sessionMock.Setup(s => s.Get<UserDetails>(Session.UserDetailsKey))
.Returns(new UserDetails() { UserUrn = "testUserUrn", PersonId = null});

var result = await _joinOrganisationModel.OnPost(organisationId);
var result = await _joinOrganisationModel.OnPost(_identifier);

result.Should().BeOfType<RedirectResult>()
.Which.Url.Should().Be("/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public class CompanyHouseNumberQuestionModel(ISession session,
[BindProperty]
public string? FailedCompaniesHouseNumber { get; set; }

public Guid? OrganisationId;
public string? OrganisationIdentifier;

public string? OrganisationName;

public string NotificationBannerCompanyNotFound { get { return "We cannot find your company number on Companies House. If it’s correct continue and enter your details manually."; } }
public string NotificationBannerCompanyAlreadyRegistered { get { return "An organisation with this company number already exists. Change the company number or <a class='govuk-notification-banner__link' href='/registration/" + OrganisationId + "/join-organisation'>request to join " + OrganisationName + ".</a>"; } }
public string NotificationBannerCompanyAlreadyRegistered { get { return "An organisation with this company number already exists. Change the company number or <a class='govuk-notification-banner__link' href='/registration/" + OrganisationIdentifier + "/join-organisation'>request to join " + OrganisationName + ".</a>"; } }

public void OnGet()
{
Expand All @@ -57,11 +57,11 @@ public async Task<IActionResult> OnPost()
{
RegistrationDetails.OrganisationIdentificationNumber = CompaniesHouseNumber;
RegistrationDetails.OrganisationScheme = "GB-COH";
OrganisationIdentifier = $"GB-COH:{RegistrationDetails.OrganisationIdentificationNumber}";

try
{
var organisation = await organisationClient.LookupOrganisationAsync(string.Empty, $"GB-COH:{RegistrationDetails.OrganisationIdentificationNumber}");
OrganisationId = organisation?.Id;
var organisation = await organisationClient.LookupOrganisationAsync(string.Empty, OrganisationIdentifier);
OrganisationName = organisation?.Name;
tempDataService.Put(FlashMessageTypes.Important, NotificationBannerCompanyAlreadyRegistered);
return Page();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page "/registration/{organisationId}/join-organisation"
@page "/registration/{identifier}/join-organisation"
@model JoinOrganisationModel

@{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class JoinOrganisationModel(
[Required(ErrorMessage = "Select an option")]
public bool Join { get; set; }

public async Task<IActionResult> OnGet(Guid organisationId)
public async Task<IActionResult> OnGet(string identifier)
{
try
{
OrganisationDetails = await organisationClient.GetOrganisationAsync(organisationId);
OrganisationDetails = await organisationClient.LookupOrganisationAsync(string.Empty, $"{identifier}");
return Page();
}
catch (ApiException ex) when (ex.StatusCode == 404)
Expand All @@ -28,23 +28,24 @@ public async Task<IActionResult> OnGet(Guid organisationId)
}
}

public async Task<IActionResult> OnPost(Guid organisationId)
public async Task<IActionResult> OnPost(string identifier)
{
OrganisationDetails = await organisationClient.LookupOrganisationAsync(string.Empty, $"{identifier}");

if (!ModelState.IsValid)
{
OrganisationDetails = await organisationClient.GetOrganisationAsync(organisationId);
return Page();
}

if (UserDetails.PersonId != null)
if (UserDetails.PersonId != null && OrganisationDetails != null)
{
if (Join == true)
{
await organisationClient.CreateJoinRequestAsync(organisationId, new CreateOrganisationJoinRequest(
await organisationClient.CreateJoinRequestAsync(OrganisationDetails.Id, new CreateOrganisationJoinRequest(
personId: UserDetails.PersonId.Value
));

return Redirect("/registration/" + organisationId + "/join-organisation/success");
return Redirect("/registration/" + identifier + "/join-organisation/success");
}

return Redirect("/registration/has-companies-house-number");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page "/registration/{organisationId}/join-organisation/success"
@page "/registration/{identifier}/join-organisation/success"
@model JoinOrganisationSuccessModel

@{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class JoinOrganisationSuccessModel(
{
public OrganisationWebApiClient.Organisation? OrganisationDetails { get; set; }

public async Task<IActionResult> OnGet(Guid organisationId)
public async Task<IActionResult> OnGet(string identifier)
{
try
{
OrganisationDetails = await organisationClient.GetOrganisationAsync(organisationId);
OrganisationDetails = await organisationClient.LookupOrganisationAsync(string.Empty, $"{identifier}");
return Page();
}
catch (ApiException ex) when (ex.StatusCode == 404)
Expand Down

0 comments on commit 25343d4

Please sign in to comment.