Skip to content

Commit

Permalink
test: refactor DIDs
Browse files Browse the repository at this point in the history
Signed-off-by: Allain Magyar <allain.magyar@iohk.io>
  • Loading branch information
amagyar-iohk committed Jun 26, 2024
1 parent a33d613 commit 9b83bbf
Show file tree
Hide file tree
Showing 17 changed files with 277 additions and 222 deletions.
25 changes: 25 additions & 0 deletions tests/integration-tests/src/test/kotlin/common/DidPurpose.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package common

import org.hyperledger.identus.client.models.*

enum class DidPurpose {
EMPTY {
override val publicKeys = emptyList<ManagedDIDKeyTemplate>()
override val services = emptyList<Service>()
},
JWT {
override val publicKeys = listOf(
ManagedDIDKeyTemplate("auth-1", Purpose.AUTHENTICATION, Curve.SECP256K1),
ManagedDIDKeyTemplate("auth-2", Purpose.AUTHENTICATION, Curve.ED25519),
ManagedDIDKeyTemplate("assertion-1", Purpose.ASSERTION_METHOD, Curve.SECP256K1),
)
override val services = emptyList<Service>()
},
ANONCRED {
override val publicKeys = emptyList<ManagedDIDKeyTemplate>()
override val services = emptyList<Service>()
};

abstract val publicKeys: List<ManagedDIDKeyTemplate>
abstract val services: List<Service>
}
12 changes: 0 additions & 12 deletions tests/integration-tests/src/test/kotlin/common/TestConstants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ object TestConstants {
),
)

val PRISM_DID_AUTH_KEY = ManagedDIDKeyTemplate("auth-1", Purpose.AUTHENTICATION)
val PRISM_DID_SERVICE_FOR_UPDATE = Service(
"https://update.com",
listOf("LinkedDomains"),
Json("https://update.com/"),
)
val PRISM_DID_UPDATE_NEW_SERVICE_URL = "https://bar.foo.com/"
val PRISM_DID_UPDATE_NEW_SERVICE = Service(
"https://new.service.com",
listOf("LinkedDomains"),
Json("https://new.service.com/"),
)
val EVENT_TYPE_CONNECTION_UPDATED = "ConnectionUpdated"
val EVENT_TYPE_ISSUE_CREDENTIAL_RECORD_UPDATED = "IssueCredentialRecordUpdated"
val EVENT_TYPE_PRESENTATION_UPDATED = "PresentationUpdated"
Expand Down
45 changes: 14 additions & 31 deletions tests/integration-tests/src/test/kotlin/steps/common/CommonSteps.kt
Original file line number Diff line number Diff line change
@@ -1,50 +1,29 @@
package steps.common

import common.CredentialSchema
import common.DidPurpose
import interactions.Get
import io.cucumber.java.DataTableType
import io.cucumber.java.ParameterType
import io.cucumber.java.en.Given
import io.iohk.atala.automation.extensions.get
import io.iohk.atala.automation.serenity.ensure.Ensure
import net.serenitybdd.rest.SerenityRest
import net.serenitybdd.screenplay.Actor
import net.serenitybdd.screenplay.actors.OnStage
import org.apache.http.HttpStatus
import org.hyperledger.identus.client.models.*
import org.hyperledger.identus.client.models.Connection
import org.hyperledger.identus.client.models.ConnectionsPage
import steps.connection.ConnectionSteps
import steps.credentials.IssueCredentialsSteps
import steps.did.PublishDidSteps
import steps.schemas.CredentialSchemasSteps

