Skip to content

Commit

Permalink
#2024 Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed May 21, 2021
1 parent 6cef50b commit df79001
Show file tree
Hide file tree
Showing 18 changed files with 73 additions and 47 deletions.
28 changes: 17 additions & 11 deletions dto/src/main/scala/org/thp/thehive/dto/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import org.thp.scalligraph.InvalidFormatAttributeError
import org.thp.scalligraph.controllers.{FNumber, FString}

package object dto {
type Severity = Int Refined And[Positive, LessEqual[W.`4`.T]]
type Tlp = Int Refined And[NonNegative, LessEqual[W.`3`.T]]
type Pap = Int Refined And[NonNegative, LessEqual[W.`3`.T]]
type Color = String Refined MatchesRegex[W.`"""^#[0-9a-fA-F]{6,6}|$"""`.T]
type StringX[N] = String Refined AllOf[Not[Empty] :: MaxSize[N] :: Not[MatchesRegex[W.`"""[\n\r]"""`.T]] :: HNil]
type String16 = StringX[W.`16`.T]
type String32 = StringX[W.`32`.T]
type String64 = StringX[W.`64`.T]
type String128 = StringX[W.`128`.T]
type String512 = StringX[W.`512`.T]
type Description = String Refined MaxSize[W.`1048576`.T]
type Severity = Int Refined And[Positive, LessEqual[W.`4`.T]]
type Tlp = Int Refined And[NonNegative, LessEqual[W.`3`.T]]
type Pap = Int Refined And[NonNegative, LessEqual[W.`3`.T]]
type Color = String Refined MatchesRegex[W.`"""^#[0-9a-fA-F]{6,6}|$"""`.T]
type StringX[N] = String Refined AllOf[Not[Empty] :: MaxSize[N] :: Not[MatchesRegex[W.`"""[\n\r]"""`.T]] :: HNil]
type String16 = StringX[W.`16`.T]
type String32 = StringX[W.`32`.T]
type String64 = StringX[W.`64`.T]
type String128 = StringX[W.`128`.T]
type String512 = StringX[W.`512`.T]
type MultiLineString512 = String Refined And[Not[Empty], MaxSize[512]]
type Description = String Refined MaxSize[W.`1048576`.T]

object Severity {
def apply(value: Int): Severity =
Expand Down Expand Up @@ -52,6 +53,11 @@ package object dto {
RefType.applyRef[String512](value).fold(error => throw InvalidFormatAttributeError(name, error, Set.empty, FString(value)), identity)
}

object MultiLineString512 {
def apply(name: String, value: String): MultiLineString512 =
RefType.applyRef[MultiLineString512](value).fold(error => throw InvalidFormatAttributeError(name, error, Set.empty, FString(value)), identity)
}

object Description {
def apply(name: String, value: String): Description =
RefType.applyRef[Description](value).fold(error => throw InvalidFormatAttributeError(name, error, Set.empty, FString(value)), identity)
Expand Down
14 changes: 7 additions & 7 deletions dto/src/main/scala/org/thp/thehive/dto/v0/Observable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package org.thp.thehive.dto.v0
import org.scalactic.Accumulation._
import org.scalactic.Good
import org.thp.scalligraph.controllers._
import org.thp.thehive.dto.{Description, String128, String16, String512, Tlp}
import org.thp.thehive.dto.{Description, MultiLineString512, String128, String32, String512, Tlp}
import play.api.libs.json.{JsObject, Json, OFormat, Writes}

import java.util.Date
import be.venneborg.refined.play.RefinedJsonFormats._

case class InputObservable(
dataType: String16,
dataType: String32,
@WithParser(InputObservable.dataParser)
data: Seq[String512] = Nil,
data: Seq[MultiLineString512] = Nil,
message: Option[Description] = None,
startDate: Option[Date] = None,
@WithParser(InputObservable.fileOrAttachmentParser)
Expand All @@ -31,10 +31,10 @@ object InputObservable {
}
implicit val writes: Writes[InputObservable] = Json.writes[InputObservable]

val dataParser: FieldsParser[Seq[String]] = FieldsParser[Seq[String]]("data") {
case (_, FString(s)) => Good(Seq(s))
case (_, FAny(s)) => Good(s)
case (_, FSeq(a)) => a.validatedBy(FieldsParser.string(_))
val dataParser: FieldsParser[Seq[MultiLineString512]] = FieldsParser[Seq[MultiLineString512]]("data") {
case (_, FString(s)) => Good(Seq(MultiLineString512("data", s)))
case (_, FAny(s)) => Good(s.map(MultiLineString512("data", _)))
case (_, FSeq(a)) => a.validatedBy(d => FieldsParser.string(d).map(MultiLineString512("data", _)))
case (_, FUndefined) => Good(Nil)
}

Expand Down
4 changes: 2 additions & 2 deletions dto/src/main/scala/org/thp/thehive/dto/v1/Observable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package org.thp.thehive.dto.v1
import org.scalactic.Accumulation._
import org.scalactic.Good
import org.thp.scalligraph.controllers._
import org.thp.thehive.dto.{Description, String128, String16, String512, Tlp}
import org.thp.thehive.dto.{Description, String128, String32, String512, Tlp}
import play.api.libs.json.{JsObject, Json, OFormat, Writes}

import java.util.Date
import be.venneborg.refined.play.RefinedJsonFormats._

case class InputObservable(
dataType: String16,
dataType: String32,
@WithParser(InputObservable.dataParser)
data: Seq[String512] = Nil,
message: Option[Description] = None,
Expand Down
4 changes: 2 additions & 2 deletions dto/src/main/scala/org/thp/thehive/dto/v1/Taxonomy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ case class InputTaxonomy(

case class InputPredicate(
value: String128,
expanded: Option[String128],
expanded: Option[Description],
exclusive: Option[Boolean],
description: Option[Description],
colour: Option[Color]
Expand All @@ -36,7 +36,7 @@ case class InputValue(

case class InputEntry(
value: String128,
expanded: Option[String128],
expanded: Option[Description],
colour: Option[Color],
description: Option[Description],
numerical_value: Option[Int]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class TaxonomyCtrl(

private def parseJsonFile(zipFile: ZipFile, h: FileHeader): Try[InputTaxonomy] =
Try(Json.parse(zipFile.getInputStream(h)).as[InputTaxonomy]).recoverWith {
case _ => Failure(BadRequestError(s"File '${h.getFileName}' does not comply with the MISP taxonomy formatting"))
case e => Failure(BadRequestError(s"File '${h.getFileName}' does not comply with the MISP taxonomy formatting ($e)"))
}

private def createFromInput(inputTaxo: InputTaxonomy)(implicit graph: Graph, authContext: AuthContext): Try[RichTaxonomy] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.thp.thehive.controllers.v0

import eu.timepit.refined.collection.MaxSize
import eu.timepit.refined.refineMV
import io.scalaland.chimney.dsl._
import org.thp.scalligraph.EntityIdOrName
import org.thp.scalligraph.models.DummyUserSrv
Expand All @@ -12,6 +10,7 @@ import play.api.libs.json.{JsNull, JsObject, JsString, Json}
import play.api.test.{FakeRequest, PlaySpecification}

import java.util.Date
import eu.timepit.refined.auto._

case class TestAlert(
`type`: String,
Expand Down Expand Up @@ -84,8 +83,8 @@ class AlertCtrlTest extends PlaySpecification with TestAppBuilder with TheHiveOp
Json
.toJson(
InputAlert(
`type` = refineMV[MaxSize[16]]("test"),
source = "alert_creation_test",
`type` = "test",
source = "alert_test",
sourceRef = "#1",
externalLink = None,
title = "alert title (create alert test)",
Expand All @@ -111,7 +110,7 @@ class AlertCtrlTest extends PlaySpecification with TestAppBuilder with TheHiveOp
val resultAlertOutput = resultAlert.as[OutputAlert]
val expected = TestAlert(
`type` = "test",
source = "alert_creation_test",
source = "alert_test",
sourceRef = "#1",
title = "alert title (create alert test)",
description = "alert description (create alert test)",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.thp.thehive.controllers.v0

import eu.timepit.refined.collection.MaxSize
import eu.timepit.refined.{refineMV, W}
import eu.timepit.refined.auto._
import io.scalaland.chimney.dsl._
import org.thp.scalligraph.EntityIdOrName
import org.thp.scalligraph.models.DummyUserSrv
Expand Down Expand Up @@ -55,7 +54,7 @@ class CaseCtrlTest extends PlaySpecification with TestAppBuilder with TheHiveOps
Json
.toJson(
InputCase(
title = refineMV[MaxSize[W.`100`.T]]("case title (create case test)"),
title = "case title (create case test)",
description = "case description (create case test)",
severity = Some(1),
startDate = Some(now),
Expand Down
10 changes: 7 additions & 3 deletions thehive/test/org/thp/thehive/controllers/v0/ConfigCtrlTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ class ConfigCtrlTest extends PlaySpecification with TestAppBuilder {
import app.thehiveModuleV0._

tagSrv.freeTagColour
val request = FakeRequest("PUT", "/api/config/tags.freeTagColour")
val setRequest = FakeRequest("PUT", "/api/config/tags.freeTagColour")
.withHeaders("user" -> "[email protected]")
.withJsonBody(Json.parse("""{"value": "#00FF00"}"""))
val result = configCtrl.set("tags.freeTagColour")(request)
val result = configCtrl.set("tags.freeTagColour")(setRequest)

status(result) must equalTo(204).updateMessage(s => s"$s\n${contentAsString(result)}")

tagSrv.freeTagColour must beEqualTo("#00FF00")
val getRequest = FakeRequest("GET", "/config/tags.freeTagColour")
.withHeaders("user" -> "[email protected]")
(contentAsJson(configCtrl.get("tags.freeTagColour")(getRequest)) \ "value").as[String] must beEqualTo("#00FF00")

// tagSrv.freeTagColour must beEqualTo("#00FF00")

}
// TODO leave unused tests ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class ObservableCtrlTest extends PlaySpecification with TestAppBuilder with TheH
"""))
val result = observableCtrl.createInCase("1")(request)

status(result) shouldEqual 201
status(result) must equalTo(201).updateMessage(s => s"$s\n${contentAsString(result)}")
contentAsJson(result).as[Seq[OutputObservable]]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.thp.scalligraph.traversal.TraversalOps
import org.thp.thehive.dto.v0.{InputOrganisation, OutputOrganisation}
import play.api.libs.json.Json
import play.api.test.{FakeRequest, PlaySpecification}
import eu.timepit.refined.auto._

class OrganisationCtrlTest extends PlaySpecification with TestAppBuilder with TraversalOps {
"organisation controller" should {
Expand Down
14 changes: 12 additions & 2 deletions thehive/test/org/thp/thehive/controllers/v0/ShareCtrlTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.thp.thehive.models.Profile
import org.thp.thehive.services.TheHiveOps
import play.api.libs.json.Json
import play.api.test.{FakeRequest, PlaySpecification}
import eu.timepit.refined.auto._
import org.thp.thehive.dto.String64

class ShareCtrlTest extends PlaySpecification with TestAppBuilder {
"share a case" in testApp { app =>
Expand All @@ -18,7 +20,11 @@ class ShareCtrlTest extends PlaySpecification with TestAppBuilder {
import ops._

val request = FakeRequest("POST", "/api/case/1/shares")
.withJsonBody(Json.obj("shares" -> List(Json.toJson(InputShare("soc", Profile.orgAdmin.name, TasksFilter.all, ObservablesFilter.all)))))
.withJsonBody(
Json.obj(
"shares" -> List(Json.toJson(InputShare("soc", String64("profileName", Profile.orgAdmin.name), TasksFilter.all, ObservablesFilter.all)))
)
)
.withHeaders("user" -> "[email protected]")
val result = shareCtrl.shareCase("1")(request)

Expand All @@ -34,7 +40,11 @@ class ShareCtrlTest extends PlaySpecification with TestAppBuilder {
import app.thehiveModuleV0._

val request = FakeRequest("POST", "/api/case/2/shares")
.withJsonBody(Json.obj("shares" -> Seq(Json.toJson(InputShare("soc", Profile.orgAdmin.name, TasksFilter.all, ObservablesFilter.all)))))
.withJsonBody(
Json.obj(
"shares" -> Seq(Json.toJson(InputShare("soc", String64("profileName", Profile.orgAdmin.name), TasksFilter.all, ObservablesFilter.all)))
)
)
.withHeaders("user" -> "[email protected]")
val result = shareCtrl.shareCase("2")(request)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import play.api.libs.json.{JsObject, JsString, Json}
import play.api.test.{FakeRequest, PlaySpecification}

import java.util.Date
import eu.timepit.refined.auto._

case class TestAlert(
`type`: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.thp.thehive.controllers.v1

import eu.timepit.refined.collection.MaxSize
import eu.timepit.refined.{refineMV, W}
import io.scalaland.chimney.dsl.TransformerOps
import org.thp.thehive.dto.v1.{InputCase, OutputCase, OutputCustomFieldValue}
import play.api.libs.json.{JsNull, JsString, JsValue, Json}
import play.api.test.{FakeRequest, PlaySpecification}

import java.util.Date
import eu.timepit.refined.auto._

case class TestCustomFieldValue(name: String, description: String, `type`: String, value: JsValue, order: Int)

Expand Down Expand Up @@ -53,7 +52,7 @@ class CaseCtrlTest extends PlaySpecification with TestAppBuilder {
.withJsonBody(
Json.toJson(
InputCase(
title = refineMV[MaxSize[W.`100`.T]]("case title (create case test)"),
title = "case title (create case test)",
description = "case description (create case test)",
severity = Some(2),
startDate = Some(now),
Expand Down Expand Up @@ -96,7 +95,7 @@ class CaseCtrlTest extends PlaySpecification with TestAppBuilder {
.withJsonBody(
Json.toJsObject(
InputCase(
title = refineMV[MaxSize[W.`100`.T]]("case title (create case test with template)"),
title = "case title (create case test with template)",
description = "case description (create case test with template)",
severity = None,
startDate = Some(now),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.thp.thehive.dto.v1.{InputOrganisation, OutputOrganisation}
import org.thp.thehive.models.Organisation
import play.api.libs.json.Json
import play.api.test.{FakeRequest, PlaySpecification}
import eu.timepit.refined.auto._

class OrganisationCtrlTest extends PlaySpecification with TestAppBuilder with TraversalOps {
"organisation controller" should {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import play.api.libs.json.Json
import play.api.test.{FakeRequest, PlaySpecification}

import java.util.Date
import eu.timepit.refined.auto._

case class TestProcedure(
description: Option[String],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.thp.thehive.controllers.v1

import eu.timepit.refined.auto._
import io.scalaland.chimney.dsl.TransformerOps
import org.thp.scalligraph.controllers.FakeTemporaryFile
import org.thp.thehive.dto.String128
import org.thp.thehive.dto.v1._
import play.api.libs.Files
import play.api.libs.json.{JsArray, Json}
import play.api.libs.json.{JsArray, JsString, Json}
import play.api.mvc.MultipartFormData.FilePart
import play.api.mvc.{AnyContentAsMultipartFormData, MultipartFormData}
import play.api.test.{FakeRequest, PlaySpecification}
Expand Down Expand Up @@ -102,15 +104,15 @@ class TaxonomyCtrlTest extends PlaySpecification with TestAppBuilder {
"return error if namespace is empty" in testApp { app =>
import app.thehiveModuleV1._

val emptyNamespace = inputTaxo.copy(namespace = "")
// val emptyNamespace = inputTaxo.copy(namespace = String128("namespace", ""))

val request = FakeRequest("POST", "/api/v1/taxonomy")
.withJsonBody(Json.toJson(emptyNamespace))
.withJsonBody(Json.toJsObject(inputTaxo) + ("namespace" -> JsString("")))
.withHeaders("user" -> "[email protected]")

val result = taxonomyCtrl.create(request)
status(result) must beEqualTo(400).updateMessage(s => s"$s\n${contentAsString(result)}")
(contentAsJson(result) \ "type").as[String] must beEqualTo("BadRequest")
(contentAsJson(result) \ "type").as[String] must beEqualTo("AttributeCheckingError")

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import play.api.libs.json.Json
import play.api.test.{FakeRequest, PlaySpecification}

import scala.util.{Success, Try}
import eu.timepit.refined.auto._
import org.thp.thehive.dto.String128

case class TestUser(login: String, name: String, profile: String, permissions: Set[String], organisation: String)

Expand Down Expand Up @@ -55,7 +57,7 @@ class UserCtrlTest extends PlaySpecification with TestAppBuilder {
email = None,
password = Some("azerty"),
profile = "read-only",
organisation = Some(Organisation.administration.name),
organisation = Some(String128("organisationName", Organisation.administration.name)),
avatar = None
)
)
Expand All @@ -69,7 +71,7 @@ class UserCtrlTest extends PlaySpecification with TestAppBuilder {
name = "create user test",
profile = "read-only",
permissions = Set.empty,
organisation = Organisation.administration.name
organisation = String128("organisationName", Organisation.administration.name)
)

TestUser(resultCase) must_=== expected
Expand Down
1 change: 1 addition & 0 deletions thehive/test/org/thp/thehive/services/AlertSrvTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import play.api.libs.json.JsString
import play.api.test.PlaySpecification

import java.util.Date
import eu.timepit.refined.auto._

class AlertSrvTest extends PlaySpecification with TestAppBuilder with TheHiveOpsNoDeps {
implicit val authContext: AuthContext = DummyUserSrv(userId = "[email protected]", organisation = "cert").authContext
Expand Down

0 comments on commit df79001

Please sign in to comment.