Skip to content

Commit

Permalink
Merge pull request #4151 from nationalarchives/TDRD-425-fix-metadata-…
Browse files Browse the repository at this point in the history
…review-issues

Fix download metadata and update status issues for DTA
  • Loading branch information
vimleshtna authored Sep 13, 2024
2 parents c076abd + cb82afa commit a760440
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 77 deletions.
4 changes: 4 additions & 0 deletions app/auth/TokenSecurity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/DownloadMetadataController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/MetadataReviewActionController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ object MetadataReviewActionController {
(DraftMetadataType.id, InProgressValue.value)
)
else Seq.empty
Seq(metadataReviewStatusUpdate) ++ metadataStatusResets
metadataStatusResets :+ metadataReviewStatusUpdate
}
}

Expand Down
152 changes: 79 additions & 73 deletions test/controllers/DownloadMetadataControllerSpec.scala
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
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
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}
Expand All @@ -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 {

Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/MetadataReviewActionControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}

Expand Down

0 comments on commit a760440

Please sign in to comment.