class CommonSteps {
@ParameterType(".*")
fun actor(actorName: String): Actor {
return OnStage.theActorCalled(actorName)
}

@ParameterType(".*")
fun curve(value: String): Curve {
return Curve.decode(value) ?: throw IllegalArgumentException("$value is not a valid Curve value")
}

@ParameterType(".*")
fun purpose(value: String): Purpose {
return Purpose.decode(value) ?: throw IllegalArgumentException("$value is not a valid Purpose value")
}

@DataTableType
fun vcVerification(cell: String): VcVerification {
return VcVerification.valueOf(cell)
}

@Given("{actor} has an issued credential from {actor}")
@Given("{actor} has a jwt issued credential from {actor}")
fun holderHasIssuedCredentialFromIssuer(holder: Actor, issuer: Actor) {
actorsHaveExistingConnection(issuer, holder)

val publishDidSteps = PublishDidSteps()
publishDidSteps.createsUnpublishedDid(holder)
publishDidSteps.agentHasAPublishedDID(issuer)
publishDidSteps.agentHasAnUnpublishedDID(holder, DidPurpose.JWT)
publishDidSteps.agentHasAPublishedDID(issuer, DidPurpose.JWT)

val issueSteps = IssueCredentialsSteps()
issueSteps.issuerOffersACredential(issuer, holder, "short")
Expand All @@ -54,13 +33,17 @@ class CommonSteps {
issueSteps.bobHasTheCredentialIssued(holder)
}

@Given("{actor} has an issued credential with {} schema from {actor}")
fun holderHasIssuedCredentialFromIssuerWithSchema(holder: Actor, schema: CredentialSchema, issuer: Actor) {
@Given("{actor} has a jwt issued credential with {} schema from {actor}")
fun holderHasIssuedCredentialFromIssuerWithSchema(
holder: Actor,
schema: CredentialSchema,
issuer: Actor
) {
actorsHaveExistingConnection(issuer, holder)

val publishDidSteps = PublishDidSteps()
publishDidSteps.createsUnpublishedDid(holder)
publishDidSteps.agentHasAPublishedDID(issuer)
publishDidSteps.agentHasAnUnpublishedDID(holder, DidPurpose.JWT)
publishDidSteps.agentHasAPublishedDID(issuer, DidPurpose.JWT)

val schemaSteps = CredentialSchemasSteps()
schemaSteps.agentHasAPublishedSchema(issuer, schema)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package steps.common

import io.cucumber.java.DataTableType
import io.cucumber.java.ParameterType
import net.serenitybdd.screenplay.Actor
import net.serenitybdd.screenplay.actors.OnStage
import org.hyperledger.identus.client.models.*

class ParameterSteps {
@ParameterType(".*")
fun actor(actorName: String): Actor {
return OnStage.theActorCalled(actorName)
}

@ParameterType(".*")
fun curve(value: String): Curve {
return Curve.decode(value) ?: throw IllegalArgumentException("$value is not a valid Curve value")
}

@ParameterType(".*")
fun purpose(value: String): Purpose {
return Purpose.decode(value) ?: throw IllegalArgumentException("$value is not a valid Purpose value")
}

@DataTableType
fun vcVerification(cell: String): VcVerification {
return VcVerification.valueOf(cell)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package steps.did

import common.body
import interactions.Get
import interactions.Post
import io.cucumber.java.en.*
Expand Down Expand Up @@ -31,10 +32,7 @@ class ManageDidSteps {
val createDidRequest = createPrismDidRequest(curve, purpose)

actor.attemptsTo(
Post.to("/did-registrar/dids")
.with {
it.body(createDidRequest)
},
Post.to("/did-registrar/dids").body(createDidRequest)
)

if (SerenityRest.lastResponse().statusCode() == SC_CREATED) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package steps.did

import abilities.ListenToEvents
import common.TestConstants
import common.DidPurpose
import common.body
import interactions.Get
import interactions.Post
import io.cucumber.java.en.*
Expand All @@ -17,28 +18,44 @@ import org.hyperledger.identus.client.models.*
import kotlin.time.Duration.Companion.seconds

class PublishDidSteps {

@Given("{actor} has a published DID for {}")
fun agentHasAPublishedDID(agent: Actor, didPurpose: DidPurpose) {
if (agent.recallAll().containsKey("hasPublishedDid") && actualDidHasSamePurpose(agent, didPurpose)) {
return
}
agentHasAnUnpublishedDID(agent, didPurpose)
hePublishesDidToLedger(agent)
}

@Given("{actor} has an unpublished DID for {}")
fun agentHasAnUnpublishedDID(agent: Actor, didPurpose: DidPurpose) {
if (agent.recallAll().containsKey("shortFormDid") || agent.recallAll().containsKey("longFormDid")) {
// is not published and has the same purpose
if (!agent.recallAll().containsKey("hasPublishedDid") && actualDidHasSamePurpose(agent, didPurpose)) {
return
}
}
agentCreatesUnpublishedDid(agent, didPurpose)
}

private fun actualDidHasSamePurpose(agent: Actor, didPurpose: DidPurpose): Boolean {
val actualPurpose: DidPurpose = agent.recall<DidPurpose?>("didPurpose") ?: return false
return actualPurpose == didPurpose
}

@Given("{actor} creates unpublished DID")
fun createsUnpublishedDid(actor: Actor) {
fun agentCreatesEmptyUnpublishedDid(actor: Actor) {
agentCreatesUnpublishedDid(actor, DidPurpose.EMPTY)
}

@Given("{actor} creates unpublished DID for {}")
fun agentCreatesUnpublishedDid(actor: Actor, didPurpose: DidPurpose) {
val createDidRequest = CreateManagedDidRequest(
CreateManagedDidRequestDocumentTemplate(
publicKeys = listOf(
ManagedDIDKeyTemplate("auth-1", Purpose.AUTHENTICATION, Curve.SECP256K1),
ManagedDIDKeyTemplate("auth-2", Purpose.AUTHENTICATION, Curve.ED25519),
ManagedDIDKeyTemplate("assertion-1", Purpose.ASSERTION_METHOD, Curve.SECP256K1),
ManagedDIDKeyTemplate("comm-1", Purpose.KEY_AGREEMENT, Curve.X25519),
),
services = listOf(
Service("https://foo.bar.com", listOf("LinkedDomains"), Json("https://foo.bar.com/")),
Service("https://update.com", listOf("LinkedDomains"), Json("https://update.com/")),
Service("https://remove.com", listOf("LinkedDomains"), Json("https://remove.com/")),
),
),
CreateManagedDidRequestDocumentTemplate(didPurpose.publicKeys, services = didPurpose.services),
)
actor.attemptsTo(
Post.to("/did-registrar/dids")
.with {
it.body(createDidRequest)
},
Post.to("/did-registrar/dids").body(createDidRequest),
Ensure.thatTheLastResponse().statusCode().isEqualTo(SC_CREATED),
)

Expand All @@ -54,42 +71,18 @@ class PublishDidSteps {

actor.remember("longFormDid", managedDid.longFormDid)
actor.remember("shortFormDid", did.did)
actor.remember("didPurpose", didPurpose)
actor.forget<String>("hasPublishedDid")
}

@Given("{actor} has a published DID")
fun agentHasAPublishedDID(agent: Actor) {
if (agent.recallAll().containsKey("hasPublishedDid")) {
return
}
if (!agent.recallAll().containsKey("shortFormDid") &&
!agent.recallAll().containsKey("longFormDid")
) {
createsUnpublishedDid(agent)
}
hePublishesDidToLedger(agent)
}

@Given("{actor} has an unpublished DID")
fun agentHasAnUnpublishedDID(agent: Actor) {
if (agent.recallAll().containsKey("shortFormDid") ||
agent.recallAll().containsKey("longFormDid")
) {
// is not published
if (!agent.recallAll().containsKey("hasPublishedDid")) {
return
}
}
createsUnpublishedDid(agent)
}

@When("{actor} publishes DID to ledger")
fun hePublishesDidToLedger(actor: Actor) {
val shortFormDid = actor.recall<String>("shortFormDid")
actor.attemptsTo(
Post.to("/did-registrar/dids/${actor.recall<String>("shortFormDid")}/publications"),
Post.to("/did-registrar/dids/$shortFormDid/publications"),
)
val didOperationResponse = SerenityRest.lastResponse().get<DIDOperationResponse>()

val didOperationResponse = SerenityRest.lastResponse().get<DIDOperationResponse>()
actor.attemptsTo(
Ensure.thatTheLastResponse().statusCode().isEqualTo(HttpStatus.SC_ACCEPTED),
Ensure.that(didOperationResponse.scheduledOperation.didRef).isNotEmpty(),
Expand Down Expand Up @@ -126,9 +119,6 @@ class PublishDidSteps {
val shortFormDid = actor.recall<String>("shortFormDid")
actor.attemptsTo(
Ensure.that(didDocument.id).isEqualTo(shortFormDid),
Ensure.that(didDocument.authentication!![0])
.isEqualTo("$shortFormDid#${TestConstants.PRISM_DID_AUTH_KEY.id}"),
Ensure.that(didDocument.verificationMethod!![0].controller).isEqualTo(shortFormDid),
Ensure.that(didResolutionResult.didDocumentMetadata.deactivated!!).isFalse(),
)
}
Expand Down
Loading

0 comments on commit 9b83bbf

Please sign in to comment.