<commit_before>import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import play.api.libs.json.{JsValue, Json}
import play.api.libs.json._
import play.api.mvc.Result
import play.api.test.Helpers._
import play.api.test._

import scala.concurrent.Future

class ApplicationSpec extends PlaySpec with OneAppPerSuite {

  implicit override lazy val app: FakeApplication = {
    FakeApplication(
      additionalConfiguration = Map(
        "ansible.playbooks" -> "test/resources",
        "ansible.vault_password_file" -> "test/resources/password",
        "ansible.command" -> "test/resources/ansible")
    )
  }

  "Rocannon" should {

    implicit val extraVars: JsValue = Json.parse( """{"version": "1.0" }""")

    "send 404 on a non existing inventory" in {
      val result = route(FakeRequest(GET, "/not-an-inventory")).get
      status(result) must be(NOT_FOUND)
    }

    "send 404 on a non existing playbook" in {
      val result = route(FakeRequest(GET, "/inventory/not-a-file")).get
      status(result) must be(NOT_FOUND)
    }

    "render the index page" in {
      val home = route(FakeRequest(GET, "/")).get
      status(home) must be(OK)
      contentType(home) must be(Some("text/html"))
      contentAsString(home) must include("Rocannon")
    }

    "respond on ping" in {
      val home = route(FakeRequest(GET, "/ping")).get
      status(home) must be(OK)
    }

    "run a playbook with success" in {
      val refId = "4711"
      val  result = post(refId)
      verifySuccess(result, refId)
    }

    "build with \" are escaped" in {
      val refId = """ evil\" ' \'"""
      val refEscaped = " evil/^ ^ /^"
      val result = post(refId)
      verifySuccess(result, refEscaped)
    }

    "report error if the playbook fails" in {
      val refId = "17"
      val result = post(refId)(Json.parse( """{"failure": "true" }"""))
      status(result) must be(SERVICE_UNAVAILABLE)
      contentType(result) must be(Some("application/json"))
      contentAsString(result) must include( """"refId":"17","status":"failed","execTime":0,"message":""")
    }

    "report empty refId in response if refId query parameter is empty" in {
      val result = post()
      verifySuccess(result, "")
    }

  }

  def post()(implicit extraVars:JsValue): Future[Result] = {
    route(FakeRequest(POST, s"/inventory/play.yaml", FakeHeaders(), extraVars)).get
  }

  def post(refId: String)(implicit extraVars:JsValue): Future[Result] = {
    route(FakeRequest(POST, s"/inventory/play.yaml?refId=$refId", FakeHeaders(), extraVars)).get
  }

  def verifySuccess(result: Future[Result], refId: String): Unit = {
    status(result) must be(OK)
    contentType(result) must be(Some("application/json"))
    val js = Json.parse(contentAsString(result))
    (js \ "buildId").as[Port] must be > 0
    (js \ "refId").as[String] must startWith (refId)
    (js \ "status").as[String] must be("success")
    (js \ "execTime").as[Port] must be(0)
    val message = (js \ "message").as[String]
    message must startWith("-i test/resources/inventory -e {^version^:^1.0^} --vault-password-file")
    message must endWith("test/resources/play.yaml")
  }
}<commit_msg>Fixed failing test case, using regex.<commit_after>import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import play.api.libs.json.{JsValue, Json}
import play.api.libs.json._
import play.api.mvc.Result
import play.api.test.Helpers._
import play.api.test._

import scala.concurrent.Future

class ApplicationSpec extends PlaySpec with OneAppPerSuite {

  implicit override lazy val app: FakeApplication = {
    FakeApplication(
      additionalConfiguration = Map(
        "ansible.playbooks" -> "test/resources",
        "ansible.vault_password_file" -> "test/resources/password",
        "ansible.command" -> "test/resources/ansible")
    )
  }

  "Rocannon" should {

    implicit val extraVars: JsValue = Json.parse( """{"version": "1.0" }""")

    "send 404 on a non existing inventory" in {
      val result = route(FakeRequest(GET, "/not-an-inventory")).get
      status(result) must be(NOT_FOUND)
    }

    "send 404 on a non existing playbook" in {
      val result = route(FakeRequest(GET, "/inventory/not-a-file")).get
      status(result) must be(NOT_FOUND)
    }

    "render the index page" in {
      val home = route(FakeRequest(GET, "/")).get
      status(home) must be(OK)
      contentType(home) must be(Some("text/html"))
      contentAsString(home) must include("Rocannon")
    }

    "respond on ping" in {
      val home = route(FakeRequest(GET, "/ping")).get
      status(home) must be(OK)
    }

    "run a playbook with success" in {
      val refId = "4711"
      val  result = post(refId)
      verifySuccess(result, refId)
    }

    "build with \" are escaped" in {
      val refId = """ evil\" ' \'"""
      val refEscaped = " evil/^ ^ /^"
      val result = post(refId)
      verifySuccess(result, refEscaped)
    }

    "report error if the playbook fails" in {
      val refId = "17"
      val result = post(refId)(Json.parse( """{"failure": "true" }"""))
      status(result) must be(SERVICE_UNAVAILABLE)
      contentType(result) must be(Some("application/json"))
      contentAsString(result) must include( """"refId":"17","status":"failed","execTime":0,"message":""")
    }

    "report empty refId in response if refId query parameter is empty" in {
      val result = post()
      verifySuccess(result, "")
    }

  }

  def post()(implicit extraVars:JsValue): Future[Result] = {
    route(FakeRequest(POST, s"/inventory/play.yaml", FakeHeaders(), extraVars)).get
  }

  def post(refId: String)(implicit extraVars:JsValue): Future[Result] = {
    route(FakeRequest(POST, s"/inventory/play.yaml?refId=$refId", FakeHeaders(), extraVars)).get
  }

  def verifySuccess(result: Future[Result], refId: String): Unit = {
    status(result) must be(OK)
    contentType(result) must be(Some("application/json"))
    val js = Json.parse(contentAsString(result))
    (js \ "buildId").as[Port] must be > 0
    (js \ "refId").as[String] must startWith (refId)
    (js \ "status").as[String] must be("success")
    (js \ "execTime").as[Port] must be(0)
    val message = (js \ "message").as[String]
    message must fullyMatch regex """-i test/resources/inventory -e \{\^version\^:\^1.0\^\} --vault-password-file .* test/resources/play.yaml\n""".r
  }
}
{'fraction_non_alphanumeric': 0.15166325082768406, 'fraction_numerical': 0.006621472489358348, 'mean_word_length': 3.295192958700068, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4981263', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2055, 'n_words': 485}