|
| 1 | +package lib |
| 2 | + |
| 3 | +import io.circe.Json |
| 4 | +import io.circe.parser._ |
| 5 | +import lib.db.DbScheme |
| 6 | +import org.scalatest.FunSuite |
| 7 | +import org.scalatest.concurrent.Eventually |
| 8 | +import org.scalatest.time.{Seconds, Span} |
| 9 | +import org.scalatestplus.play.guice.GuiceOneServerPerSuite |
| 10 | +import play.api.Application |
| 11 | +import play.api.inject.guice.GuiceApplicationBuilder |
| 12 | +import scalaj.http.Http |
| 13 | +import scalikejdbc.{ConnectionPool, DB} |
| 14 | + |
| 15 | +import scala.io.Source |
| 16 | + |
| 17 | +class AppTest extends FunSuite with Eventually with GuiceOneServerPerSuite { |
| 18 | + |
| 19 | + private val rbackupIp: String = { |
| 20 | + val ip = Option(System.getenv("RBACKUP_IP")).getOrElse("localhost") |
| 21 | + |
| 22 | + if (ip == "0.0.0.0") "localhost" else ip |
| 23 | + } |
| 24 | + |
| 25 | + test("app starts") { |
| 26 | + eventually(timeout(Span(10, Seconds))) { |
| 27 | + Source.fromURL(s"http://$rbackupIp:$port/").mkString // it should only not fail |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + test("returns js bundle") { |
| 32 | + eventually(timeout(Span(10, Seconds))) { |
| 33 | + Source.fromURL(s"http://$rbackupIp:$port/assets/bundle/js.bundle.txt").mkString // it should only not fail |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + test("returns status") { |
| 38 | + eventually(timeout(Span(10, Seconds))) { |
| 39 | + val json = sendCommand("status", Json.Null) |
| 40 | + |
| 41 | + assertResult(Right("INSTALLED"))(json.hcursor.get[String]("status")) |
| 42 | + assertResult(Right(true))(json.hcursor.get[Boolean]("success")) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + override def fakeApplication(): Application = { |
| 47 | + ConnectionPool.singleton(s"jdbc:h2:tcp://$rbackupIp:1521/test;MODE=MySQL", "sa", "") |
| 48 | + |
| 49 | + DB.autoCommit { implicit session => |
| 50 | + DbScheme.dropAll |
| 51 | + } |
| 52 | + |
| 53 | + new GuiceApplicationBuilder() |
| 54 | + .configure("play.filters.hosts.allowed" -> Seq(s"localhost:$port")) |
| 55 | + .build() |
| 56 | + } |
| 57 | + |
| 58 | + private def sendCommand(name: String, data: Json): Json = { |
| 59 | + val resp = Http(s"http://localhost:$port/ajax-api").postData(s"""{"name": "$name", "data": $data}""").asString |
| 60 | + |
| 61 | + if (!resp.is2xx) fail(resp.body) |
| 62 | + |
| 63 | + parse(resp.body).toTry.fold(throw _, identity) |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments