Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into TDRSP-15-initiate-met…
Browse files Browse the repository at this point in the history
…adata-config
  • Loading branch information
TomJKing committed Aug 19, 2024
2 parents 1b0e651 + 013a2c4 commit 4ed4152
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import sttp.tapir.{Endpoint, EndpointInput, auth, endpoint, path, statusCode}
import uk.gov.nationalarchives.tdr.transfer.service.api.auth.{AuthenticatedContext, TokenAuthenticator}
import uk.gov.nationalarchives.tdr.transfer.service.api.errors.BackendException.AuthenticationError
import uk.gov.nationalarchives.tdr.transfer.service.api.model.Serializers._
import uk.gov.nationalarchives.tdr.transfer.service.api.model.SourceSystem.SourceSystemEnum.SourceSystem

import java.util.UUID

trait BaseController {

val sourceSystem: EndpointInput[SourceSystem] = path("sourceSystem")

private val tokenAuthenticator = TokenAuthenticator()

private val securedWithBearerEndpoint: Endpoint[String, Unit, AuthenticationError, Unit, Any] = endpoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,39 @@ import uk.gov.nationalarchives.tdr.transfer.service.api.auth.AuthenticatedContex
import uk.gov.nationalarchives.tdr.transfer.service.api.errors.BackendException
import uk.gov.nationalarchives.tdr.transfer.service.api.model.LoadModel.LoadDetails
import uk.gov.nationalarchives.tdr.transfer.service.api.model.Serializers._
import uk.gov.nationalarchives.tdr.transfer.service.api.model.SourceSystem.SourceSystemEnum.SourceSystem
import uk.gov.nationalarchives.tdr.transfer.service.services.dataload.{DataLoadInitiation, DataLoadProcessor}

import java.util.UUID

class LoadController(dataLoadInitiation: DataLoadInitiation, dataLoadProcessor: DataLoadProcessor) extends BaseController {
def endpoints: List[Endpoint[String, _ >: Unit with UUID, BackendException.AuthenticationError, _ >: LoadDetails with String <: Serializable, Any]] =
def endpoints: List[
Endpoint[String, _ >: SourceSystem with (SourceSystem, UUID) <: Serializable, BackendException.AuthenticationError, _ >: LoadDetails with String <: Serializable, Any]
] =
List(initiateLoadEndpoint.endpoint, completeLoadEndpoint.endpoint)

def routes: HttpRoutes[IO] = initiateLoadRoute <+> completeLoadRoute

private val initiateLoadEndpoint: PartialServerEndpoint[String, AuthenticatedContext, Unit, BackendException.AuthenticationError, LoadDetails, Any, IO] = securedWithBearer
.summary("Initiate the load of records and metadata")
.post
.in("load" / "sharepoint" / "initiate")
.out(jsonBody[LoadDetails])

private val completeLoadEndpoint: PartialServerEndpoint[String, AuthenticatedContext, UUID, BackendException.AuthenticationError, String, Any, IO] = securedWithBearer
.summary("Notify that loading has completed")
.description("Triggers the processing of the transfer's loaded metadata and records in TDR")
.post
.in("load" / "sharepoint" / "complete" / transferId)
.out(jsonBody[String])
private val initiateLoadEndpoint: PartialServerEndpoint[String, AuthenticatedContext, SourceSystem, BackendException.AuthenticationError, LoadDetails, Any, IO] =
securedWithBearer
.summary("Initiate the load of records and metadata")
.post
.in("load" / sourceSystem / "initiate")
.out(jsonBody[LoadDetails])

private val completeLoadEndpoint: PartialServerEndpoint[String, AuthenticatedContext, (SourceSystem, UUID), BackendException.AuthenticationError, String, Any, IO] =
securedWithBearer
.summary("Notify that loading has completed")
.description("Triggers the processing of the transfer's loaded metadata and records in TDR")
.post
.in("load" / sourceSystem / "complete" / transferId)
.out(jsonBody[String])

val initiateLoadRoute: HttpRoutes[IO] =
Http4sServerInterpreter[IO]().toRoutes(initiateLoadEndpoint.serverLogicSuccess(ac => _ => dataLoadInitiation.initiateConsignmentLoad(ac.token)))

val completeLoadRoute: HttpRoutes[IO] =
Http4sServerInterpreter[IO]().toRoutes(completeLoadEndpoint.serverLogicSuccess(ac => ci => dataLoadProcessor.trigger(ci, ac.token)))
Http4sServerInterpreter[IO]().toRoutes(completeLoadEndpoint.serverLogicSuccess(ac => ci => dataLoadProcessor.trigger(ci._2, ac.token)))
}

object LoadController {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package uk.gov.nationalarchives.tdr.transfer.service.api.model

import io.circe.{Decoder, Encoder}
import io.circe.generic.AutoDerivation
import sttp.tapir.Schema
import sttp.tapir.generic.auto.SchemaDerivation
import sttp.tapir.generic.{Configuration => TapirConfiguration}
import uk.gov.nationalarchives.tdr.transfer.service.api.model.SourceSystem.SourceSystemEnum
import uk.gov.nationalarchives.tdr.transfer.service.api.model.SourceSystem.SourceSystemEnum.SourceSystem

object Serializers extends AutoDerivation with SchemaDerivation {
implicit val schemaConfiguration: TapirConfiguration = TapirConfiguration.default.withDiscriminator("type")

implicit val sourceSystemEnc: Encoder[SourceSystem] = Encoder.encodeEnumeration(SourceSystemEnum)
implicit val genderDec: Decoder[SourceSystem] = Decoder.decodeEnumeration(SourceSystemEnum)
implicit val genderSch: Schema[SourceSystem] = Schema.derivedEnumerationValue[SourceSystem]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.gov.nationalarchives.tdr.transfer.service.api.model

object SourceSystem {
object SourceSystemEnum extends Enumeration {
type SourceSystem = Value
val SharePoint: Value = Value("sharepoint")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,39 @@ class TransferServiceServerSpec extends ExternalServicesSpec with Matchers {
response.status shouldBe Status.Unauthorized
response.as[Json].unsafeRunSync() shouldEqual invalidTokenExpectedResponse
}

"unknown source system in endpoint" should "return 400 response with correct authorisation header" in {
graphqlOkJson()
val validToken = validUserToken()
val bearer = CIString("Authorization")
val authHeader = Header.Raw.apply(bearer, s"$validToken")
val fakeHeaders = Headers.apply(authHeader)
val response = LoadController
.apply()
.initiateLoadRoute
.orNotFound
.run(
Request(method = Method.POST, uri = uri"/load/unknown/initiate", headers = fakeHeaders)
)
.unsafeRunSync()

response.status shouldBe Status.BadRequest
}

"unknown source system in endpoint endpoint" should "return 400 response with incorrect authorisation header" in {
val token = invalidToken
val bearer = CIString("Authorization")
val authHeader = Header.Raw.apply(bearer, s"$token")
val fakeHeaders = Headers.apply(authHeader)
val response = LoadController
.apply()
.initiateLoadRoute
.orNotFound
.run(
Request(method = Method.POST, uri = uri"/load/unknown/initiate", headers = fakeHeaders)
)
.unsafeRunSync()

response.status shouldBe Status.BadRequest
}
}

0 comments on commit 4ed4152

Please sign in to comment.