Skip to content

Commit

Permalink
Merge pull request #3764 from nationalarchives/TDR-2859-File-Check-Va…
Browse files Browse the repository at this point in the history
…lidation

Tdr 2859 file check validation
  • Loading branch information
Tom-Hallett authored Mar 12, 2024
2 parents 9152ef8 + 68803b5 commit 34b1555
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 22 deletions.
43 changes: 29 additions & 14 deletions app/controllers/ConfirmTransferController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import play.api.data.Form
import play.api.data.Forms.{boolean, mapping}
import play.api.i18n.{I18nSupport, Lang, Langs}
import play.api.mvc._
import services.Statuses.{CompletedValue, ExportType, FailedValue, InProgressValue}
import services.{ConfirmTransferService, ConsignmentExportService, ConsignmentService, ConsignmentStatusService}
import play.api.mvc.Results._
import services.Statuses.{ClientChecksType, CompletedValue, ExportType, FailedValue, InProgressValue, SeriesType, TransferAgreementType, UploadType}
import services.{ConfirmTransferService, ConsignmentExportService, ConsignmentService, ConsignmentStatusService, Statuses}
import viewsapi.Caching.preventCaching

import java.util.UUID
Expand Down Expand Up @@ -49,19 +50,33 @@ class ConfirmTransferController @Inject() (
implicit request: Request[AnyContent]
): Future[Result] = {
consignmentStatusService.getConsignmentStatuses(consignmentId, request.token.bearerAccessToken).flatMap { consignmentStatuses =>
val exportTransferStatus = consignmentStatusService.getStatusValues(consignmentStatuses, ExportType).values.headOption.flatten
exportTransferStatus match {
case Some(InProgressValue.value) | Some(CompletedValue.value) | Some(FailedValue.value) =>
consignmentService.getConsignmentRef(consignmentId, request.token.bearerAccessToken).map { consignmentRef =>
Ok(views.html.transferAlreadyCompleted(consignmentId, consignmentRef, request.token.name)).uncache()
}
case None =>
getConsignmentSummary(request, consignmentId).map { consignmentSummary =>
httpStatus(views.html.standard.confirmTransfer(consignmentId, consignmentSummary, finalTransferForm, request.token.name)).uncache()
val consignmentStatusValues: Map[Statuses.StatusType, Option[String]] =
consignmentStatusService.getStatusValues(consignmentStatuses, SeriesType, TransferAgreementType, UploadType, ClientChecksType, ExportType)
val exportTransferStatus = consignmentStatusValues.get(ExportType).headOption.flatten
val incompleteStatuses = consignmentStatusValues.filter { case (_, statusValue) => !statusValue.contains(CompletedValue.value) }

Seq(SeriesType, TransferAgreementType, UploadType, ClientChecksType)
.find(statusType => incompleteStatuses.contains(statusType))
.map {
case SeriesType => Future(Redirect(routes.SeriesDetailsController.seriesDetails(consignmentId)))
case TransferAgreementType => Future(Redirect(routes.TransferAgreementPart1Controller.transferAgreement(consignmentId)))
case UploadType => Future(Redirect(routes.UploadController.uploadPage(consignmentId)))
case ClientChecksType => Future(Redirect(routes.FileChecksController.fileChecksPage(consignmentId, Some("false"))))
}
.getOrElse(
exportTransferStatus match {
case Some(InProgressValue.value) | Some(CompletedValue.value) | Some(FailedValue.value) =>
consignmentService.getConsignmentRef(consignmentId, request.token.bearerAccessToken).map { consignmentRef =>
Ok(views.html.transferAlreadyCompleted(consignmentId, consignmentRef, request.token.name)).uncache()
}
case None =>
getConsignmentSummary(request, consignmentId).map { consignmentSummary =>
httpStatus(views.html.standard.confirmTransfer(consignmentId, consignmentSummary, finalTransferForm, request.token.name)).uncache()
}
case _ =>
throw new IllegalStateException(s"Unexpected Export status: $exportTransferStatus for consignment $consignmentId")
}
case _ =>
throw new IllegalStateException(s"Unexpected Export status: $exportTransferStatus for consignment $consignmentId")
}
)
}
}

Expand Down
129 changes: 121 additions & 8 deletions test/controllers/ConfirmTransferControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper {
}

"ConfirmTransferController GET" should {
"render the confirm transfer page with an authenticated user" in {
"render the series page with an authenticated user if series status is not 'Completed'" in {
val consignmentId = UUID.randomUUID()

val client = new GraphQLConfiguration(app.configuration).getClient[gcs.Data, gcs.Variables]()
val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents)
setConsignmentStatusResponse(app.configuration, wiremockServer)
Expand All @@ -76,6 +78,102 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper {
val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, ""))
mockGraphqlConsignmentSummaryResponse(dataString)

val confirmTransferPage = controller
.confirmTransfer(consignmentId)
.apply(FakeRequest(GET, s"/consignment/$consignmentId/confirm-transfer").withCSRFToken)

playStatus(confirmTransferPage) mustBe SEE_OTHER
redirectLocation(confirmTransferPage).get must equal(s"/consignment/$consignmentId/series")
}

"redirect to the transfer agreement page with an authenticated user if the transfer agreement for that consignment has not been signed or agreed to" in {
val consignmentId = UUID.randomUUID()

val client = new GraphQLConfiguration(app.configuration).getClient[gcs.Data, gcs.Variables]()
val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents)
val consignmentStatuses = List(
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None)
)
setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses)

val consignmentSummaryResponse: gcs.GetConsignment = getConsignmentSummaryResponse
val data: client.GraphqlData = client.GraphqlData(Some(gcs.Data(Some(consignmentSummaryResponse))), List())
val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, ""))
mockGraphqlConsignmentSummaryResponse(dataString)

val confirmTransferPage = controller
.confirmTransfer(consignmentId)
.apply(FakeRequest(GET, s"/consignment/$consignmentId/confirm-transfer").withCSRFToken)

playStatus(confirmTransferPage) mustBe SEE_OTHER
redirectLocation(confirmTransferPage).get must equal(s"/consignment/$consignmentId/transfer-agreement")
}

"redirect to the upload page with an authenticated user if the upload status is not 'Completed'" in {
val consignmentId = UUID.randomUUID()

val client = new GraphQLConfiguration(app.configuration).getClient[gcs.Data, gcs.Variables]()
val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents)
val consignmentStatuses = List(
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "TransferAgreement", "Completed", someDateTime, None)
)
setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses)

val consignmentSummaryResponse: gcs.GetConsignment = getConsignmentSummaryResponse
val data: client.GraphqlData = client.GraphqlData(Some(gcs.Data(Some(consignmentSummaryResponse))), List())
val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, ""))
mockGraphqlConsignmentSummaryResponse(dataString)

val confirmTransferPage = controller
.confirmTransfer(consignmentId)
.apply(FakeRequest(GET, s"/consignment/$consignmentId/confirm-transfer").withCSRFToken)

playStatus(confirmTransferPage) mustBe SEE_OTHER
redirectLocation(confirmTransferPage).get must equal(s"/consignment/$consignmentId/upload")
}

"redirect to the file checks page with an authenticated user if the file checks status is not 'Completed'" in {
val consignmentId = UUID.randomUUID()

val client = new GraphQLConfiguration(app.configuration).getClient[gcs.Data, gcs.Variables]()
val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents)
val consignmentStatuses = List(
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "TransferAgreement", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Upload", "Completed", someDateTime, None)
)
setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses)

val consignmentSummaryResponse: gcs.GetConsignment = getConsignmentSummaryResponse
val data: client.GraphqlData = client.GraphqlData(Some(gcs.Data(Some(consignmentSummaryResponse))), List())
val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, ""))
mockGraphqlConsignmentSummaryResponse(dataString)

