-
Notifications
You must be signed in to change notification settings - Fork 78
[ETCM-395] faucet unit tests #816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e6624e5
add new unit tests
biandratti e98cd9e
fix faucet rpc service when dont find the faucet actor
biandratti a25d06e
Merge branch 'develop' into ETCM-395-faucet-test
lemastero 0e13a76
change test name
biandratti d75714f
Merge branch 'develop' of github.com:input-output-hk/mantis into ETCM…
biandratti 8bc649e
Merge branch 'ETCM-395-faucet-test' of github.com:input-output-hk/man…
biandratti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
161 changes: 161 additions & 0 deletions
161
src/test/scala/io/iohk/ethereum/faucet/jsonrpc/FaucetRpcServiceSpec.scala
This file contains hidden or 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,161 @@ | ||
| package io.iohk.ethereum.faucet.jsonrpc | ||
|
|
||
| import akka.actor.{ActorRef, ActorSystem} | ||
| import akka.testkit.{TestKit, TestProbe} | ||
| import akka.util.ByteString | ||
| import io.iohk.ethereum.domain.Address | ||
| import io.iohk.ethereum.faucet.FaucetHandler.FaucetHandlerMsg | ||
| import io.iohk.ethereum.faucet.FaucetHandler.FaucetHandlerResponse.{ | ||
| FaucetIsUnavailable, | ||
| StatusResponse, | ||
| TransactionSent, | ||
| WalletRpcClientError | ||
| } | ||
| import io.iohk.ethereum.faucet.FaucetStatus.WalletAvailable | ||
| import io.iohk.ethereum.faucet.jsonrpc.FaucetDomain.{SendFundsRequest, StatusRequest} | ||
| import io.iohk.ethereum.faucet.{FaucetConfig, SupervisorConfig} | ||
| import io.iohk.ethereum.jsonrpc.JsonRpcError | ||
| import io.iohk.ethereum.testing.ActorsTesting.simpleAutoPilot | ||
| import io.iohk.ethereum.{NormalPatience, WithActorSystemShutDown} | ||
| import monix.eval.Task | ||
| import monix.execution.Scheduler.Implicits.global | ||
| import org.bouncycastle.util.encoders.Hex | ||
| import org.scalactic.TypeCheckedTripleEquals | ||
| import org.scalamock.scalatest.MockFactory | ||
| import org.scalatest.OptionValues | ||
| import org.scalatest.concurrent.ScalaFutures | ||
| import org.scalatest.flatspec.AnyFlatSpecLike | ||
| import org.scalatest.matchers.should.Matchers | ||
|
|
||
| import scala.concurrent.duration._ | ||
|
|
||
| class FaucetRpcServiceSpec | ||
| extends TestKit(ActorSystem("ActorSystem_DebugFaucetRpcServiceSpec")) | ||
| with AnyFlatSpecLike | ||
| with WithActorSystemShutDown | ||
| with Matchers | ||
| with ScalaFutures | ||
| with OptionValues | ||
| with MockFactory | ||
| with NormalPatience | ||
| with TypeCheckedTripleEquals { | ||
|
|
||
| "FaucetRpcService" should "answer txHash correctly when the wallet is available and the requested send funds be successfully" in new TestSetup { | ||
| val address: Address = Address("0x00") | ||
| val request: SendFundsRequest = SendFundsRequest(address) | ||
| val txHash: ByteString = ByteString(Hex.decode("112233")) | ||
|
|
||
| fHandler.setAutoPilot(simpleAutoPilot { case FaucetHandlerMsg.SendFunds(`address`) => | ||
| TransactionSent(txHash) | ||
| }) | ||
| faucetRpcService.sendFunds(request).runSyncUnsafe(Duration.Inf) match { | ||
| case Left(error) => fail(s"failure with error: $error") | ||
| case Right(response) => response.txId shouldBe txHash | ||
| } | ||
| } | ||
|
|
||
| it should "answer WalletRpcClientError when the wallet is available and the requested send funds be failure" in new TestSetup { | ||
| val address: Address = Address("0x00") | ||
| val request: SendFundsRequest = SendFundsRequest(address) | ||
| val clientError: String = "Parser error" | ||
|
|
||
| fHandler.setAutoPilot(simpleAutoPilot { case FaucetHandlerMsg.SendFunds(`address`) => | ||
| WalletRpcClientError(clientError) | ||
| }) | ||
| faucetRpcService.sendFunds(request).runSyncUnsafe(Duration.Inf) match { | ||
| case Right(_) => fail() | ||
| case Left(error) => error shouldBe JsonRpcError.LogicError(s"Faucet error: $clientError") | ||
| } | ||
| } | ||
|
|
||
| it should "answer FaucetIsUnavailable when tried to send funds and the wallet is unavailable" in new TestSetup { | ||
| val address: Address = Address("0x00") | ||
| val request: SendFundsRequest = SendFundsRequest(address) | ||
|
|
||
| fHandler.setAutoPilot(simpleAutoPilot { case FaucetHandlerMsg.SendFunds(`address`) => | ||
| FaucetIsUnavailable | ||
| }) | ||
| faucetRpcService.sendFunds(request).runSyncUnsafe(Duration.Inf) match { | ||
| case Right(_) => fail() | ||
| case Left(error) => | ||
| error shouldBe JsonRpcError.LogicError("Faucet is unavailable: Please try again in a few more seconds") | ||
| } | ||
| } | ||
|
|
||
| it should "answer FaucetIsUnavailable when tried to get status and the wallet is unavailable" in new TestSetup { | ||
| fHandler.setAutoPilot(simpleAutoPilot { case FaucetHandlerMsg.Status => | ||
| FaucetIsUnavailable | ||
| }) | ||
| faucetRpcService.status(StatusRequest()).runSyncUnsafe(Duration.Inf) match { | ||
| case Right(_) => fail() | ||
| case Left(error) => | ||
| error shouldBe JsonRpcError.LogicError("Faucet is unavailable: Please try again in a few more seconds") | ||
| } | ||
| } | ||
|
|
||
| it should "answer WalletAvailable when tried to get status and the wallet is available" in new TestSetup { | ||
| fHandler.setAutoPilot(simpleAutoPilot { case FaucetHandlerMsg.Status => | ||
| StatusResponse(WalletAvailable) | ||
| }) | ||
| faucetRpcService.status(StatusRequest()).runSyncUnsafe(Duration.Inf) match { | ||
| case Left(error) => fail(s"failure with error: $error") | ||
| case Right(response) => response shouldBe FaucetDomain.StatusResponse(WalletAvailable) | ||
| } | ||
| } | ||
|
|
||
| it should "answer internal error when tried to send funds but the Faucet Handler is disable" in new TestSetup { | ||
| val address: Address = Address("0x00") | ||
| val request: SendFundsRequest = SendFundsRequest(address) | ||
|
|
||
| faucetRpcServiceWithoutFaucetHandler.sendFunds(request).runSyncUnsafe(Duration.Inf) match { | ||
| case Right(_) => fail() | ||
| case Left(error) => | ||
| error shouldBe JsonRpcError.InternalError | ||
| } | ||
| } | ||
|
|
||
| it should "answer internal error when tried to get status but the Faucet Handler is disable" in new TestSetup { | ||
| val address: Address = Address("0x00") | ||
| val request: SendFundsRequest = SendFundsRequest(address) | ||
|
|
||
| faucetRpcServiceWithoutFaucetHandler.status(StatusRequest()).runSyncUnsafe(Duration.Inf) match { | ||
| case Right(_) => fail() | ||
| case Left(error) => | ||
| error shouldBe JsonRpcError.InternalError | ||
| } | ||
| } | ||
|
|
||
| class TestSetup(implicit system: ActorSystem) { | ||
|
|
||
| val config: FaucetConfig = FaucetConfig( | ||
| walletAddress = Address("0x99"), | ||
| walletPassword = "", | ||
| txGasPrice = 10, | ||
| txGasLimit = 20, | ||
| txValue = 1, | ||
| rpcAddress = "", | ||
| keyStoreDir = "", | ||
| minRequestInterval = 10.seconds, | ||
| handlerTimeout = 10.seconds, | ||
| responseTimeout = 10.seconds, | ||
| supervisor = mock[SupervisorConfig], | ||
| shutdownTimeout = 15.seconds | ||
| ) | ||
|
|
||
| val fHandler = TestProbe() | ||
|
|
||
| val faucetRpcService: FaucetRpcService = new FaucetRpcService(config) { | ||
|
|
||
| override def faucetHandler()(implicit system: ActorSystem): Task[ActorRef] = { | ||
| Task(fHandler.ref) | ||
| } | ||
| } | ||
|
|
||
| val faucetRpcServiceWithoutFaucetHandler: FaucetRpcService = new FaucetRpcService(config) { | ||
| override def faucetHandler()(implicit system: ActorSystem): Task[ActorRef] = { | ||
| Task.raiseError(new RuntimeException("time out")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.