Files
portal-api/tests/Unit/Services/VCenter/PowerShellServiceSecretLoggingTest.php
2026-09-27 11:42:40 +07:00

75 lines
2.1 KiB
PHP

<?php
namespace Tests\Unit\Services\VCenter;
use App\Http\Services\vCenter\Client;
use App\Http\Services\vCenter\PowerShellService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;
class PowerShellServiceSecretLoggingTest extends TestCase
{
use RefreshDatabase;
private const SECRET = 'S3cr3t!pass';
/** @var array<int, array<int, mixed>> */
private array $capturedLogs = [];
public function test_change_ip_vm_linux_never_logs_the_guest_password(): void
{
$this->interceptLogs();
(new PowerShellService(new Client()))->changeIpVM(
vmName: 'vm-test',
vmwareId: 'vm-3340',
ipAddress: '10.0.0.5',
gateway: '10.0.0.1',
osType: 'linux',
guestUser: 'root',
guestPassword: self::SECRET,
);
$this->assertLogsNeverContainSecret();
}
public function test_change_ip_vm_windows_never_logs_the_guest_password(): void
{
$this->interceptLogs();
(new PowerShellService(new Client()))->changeIpVM(
vmName: 'vm-test',
vmwareId: 'vm-3340',
ipAddress: '10.0.0.5',
gateway: '10.0.0.1',
osType: 'windows',
guestUser: 'administrator',
guestPassword: self::SECRET,
);
$this->assertLogsNeverContainSecret();
}
private function interceptLogs(): void
{
$collect = function (...$args): void {
$this->capturedLogs[] = $args;
};
Log::shouldReceive('channel')->andReturnSelf();
Log::shouldReceive('info')->andReturnUsing($collect);
Log::shouldReceive('warning')->andReturnUsing($collect);
Log::shouldReceive('error')->andReturnUsing($collect);
}
private function assertLogsNeverContainSecret(): void
{
$this->assertNotEmpty($this->capturedLogs, 'Expected at least one log call to have been made.');
foreach ($this->capturedLogs as $call) {
$this->assertStringNotContainsString(self::SECRET, (string) json_encode($call));
}
}
}