val confirmTransferPage = controller
.confirmTransfer(consignmentId)
.apply(FakeRequest(GET, s"/consignment/$consignmentId/confirm-transfer").withCSRFToken)

playStatus(confirmTransferPage) mustBe SEE_OTHER
redirectLocation(confirmTransferPage).get must equal(s"/consignment/$consignmentId/file-checks?uploadFailed=false")
}

"render the confirm transfer page with an authenticated user when all relevant statuses are 'Completed'" in {
val client = new GraphQLConfiguration(app.configuration).getClient[gcs.Data, gcs.Variables]()
val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents)
val consignmentStatuses = List(
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "TransferAgreement", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Upload", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "ClientChecks", "Completed", someDateTime, None)
)
setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses)

val consignmentSummaryResponse: gcs.GetConsignment = getConsignmentSummaryResponse
val data: client.GraphqlData = client.GraphqlData(Some(gcs.Data(Some(consignmentSummaryResponse))), List())
val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, ""))
mockGraphqlConsignmentSummaryResponse(dataString)

val confirmTransferPage = controller
.confirmTransfer(consignmentId)
.apply(FakeRequest(GET, s"/consignment/$consignmentId/confirm-transfer").withCSRFToken)
Expand Down Expand Up @@ -211,7 +309,13 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper {
val data: client.GraphqlData = client.GraphqlData(Some(gcs.Data(Some(consignmentSummaryResponse))), List())
val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, ""))
mockGraphqlConsignmentSummaryResponse(dataString)
setConsignmentStatusResponse(app.configuration, wiremockServer)
val consignmentStatuses = List(
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "TransferAgreement", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Upload", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "ClientChecks", "Completed", someDateTime, None)
)
setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses)

val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents)
val finalTransferConfirmationSubmitResult = controller
Expand Down Expand Up @@ -549,8 +653,8 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper {
forAll(consignmentStatuses) { consignmentStatus =>
s"render the confirm transfer 'already confirmed' page with an authenticated $userType user if export status is '$consignmentStatus'" in {
val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents)
val consignmentStatuses = List(ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Export", consignmentStatus, someDateTime, None))
setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses)

setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = getMockConsignmentStatuses(consignmentStatus))
setConsignmentReferenceResponse(wiremockServer)
setConsignmentTypeResponse(wiremockServer, "standard")

Expand All @@ -569,8 +673,8 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper {
s"render the confirm transfer 'already confirmed' page with an authenticated user if the $userType user navigates back to the " +
s"confirmTransfer after previously successfully submitting the transfer and the export status is '$consignmentStatus'" in {
val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents)
val consignmentStatuses = List(ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Export", consignmentStatus, someDateTime, None))
setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses)

setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = getMockConsignmentStatuses(consignmentStatus))
setConsignmentReferenceResponse(wiremockServer)
setConsignmentTypeResponse(wiremockServer, "standard")

Expand All @@ -590,8 +694,7 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper {
s"confirmTransfer after previously submitting an incorrect form and the export status is '$consignmentStatus'" in {
val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents)
val incompleteTransferConfirmationForm = Seq()
val consignmentStatuses = List(ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Export", consignmentStatus, someDateTime, None))
setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses)
setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = getMockConsignmentStatuses(consignmentStatus))
setConsignmentReferenceResponse(wiremockServer)
setConsignmentTypeResponse(wiremockServer, "standard")

Expand Down Expand Up @@ -683,6 +786,16 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper {
new gcs.GetConsignment(seriesCode, transferringBodyName, totalFiles, consignmentReference)
}

private def getMockConsignmentStatuses(exportStatus: String): List[ConsignmentStatuses] = {
List(
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "TransferAgreement", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Upload", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "ClientChecks", "Completed", someDateTime, None),
ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Export", exportStatus, someDateTime, None)
)
}

private def createFinalTransferConfirmationResponse = new aftc.AddFinalTransferConfirmation(
consignmentId,
legalCustodyTransferConfirmed = true
Expand Down

0 comments on commit 34b1555

Please sign in to comment.