Skip to content

Commit

Permalink
Refactor and improve alert IT tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Hamon committed Jan 17, 2017
1 parent 01905df commit 9178b29
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
32 changes: 1 addition & 31 deletions api/alerts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,7 @@

try
{
$jobj = PluginArmaditoToolbox::parseJSON($rawdata);
$job_id = isset($jobj->task->obj->job_id) ? $jobj->task->obj->job_id : 0;

$alertdb = new PluginArmaditoAlert();
$alertdb->prepareInsertQuery();
$dbmanager = $alertdb->getDbManager();

foreach( $jobj->task->obj->alerts as $alert_jobj )
{
$tmp_jobj = $jobj;
$tmp_jobj->task->obj = $alert_jobj;
$tmp_jobj->task->obj->job_id = $job_id;

$alert = new PluginArmaditoAlert();
$alert->initFromJson($tmp_jobj);
$alert->setDbManager($dbmanager);

if(!$alert->isAlertInDB()) {
$alert->insertAlert();
$alert->updateAlertStat();
}
}

$alertdb->closeInsertQuery();

if($alert)
{
$agent = new PluginArmaditoAgent();
$agent->updateLastAlert($alert);
}

PluginArmaditoAlert::manageApiRequest($rawdata);
writeHttpOKResponse("");
}
catch(PluginArmaditoJsonException $e)
Expand Down
34 changes: 34 additions & 0 deletions inc/alert.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,40 @@ function getSearchOptions()
return $search_options->get($items);
}

static function manageApiRequest($rawdata)
{
$jobj = PluginArmaditoToolbox::parseJSON($rawdata);
$job_id = isset($jobj->task->obj->job_id) ? $jobj->task->obj->job_id : 0;

$alertdb = new PluginArmaditoAlert();
$alertdb->prepareInsertQuery();
$dbmanager = $alertdb->getDbManager();

foreach( $jobj->task->obj->alerts as $alert_jobj )
{
$tmp_jobj = $jobj;
$tmp_jobj->task->obj = $alert_jobj;
$tmp_jobj->task->obj->job_id = $job_id;

$alert = new PluginArmaditoAlert();
$alert->initFromJson($tmp_jobj);
$alert->setDbManager($dbmanager);

if(!$alert->isAlertInDB()) {
$alert->insertAlert();
$alert->updateAlertStat();
}
}

$alertdb->closeInsertQuery();

if($alert)
{
$agent = new PluginArmaditoAgent();
$agent->updateLastAlert($alert);
}
}

function isAlertInDB()
{
global $DB;
Expand Down
14 changes: 2 additions & 12 deletions tests/2_Integration/05-Results/ApiAlertsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function POSTrequests()

protected function insertOrUpdateAlert($agentid, $detection_time, $malware_type, $uuid)
{
$json_alert = '{
$rawdata = '{
"job_id": 1,
"name" : "'.$malware_type.'",
"filepath" : "/home/malwares/X",
Expand All @@ -54,17 +54,7 @@ protected function insertOrUpdateAlert($agentid, $detection_time, $malware_type,
"agent_id":'.$agentid.',
"uuid": "'.$uuid.'"}';

$jobj = PluginArmaditoToolbox::parseJSON($json);
$alert = new PluginArmaditoAlert();
$alert->initFromJson($jobj);

if(!$alert->isAlertInDB()) {
$alert->insertAlert();
$alert->updateAlertStat();
}

$agent = new PluginArmaditoAgent();
$agent->updateLastAlert($alert);
PluginArmaditoAlert::manageApiRequest($rawdata);
}
}

0 comments on commit 9178b29

Please sign in to comment.