Skip to content

Commit

Permalink
feat: add e2e tests for different createCredentialOffer API variations (
Browse files Browse the repository at this point in the history
#1476)

Signed-off-by: Yurii Shynbuiev <[email protected]>
  • Loading branch information
yshyn-iohk authored Jan 17, 2025
1 parent 1e19b04 commit 2ce192e
Show file tree
Hide file tree
Showing 21 changed files with 465 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,38 @@ class IssueControllerImpl(
)
} yield ()

private def getIssuingDIDFromRequestJwtProperties(
request: CreateIssueCredentialRecordRequest
): IO[ErrorResponse, PrismDID] =
ZIO
.fromOption(request.jwtVcPropertiesV1.map(_.issuingDID))
.orElse(ZIO.fromOption(request.issuingDID))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: issuingDID")))
.flatMap(extractPrismDIDFromString)

private def getIssuingDIDFromRequestSDJWTProperties(
request: CreateIssueCredentialRecordRequest
): IO[ErrorResponse, PrismDID] =
ZIO
.fromOption(request.sdJwtVcPropertiesV1.map(_.issuingDID))
.orElse(ZIO.fromOption(request.issuingDID))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: issuingDID")))
.flatMap(extractPrismDIDFromString)

private def getIssuingDIDFromAnonCredsProperties(
request: CreateIssueCredentialRecordRequest
): IO[ErrorResponse, PrismDID] =
ZIO
.fromOption(request.anoncredsVcPropertiesV1.map(_.issuingDID))
.orElse(ZIO.fromOption(request.issuingDID))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: issuingDID")))
.flatMap(extractPrismDIDFromString)

