Files

47 lines
1.9 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Tests\Unit\Jobs;
use App\Jobs\RunFinding;
use App\Models\Finding;
use App\Models\Investigation;
use App\Models\Source;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Queue\Middleware\RateLimited;
use Tests\TestCase;
class RunFindingTest extends TestCase
{
use RefreshDatabase;
public function test_middleware_rate_limits_by_the_findings_source_key(): void
{
$user = User::factory()->create();
$investigation = Investigation::factory()->for($user, 'owner')->create();
$target = $investigation->targets()->create(['type' => 'ip', 'value' => '8.8.8.8', 'created_by' => $user->id]);
$source = Source::create(['key' => 'ip-api', 'name' => 'ip-api.com', 'target_types' => ['ip'], 'enabled' => true]);
$finding = $target->findings()->create(['source_id' => $source->id, 'status' => 'pending']);
$middleware = (new RunFinding($finding->id))->middleware();
$this->assertCount(1, $middleware);
$this->assertInstanceOf(RateLimited::class, $middleware[0]);
}
public function test_failed_marks_the_finding_as_failed(): void
{
$user = User::factory()->create();
$investigation = Investigation::factory()->for($user, 'owner')->create();
$target = $investigation->targets()->create(['type' => 'ip', 'value' => '8.8.8.8', 'created_by' => $user->id]);
$source = Source::create(['key' => 'ip-api', 'name' => 'ip-api.com', 'target_types' => ['ip'], 'enabled' => true]);
$finding = $target->findings()->create(['source_id' => $source->id, 'status' => 'pending']);
(new RunFinding($finding->id))->failed(new \RuntimeException('retries exhausted'));
$finding->refresh();
$this->assertSame('failed', $finding->status);
$this->assertStringContainsString('retries exhausted', $finding->error);
}
}