diff --git a/app/Http/Controllers/ScanController.php b/app/Http/Controllers/ScanController.php index a57131c4..e7f86aed 100644 --- a/app/Http/Controllers/ScanController.php +++ b/app/Http/Controllers/ScanController.php @@ -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)); } } diff --git a/app/Http/Responses/ScanCallbackResponse.php b/app/Http/Responses/ScanCallbackResponse.php new file mode 100644 index 00000000..2a6887ed --- /dev/null +++ b/app/Http/Responses/ScanCallbackResponse.php @@ -0,0 +1,32 @@ +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; + } +} diff --git a/app/Jobs/NotifyCallbacksJob.php b/app/Jobs/NotifyCallbacksJob.php index 6e01146f..8cf6c8b0 100644 --- a/app/Jobs/NotifyCallbacksJob.php +++ b/app/Jobs/NotifyCallbacksJob.php @@ -10,6 +10,7 @@ use App\Scan; use App\HTTPClient; use Illuminate\Support\Facades\Log; +use App\Http\Responses\ScanCallbackResponse; class NotifyCallbacksJob implements ShouldQueue { @@ -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) { diff --git a/app/Jobs/StartScannerJob.php b/app/Jobs/StartScannerJob.php index dc409b65..9a31df05 100644 --- a/app/Jobs/StartScannerJob.php +++ b/app/Jobs/StartScannerJob.php @@ -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; diff --git a/app/Scan.php b/app/Scan.php index 7f9b155b..99f73c73 100644 --- a/app/Scan.php +++ b/app/Scan.php @@ -8,7 +8,7 @@ class Scan extends Model { protected $fillable = [ - 'url', 'callbackurls', 'dangerLevel' + 'url', 'callbackurls', 'dangerLevel', 'started_at', 'finished_at' ]; protected $dates = [ diff --git a/tests/Unit/NotifyCallbacksJobTest.php b/tests/Unit/NotifyCallbacksJobTest.php index 060ee5fb..e6d736d0 100644 --- a/tests/Unit/NotifyCallbacksJobTest.php +++ b/tests/Unit/NotifyCallbacksJobTest.php @@ -24,6 +24,7 @@ public function setUp(): void Log::swap(new LogFake); $this->scan = $this->generateScanWithResult(); + $this->scan->update(['finished_at' => now()]); } /** @test */ diff --git a/tests/Unit/ScanCallbackResponseTest.php b/tests/Unit/ScanCallbackResponseTest.php new file mode 100644 index 00000000..0ba1c921 --- /dev/null +++ b/tests/Unit/ScanCallbackResponseTest.php @@ -0,0 +1,78 @@ +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()); + } +}