Skip to content

Commit

Permalink
test: finalize backup test
Browse files Browse the repository at this point in the history
Signed-off-by: Allain Magyar <[email protected]>
  • Loading branch information
amagyar-iohk committed Jul 4, 2024
1 parent df2910b commit 75116d7
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 41 deletions.
1 change: 1 addition & 0 deletions tests/end-to-end/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
testImplementation("io.iohk.atala.prism:prism-kotlin-client:1.31.0")
testImplementation("io.iohk.atala:atala-automation:0.3.2")
testImplementation("app.cash.sqldelight:sqlite-driver:2.0.2")
testImplementation("io.ktor:ktor-client-core-jvm:2.3.12")
}

tasks.register<Delete>("cleanTarget") {
Expand Down
Binary file removed tests/end-to-end/prism.db
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.serenitybdd.screenplay.Ability
import net.serenitybdd.screenplay.Actor
import net.serenitybdd.screenplay.HasTeardown
import net.serenitybdd.screenplay.Question
import net.serenitybdd.screenplay.SilentInteraction
import org.hyperledger.identus.walletsdk.apollo.ApolloImpl
import org.hyperledger.identus.walletsdk.castor.CastorImpl
import org.hyperledger.identus.walletsdk.configuration.DbConnectionInMemory
import org.hyperledger.identus.walletsdk.configuration.Environment
import org.hyperledger.identus.walletsdk.domain.models.ApiImpl
import org.hyperledger.identus.walletsdk.domain.models.DID
Expand All @@ -28,15 +28,13 @@ import org.hyperledger.identus.walletsdk.edgeagent.protocols.ProtocolType
import org.hyperledger.identus.walletsdk.mercury.MercuryImpl
import org.hyperledger.identus.walletsdk.mercury.resolvers.DIDCommWrapper
import org.hyperledger.identus.walletsdk.pluto.PlutoImpl
import org.hyperledger.identus.walletsdk.pluto.data.DbConnection
import org.hyperledger.identus.walletsdk.pollux.PolluxImpl
import org.hyperledger.identus.walletsdk.workflow.EdgeAgentWorkflow
import java.io.File
import java.util.Base64
import java.util.Collections


class UseWalletSdk : Ability, HasTeardown {
class UseWalletSdk : Ability {
companion object {
private fun `as`(actor: Actor): UseWalletSdk {
if (actor.abilityTo(UseWalletSdk::class.java) != null) {
Expand Down Expand Up @@ -105,19 +103,30 @@ class UseWalletSdk : Ability, HasTeardown {

fun initialize() {
createSdk()
start()
startPluto()
startSdk()
listenToMessages()
isInitialized = true
}

fun createSdk(initialSeed: Seed? = null) {
tearDown()
fun recoverWallet(seed: Seed, jwe: String) {
createSdk(seed)
startPluto()
runBlocking {
context.sdk.recoverWallet(jwe)
}
startSdk()
listenToMessages()
isInitialized = true
}

private fun createSdk(initialSeed: Seed? = null) {
val api = ApiImpl(httpClient())
val apollo = ApolloImpl()
val castor = CastorImpl(apollo)
val pluto = PlutoImpl(DbConnection())
val pollux = PolluxImpl(apollo, castor)
val pluto = PlutoImpl(DbConnectionInMemory())
val pollux = PolluxImpl(apollo, castor, api)
val didcommWrapper = DIDCommWrapper(castor, pluto, apollo)
val api = ApiImpl(httpClient())
val mercury = MercuryImpl(castor, didcommWrapper, api)

val mediatorDid = DID(getMediatorDidThroughOob())
Expand Down Expand Up @@ -145,13 +154,20 @@ class UseWalletSdk : Ability, HasTeardown {
this.context = SdkContext(sdk)
}

fun start() {
private fun startPluto() {
runBlocking {
context.sdk.pluto.start(this)
}
}

private fun startSdk() {
runBlocking {
context.sdk.start()
context.sdk.startFetchingMessages(1)
}
context.sdk.startFetchingMessages(1)
}

private fun listenToMessages() {
CoroutineScope(Dispatchers.Default).launch {
context.sdk.handleReceivedMessagesEvents().collect { messageList: List<Message> ->
messageList.forEach { message ->
Expand Down Expand Up @@ -179,10 +195,6 @@ class UseWalletSdk : Ability, HasTeardown {
val json = JsonPath.parse(decodedData)
return json.read("from")
}

override fun tearDown() {
File("prism.db").delete()
}
}

data class SdkContext(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.hyperledger.identus.walletsdk.configuration

import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import org.hyperledger.identus.walletsdk.SdkPlutoDb
import org.hyperledger.identus.walletsdk.pluto.data.DbConnection

class DbConnectionInMemory : DbConnection {
override var driver: SqlDriver? = null

override suspend fun connectDb(context: Any?): SqlDriver {
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
SdkPlutoDb.Schema.create(driver)
this.driver = driver
return driver
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import io.cucumber.java.After
import io.cucumber.java.en.Given
import io.cucumber.java.en.Then
import io.cucumber.java.en.When
import net.serenitybdd.screenplay.Actor
import net.serenitybdd.screenplay.actors.OnStage
import org.hyperledger.identus.walletsdk.abilities.UseWalletSdk
import org.hyperledger.identus.walletsdk.workflow.CloudAgentWorkflow
import org.hyperledger.identus.walletsdk.workflow.EdgeAgentWorkflow
import net.serenitybdd.screenplay.Actor
import net.serenitybdd.screenplay.actors.OnStage
import javax.inject.Inject

class EdgeAgentSteps {
Expand All @@ -29,6 +29,16 @@ class EdgeAgentSteps {
edgeAgentWorkflow.connect(edgeAgent)
}

@When("{actor} creates '{}' peer DIDs")
fun `Edge Agent creates Peer DIDs`(edgeAgent: Actor, numberOfDids: Int) {
edgeAgentWorkflow.createPeerDids(edgeAgent, numberOfDids)
}

@When("{actor} creates '{}' prism DIDs")
fun `Edge Agent creates Prism DIDs`(edgeAgent: Actor, numberOfDids: Int) {
edgeAgentWorkflow.createPrismDids(edgeAgent, numberOfDids)
}

@When("{actor} has '{}' jwt credentials issued by {actor}")
fun `Edge Agent has {} jwt issued credential`(edgeAgent: Actor, numberOfCredentialsIssued: Int, cloudAgent: Actor) {
val recordIdList = mutableListOf<String>()
Expand All @@ -46,7 +56,11 @@ class EdgeAgentSteps {
}

@When("{actor} has '{}' anonymous credentials issued by {actor}")
fun `Edge Agent has {} anonymous issued credential`(edgeAgent: Actor, numberOfCredentialsIssued: Int, cloudAgent: Actor) {
fun `Edge Agent has {} anonymous issued credential`(
edgeAgent: Actor,
numberOfCredentialsIssued: Int,
cloudAgent: Actor
) {
repeat(numberOfCredentialsIssued) {
cloudAgentWorkflow.offerAnonymousCredential(cloudAgent)
edgeAgentWorkflow.waitForCredentialOffer(edgeAgent, 1)
Expand Down Expand Up @@ -135,7 +149,10 @@ class EdgeAgentSteps {
}

@Then("{actor} waits to receive the revocation notifications from {actor}")
fun `Edge Agent waits to receive the revocation notifications from Cloud Agent`(edgeAgent: Actor, cloudAgent: Actor) {
fun `Edge Agent waits to receive the revocation notifications from Cloud Agent`(
edgeAgent: Actor,
cloudAgent: Actor
) {
val revokedRecordIdList = cloudAgent.recall<MutableList<String>>("revokedRecordIdList")
edgeAgentWorkflow.waitForCredentialRevocationMessage(edgeAgent, revokedRecordIdList.size)
}
Expand All @@ -156,6 +173,24 @@ class EdgeAgentSteps {
edgeAgentWorkflow.createNewWalletFromBackupWithWrongSeed(edgeAgent)
}

@Then("a new {actor} is restored from {actor}")
fun `A new Agent is restored from Edge Agent`(newAgent: Actor, originalAgent: Actor) {
edgeAgentWorkflow.backupAndRestoreToNewAgent(newAgent, originalAgent)
}

@Then("{actor} should have the expected values from {actor}")
fun `Restored Agent should have the expected values from Original Edge Agent`(
restoredEdgeAgent: Actor,
originalEdgeAgent: Actor
) {
edgeAgentWorkflow.copyAgentShouldMatchOriginalAgent(restoredEdgeAgent, originalEdgeAgent)
}

@Then("{actor} is dismissed")
fun `Edge Agent is dismissed`(edgeAgent: Actor) {
edgeAgent.wrapUp()
}

@After
fun stopAgent() {
OnStage.theActor("Edge Agent").attemptsTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ package org.hyperledger.identus.walletsdk.workflow
import com.google.gson.GsonBuilder
import io.iohk.atala.automation.serenity.interactions.PollingWait
import io.iohk.atala.automation.utils.Logger
import org.hyperledger.identus.walletsdk.abilities.UseWalletSdk
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import net.serenitybdd.screenplay.Actor
import net.serenitybdd.screenplay.rest.interactions.Ensure
import org.assertj.core.api.Assertions
import net.serenitybdd.screenplay.actors.Cast
import net.serenitybdd.screenplay.actors.OnStage
import net.serenitybdd.screenplay.rest.abilities.CallAnApi
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.fail
import org.hamcrest.CoreMatchers.equalTo
import org.hyperledger.identus.walletsdk.abilities.SdkContext
import org.hyperledger.identus.walletsdk.abilities.UseWalletSdk
import org.hyperledger.identus.walletsdk.apollo.ApolloImpl
import org.hyperledger.identus.walletsdk.configuration.Environment
import org.hyperledger.identus.walletsdk.domain.models.CastorError
import org.hyperledger.identus.walletsdk.domain.models.Seed
import org.hyperledger.identus.walletsdk.edgeagent.protocols.issueCredential.IssueCredential
import org.hyperledger.identus.walletsdk.edgeagent.protocols.issueCredential.OfferCredential
import org.hyperledger.identus.walletsdk.edgeagent.protocols.outOfBand.OutOfBandInvitation
import org.hyperledger.identus.walletsdk.edgeagent.protocols.proofOfPresentation.RequestPresentation
import org.hyperledger.identus.walletsdk.pluto.PlutoBackupTask
import java.util.function.Consumer

class EdgeAgentWorkflow {
private val logger = Logger.get<EdgeAgentWorkflow>()
Expand Down Expand Up @@ -146,14 +147,7 @@ class EdgeAgentWorkflow {
val revokedCredentials = credentials.filter { credential ->
credential.revoked == true && revokedIdList.contains(credential.id)
}
edgeAgent.attemptsTo(
Ensure.that(
"The number of revoked credentials matches the expected number",
Consumer { context ->
assertThat(revokedCredentials.size).isEqualTo(revokedRecordIdList.size)
}
)
)
assertThat(revokedCredentials.size).isEqualTo(revokedRecordIdList.size)
}
)
}
Expand All @@ -173,12 +167,8 @@ class EdgeAgentWorkflow {
val backup = edgeAgent.recall<String>("backup")
val seed = edgeAgent.recall<Seed>("seed")
val walletSdk = UseWalletSdk()
walletSdk.tearDown() // removes prism.db
walletSdk.createSdk(seed)
walletSdk.recoverWallet(seed, backup)
runBlocking {
walletSdk.context.sdk.pluto.start()
walletSdk.context.sdk.recoverWallet(backup)
walletSdk.context.sdk.start()
walletSdk.context.sdk.stop()
}
}
Expand All @@ -187,16 +177,70 @@ class EdgeAgentWorkflow {
val backup = edgeAgent.recall<String>("backup")
val seed = ApolloImpl().createRandomSeed().seed
val walletSdk = UseWalletSdk()
walletSdk.tearDown() // removes prism.db
walletSdk.createSdk(seed)
runBlocking {
walletSdk.context.sdk.pluto.start()
try {
walletSdk.context.sdk.recoverWallet(backup)
walletSdk.recoverWallet(seed, backup)
fail<String>("SDK should not be able to restore with wrong seed phrase.")
} catch (e: Exception) {
assertThat(e).isNotNull()
}
}
}

fun createPeerDids(edgeAgent: Actor, numberOfDids: Int) {
edgeAgent.attemptsTo(
UseWalletSdk.execute { sdkContext ->
repeat(numberOfDids) {
sdkContext.sdk.createNewPeerDID(updateMediator = false)
}
}
)
}

fun createPrismDids(edgeAgent: Actor, numberOfDids: Int) {
edgeAgent.attemptsTo(
UseWalletSdk.execute { sdkContext ->
repeat(numberOfDids) {
sdkContext.sdk.createNewPrismDID()
}
}
)
}

fun backupAndRestoreToNewAgent(newAgent: Actor, originalAgent: Actor) {
val backup = originalAgent.recall<String>("backup")
val seed = originalAgent.recall<Seed>("seed")
val walletSdk = UseWalletSdk()
walletSdk.recoverWallet(seed, backup)
newAgent.whoCan(walletSdk).whoCan(CallAnApi.at(Environment.mediatorOobUrl))
}

fun copyAgentShouldMatchOriginalAgent(restoredEdgeAgent: Actor, originalEdgeAgent: Actor) {
val expectedCredentials = mutableListOf<String>()
val expectedPeerDids = mutableListOf<String>()
val expectedPrismDids = mutableListOf<String>()

originalEdgeAgent.attemptsTo(
UseWalletSdk.execute { sdkContext ->
expectedCredentials.addAll(sdkContext.sdk.getAllCredentials().first().map { it.id })
expectedPeerDids.addAll(sdkContext.sdk.pluto.getAllPeerDIDs().first().map { it.did.toString() })
expectedPrismDids.addAll(sdkContext.sdk.pluto.getAllPrismDIDs().first().map { it.did.toString() })
}
)

restoredEdgeAgent.attemptsTo(
UseWalletSdk.execute { sdkContext ->
val actualCredentials = sdkContext.sdk.getAllCredentials().first().map { it.id }
val actualPeerDids = sdkContext.sdk.pluto.getAllPeerDIDs().first().map { it.did.toString() }
val actualPrismDids = sdkContext.sdk.pluto.getAllPrismDIDs().first().map { it.did.toString() }

assertThat(actualCredentials.size).isEqualTo(expectedCredentials.size)
assertThat(actualCredentials.containsAll(expectedCredentials)).isTrue()
assertThat(actualPeerDids.size).isEqualTo(expectedPeerDids.size)
assertThat(actualPeerDids.containsAll(expectedPeerDids)).isTrue()
assertThat(actualPrismDids.size).isEqualTo(expectedPrismDids.size)
assertThat(actualPrismDids.containsAll(expectedPrismDids)).isTrue()
}
)
}
}

0 comments on commit 75116d7

Please sign in to comment.