Skip to content

Commit

Permalink
Merge pull request #515 from cabinetoffice/feature/DP-433-DataSharing…
Browse files Browse the repository at this point in the history
…-verification-endpoint

Feature/dp 433 data sharing verification endpoint
  • Loading branch information
shilpigoel-goaco authored Aug 28, 2024
2 parents cb17dd2 + 28b63b6 commit 5e37570
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 103 deletions.
109 changes: 97 additions & 12 deletions Services/CO.CDP.DataSharing.WebApi.Tests/EntityFactory.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CO.CDP.DataSharing.WebApi.Extensions;
using CO.CDP.DataSharing.WebApi.Model;
using CO.CDP.OrganisationInformation.Persistence;
using CO.CDP.OrganisationInformation.Persistence.Forms;
Expand All @@ -15,6 +16,15 @@ internal static ShareRequest GetShareRequest(Guid organisationGuid, Guid formId)
};
}

internal static ShareVerificationRequest GetShareVerificationRequest(string formVersionId, string shareCode)
{
return new ShareVerificationRequest
{
FormVersionId = formVersionId,
ShareCode = shareCode
};
}

internal static OrganisationInformation.Persistence.Forms.SharedConsent GetSharedConsent(int organisationId, Guid organisationGuid, Guid formId)
{
var form = new CO.CDP.OrganisationInformation.Persistence.Forms.Form
Expand All @@ -27,28 +37,103 @@ internal static OrganisationInformation.Persistence.Forms.SharedConsent GetShare
Sections = new List<FormSection> { }
};

var orgnisation = new Organisation
{
Id = organisationId,
Guid = organisationGuid,
Name = string.Empty,
Tenant = new Tenant
{
Guid = Guid.NewGuid(),
Name = string.Empty
}
};

return new OrganisationInformation.Persistence.Forms.SharedConsent()
{
Guid = formId,
OrganisationId = organisationId,
Organisation = new Organisation
{
Id = organisationId,
Guid = organisationGuid,
Name = string.Empty,
Tenant = new Tenant
{
Guid = Guid.NewGuid(),
Name = string.Empty
}
},
OrganisationId = orgnisation.Id,
Organisation = orgnisation,
FormId = form.Id,
Form = form,
AnswerSets = new List<FormAnswerSet> { },
SubmissionState = SubmissionState.Draft,
SubmittedAt = DateTime.UtcNow,
FormVersionId = string.Empty,
ShareCode = string.Empty

};
}

