@@ -7,9 +7,10 @@ import org.thp.scalligraph.models.{Database, DummyUserSrv, Schema}
7
7
import org .thp .scalligraph .traversal .TraversalOps ._
8
8
import org .thp .scalligraph .{AppBuilder , EntityName }
9
9
import org .thp .thehive .TestAppBuilder
10
- import org .thp .thehive .connector .cortex .models .{Job , JobStatus , TheHiveCortexSchemaProvider }
10
+ import org .thp .thehive .connector .cortex .models .{ActionOperationStatus , Job , JobStatus , TheHiveCortexSchemaProvider }
11
11
import org .thp .thehive .connector .cortex .services .JobOps ._
12
12
import org .thp .thehive .models .Permissions
13
+ import org .thp .thehive .services .CaseOps ._
13
14
import org .thp .thehive .services .ObservableOps ._
14
15
import org .thp .thehive .services .UserOps ._
15
16
import org .thp .thehive .services ._
@@ -23,7 +24,8 @@ import scala.concurrent.duration.DurationInt
23
24
import scala .io .Source
24
25
25
26
class JobSrvTest extends PlaySpecification with TestAppBuilder {
26
- implicit val authContext : AuthContext = DummyUserSrv (userId
= " [email protected] " , permissions
= Permissions .all).authContext
27
+ implicit val authContext : AuthContext =
28
+ DummyUserSrv (userId
= " [email protected] " , organisation
= " cert" , permissions
= Permissions .all).authContext
27
29
override def appConfigure : AppBuilder =
28
30
super
29
31
.appConfigure
@@ -33,53 +35,76 @@ class JobSrvTest extends PlaySpecification with TestAppBuilder {
33
35
.`override`(_.bindToProvider[Schema , TheHiveCortexSchemaProvider ])
34
36
35
37
" job service" should {
38
+ val cortexOutputJob = {
39
+ val dataSource = Source .fromResource(" cortex-jobs.json" )
40
+ val data = dataSource.mkString
41
+ dataSource.close()
42
+ Json .parse(data).as[List [OutputJob ]].find(_.id == " ZWu85Q1OCVNx03hXK4df" ).get
43
+ }
44
+
36
45
" handle creation and then finished job" in testApp { app =>
37
- // val job = Job(
38
- // workerId = "anaTest2",
39
- // workerName = "anaTest2",
40
- // workerDefinition = "test2",
41
- // status = JobStatus.Waiting,
42
- // startDate = new Date(1561625908856L),
43
- // endDate = new Date(1561625908856L),
44
- // report = None,
45
- // cortexId = "test",
46
- // cortexJobId = "LVyYKFstq3Rtrdc9DFmL"
47
- // )
48
- //
49
- // val cortexOutputJob = {
50
- // val dataSource = Source.fromResource("cortex-jobs.json")
51
- // val data = dataSource.mkString
52
- // dataSource.close()
53
- // Json.parse(data).as[List[OutputJob]].find(_.id == "ZWu85Q1OCVNx03hXK4df").get
54
- // }
55
- //
56
- // val createdJobTry = app[Database].tryTransaction { implicit graph =>
57
- // for {
58
- // observable <- app[ObservableSrv].startTraversal.has(_.message, "hello world").getOrFail("Observable")
59
- // createdJob <- app[JobSrv].create(job, observable)
60
- // } yield createdJob
61
- // }
62
- // createdJobTry.map { createdJob =>
63
- // Await.result(app[JobSrv].finished(app[CortexClient].name, createdJob._id, cortexOutputJob), 20.seconds)
64
- // } must beASuccessfulTry.which { updatedJob =>
65
- // updatedJob.status shouldEqual JobStatus.Success
66
- // updatedJob.report must beSome
67
- // (updatedJob.report.get \ "data").as[String] shouldEqual "imageedit_2_3904987689.jpg"
68
- //
69
- // app[Database].roTransaction { implicit graph =>
70
- // app[JobSrv].get(updatedJob).observable.has(_.message, "hello world").exists must beTrue
71
- // app[JobSrv].get(updatedJob).reportObservables.toList.length must equalTo(2).updateMessage { s =>
72
- // s"$s\nreport observables are : ${app[JobSrv].get(updatedJob).reportObservables.richObservable.toList.mkString("\n")}"
73
- // }
74
- //
75
- // for {
76
- // audit <- app[AuditSrv].startTraversal.has(_.objectId, updatedJob._id.toString).getOrFail("Audit")
77
- // organisation <- app[OrganisationSrv].getByName("cert").getOrFail("Organisation")
78
- // user <- app[UserSrv].startTraversal.getByName("[email protected] ").getOrFail("User")
79
- // } yield new JobFinished().filter(audit, Some(updatedJob), organisation, Some(user))
80
- // } must beASuccessfulTry(true)
81
- // }
82
- pending(" flaky test" )
46
+ val job = Job (
47
+ workerId = " anaTest2" ,
48
+ workerName = " anaTest2" ,
49
+ workerDefinition = " test2" ,
50
+ status = JobStatus .Waiting ,
51
+ startDate = new Date (1561625908856L ),
52
+ endDate = new Date (1561625908856L ),
53
+ report = None ,
54
+ cortexId = " test" ,
55
+ cortexJobId = " LVyYKFstq3Rtrdc9DFmL" ,
56
+ operations = Nil
57
+ )
58
+
59
+ val createdJobTry = app[Database ].tryTransaction { implicit graph =>
60
+ for {
61
+ observable <- app[ObservableSrv ].startTraversal.has(_.message, " hello world" ).getOrFail(" Observable" )
62
+ createdJob <- app[JobSrv ].create(job, observable)
63
+ } yield createdJob
64
+ }
65
+ val finishedJobTry = createdJobTry.map { createdJob =>
66
+ Await .result(app[JobSrv ].finished(app[CortexClient ].name, createdJob._id, cortexOutputJob), 20 .seconds)
67
+ }
68
+ finishedJobTry must beASuccessfulTry
69
+ val updatedJob = finishedJobTry.get
70
+ updatedJob.status shouldEqual JobStatus .Success
71
+ updatedJob.report must beSome
72
+ (updatedJob.report.get \ " data" ).as[String ] shouldEqual " imageedit_2_3904987689.jpg"
73
+ updatedJob.operations must haveSize(1 )
74
+ updatedJob.operations.map(o => (o \ " status" ).as[String ]) must contain(beEqualTo(" Success" ))
75
+ .forall
76
+ .updateMessage(s => s " $s\n Operation has failed: ${updatedJob.operations.map(" \n -" + _).mkString}" )
77
+
78
+ app[Database ].roTransaction { implicit graph =>
79
+ app[JobSrv ].get(updatedJob).observable.has(_.message, " hello world" ).exists must beTrue
80
+ val reportObservables = app[JobSrv ].get(updatedJob).reportObservables.toSeq
81
+ reportObservables.length must equalTo(2 ).updateMessage { s =>
82
+ s " $s\n report observables are : ${app[JobSrv ].get(updatedJob).reportObservables.richObservable.toList.mkString(" \n " )}"
83
+ }
84
+ val ipObservable = reportObservables.find(_.dataType == " ip" ).get
85
+ ipObservable.data must beSome(" 192.168.1.1" )
86
+ ipObservable.message must beSome(" myIp" )
87
+ ipObservable.tags must contain(exactly(" tag-test" ))
88
+ ipObservable.tlp must beEqualTo(2 )
89
+
90
+ val operationObservableMaybe = app[JobSrv ]
91
+ .get(updatedJob)
92
+ .observable
93
+ .`case`
94
+ .observables
95
+ .has(_.message, " test-operation" )
96
+ .headOption
97
+ operationObservableMaybe must beSome.which { operationObservable =>
98
+ operationObservable.data must beSome(" myData" )
99
+ operationObservable.tlp must beEqualTo(3 )
100
+ operationObservable.tags must contain(exactly(" tag1" , " tag2" ))
101
+ }
102
+ for {
103
+ audit <- app[AuditSrv ].startTraversal.has(_.objectId, updatedJob._id.toString).getOrFail(" Audit" )
104
+ organisation <- app[OrganisationSrv ].getByName(" cert" ).getOrFail(" Organisation" )
105
+ user
<- app[
UserSrv ].startTraversal.getByName(
" [email protected] " ).getOrFail(
" User" )
106
+ } yield JobFinished .filter(audit, Some (updatedJob), organisation, Some (user))
107
+ } must beASuccessfulTry(true ).setMessage(" The audit doesn't match the expected criteria" )
83
108
}
84
109
85
110
" submit a job" in testApp { app =>
0 commit comments