|
| 1 | +package org.scalaexercises.evaluator |
| 2 | + |
| 3 | +import org.scalatest._ |
| 4 | +import org.http4s._ |
| 5 | +import org.http4s.client.blaze._ |
| 6 | +import org.http4s.circe._ |
| 7 | +import io.circe.generic.auto._ |
| 8 | +import scalaz.concurrent.Task |
| 9 | +import scala.concurrent.duration._ |
| 10 | + |
| 11 | +import pdi.jwt.{Jwt, JwtAlgorithm} |
| 12 | + |
| 13 | +class Smoketests extends FunSpec with Matchers with CirceInstances { |
| 14 | + |
| 15 | + case class EvaluatorResponse(msg: String, |
| 16 | + value: String, |
| 17 | + valueType: String, |
| 18 | + compilationInfos: Map[String, String]) |
| 19 | + |
| 20 | + implicit val decoder: EntityDecoder[EvaluatorResponse] = |
| 21 | + jsonOf[EvaluatorResponse] |
| 22 | + |
| 23 | + val validToken = Jwt.encode( |
| 24 | + """{"user": "scala-exercises"}""", |
| 25 | + auth.secretKey, |
| 26 | + JwtAlgorithm.HS256) |
| 27 | + |
| 28 | + def makeRequest(code: String)( |
| 29 | + expectation: EvaluatorResponse => Unit, |
| 30 | + failExpectation: Throwable => Unit = fail(_)): Unit = { |
| 31 | + |
| 32 | + val request = new Request( |
| 33 | + method = Method.POST, |
| 34 | + uri = Uri.uri("http://scala-evaluator-sandbox.herokuapp.com/eval"), |
| 35 | + headers = Headers(headers) |
| 36 | + ).withBody( |
| 37 | + s"""{"resolvers" : [], "dependencies" : [], "code" : "$code"}""") |
| 38 | + |
| 39 | + val task = client.expect[EvaluatorResponse](request) |
| 40 | + |
| 41 | + val response = task.unsafePerformSyncAttemptFor(60.seconds) |
| 42 | + response.fold(failExpectation, expectation) |
| 43 | + } |
| 44 | + |
| 45 | + val headers = List( |
| 46 | + Header("Content-Type", "application/json").parsed, |
| 47 | + Header("x-scala-eval-api-token", validToken).parsed |
| 48 | + ) |
| 49 | + |
| 50 | + val client = PooledHttp1Client() |
| 51 | + |
| 52 | + describe("Querying the /eval endpoint") { |
| 53 | + it("should succeed for a simple request") { |
| 54 | + makeRequest("1 + 1") { evaluatorResponse => |
| 55 | + evaluatorResponse.value shouldBe "2" |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + it("should continue to work after calling System.exit") { |
| 60 | + makeRequest("System.exit(1)")( |
| 61 | + expectation = _ => fail("Request should not succeed"), |
| 62 | + failExpectation = _ => () |
| 63 | + ) |
| 64 | + |
| 65 | + makeRequest("1 + 1") { evaluatorResponse => |
| 66 | + evaluatorResponse.value shouldBe "2" |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + it("should not expose sensitive details by calling sys.env") { |
| 71 | + val keywords = List("password", "key", "api") |
| 72 | + makeRequest("sys.env") { evaluatorResponse => |
| 73 | + keywords.foreach(kw => |
| 74 | + evaluatorResponse.value.contains(kw) shouldBe false) |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments