-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added test for executor and problem reports (#117)
* feat: Added test for executor and problem reports Signed-off-by: Shailesh Patil <shailesh.patil@iohk.io> * lift the future effec into zio Signed-off-by: Shailesh Patil <shailesh.patil@iohk.io> --------- Signed-off-by: Shailesh Patil <shailesh.patil@iohk.io>
- Loading branch information
1 parent
2db9138
commit ce05d5a
Showing
10 changed files
with
772 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
mediator/src/test/scala/io/iohk/atala/mediator/db/AgentStub.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.iohk.atala.mediator.db | ||
import fmgp.crypto.{Curve, KTY, OKPPrivateKey} | ||
import fmgp.did.Agent | ||
import fmgp.did.comm.EncryptedMessage | ||
import fmgp.did.method.peer.{DIDPeer2, DIDPeerServiceEncoded} | ||
import io.iohk.atala.mediator.app.{MediatorAgent, MediatorConfig} | ||
import zio.{ULayer, ZLayer} | ||
|
||
import java.net.URI | ||
|
||
object AgentStub { | ||
|
||
def keyAgreement(d: String, x: String): OKPPrivateKey = | ||
OKPPrivateKey(kty = KTY.OKP, crv = Curve.X25519, d = d, x = x, kid = None) | ||
|
||
def keyAuthentication(d: String, x: String): OKPPrivateKey = | ||
OKPPrivateKey(kty = KTY.OKP, crv = Curve.Ed25519, d = d, x = x, kid = None) | ||
|
||
val endpoint = new URI("http://localhost:8080") | ||
val mediatorConfig = MediatorConfig( | ||
endpoint, | ||
keyAgreement("Z6D8LduZgZ6LnrOHPrMTS6uU2u5Btsrk1SGs4fn8M7c", "Sr4SkIskjN_VdKTn0zkjYbhGTWArdUNE4j_DmUpnQGw"), | ||
keyAuthentication("INXCnxFEl0atLIIQYruHzGd5sUivMRyQOzu87qVerug", "MBjnXZxkMcoQVVL21hahWAw43RuAG-i64ipbeKKqwoA") | ||
) | ||
|
||
val endpointBob = "http://localhost:8081" | ||
val bobAgent = DIDPeer2.makeAgent( | ||
Seq( | ||
keyAgreement("H5wHQcecUqobAMT3RiNsAaYaFXIfTLCNhWAYXgTYv7E", "f8ce_zxdhIEy76JE21XpVDviRtR2amXaZ6NjYyIPjg4"), | ||
keyAuthentication("LyMSyr_usdn3pHZc00IbJaS2RcvF4OcJTJIB2Vw6dLQ", "TQdV8Wduyz3OylN3YbyHR0R-aynF3C1tmvHAgl6b34I") | ||
), | ||
Seq(DIDPeerServiceEncoded(endpointBob)) | ||
) | ||
|
||
val endpointAlice = "http://localhost:8081" | ||
val aliceAgent = DIDPeer2.makeAgent( | ||
Seq( | ||
keyAgreement("Z6D8LduZgZ6LnrOHPrMTS6uU2u5Btsrk1SGs4fn8M7c", "Sr4SkIskjN_VdKTn0zkjYbhGTWArdUNE4j_DmUpnQGw"), | ||
keyAuthentication("INXCnxFEl0atLIIQYruHzGd5sUivMRyQOzu87qVerug", "MBjnXZxkMcoQVVL21hahWAw43RuAG-i64ipbeKKqwoA") | ||
), | ||
Seq(DIDPeerServiceEncoded(endpointAlice)) | ||
) | ||
|
||
def aliceAgentLayer: ULayer[Agent] = ZLayer.succeed(aliceAgent) | ||
|
||
def bobAgentLayer: ULayer[Agent] = ZLayer.succeed(bobAgent) | ||
|
||
val agentLayer = mediatorConfig.agentLayer | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
mediator/src/test/scala/io/iohk/atala/mediator/protocols/ForwardMessageExecutorSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package io.iohk.atala.mediator.protocols | ||
|
||
import fmgp.did.comm.protocol.reportproblem2.{ProblemCode, ProblemReport} | ||
import fmgp.did.comm.{EncryptedMessage, Operations, PlaintextMessage, SignedMessage, layerDefault} | ||
import fmgp.did.method.peer.DidPeerResolver | ||
import fmgp.util.Base64 | ||
import io.iohk.atala.mediator.comm.MessageDispatcherJVM | ||
import io.iohk.atala.mediator.db.* | ||
import io.iohk.atala.mediator.db.MessageItemRepoSpec.encryptedMessageAlice | ||
import io.iohk.atala.mediator.protocols.ForwardMessageExecuter | ||
import zio.* | ||
import zio.ExecutionStrategy.Sequential | ||
import zio.http.Client | ||
import zio.json.* | ||
import zio.test.* | ||
import zio.test.Assertion.* | ||
import fmgp.did.DIDSubject | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import io.iohk.atala.mediator.db.EmbeddedMongoDBInstance.* | ||
import io.iohk.atala.mediator.protocols.MediatorCoordinationExecuterSpec.setupAndClean | ||
import reactivemongo.api.bson.BSONDocument | ||
object ForwardMessageExecutorSpec extends ZIOSpecDefault with DidAccountStubSetup with MessageSetup { | ||
|
||
override def spec = suite("ForwardMessageExecutorSpec")( | ||
test("Forward message for notEnrolled DID receives problem report ") { | ||
val executer = ForwardMessageExecuter | ||
for { | ||
userAccount <- ZIO.service[UserAccountRepo] | ||
result <- userAccount.createOrFindDidAccount(DIDSubject(alice)) | ||
msg <- ZIO.fromEither(plaintextForwardNotEnrolledDidMessage) | ||
result <- executer.execute(msg) | ||
message <- ZIO.fromOption(result) | ||
} yield { | ||
assertTrue(message.isInstanceOf[SignedMessage]) | ||
val signedMessage = message.asInstanceOf[SignedMessage] | ||
val jsonString = Base64.fromBase64url(signedMessage.payload.base64url).decodeToString | ||
val problemReport = jsonString.fromJson[PlaintextMessage].flatMap(ProblemReport.fromPlaintextMessage) | ||
assert(problemReport)( | ||
isRight( | ||
hasField("code", (p: ProblemReport) => p.code, equalTo(ProblemCode.ErroFail("req", "not_enroll"))) && | ||
hasField( | ||
"from", | ||
(p: ProblemReport) => p.from, | ||
equalTo(alice) | ||
) | ||
) | ||
) | ||
} | ||
} @@ TestAspect.before(setupAndClean), | ||
test("Forward message for enrolled DID receives NoReply") { | ||
val executer = ForwardMessageExecuter | ||
for { | ||
userAccount <- ZIO.service[UserAccountRepo] | ||
result <- userAccount.createOrFindDidAccount(DIDSubject(alice)) | ||
result <- userAccount.addAlias(owner = DIDSubject(alice), newAlias = DIDSubject(alice)) | ||
msg <- ZIO.fromEither(plaintextForwardEnrolledDidMessage) | ||
result <- executer.execute(msg) | ||
} yield assertTrue(result.isEmpty) | ||
|
||
} @@ TestAspect.before(setupAndClean) | ||
).provideSomeLayer(DidPeerResolver.layerDidPeerResolver) | ||
.provideSomeLayer(Operations.layerDefault) | ||
.provideSomeLayer(Scope.default >>> Client.default >>> MessageDispatcherJVM.layer) | ||
.provideSomeLayer(DidPeerResolver.layerDidPeerResolver) | ||
.provideSomeLayer(AgentStub.agentLayer) | ||
.provideLayerShared(dataAccessLayer) @@ TestAspect.sequential | ||
|
||
def setupAndClean = { | ||
for { | ||
userAccount <- ZIO.service[UserAccountRepo] | ||
col <- userAccount.collection | ||
_ = col.indexesManager.create(index) | ||
_ = col.delete.one(BSONDocument()) | ||
} yield {} | ||
} | ||
|
||
val dataAccessLayer = EmbeddedMongoDBInstance.layer(port, hostIp) | ||
>>> AsyncDriverResource.layer | ||
>>> ReactiveMongoApi.layer(connectionString) | ||
>>> (UserAccountRepo.layer ++ MessageItemRepo.layer ++ OutboxMessageRepo.layer) | ||
|
||
} |
Oops, something went wrong.