50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domovoy\Tests\Services;
|
|
|
|
use Domovoy\Models\Credential;
|
|
use Domovoy\Services\Security\CredentialVault;
|
|
use Domovoy\Services\Ssh\SshCredentialTester;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class SshCredentialTesterTest extends TestCase
|
|
{
|
|
public function testTestsPasswordCredentialWithDecryptedSecret(): void
|
|
{
|
|
$vault = new CredentialVault('test-key-material');
|
|
$credential = new Credential();
|
|
$credential->username = 'root';
|
|
$credential->authMethod = 'password';
|
|
$credential->encryptedSecret = $vault->encrypt('secret-password');
|
|
|
|
$tester = new SshCredentialTester($vault, function (string $host, int $port, int $timeout) {
|
|
self::assertSame('192.168.1.10', $host);
|
|
self::assertSame(22, $port);
|
|
self::assertSame(5, $timeout);
|
|
return new FakeSshClient();
|
|
}, ['connect_timeout' => 5]);
|
|
|
|
$result = $tester->test($credential, '192.168.1.10');
|
|
|
|
self::assertSame('ok', $result['status']);
|
|
self::assertSame('Подключение успешно', $result['message']);
|
|
self::assertSame('root', FakeSshClient::$lastUsername);
|
|
self::assertSame('secret-password', FakeSshClient::$lastPassword);
|
|
}
|
|
}
|
|
|
|
final class FakeSshClient
|
|
{
|
|
public static ?string $lastUsername = null;
|
|
public static ?string $lastPassword = null;
|
|
|
|
public function login(string $username, string $password): bool
|
|
{
|
|
self::$lastUsername = $username;
|
|
self::$lastPassword = $password;
|
|
return true;
|
|
}
|
|
}
|