private def createCredentialOfferRecord(
request: CreateIssueCredentialRecordRequest,
offerContext: OfferContext
): ZIO[WalletAccessContext, ErrorResponse, IssueCredentialRecord] = {

def getIssuingDidFromRequest(request: CreateIssueCredentialRecordRequest) = extractPrismDIDFromString(
request.issuingDID
)

for {
credentialFormat <- ZIO.succeed(
request.credentialFormat.map(CredentialFormat.valueOf).getOrElse(CredentialFormat.JWT)
Expand All @@ -81,21 +104,30 @@ class IssueControllerImpl(
credentialFormat match
case JWT =>
for {
issuingDID <- getIssuingDidFromRequest(request)
issuingDID <- getIssuingDIDFromRequestJwtProperties(request)
_ <- validatePrismDID(issuingDID, allowUnpublished = true, Role.Issuer)
credentialSchemaRef <- parseCredentialSchemaRef_VCDM1_1(
request.schemaId,
request.jwtVcPropertiesV1.map(_.credentialSchema)
)
claims <- ZIO
.fromOption(request.jwtVcPropertiesV1.map(_.claims).orElse(request.claims))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: claims")))
kid = request.jwtVcPropertiesV1
.flatMap(_.issuingKid)
.orElse(request.issuingKid) // TODO: should it be Option[KeyId]?
validityPeriod = request.jwtVcPropertiesV1
.flatMap(_.validityPeriod)
.orElse(request.validityPeriod)
record <- credentialService
.createJWTIssueCredentialRecord(
pairwiseIssuerDID = offerContext.pairwiseIssuerDID,
pairwiseHolderDID = offerContext.pairwiseHolderDID,
kidIssuer = request.issuingKid,
kidIssuer = kid,
thid = DidCommID(),
credentialSchemaRef = Some(credentialSchemaRef),
claims = request.claims,
validityPeriod = request.validityPeriod,
claims = claims,
validityPeriod = validityPeriod,
automaticIssuance = request.automaticIssuance.orElse(Some(true)),
issuingDID = issuingDID.asCanonical,
goalCode = offerContext.goalCode,
Expand All @@ -107,20 +139,29 @@ class IssueControllerImpl(
} yield record
case SDJWT =>
for {
issuingDID <- getIssuingDidFromRequest(request)
issuingDID <- getIssuingDIDFromRequestSDJWTProperties(request)
_ <- validatePrismDID(issuingDID, allowUnpublished = true, Role.Issuer)
credentialSchemaRef <- parseCredentialSchemaRef_VCDM1_1(
request.schemaId,
request.sdJwtVcPropertiesV1.map(_.credentialSchema)
)
claims <- ZIO
.fromOption(request.sdJwtVcPropertiesV1.map(_.claims).orElse(request.claims))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: claims")))
kid = request.sdJwtVcPropertiesV1
.flatMap(_.issuingKid)
.orElse(request.issuingKid) // TODO: should it be Option[KeyId]?
validityPeriod = request.sdJwtVcPropertiesV1
.flatMap(_.validityPeriod)
.orElse(request.validityPeriod)
record <- credentialService
.createSDJWTIssueCredentialRecord(
pairwiseIssuerDID = offerContext.pairwiseIssuerDID,
pairwiseHolderDID = offerContext.pairwiseHolderDID,
kidIssuer = request.issuingKid,
kidIssuer = kid,
thid = DidCommID(),
credentialSchemaRef = Option(credentialSchemaRef),
claims = request.claims,
claims = claims,
validityPeriod = request.validityPeriod,
automaticIssuance = request.automaticIssuance.orElse(Some(true)),
issuingDID = issuingDID.asCanonical,
Expand All @@ -133,7 +174,7 @@ class IssueControllerImpl(
} yield record
case AnonCreds =>
for {
issuingDID <- getIssuingDidFromRequest(request)
issuingDID <- getIssuingDIDFromAnonCredsProperties(request)
credentialDefinitionGUID <- ZIO
.fromOption(request.credentialDefinitionId)
.mapError(_ =>
Expand Down Expand Up @@ -175,14 +216,17 @@ class IssueControllerImpl(
val urlPrefix = if (publicEndpointUrl.endsWith("/")) publicEndpointUrl else publicEndpointUrl + "/"
ZIO.succeed(s"$urlPrefix$httpUrlSuffix")
}
claims <- ZIO
.fromOption(request.anoncredsVcPropertiesV1.map(_.claims).orElse(request.claims))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: claims")))
record <- credentialService
.createAnonCredsIssueCredentialRecord(
pairwiseIssuerDID = offerContext.pairwiseIssuerDID,
pairwiseHolderDID = offerContext.pairwiseHolderDID,
thid = DidCommID(),
credentialDefinitionGUID = credentialDefinitionGUID,
credentialDefinitionId = credentialDefinitionId,
claims = request.claims,
claims = claims,
validityPeriod = request.validityPeriod,
automaticIssuance = request.automaticIssuance.orElse(Some(true)),
goalCode = offerContext.goalCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ final case class CreateIssueCredentialRecordRequest(
@description(annotations.claims.description)
@encodedExample(annotations.claims.example)
@deprecated("Use specific properties of the verifiable credentials *.claims instead", "2.0.0")
claims: zio.json.ast.Json,
claims: Option[zio.json.ast.Json],
@description(annotations.automaticIssuance.description)
@encodedExample(annotations.automaticIssuance.example)
automaticIssuance: Option[Boolean] = None,
@description(annotations.issuingDID.description)
@encodedExample(annotations.issuingDID.example)
@deprecated("Use specific properties of the verifiable credentials *.issuingDID instead", "2.0.0")
issuingDID: String,
issuingDID: Option[String],
@description(annotations.issuingKid.description)
@encodedExample(annotations.issuingKid.example)
@deprecated("Use specific jwtVcPropertiesV1.issuingKid instead", "2.0.0")
Expand Down Expand Up @@ -130,16 +130,23 @@ case class JwtVCPropertiesV1(
@description(JwtVCPropertiesV1.annotations.issuingDID.description)
@encodedExample(JwtVCPropertiesV1.annotations.issuingDID.example)
issuingDID: String,
@description(annotations.issuingKid.description)
@encodedExample(annotations.issuingKid.example)
issuingKid: Option[KeyId],
@description(JwtVCPropertiesV1.annotations.validityPeriod.description)
@encodedExample(JwtVCPropertiesV1.annotations.validityPeriod.example)
validityPeriod: Double,
validityPeriod: Option[Double],
@description(JwtVCPropertiesV1.annotations.claims.description)
@encodedExample(JwtVCPropertiesV1.annotations.claims.example)
claims: zio.json.ast.Json,
@description(JwtVCPropertiesV1.annotations.credentialSchema.description)
@encodedExample(JwtVCPropertiesV1.annotations.credentialSchema.example)
credentialSchema: CredentialSchemaRef
)

object JwtVCPropertiesV1 {
import CreateIssueCredentialRecordRequest.schemaJson

given schema: Schema[JwtVCPropertiesV1] = Schema.derived
given encoder: JsonEncoder[JwtVCPropertiesV1] = DeriveJsonEncoder.gen
given decoder: JsonDecoder[JwtVCPropertiesV1] = DeriveJsonDecoder.gen
Expand Down Expand Up @@ -233,14 +240,37 @@ object AnonCredsVCPropertiesV1 {
}
}

case class SDJWTVCPropertiesV1(issuingDID: String, credentialSchema: CredentialSchemaRef, claims: zio.json.ast.Json)
case class SDJWTVCPropertiesV1(
@description(annotations.issuingDID.description)
@encodedExample(annotations.issuingDID.example)
issuingDID: String,
@description(annotations.issuingKid.description)
@encodedExample(annotations.issuingKid.example)
issuingKid: Option[KeyId],
@description(SDJWTVCPropertiesV1.annotations.validityPeriod.description)
@encodedExample(SDJWTVCPropertiesV1.annotations.validityPeriod.example)
validityPeriod: Option[Double],
@description(SDJWTVCPropertiesV1.annotations.credentialSchema.description)
@encodedExample(SDJWTVCPropertiesV1.annotations.credentialSchema.example)
credentialSchema: CredentialSchemaRef,
@description(annotations.claims.description)
@encodedExample(annotations.claims.example)
claims: zio.json.ast.Json
)

object SDJWTVCPropertiesV1 {
import CreateIssueCredentialRecordRequest.schemaJson

given schema: Schema[SDJWTVCPropertiesV1] = Schema.derived
given encoder: JsonEncoder[SDJWTVCPropertiesV1] = DeriveJsonEncoder.gen
given decoder: JsonDecoder[SDJWTVCPropertiesV1] = DeriveJsonDecoder.gen

object annotations {
object validityPeriod
extends Annotation[Double](
description = "The validity period in seconds of the verifiable credential that will be issued.",
example = 3600
)
object issuingDID
extends Annotation[String](
description = """
Expand All @@ -259,6 +289,17 @@ object SDJWTVCPropertiesV1 {
"lastname" -> zio.json.ast.Json.Str("Wonderland"),
)
)
object credentialSchema
extends Annotation[CredentialSchemaRef](
description = """
|The properties of the SD-JWT verifiable credential that will be issued complied with VCDM 1.1.
|The current implementation of SD-JWT doesn't includ this property in the JWT payload, but the it is used to validate the credential.
|""".stripMargin,
example = CredentialSchemaRef(
"https://agent-host.com/cloud-agent/schema-registry/schemas/d9569cec-c81e-4779-aa86-0d5994d82676",
"JsonSchemaValidator2018"
)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.hyperledger.identus.issue.controller

import org.hyperledger.identus.api.http.ErrorResponse
import org.hyperledger.identus.issue.controller.http.CredentialSchemaRef as HTTPCredentialSchemaRef
import org.hyperledger.identus.pollux.core.model.primitives.UriString
import org.hyperledger.identus.pollux.core.model.schema.{
CredentialSchemaRef as DomainCredentialSchemaRef,
CredentialSchemaRefType
}
import zio.test.*
import zio.test.Assertion.*

object CredentialSchemaReferenceParsingLogicSpec extends ZIOSpecDefault with CredentialSchemaReferenceParsingLogic {

private val credentialSchemaExample = "http://example.com/schema"

private def isErrorResponseWithDetailFieldEqualTo(detail: String) =
isSubtype[ErrorResponse](hasField("detail", _.detail, isSome(equalTo(detail))))

def spec = suite("CredentialSchemaReferenceParsingLogic")(
suite("parseCredentialSchemaRef_VCDM1_1")(
test("should parse valid schema ref with correct type") {
val httpSchemaRef = HTTPCredentialSchemaRef(credentialSchemaExample, "JsonSchemaValidator2018")
for {
result <- parseCredentialSchemaRef_VCDM1_1(None, Some(httpSchemaRef)).either
expectedUriString <- UriString.make(credentialSchemaExample).toZIO
} yield assert(result)(
isRight(
equalTo(DomainCredentialSchemaRef(CredentialSchemaRefType.JsonSchemaValidator2018, expectedUriString))
)
)
},
test("should fail for schema ref with invalid type") {
val httpSchemaRef = HTTPCredentialSchemaRef(credentialSchemaExample, "InvalidType")
for {
result <- parseCredentialSchemaRef_VCDM1_1(None, Some(httpSchemaRef)).either
} yield assert(result)(
isLeft(isErrorResponseWithDetailFieldEqualTo("Invalid credentialSchema type: InvalidType."))
)
},
test("should parse deprecated schema ID property") {
for {
result <- parseCredentialSchemaRef_VCDM1_1(Some(credentialSchemaExample), None).either
expectedUriString <- UriString.make(credentialSchemaExample).toZIO
} yield assert(result)(
isRight(
equalTo(DomainCredentialSchemaRef(CredentialSchemaRefType.JsonSchemaValidator2018, expectedUriString))
)
)
},
test("should fail if no schema is provided") {
for {
result <- parseCredentialSchemaRef_VCDM1_1(None, None).either
} yield assert(result)(
isLeft(isErrorResponseWithDetailFieldEqualTo("Credential schema property missed."))
)
}
),
suite("parseSchemaIdForAnonCredsModelV1")(
test("should parse schema ID property") {
for {
result <- parseSchemaIdForAnonCredsModelV1(None, Some(credentialSchemaExample)).either
expectedUriString <- UriString.make(credentialSchemaExample).toZIO
} yield assert(result)(isRight(equalTo(expectedUriString)))
},
test("should parse deprecated schema ID property") {
for {
result <- parseSchemaIdForAnonCredsModelV1(Some(credentialSchemaExample), None).either
expectedUriString <- UriString.make(credentialSchemaExample).toZIO
} yield assert(result)(isRight(equalTo(expectedUriString)))
},
test("should fail if no schema ID is provided") {
for {
result <- parseSchemaIdForAnonCredsModelV1(None, None).either
} yield assert(result)(
isLeft(isErrorResponseWithDetailFieldEqualTo("Credential schema property missed."))
)
}
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ object IssueControllerImplSpec extends ZIOSpecDefault with IssueControllerTestTo
schemaId = Some("mySchemaId"),
credentialDefinitionId = Some(UUID.fromString("123e4567-e89b-12d3-a456-426614174000")),
credentialFormat = Some("JWT"),
claims = json.fromJson[Json].toOption.get,
claims = json.fromJson[Json].toOption,
automaticIssuance = Some(true),
issuingDID =
"did:prism:332518729a7b7805f73a788e0944802527911901d9b7c16152281be9bc62d944:CosBCogBEkkKFW15LWtleS1hdXRoZW50aWNhdGlvbhAESi4KCXNlY3AyNTZrMRIhAuYoRIefsLhkvYwHz8gDtkG2b0kaZTDOLj_SExWX1fOXEjsKB21hc3RlcjAQAUouCglzZWNwMjU2azESIQLOzab8f0ibt1P0zdMfoWDQTSlPc8_tkV9Jk5BBsXB8fA",
issuingDID = Option(
"did:prism:332518729a7b7805f73a788e0944802527911901d9b7c16152281be9bc62d944:CosBCogBEkkKFW15LWtleS1hdXRoZW50aWNhdGlvbhAESi4KCXNlY3AyNTZrMRIhAuYoRIefsLhkvYwHz8gDtkG2b0kaZTDOLj_SExWX1fOXEjsKB21hc3RlcjAQAUouCglzZWNwMjU2azESIQLOzab8f0ibt1P0zdMfoWDQTSlPc8_tkV9Jk5BBsXB8fA"
),
issuingKid = Some(KeyId("some_kid_id")),
connectionId = Some(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"))
)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies {
testImplementation("io.ktor:ktor-server-netty:2.3.0")
testImplementation("io.ktor:ktor-client-apache:2.3.0")
// RestAPI client
testImplementation("org.hyperledger.identus:cloud-agent-client-kotlin:1.40.1-d092f31")
testImplementation("org.hyperledger.identus:cloud-agent-client-kotlin:1.40.1-8363d1e")
// Test helpers library
testImplementation("io.iohk.atala:atala-automation:0.4.0")
// Hoplite for configuration
Expand Down
Loading

0 comments on commit 2ce192e

Please sign in to comment.