This repository has been archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tests for utils and peers routes
- Loading branch information
Showing
5 changed files
with
184 additions
and
4 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
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
70 changes: 70 additions & 0 deletions
70
src/test/scala/scorex/core/api/http/PeersApiRouteSpec.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,70 @@ | ||
package scorex.core.api.http | ||
|
||
import java.net.InetSocketAddress | ||
|
||
import akka.http.scaladsl.model.StatusCodes | ||
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest} | ||
import akka.testkit.TestDuration | ||
import io.circe.syntax._ | ||
import org.scalatest.{FlatSpec, Matchers} | ||
import scorex.core.settings.RESTApiSettings | ||
|
||
import scala.concurrent.duration._ | ||
|
||
class PeersApiRouteSpec extends FlatSpec | ||
with Matchers | ||
with ScalatestRouteTest | ||
with Stubs { | ||
|
||
implicit val timeout = RouteTestTimeout(15.seconds dilated) | ||
|
||
val addr = new InetSocketAddress("localhost", 8080) | ||
val restApiSettings = RESTApiSettings(addr, None, false, 10 seconds) | ||
val prefix = "/peers" | ||
val routes = PeersApiRoute(pmRef, networkControllerRef, restApiSettings).route | ||
|
||
val peersResp = peers.map { case (address, peerInfo) => | ||
Seq( | ||
Some("address" -> address.toString.asJson), | ||
Some("lastSeen" -> peerInfo.lastSeen.asJson), | ||
peerInfo.nodeName.map(name => "name" -> name.asJson), | ||
peerInfo.nonce.map(nonce => "nonce" -> nonce.asJson)).flatten.toMap | ||
}.asJson.toString | ||
|
||
val connectedPeersResp = connectedPeers.map { handshake => | ||
Map( | ||
"address" -> handshake.declaredAddress.toString.asJson, | ||
"name" -> handshake.nodeName.asJson, | ||
"lastSeen" -> handshake.time.asJson | ||
).asJson | ||
}.asJson | ||
|
||
it should "get all peers" in { | ||
Get(prefix + "/all") ~> routes ~> check { | ||
status shouldBe StatusCodes.OK | ||
peersResp shouldBe responseAs[String] | ||
} | ||
} | ||
|
||
//can't check it cause original node using now() timestamp for last seen field | ||
ignore should "get connected peers" in { | ||
Get(prefix + "/connected") ~> routes ~> check { | ||
status shouldBe StatusCodes.OK | ||
connectedPeersResp shouldBe responseAs[String] | ||
} | ||
} | ||
|
||
it should "connect to peer" in { | ||
Post(prefix + "/connect", "localhost:8080") ~> routes ~> check { | ||
status shouldBe StatusCodes.OK | ||
} | ||
} | ||
|
||
it should "get blacklisted peers" in { | ||
Get(prefix + "/blacklisted") ~> routes ~> check { | ||
status shouldBe StatusCodes.OK | ||
Map("address" -> blacklistedPeers).asJson.toString shouldBe responseAs[String] | ||
} | ||
} | ||
|
||
} |
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,56 @@ | ||
package scorex.core.api.http | ||
|
||
import java.net.InetSocketAddress | ||
|
||
import akka.actor.{Actor, ActorSystem, Props} | ||
import scorex.core.app.Version | ||
import scorex.core.network.Handshake | ||
import scorex.core.network.peer.{PeerInfo, PeerManager} | ||
|
||
trait Stubs { | ||
|
||
implicit val system: ActorSystem | ||
|
||
val inetAddr1 = new InetSocketAddress("92.92.92.92",27017) | ||
val inetAddr2 = new InetSocketAddress("93.93.93.93",27017) | ||
val ts1 = System.currentTimeMillis() - 100 | ||
val ts2 = System.currentTimeMillis() + 100 | ||
|
||
val peers = Map( | ||
inetAddr1 -> PeerInfo(ts1, Some(1L), Some("first")), | ||
inetAddr2 -> PeerInfo(ts2, Some(2L), Some("second")) | ||
) | ||
|
||
val protocolVersion = Version("1.1.1") | ||
|
||
val connectedPeers = Seq( | ||
Handshake("node_pop", protocolVersion, "first", Some(inetAddr1), ts1), | ||
Handshake("node_pop", protocolVersion, "second", Some(inetAddr2), ts2) | ||
) | ||
|
||
val blacklistedPeers = Seq("4.4.4.4:1111", "8.8.8.8:2222") | ||
|
||
class PeersManagerStub extends Actor { | ||
def receive = { | ||
case PeerManager.GetConnectedPeers => sender() ! connectedPeers | ||
case PeerManager.GetAllPeers => sender() ! peers | ||
case PeerManager.GetBlacklistedPeers => sender() ! blacklistedPeers | ||
} | ||
} | ||
|
||
object PeersManagerStub { | ||
def props() = Props(new PeersManagerStub) | ||
} | ||
|
||
class NetworkControllerStub extends Actor { | ||
def receive = { case _ => () } | ||
} | ||
|
||
object NetworkControllerStub { | ||
def props() = Props(new NetworkControllerStub) | ||
} | ||
|
||
lazy val pmRef = system.actorOf(PeersManagerStub.props()) | ||
lazy val networkControllerRef = system.actorOf(NetworkControllerStub.props()) | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/test/scala/scorex/core/api/http/UtilsApiRouteSpec.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,52 @@ | ||
package scorex.core.api.http | ||
|
||
import java.net.InetSocketAddress | ||
|
||
import akka.http.scaladsl.model.StatusCodes | ||
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest} | ||
import akka.testkit.TestDuration | ||
import org.scalatest.{FlatSpec, Matchers} | ||
import scorex.core.settings.RESTApiSettings | ||
|
||
import scala.concurrent.duration._ | ||
|
||
class UtilsApiRouteSpec extends FlatSpec | ||
with Matchers | ||
with ScalatestRouteTest | ||
with Stubs { | ||
|
||
implicit val timeout = RouteTestTimeout(15.seconds dilated) | ||
|
||
val addr = new InetSocketAddress("localhost", 8080) | ||
val restApiSettings = RESTApiSettings(addr, None, false, 10 seconds) | ||
val prefix = "/utils" | ||
val routes = UtilsApiRoute(restApiSettings).route | ||
|
||
it should "send random seeds" in { | ||
Get(prefix + "/seed") ~> routes ~> check { | ||
status shouldBe StatusCodes.OK | ||
responseAs[String] should not be empty | ||
} | ||
|
||
Get(prefix + "/seed/32") ~> routes ~> check { | ||
status shouldBe StatusCodes.OK | ||
responseAs[String] should not be empty | ||
} | ||
|
||
Get(prefix + "/seed/64") ~> routes ~> check { | ||
status shouldBe StatusCodes.OK | ||
responseAs[String] should not be empty | ||
} | ||
} | ||
|
||
val msg = "hash_me" | ||
|
||
it should "hash string with blake2b" in { | ||
Post(prefix + "/hash/blake2b", msg) ~> routes ~> check { | ||
status shouldBe StatusCodes.OK | ||
responseAs[String] should not be empty | ||
responseAs[String] should not be msg | ||
} | ||
} | ||
|
||
} |