From 342f20c5d1478ec137c116c3200d87df38555778 Mon Sep 17 00:00:00 2001 From: VimleshGupta Date: Thu, 12 Sep 2024 17:18:37 +0100 Subject: [PATCH] Fix download metadata and update status issues for DTA --- app/auth/TokenSecurity.scala | 4 + .../DownloadMetadataController.scala | 2 +- .../MetadataReviewActionController.scala | 2 +- .../DownloadMetadataControllerSpec.scala | 152 +++++++++--------- .../MetadataReviewActionControllerSpec.scala | 4 +- 5 files changed, 87 insertions(+), 77 deletions(-) diff --git a/app/auth/TokenSecurity.scala b/app/auth/TokenSecurity.scala index c9d0cd19f..64e88ea6a 100644 --- a/app/auth/TokenSecurity.scala +++ b/app/auth/TokenSecurity.scala @@ -60,6 +60,10 @@ trait TokenSecurity extends OidcSecurity with I18nSupport { createResult(action, request, request.token.isTNAUser) } + def standardAndTnaUserAction(consignmentId: UUID)(action: Request[AnyContent] => Future[Result]): Action[AnyContent] = { + validatedAction(consignmentId, "standard", token => token.isStandardUser || token.isTNAUser)(action) + } + private def createResult(action: Request[AnyContent] => Future[Result], request: AuthenticatedRequest[AnyContent], isPermitted: Boolean) = { if (isPermitted) { action(request) diff --git a/app/controllers/DownloadMetadataController.scala b/app/controllers/DownloadMetadataController.scala index 30d076297..f41927e52 100644 --- a/app/controllers/DownloadMetadataController.scala +++ b/app/controllers/DownloadMetadataController.scala @@ -45,7 +45,7 @@ class DownloadMetadataController @Inject() ( } } - def downloadMetadataFile(consignmentId: UUID): Action[AnyContent] = standardUserAndTypeAction(consignmentId) { implicit request: Request[AnyContent] => + def downloadMetadataFile(consignmentId: UUID): Action[AnyContent] = standardAndTnaUserAction(consignmentId) { implicit request: Request[AnyContent] => if (request.token.isTNAUser) logger.info(s"TNA User: ${request.token.userId} downloaded metadata for consignmentId: $consignmentId") for { metadata <- consignmentService.getConsignmentFileMetadata(consignmentId, request.token.bearerAccessToken, None, None) diff --git a/app/controllers/MetadataReviewActionController.scala b/app/controllers/MetadataReviewActionController.scala index 501c829b3..ccff73ed7 100644 --- a/app/controllers/MetadataReviewActionController.scala +++ b/app/controllers/MetadataReviewActionController.scala @@ -142,7 +142,7 @@ object MetadataReviewActionController { (DraftMetadataType.id, InProgressValue.value) ) else Seq.empty - Seq(metadataReviewStatusUpdate) ++ metadataStatusResets + metadataStatusResets :+ metadataReviewStatusUpdate } } diff --git a/test/controllers/DownloadMetadataControllerSpec.scala b/test/controllers/DownloadMetadataControllerSpec.scala index bd9613196..aa1a1471b 100644 --- a/test/controllers/DownloadMetadataControllerSpec.scala +++ b/test/controllers/DownloadMetadataControllerSpec.scala @@ -1,18 +1,16 @@ package controllers +import cats.implicits.catsSyntaxOptionId import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock.{containing, okJson, post, urlEqualTo} import com.typesafe.config.{ConfigFactory, ConfigValue, ConfigValueFactory} -import org.dhatim.fastexcel.reader._ - -import scala.jdk.CollectionConverters._ import configuration.{ApplicationConfig, GraphQLConfiguration} import controllers.util.MetadataProperty._ import graphql.codegen.GetConsignmentFilesMetadata.getConsignmentFilesMetadata.GetConsignment.Files import graphql.codegen.GetConsignmentFilesMetadata.getConsignmentFilesMetadata.GetConsignment.Files.FileMetadata +import graphql.codegen.GetConsignmentFilesMetadata.{getConsignmentFilesMetadata => gcfm} import graphql.codegen.GetCustomMetadata.{customMetadata => cm} import graphql.codegen.GetDisplayProperties.{displayProperties => dp} -import graphql.codegen.GetConsignmentFilesMetadata.{getConsignmentFilesMetadata => gcfm} import graphql.codegen.types.DataType import graphql.codegen.types.DataType.{DateTime, Text} import graphql.codegen.types.PropertyType.Supplied @@ -20,7 +18,8 @@ import io.circe.Printer import io.circe.generic.auto._ import io.circe.syntax.EncoderOps import org.apache.pekko.util.ByteString -import org.mockito.Mockito.when +import org.dhatim.fastexcel.reader._ +import org.scalatest.prop.TableFor1 import play.api.Configuration import play.api.http.HttpVerbs.GET import play.api.http.Status.{FORBIDDEN, FOUND} @@ -35,6 +34,7 @@ import java.time.LocalDateTime import java.time.format.DateTimeFormatter import java.util.UUID import scala.concurrent.ExecutionContext.Implicits.global +import scala.jdk.CollectionConverters._ class DownloadMetadataControllerSpec extends FrontEndTestHelper { @@ -80,72 +80,80 @@ class DownloadMetadataControllerSpec extends FrontEndTestHelper { wiremockServer.stop() } + val userTypeTable: TableFor1[String] = Table( + "userType", + "standard", + "TNA" + ) + "DownloadMetadataController downloadMetadataCsv GET" should { - "download the csv for a multiple properties and rows" in { - val lastModified = LocalDateTime.parse("2021-02-03T10:33:30.414") - val uuid1 = UUID.randomUUID().toString - val uuid2 = UUID.randomUUID().toString - val displayProperties = List( - displayProperty(fileUUID, "UUID"), - displayProperty(fileName, "File Name"), - displayProperty(clientSideOriginalFilepath, "Filepath"), - displayProperty(clientSideFileLastModifiedDate, "Date last modified", DataType.DateTime), - displayProperty(end_date, "Date of the record", DataType.DateTime), - displayProperty(description, "Description") - ) - val customProperties = List( - customMetadata(fileUUID, "UUID"), - customMetadata(fileName, "FileName"), - customMetadata(clientSideOriginalFilepath, "Filepath"), - customMetadata(clientSideFileLastModifiedDate, "Date last modified"), - customMetadata(end_date, ""), - customMetadata(description, "") - ) - val metadataFileOne = List( - FileMetadata(fileUUID, uuid1), - FileMetadata(fileName, "FileName1"), - FileMetadata(clientSideOriginalFilepath, "test/path1"), - FileMetadata(clientSideFileLastModifiedDate, lastModified.format(DateTimeFormatter.ISO_DATE_TIME)), - FileMetadata(end_date, ""), - FileMetadata(description, "") - ) - val metadataFileTwo = List( - FileMetadata(fileUUID, uuid2), - FileMetadata(fileName, "FileName2"), - FileMetadata(clientSideOriginalFilepath, "test/path2"), - FileMetadata(clientSideFileLastModifiedDate, lastModified.format(DateTimeFormatter.ISO_DATE_TIME)), - FileMetadata(end_date, ""), - FileMetadata(description, "") - ) - val files = List( - gcfm.GetConsignment.Files(UUID.randomUUID(), Some("FileName"), metadataFileOne, Nil), - gcfm.GetConsignment.Files(UUID.randomUUID(), Some("FileName"), metadataFileTwo, Nil) - ) + forAll(userTypeTable)(userType => { + s"download the csv for a multiple properties and rows when $userType user" in { + val lastModified = LocalDateTime.parse("2021-02-03T10:33:30.414") + val uuid1 = UUID.randomUUID().toString + val uuid2 = UUID.randomUUID().toString + val displayProperties = List( + displayProperty(fileUUID, "UUID"), + displayProperty(fileName, "File Name"), + displayProperty(clientSideOriginalFilepath, "Filepath"), + displayProperty(clientSideFileLastModifiedDate, "Date last modified", DataType.DateTime), + displayProperty(end_date, "Date of the record", DataType.DateTime), + displayProperty(description, "Description") + ) + val customProperties = List( + customMetadata(fileUUID, "UUID"), + customMetadata(fileName, "FileName"), + customMetadata(clientSideOriginalFilepath, "Filepath"), + customMetadata(clientSideFileLastModifiedDate, "Date last modified"), + customMetadata(end_date, ""), + customMetadata(description, "") + ) + val metadataFileOne = List( + FileMetadata(fileUUID, uuid1), + FileMetadata(fileName, "FileName1"), + FileMetadata(clientSideOriginalFilepath, "test/path1"), + FileMetadata(clientSideFileLastModifiedDate, lastModified.format(DateTimeFormatter.ISO_DATE_TIME)), + FileMetadata(end_date, ""), + FileMetadata(description, "") + ) + val metadataFileTwo = List( + FileMetadata(fileUUID, uuid2), + FileMetadata(fileName, "FileName2"), + FileMetadata(clientSideOriginalFilepath, "test/path2"), + FileMetadata(clientSideFileLastModifiedDate, lastModified.format(DateTimeFormatter.ISO_DATE_TIME)), + FileMetadata(end_date, ""), + FileMetadata(description, "") + ) + val files = List( + gcfm.GetConsignment.Files(UUID.randomUUID(), Some("FileName"), metadataFileOne, Nil), + gcfm.GetConsignment.Files(UUID.randomUUID(), Some("FileName"), metadataFileTwo, Nil) + ) - val wb: ReadableWorkbook = getFileFromController(customProperties, files, displayProperties) - val ws: Sheet = wb.getFirstSheet - val rows: List[Row] = ws.read.asScala.toList + val wb: ReadableWorkbook = getFileFromController(customProperties, files, displayProperties, userType) + val ws: Sheet = wb.getFirstSheet + val rows: List[Row] = ws.read.asScala.toList - rows.length must equal(3) + rows.length must equal(3) - rows.head.getCell(0).asString must equal("Filepath") - rows.head.getCell(1).asString must equal("File Name") - rows.head.getCell(2).asString must equal("Date last modified") - rows.head.getCell(3).asString must equal("Date of the record") - rows.head.getCell(4).asString must equal("Description") - rows.head.getCell(5).asString must equal("UUID") + rows.head.getCell(0).asString must equal("Filepath") + rows.head.getCell(1).asString must equal("File Name") + rows.head.getCell(2).asString must equal("Date last modified") + rows.head.getCell(3).asString must equal("Date of the record") + rows.head.getCell(4).asString must equal("Description") + rows.head.getCell(5).asString must equal("UUID") - rows(1).getCell(0).asString must equal("test/path1") - rows(1).getCell(1).asString must equal("FileName1") - rows(1).getCell(2).asDate.toLocalDate.toString must equal(lastModified.format(DateTimeFormatter.ISO_DATE)) - rows(2).getCell(5).asString must equal(uuid2) + rows(1).getCell(0).asString must equal("test/path1") + rows(1).getCell(1).asString must equal("FileName1") + rows(1).getCell(2).asDate.toLocalDate.toString must equal(lastModified.format(DateTimeFormatter.ISO_DATE)) + rows(2).getCell(5).asString must equal(uuid2) - rows(2).getCell(0).asString must equal("test/path2") - rows(2).getCell(1).asString must equal("FileName2") - rows(2).getCell(2).asDate.toLocalDate.toString must equal(lastModified.format(DateTimeFormatter.ISO_DATE)) - rows(2).getCell(5).asString must equal(uuid2) + rows(2).getCell(0).asString must equal("test/path2") + rows(2).getCell(1).asString must equal("FileName2") + rows(2).getCell(2).asDate.toLocalDate.toString must equal(lastModified.format(DateTimeFormatter.ISO_DATE)) + rows(2).getCell(5).asString must equal(uuid2) - } + } + }) "return forbidden for a judgment user" in { val controller = createController("judgment") @@ -154,13 +162,6 @@ class DownloadMetadataControllerSpec extends FrontEndTestHelper { status(response) must be(FORBIDDEN) } - "return forbidden for a TNA user" in { - val controller = createController(consignmentType = "standard", userType = Some("TNA")) - val consignmentId = UUID.randomUUID() - val response = controller.downloadMetadataFile(consignmentId)(FakeRequest(GET, s"/consignment/$consignmentId/additional-metadata/download-metadata/csv")) - status(response) must be(FORBIDDEN) - } - "return a redirect to login for a logged out user" in { val graphQLConfiguration = new GraphQLConfiguration(app.configuration) val consignmentService = new ConsignmentService(graphQLConfiguration) @@ -252,13 +253,18 @@ class DownloadMetadataControllerSpec extends FrontEndTestHelper { } } - private def getFileFromController(customProperties: List[cm.CustomMetadata], files: List[Files], displayProperties: List[dp.DisplayProperties]): ReadableWorkbook = { + private def getFileFromController( + customProperties: List[cm.CustomMetadata], + files: List[Files], + displayProperties: List[dp.DisplayProperties], + userType: String + ): ReadableWorkbook = { mockFileMetadataResponse(files) mockCustomMetadataResponse(customProperties) mockDisplayPropertiesResponse(displayProperties) val consignmentId = UUID.randomUUID() - val controller = createController("standard") + val controller = createController("standard", userType.some) val response = controller.downloadMetadataFile(consignmentId)(FakeRequest(GET, s"/consignment/$consignmentId/additional-metadata/download-metadata/csv")) val responseByteArray: ByteString = contentAsBytes(response) val bufferedSource = new ByteArrayInputStream(responseByteArray.toArray) diff --git a/test/controllers/MetadataReviewActionControllerSpec.scala b/test/controllers/MetadataReviewActionControllerSpec.scala index d54f33b8f..212e646b3 100644 --- a/test/controllers/MetadataReviewActionControllerSpec.scala +++ b/test/controllers/MetadataReviewActionControllerSpec.scala @@ -255,10 +255,10 @@ class MetadataReviewActionControllerSpec extends FrontEndTestHelper { val result = consignmentStatusUpdates(formData) result mustBe Seq( - (MetadataReviewType.id, CompletedWithIssuesValue.value), (DescriptiveMetadataType.id, InProgressValue.value), (ClosureMetadataType.id, InProgressValue.value), - (DraftMetadataType.id, InProgressValue.value) + (DraftMetadataType.id, InProgressValue.value), + (MetadataReviewType.id, CompletedWithIssuesValue.value) ) }