From c1386ff2428f0f35fdb3d5c6a205f316cda586d2 Mon Sep 17 00:00:00 2001 From: Robin Riclet Date: Mon, 8 Mar 2021 16:45:56 +0100 Subject: [PATCH] #1264 fixing merge & tests --- .../thp/thehive/controllers/v0/CaseCtrl.scala | 6 +++--- .../thp/thehive/controllers/v1/CaseCtrl.scala | 5 ++--- .../app/org/thp/thehive/services/CaseSrv.scala | 15 +++++++-------- .../org/thp/thehive/services/TagSrvTest.scala | 3 ++- thehive/test/resources/data/Case.json | 6 ++++-- thehive/test/resources/data/Tag.json | 16 +--------------- 6 files changed, 19 insertions(+), 32 deletions(-) diff --git a/thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala b/thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala index fd86c3d278..9dcd4bf076 100644 --- a/thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala +++ b/thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala @@ -2,7 +2,7 @@ package org.thp.thehive.controllers.v0 import org.apache.tinkerpop.gremlin.process.traversal.P import org.thp.scalligraph._ -import org.apache.tinkerpop.gremlin.structure.Graph +import org.thp.scalligraph.traversal.Graph import org.thp.scalligraph.controllers.{Entrypoint, FPathElem, FPathEmpty, FieldsParser} import org.thp.scalligraph.models.{Database, Entity, UMapping} import org.thp.scalligraph.query._ @@ -151,8 +151,8 @@ class CaseCtrl @Inject() ( entrypoint("merge cases") .authTransaction(db) { implicit request => implicit graph => for { - caze <- caseSrv.get(EntityIdOrName(caseId)).visible.getOrFail("Case") - toMerge <- caseSrv.get(EntityIdOrName(caseToMerge)).visible.getOrFail("Case") + caze <- caseSrv.get(EntityIdOrName(caseId)).visible(organisationSrv).getOrFail("Case") + toMerge <- caseSrv.get(EntityIdOrName(caseToMerge)).visible(organisationSrv).getOrFail("Case") _ <- sameOrga(Seq(caze, toMerge)) _ <- sameProfile(Seq(caze, toMerge)) merged <- caseSrv.merge(Seq(caze, toMerge)) diff --git a/thehive/app/org/thp/thehive/controllers/v1/CaseCtrl.scala b/thehive/app/org/thp/thehive/controllers/v1/CaseCtrl.scala index 8359dc7dd2..6707054326 100644 --- a/thehive/app/org/thp/thehive/controllers/v1/CaseCtrl.scala +++ b/thehive/app/org/thp/thehive/controllers/v1/CaseCtrl.scala @@ -1,12 +1,11 @@ package org.thp.thehive.controllers.v1 import org.apache.tinkerpop.gremlin.process.traversal.P -import org.apache.tinkerpop.gremlin.structure.Graph import org.thp.scalligraph.controllers.{Entrypoint, FieldsParser} import org.thp.scalligraph.models.{Database, Entity} import org.thp.scalligraph.query.{ParamQuery, PropertyUpdater, PublicProperties, Query} import org.thp.scalligraph.traversal.TraversalOps._ -import org.thp.scalligraph.traversal.{IteratorOutput, Traversal} +import org.thp.scalligraph.traversal.{Graph, IteratorOutput, Traversal} import org.thp.scalligraph.{BadRequestError, EntityIdOrName, RichOptionTry, RichSeq} import org.thp.thehive.controllers.v1.Conversion._ import org.thp.thehive.dto.v1.{InputCase, InputTask} @@ -22,7 +21,7 @@ import org.thp.thehive.services.UserOps._ import org.thp.thehive.services._ import play.api.mvc.{Action, AnyContent, Results} -import javax.inject.{Inject, Named, Singleton} +import javax.inject.{Inject, Singleton} import scala.util.{Failure, Success, Try} @Singleton diff --git a/thehive/app/org/thp/thehive/services/CaseSrv.scala b/thehive/app/org/thp/thehive/services/CaseSrv.scala index e0c7fa5c17..80b2dc70ff 100644 --- a/thehive/app/org/thp/thehive/services/CaseSrv.scala +++ b/thehive/app/org/thp/thehive/services/CaseSrv.scala @@ -29,7 +29,7 @@ import play.api.libs.json.{JsNull, JsObject, JsValue, Json} import java.lang.{Long => JLong} import java.util.{Date, List => JList, Map => JMap} import javax.inject.{Inject, Named, Provider, Singleton} -import scala.util.{Failure, Success, Try} +import scala.util.{Success, Try} @Singleton class CaseSrv @Inject() ( @@ -44,6 +44,7 @@ class CaseSrv @Inject() ( impactStatusSrv: ImpactStatusSrv, observableSrv: ObservableSrv, attachmentSrv: AttachmentSrv, + userSrv: UserSrv, alertSrvProvider: Provider[AlertSrv], @Named("integrity-check-actor") integrityCheckActor: ActorRef, cache: SyncCacheApi @@ -321,7 +322,6 @@ class CaseSrv @Inject() ( def merge(cases: Seq[Case with Entity])(implicit graph: Graph, authContext: AuthContext): Try[RichCase] = { val mergedCase = Case( - nextCaseNumber, cases.map(_.title).mkString(" / "), cases.map(_.description).mkString("\n\n"), cases.map(_.severity).max, @@ -331,14 +331,13 @@ class CaseSrv @Inject() ( cases.map(_.tlp).max, cases.map(_.pap).max, CaseStatus.Open, - cases.map(_.summary).fold(None)((s1, s2) => (s1 ++ s2).reduceOption(_ + "\n\n" + _)) + cases.map(_.summary).fold(None)((s1, s2) => (s1 ++ s2).reduceOption(_ + "\n\n" + _)), + cases.flatMap(_.tags).distinct ) - - val tags = cases.flatMap(c => get(c).tags.toSeq) for { user <- userSrv.get(EntityIdOrName(authContext.userId)).getOrFail("User") orga <- organisationSrv.get(authContext.organisation).getOrFail("Organisation") - richCase <- create(mergedCase, Some(user), orga, tags.toSet, Seq(), None, Seq()) + richCase <- create(mergedCase, Some(user), orga, Seq(), None, Seq()) _ <- cases.toTry { c => for { _ <- @@ -346,13 +345,13 @@ class CaseSrv @Inject() ( .tasks .richTask .toList - .toTry(shareSrv.shareTask(_, richCase.`case`, orga)) + .toTry(shareSrv.shareTask(_, richCase.`case`, orga._id)) _ <- get(c) .observables .richObservable .toList - .toTry(shareSrv.shareObservable(_, richCase.`case`, orga)) + .toTry(shareSrv.shareObservable(_, richCase.`case`, orga._id)) _ <- get(c) .alert diff --git a/thehive/test/org/thp/thehive/services/TagSrvTest.scala b/thehive/test/org/thp/thehive/services/TagSrvTest.scala index 38d74028be..196c757b9a 100644 --- a/thehive/test/org/thp/thehive/services/TagSrvTest.scala +++ b/thehive/test/org/thp/thehive/services/TagSrvTest.scala @@ -34,7 +34,8 @@ class TagSrvTest extends PlaySpecification with TestAppBuilder { "getOrCreate" should { "get a tag from a taxonomy" in testApp { app => - app[Database].roTransaction { implicit graph => + // TODO add tags property in Taxonomy.json to test get + app[Database].transaction { implicit graph => val tag = app[TagSrv].getOrCreate("taxonomy1:pred1=value1") tag.map(_.toString) must beEqualTo(Success("taxonomy1:pred1=\"value1\"")) } diff --git a/thehive/test/resources/data/Case.json b/thehive/test/resources/data/Case.json index ce324dea22..9dd3c43015 100644 --- a/thehive/test/resources/data/Case.json +++ b/thehive/test/resources/data/Case.json @@ -77,7 +77,8 @@ "flag": false, "tlp": 4, "pap": 3, - "status": "Open" + "status": "Open", + "tags": ["toMerge:pred1=\"value1\""] }, { "id": "caseMerge22", @@ -89,7 +90,8 @@ "flag": true, "tlp": 1, "pap": 2, - "status": "Open" + "status": "Open", + "tags": ["toMerge:pred2=\"value2\""] }, { "id": "caseMerge23", diff --git a/thehive/test/resources/data/Tag.json b/thehive/test/resources/data/Tag.json index c1fcf5d724..f5c988dc28 100644 --- a/thehive/test/resources/data/Tag.json +++ b/thehive/test/resources/data/Tag.json @@ -5,19 +5,5 @@ "predicate": "pred1", "value": "value1", "colour": "#00f300" - }, - { - "id": "tagMerge1", - "namespace": "toMerge", - "predicate": "pred1", - "value": "value1", - "colour": "#00f300" - }, - { - "id": "tagMerge2", - "namespace": "toMerge", - "predicate": "pred2", - "value": "value2", - "colour": "#00f300" } -] +] \ No newline at end of file