Skip to content

Commit

Permalink
Endpoint for passing in transfer configuration
Browse files Browse the repository at this point in the history
Endpoint provides configuration for clients to set up before initiating a transfer.
  • Loading branch information
TomJKing committed Sep 6, 2024
1 parent 29244f2 commit 165976d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.nationalarchives.tdr.transfer.service.api.controllers

import cats.effect.IO
import org.http4s.HttpRoutes
import sttp.model.StatusCode
import sttp.tapir.json.circe.jsonBody
import sttp.tapir.server.PartialServerEndpoint
Expand Down Expand Up @@ -29,4 +30,6 @@ trait BaseController {
.serverSecurityLogic(
tokenAuthenticator.authenticateUserToken
)

def routes: HttpRoutes[IO]
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ class LoadController(dataLoadInitiation: DataLoadInitiation, dataLoadProcessor:
] =
List(initiateLoadEndpoint.endpoint, completeLoadEndpoint.endpoint)

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

private val configurationEndpoint: PartialServerEndpoint[String, AuthenticatedContext, SourceSystem, BackendException.AuthenticationError, String, Any, IO] =
securedWithBearer
.summary("Configuration for client transfer")
.description("Provides configuration for calling client before starting an operation")
.post
.in("load" / sourceSystem / "configuration")
.out(jsonBody[String])

private val initiateLoadEndpoint: PartialServerEndpoint[String, AuthenticatedContext, SourceSystem, BackendException.AuthenticationError, LoadDetails, Any, IO] =
securedWithBearer
Expand All @@ -39,6 +47,9 @@ class LoadController(dataLoadInitiation: DataLoadInitiation, dataLoadProcessor:
.in("load" / sourceSystem / "complete" / transferId)
.out(jsonBody[String])

val configurationRoute: HttpRoutes[IO] =
Http4sServerInterpreter[IO]().toRoutes(configurationEndpoint.serverLogicSuccess(ac => _ => IO("Hello World")))

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,43 @@ class TransferServiceServerSpec extends ExternalServicesSpec with Matchers {
response.as[String].unsafeRunSync() shouldEqual "Healthy"
}

"'load/sharepoint/configuration' endpoint" should "return 200 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()
.configurationRoute
.orNotFound
.run(
Request(method = Method.POST, uri = uri"/load/sharepoint/configuration", headers = fakeHeaders)
)
.unsafeRunSync()

response.status shouldBe Status.Ok
response.as[String].unsafeRunSync() shouldEqual "\"Hello World\""
}

"'load/sharepoint/configuration' endpoint" should "return 401 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()
.configurationRoute
.orNotFound
.run(
Request(method = Method.POST, uri = uri"/load/sharepoint/configuration", headers = fakeHeaders)
)
.unsafeRunSync()

response.status shouldBe Status.Unauthorized
response.as[Json].unsafeRunSync() shouldEqual invalidTokenExpectedResponse
}

"'load/sharepoint/initiate' endpoint" should "return 200 with correct authorisation header" in {
graphqlOkJson()
val validToken = validUserToken()
Expand Down Expand Up @@ -130,7 +167,7 @@ class TransferServiceServerSpec extends ExternalServicesSpec with Matchers {
response.status shouldBe Status.BadRequest
}

"unknown source system in endpoint endpoint" should "return 400 response with incorrect authorisation header" in {
"unknown source system in 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")
Expand Down

0 comments on commit 165976d

Please sign in to comment.