Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
added tests for utils and peers routes
Browse files Browse the repository at this point in the history
  • Loading branch information
terjokhin committed Jan 8, 2018
1 parent d1f0458 commit 2c3620d
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 4 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ val circeVersion = "0.8.0"

val networkDependencies = Seq(
"com.typesafe.akka" %% "akka-actor" % "2.4.+",
"com.typesafe.akka" % "akka-stream_2.12" % "2.4.+",
"org.bitlet" % "weupnp" % "0.1.+",
"commons-net" % "commons-net" % "3.+"
)
Expand All @@ -60,6 +61,7 @@ val loggingDependencies = Seq(

val testingDependencies = Seq(
"com.typesafe.akka" %% "akka-testkit" % "2.5.3" % "test",
"com.typesafe.akka" %% "akka-http-testkit" % "10.+" % "test",
"org.scalactic" %% "scalactic" % "3.0.3" % "test",
"org.scalatest" %% "scalatest" % "3.0.3" % "test",
"org.scalacheck" %% "scalacheck" % "1.13.+",
Expand Down
8 changes: 4 additions & 4 deletions lock.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ dependencyOverrides in ThisBuild ++= Seq(
"com.typesafe" % "config" % "1.3.1",
"com.typesafe" % "ssl-config-core_2.12" % "0.2.1",
"com.typesafe.akka" % "akka-actor_2.12" % "2.4.20",
"com.typesafe.akka" % "akka-http-core_2.12" % "10.0.11",
"com.typesafe.akka" % "akka-http_2.12" % "10.0.11",
"com.typesafe.akka" % "akka-parsing_2.12" % "10.0.11",
"com.typesafe.akka" % "akka-http-core_2.12" % "10.1.0-RC1",
"com.typesafe.akka" % "akka-http_2.12" % "10.1.0-RC1",
"com.typesafe.akka" % "akka-parsing_2.12" % "10.1.0-RC1",
"com.typesafe.akka" % "akka-stream_2.12" % "2.4.20",
"commons-net" % "commons-net" % "3.6",
"de.heikoseeberger" % "akka-http-circe_2.12" % "1.18.0",
Expand All @@ -40,4 +40,4 @@ dependencyOverrides in ThisBuild ++= Seq(
"org.typelevel" % "macro-compat_2.12" % "1.1.1",
"org.whispersystems" % "curve25519-java" % "0.4.1"
)
// LIBRARY_DEPENDENCIES_HASH fa340304e0dc7ea5f1fa6cc179f9e491ba80ed22
// LIBRARY_DEPENDENCIES_HASH 94421a7d9d024b348e7fef67ef15b92a218b200c
70 changes: 70 additions & 0 deletions src/test/scala/scorex/core/api/http/PeersApiRouteSpec.scala
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]
}
}

}
56 changes: 56 additions & 0 deletions src/test/scala/scorex/core/api/http/Stubs.scala
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 src/test/scala/scorex/core/api/http/UtilsApiRouteSpec.scala
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
}
}

}

0 comments on commit 2c3620d

Please sign in to comment.