internal static List<OrganisationInformation.Persistence.Forms.SharedConsent> GetSharedConsents(int organisationId, Guid organisationGuid, Guid formId)
{
var form = new CO.CDP.OrganisationInformation.Persistence.Forms.Form
{
Guid = formId,
Name = string.Empty,
Version = string.Empty,
IsRequired = default,
Scope = default,
Sections = new List<FormSection> { }
};

var orgnisation = new Organisation
{
Id = organisationId,
Guid = organisationGuid,
Name = string.Empty,
Tenant = new Tenant
{
Guid = Guid.NewGuid(),
Name = string.Empty
}
};

return new List<OrganisationInformation.Persistence.Forms.SharedConsent>
{
new OrganisationInformation.Persistence.Forms.SharedConsent()
{
Guid = Guid.NewGuid(),
OrganisationId = orgnisation.Id,
Organisation = orgnisation,
FormId = form.Id,
Form = form,
AnswerSets = new List<FormAnswerSet> { },
SubmissionState = SubmissionState.Draft,
SubmittedAt = DateTime.UtcNow,
FormVersionId = "V1.0",
ShareCode = ShareCodeExtensions.GenerateShareCode(),
UpdatedOn=DateTime.UtcNow.AddHours(-1)
},
new OrganisationInformation.Persistence.Forms.SharedConsent()
{
Guid = Guid.NewGuid(),
OrganisationId = orgnisation.Id,
Organisation = orgnisation,
FormId = form.Id,
Form = form,
AnswerSets = new List<FormAnswerSet> { },
SubmissionState = SubmissionState.Submitted,
SubmittedAt = DateTime.UtcNow,
FormVersionId = "V1.0",
ShareCode = ShareCodeExtensions.GenerateShareCode(),
UpdatedOn=DateTime.UtcNow.AddHours(-0.5)
}
,
new OrganisationInformation.Persistence.Forms.SharedConsent()
{
Guid = Guid.NewGuid(),
OrganisationId = orgnisation.Id,
Organisation = orgnisation,
FormId = form.Id,
Form = form,
AnswerSets = new List<FormAnswerSet> { },
SubmissionState = SubmissionState.Submitted,
SubmittedAt = DateTime.UtcNow,
FormVersionId = "V1.0",
ShareCode = ShareCodeExtensions.GenerateShareCode(),
UpdatedOn=DateTime.UtcNow
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace CO.CDP.DataSharing.WebApi.Tests.UseCase;

public class GetShareCodeDetailsUseCaseTest(AutoMapperFixture mapperFixture) : IClassFixture<AutoMapperFixture>
{
private readonly Mock<IFormRepository> _formRepository = new();
private GetShareCodeDetailsUseCase UseCase => new(_formRepository.Object, mapperFixture.Mapper);
private readonly Mock<IShareCodeRepository> _shareCodeRepository = new();
private GetShareCodeDetailsUseCase UseCase => new(_shareCodeRepository.Object, mapperFixture.Mapper);

[Fact]
public async Task ItReturnsNullIfNoSharedCodeIsFound()
Expand All @@ -36,7 +36,7 @@ public async Task ItReturnsTheSharedCodeList()
QuestionAnswers=new List<CO.CDP.OrganisationInformation.Persistence.Forms.SharedConsentQuestionAnswer>()
};

_formRepository.Setup(r => r.GetShareCodeDetailsAsync(organisationId, shareCode)).ReturnsAsync(sharedConsentDetails);
_shareCodeRepository.Setup(r => r.GetShareCodeDetailsAsync(organisationId, shareCode)).ReturnsAsync(sharedConsentDetails);

var result = await UseCase.Execute((organisationId, shareCode));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using CO.CDP.DataSharing.WebApi.Model;
using CO.CDP.DataSharing.WebApi.UseCase;
using CO.CDP.DataSharing.WebApi.Tests.AutoMapper;
using CO.CDP.OrganisationInformation.Persistence;
using FluentAssertions;
using Moq;
using CO.CDP.Authentication;
using CO.CDP.DataSharing.WebApi.Extensions;

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

public class GetShareCodeVerifyUseCaseTest() : IClassFixture<AutoMapperFixture>
{
private readonly Mock<IFormRepository> _formRepository = new();
private readonly Mock<IShareCodeRepository> _shareCodeRepository = new();
private GetShareCodeVerifyUseCase UseCase => new(_shareCodeRepository.Object);

[Fact]
public async Task ThrowsSharedConsentNotFoundException_WhenShareCodeRequestedNotFound()
{
var formVersionId = "default";
var shareCode = ShareCodeExtensions.GenerateShareCode();

var shareVerificationRequest = EntityFactory.GetShareVerificationRequest(formVersionId: formVersionId, shareCode: shareCode);

var response = async () => await UseCase.Execute(shareVerificationRequest);

await response.Should().ThrowAsync<SharedConsentNotFoundException>();
}

[Fact]
public async Task ReturnsTrue_WhenShareVerificationRequestForValidExistingShareCodeAndFormVersion()
{
var formVersionId = "V1.0";
var shareCode = ShareCodeExtensions.GenerateShareCode();

var shareVerificationRequest = EntityFactory.GetShareVerificationRequest(formVersionId: formVersionId, shareCode: shareCode);

_shareCodeRepository.Setup(r => r.GetShareCodeVerifyAsync(shareVerificationRequest.FormVersionId, shareVerificationRequest.ShareCode)).ReturnsAsync(true);

var response = await UseCase.Execute(shareVerificationRequest);

response.Should().NotBeNull();

response.As<CO.CDP.DataSharing.WebApi.Model.ShareVerificationReceipt>().ShareCode.Should().Be(shareVerificationRequest.ShareCode);
response.As<CO.CDP.DataSharing.WebApi.Model.ShareVerificationReceipt>().FormVersionId.Should().Be(shareVerificationRequest.FormVersionId);
response.As<CO.CDP.DataSharing.WebApi.Model.ShareVerificationReceipt>().IsLatest.Should().Be(true);
}
}
16 changes: 5 additions & 11 deletions Services/CO.CDP.DataSharing.WebApi/Api/DataSharing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,9 @@ await useCase.Execute(shareRequest)
})
.RequireAuthorization(Constants.OneLoginPolicy);

app.MapPost("/share/data/verify", (ShareVerificationRequest request) => Results.Ok(
new ShareVerificationReceipt
{
ShareCode = request.ShareCode,
FormVersionId = request.FormVersionId,
IsLatest = true
}
)
)
app.MapPost("/share/data/verify", async (ShareVerificationRequest request, IUseCase<ShareVerificationRequest, ShareVerificationReceipt> useCase) =>
await useCase.Execute(request)
.AndThen(shareVerificationReceipt => shareVerificationReceipt != null ? Results.Ok(shareVerificationReceipt) : Results.NotFound()))
.Produces<ShareVerificationReceipt>(StatusCodes.Status200OK, "application/json")
.Produces<ProblemDetails>(StatusCodes.Status401Unauthorized)
.Produces<ProblemDetails>(StatusCodes.Status404NotFound)
Expand All @@ -73,8 +67,8 @@ await useCase.Execute(shareRequest)
{
operation.OperationId = "VerifySharedData";
operation.Description =
"Operation to verify if shared data is the latest version available. ";
operation.Summary = "Create Supplier Submitted Information. ";
"Operation to verify if shared data is the latest version available.";
operation.Summary = "Create Supplier Submitted Information.";
operation.Responses["200"].Description = "Share code and version verification.";
operation.Responses["401"].Description = "Valid authentication credentials are missing in the request.";
operation.Responses["404"].Description = "Share code not found or the caller is not authorised to use it.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace CO.CDP.DataSharing.WebApi.Model;

internal record ShareVerificationReceipt
public record ShareVerificationReceipt
{
/// <example>"20240429"</example>
[Required]
public required string FormVersionId { get; init; }

/// <example>"f53dee53-cf59-4725-9285-44f8960507b9"</example>
/// <example>"f53dee53"</example>
[Required]
public required string ShareCode { get; init; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace CO.CDP.DataSharing.WebApi.Model;

internal record ShareVerificationRequest
public record ShareVerificationRequest
{
/// <example>"f53dee53-cf59-4725-9285-44f8960507b9"</example>
/// <example>"f53dee53"</example>
[Required]
public required string ShareCode { get; init; }

Expand Down
1 change: 1 addition & 0 deletions Services/CO.CDP.DataSharing.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
builder.Services.AddScoped<IUseCase<ShareRequest, ShareReceipt>, GenerateShareCodeUseCase>();
builder.Services.AddScoped<IUseCase<Guid, List<SharedConsent>?>, GetShareCodesUseCase>();
builder.Services.AddScoped<IUseCase<(Guid, string), SharedConsentDetails?>, GetShareCodeDetailsUseCase>();
builder.Services.AddScoped<IUseCase<ShareVerificationRequest, ShareVerificationReceipt>, GetShareCodeVerifyUseCase>();
builder.Services.AddScoped<IUseCase<string, SupplierInformation?>, GetSharedDataUseCase>();
builder.Services.AddDataSharingProblemDetails();
builder.Services.AddJwtBearerAndApiKeyAuthentication(builder.Configuration, builder.Environment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace CO.CDP.DataSharing.WebApi.UseCase;

public class GetShareCodeDetailsUseCase(IFormRepository formRepository, IMapper mapper)
public class GetShareCodeDetailsUseCase(IShareCodeRepository shareCodeRepository, IMapper mapper)
: IUseCase<(Guid organisationId, string shareCode), SharedConsentDetails?>
{
public async Task<SharedConsentDetails?> Execute((Guid organisationId, string shareCode) input)
{
var details = await formRepository.GetShareCodeDetailsAsync(input.organisationId, input.shareCode);
var details = await shareCodeRepository.GetShareCodeDetailsAsync(input.organisationId, input.shareCode);

if (details == null) return null;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using AutoMapper;
using CO.CDP.DataSharing.WebApi.Model;
using CO.CDP.OrganisationInformation.Persistence;

namespace CO.CDP.DataSharing.WebApi.UseCase;

public class GetShareCodeVerifyUseCase(
IShareCodeRepository shareCodeRepository)
: IUseCase<ShareVerificationRequest,
ShareVerificationReceipt>
{
public async Task<ShareVerificationReceipt> Execute(ShareVerificationRequest shareRequest)
{
var details = await shareCodeRepository.GetShareCodeVerifyAsync(shareRequest.FormVersionId, shareRequest.ShareCode);

if (details == null)
{
throw new SharedConsentNotFoundException("Shared Code not found.");
}

return new ShareVerificationReceipt
{
FormVersionId = shareRequest.FormVersionId,
ShareCode = shareRequest.ShareCode,
IsLatest = details.Value
};
}
}
Loading

0 comments on commit 5e37570

Please sign in to comment.