Skip to content

Commit

Permalink
test: Use JwtService to generate a jwt in SipiIT (#2756)
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Jul 20, 2023
1 parent fd1ce2b commit 70037b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
24 changes: 15 additions & 9 deletions webapi/src/it/scala/org/knora/sipi/SipiIT.scala
Expand Up @@ -20,16 +20,19 @@ import zio.http.model.Status
import zio.json.DecoderOps
import zio.json.ast.Json
import zio.test._

import scala.util.Failure
import scala.util.Success
import scala.util.Try

import org.knora.sipi.MockDspApiServer.verify._
import org.knora.webapi.messages.admin.responder.KnoraResponseADM
import org.knora.webapi.messages.admin.responder.sipimessages._
import org.knora.webapi.testcontainers.SipiTestContainer
import com.github.tomakehurst.wiremock.client.WireMock.equalTo
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder.newRequestPattern
import org.knora.webapi.config.AppConfig
import org.knora.webapi.messages.util.KnoraSystemInstances.Users.SystemUser
import org.knora.webapi.routing.{JwtService, JwtServiceLive}

object SipiIT extends ZIOSpecDefault {

Expand All @@ -44,9 +47,10 @@ object SipiIT extends ZIOSpecDefault {
private def getWithoutAuthorization(path: String) =
SipiTestContainer.resolveUrl(path).map(Request.get).flatMap(Client.request(_))

// expires May 2033
private val jwt =
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIwLjAuMC4wOjMzMzMiLCJzdWIiOiJodHRwOi8vcmRmaC5jaC91c2Vycy9yb290IiwiYXVkIjpbIktub3JhIiwiU2lwaSJdLCJleHAiOjIwMDAwMDAwMDAsImlhdCI6MTY4NzE2NDUzOSwianRpIjoiSG9SSFg5V1lSZHV6VnVmTXZFT1c4USJ9.4RO0MdoKAlm4_xa9PKU86BdU2cX9hUBxDnc2VQRgjwM"
private val getToken = JwtService
.createJwt(SystemUser)
.map(_.jwtString)
.provide(JwtServiceLive.layer, AppConfig.layer)

private val cookiesSuite =
suite("Given a request is authorized using cookies")(
Expand All @@ -57,8 +61,9 @@ object SipiIT extends ZIOSpecDefault {
"and responds with Ok"
) {
for {
_ <- copyTestFilesToSipi
_ <- MockDspApiServer.resetAndAllowWithPermissionCode(prefix, imageTestfile, 2)
jwt <- getToken
_ <- copyTestFilesToSipi
_ <- MockDspApiServer.resetAndAllowWithPermissionCode(prefix, imageTestfile, 2)
response <-
SipiTestContainer
.resolveUrl(s"/$prefix/$imageTestfile/file")
Expand All @@ -80,8 +85,9 @@ object SipiIT extends ZIOSpecDefault {
"and responds with Ok"
) {
for {
_ <- copyTestFilesToSipi
_ <- MockDspApiServer.resetAndAllowWithPermissionCode(prefix, imageTestfile, 2)
jwt <- getToken
_ <- copyTestFilesToSipi
_ <- MockDspApiServer.resetAndAllowWithPermissionCode(prefix, imageTestfile, 2)
response <-
SipiTestContainer
.resolveUrl(s"/$prefix/$imageTestfile/file")
Expand All @@ -90,7 +96,7 @@ object SipiIT extends ZIOSpecDefault {
requestToDspApiContainsJwt <- MockDspApiServer.verifyAuthBearerTokenReceived(jwt)
} yield assertTrue(response.status == Status.Ok, requestToDspApiContainsJwt)
}
)
) @@ TestAspect.withLiveClock

private val knoraJsonEndpointSuite =
suite("Endpoint /{prefix}/{identifier}/knora.json")(
Expand Down
Expand Up @@ -794,8 +794,7 @@ trait JwtService {

final case class JwtServiceLive(
private val jwtConfig: JwtConfig,
private val dspIngestConfig: DspIngestConfig,
private val stringFormatter: StringFormatter
private val dspIngestConfig: DspIngestConfig
) extends JwtService {
private val algorithm: JwtAlgorithm = JwtAlgorithm.HS256
private val header: String = """{"typ":"JWT","alg":"HS256"}"""
Expand Down Expand Up @@ -891,6 +890,6 @@ final case class JwtServiceLive(
}
}
object JwtServiceLive {
val layer: URLayer[DspIngestConfig with JwtConfig with StringFormatter, JwtServiceLive] =
val layer: URLayer[DspIngestConfig with JwtConfig, JwtServiceLive] =
ZLayer.fromFunction(JwtServiceLive.apply _)
}

0 comments on commit 70037b0

Please sign in to comment.