Skip to content

Commit

Permalink
Adjust ScanCallbackResponse to new enhanced format
Browse files Browse the repository at this point in the history
  • Loading branch information
Lednerb committed May 7, 2019
1 parent 570fc66 commit 597998c
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/ScanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function callback(ScanResult $result, Request $request)
]);

if ($result->scan->isFinished() === true) {
$result->scan->update(['finished_at' => now()]);
$this->dispatch(new NotifyCallbacksJob($result->scan));
}
}
Expand Down
32 changes: 32 additions & 0 deletions app/Http/Responses/ScanCallbackResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Responses;

use App\Scan;
use Illuminate\Database\Eloquent\Model;

class ScanCallbackResponse extends Model
{
public function __construct(Scan $scan)
{
$this->url = $scan->url;
$this->dangerLevel = $scan->dangerLevel;
$this->started_at = $scan->created_at->toDateTimeString();
$this->finished_at = now()->toDateTimeString();
$this->results = $this->getFormattedResults($scan);
}

public function getFormattedResults(Scan $scan)
{
$results = collect();

foreach ($scan->results as $result) {
$results->push(array_merge([
'started_at' => $result->created_at->toDateTimeString(),
'finished_at' => $result->updated_at->toDateTimeString(),
], $result->result->toArray()));
}

return $results;
}
}
3 changes: 2 additions & 1 deletion app/Jobs/NotifyCallbacksJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Scan;
use App\HTTPClient;
use Illuminate\Support\Facades\Log;
use App\Http\Responses\ScanCallbackResponse;

class NotifyCallbacksJob implements ShouldQueue
{
Expand Down Expand Up @@ -42,7 +43,7 @@ public function handle()
foreach ($this->scan->callbackurls as $callbackurl) {
try {
$response = $client->request('POST', $callbackurl, [
'json' => $this->scan->results
'json' => (new ScanCallbackResponse($this->scan))
]);

if ($response->getStatusCode() === 200) {
Expand Down
4 changes: 4 additions & 0 deletions app/Jobs/StartScannerJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function __construct(Scan $scan, string $scannerCode, string $scannerUrl,
*/
public function handle()
{
if ($this->scan->started_at == null) {
$this->scan->update(['started_at' => now()]);
}

$client = $this->client ?: new HTTPClient();
$logInfo = PHP_EOL . 'Scan ID: ' . $this->scan->id . PHP_EOL . 'Scan URL: ' . $this->scan->url . PHP_EOL . 'Scanner: ' . $this->scannerCode . PHP_EOL . 'Scanner-URL: ' . $this->scannerUrl;

Expand Down
2 changes: 1 addition & 1 deletion app/Scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Scan extends Model
{
protected $fillable = [
'url', 'callbackurls', 'dangerLevel'
'url', 'callbackurls', 'dangerLevel', 'started_at', 'finished_at'
];

protected $dates = [
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/NotifyCallbacksJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function setUp(): void

Log::swap(new LogFake);
$this->scan = $this->generateScanWithResult();
$this->scan->update(['finished_at' => now()]);
}

/** @test */
Expand Down
78 changes: 78 additions & 0 deletions tests/Unit/ScanCallbackResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Http\Responses\ScanCallbackResponse;
use Carbon\Carbon;

class ScanCallbackResponseTest extends TestCase
{
use RefreshDatabase;

public function setUp(): void
{
parent::setUp();

$knownDate = Carbon::create(2019, 5, 7, 11, 55, 15);
Carbon::setTestNow($knownDate);
}

/** @test */
public function the_scanCallbackResponse_has_the_defined_format()
{
$scan = $this->generateScanWithResult();
$scan->update([
'dangerLevel' => 7,
'started_at' => now()
]);

$response = new ScanCallbackResponse($scan->refresh());

$this->assertEquals(json_encode([
'url' => 'https://example.org',
'dangerLevel' => '7',
'started_at' => '2019-05-07 11:55:15',
'finished_at' => '2019-05-07 11:55:15',
'results' => [
[
'started_at' => now()->toDateTimeString(),
'finished_at' => now()->toDateTimeString(),
"name" => "INI_S",
"version" => "1.0.0",
"hasError" => false,
"errorMessage" => null,
"score" => 100,
"tests" => [
[
"name" => "PHISHING",
"hasError" => false,
"errorMessage" => null,
"score" => 100,
"scoreType" => "success",
"testDetails" => []
],
[
"name" => "SPAM",
"hasError" => false,
"errorMessage" => null,
"score" => 100,
"scoreType" => "success",
"testDetails" => []
],
[
"name" => "MALWARE",
"hasError" => false,
"errorMessage" => null,
"score" => 100,
"scoreType" => "success",
"testDetails" => []
]
]
]
]
]), $response->toJson());
}
}

0 comments on commit 597998c

Please sign in to comment.