clusev/tests/Feature/HardeningServiceTest.php

70 lines
2.4 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Server;
use App\Services\FirewallService;
use App\Services\FleetService;
use App\Services\HardeningService;
use App\Support\Os\OsDetector;
use App\Support\Os\OsProfile;
use Mockery;
use Tests\TestCase;
class HardeningServiceTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
private function debianProfile(): OsProfile
{
return new OsProfile(
family: 'debian',
id: 'debian',
prettyName: 'Debian GNU/Linux 13',
packageManager: 'apt',
firewallTool: 'ufw',
serviceManager: 'systemd',
available: ['apt-get', 'dpkg', 'systemctl', 'fail2ban-client', 'ufw'],
);
}
/**
* The checklist must agree with the fail2ban/firewall panels: a component counts as
* installed when the package manager lists it (^ii/rpm) OR its binary is on PATH OR
* the service is running — not via dpkg `^ii` alone (the false "nicht installiert").
*/
public function test_installed_probe_falls_back_to_binary_and_service(): void
{
$os = $this->debianProfile();
$detector = Mockery::mock(OsDetector::class);
$detector->shouldReceive('detect')->andReturn($os);
$captured = '';
$fleet = Mockery::mock(FleetService::class);
$fleet->shouldReceive('runPrivileged')
->andReturnUsing(function ($server, $cmd) use (&$captured) {
$captured = $cmd;
// fail2ban running from a source/`rc` install: present + active.
return ['ok' => true, 'output' => "fail2ban 1 active\nfirewall 1 active\nautoupdate 0 inactive"];
});
$service = new HardeningService($fleet, Mockery::mock(FirewallService::class), $detector);
$rows = $service->state(new Server);
// fail2ban installed-probe gains the binary fallback (was dpkg ^ii only — the bug).
$this->assertStringContainsString('command -v fail2ban-client', $captured);
// firewall installed-probe gains the package-manager fallback (was command -v only).
$this->assertStringContainsString('dpkg -l ufw', $captured);
$f2b = collect($rows)->firstWhere('key', 'fail2ban');
$this->assertTrue($f2b['featureOn'], 'fail2ban active must read as on');
}
}