Files
panel/tests/Integration/Jobs/RevokeSftpAccessJobTest.php
Dane Everitt 0e74f3aade Improve SFTP session revocation to cover password changes and account deletion (#5568)
This expands upon previous work done to better disconnect users from
SFTP when different events occur within Pterodactyl. This new logic also
accounts for password changes and their account being deleted entirely
from the system.

These events now trigger background jobs that will reach out to every
node they are associated with to ensure they're disconnected if
currently connected.
2026-02-14 10:51:26 -08:00

71 lines
2.5 KiB
PHP

<?php
namespace Pterodactyl\Tests\Integration\Jobs;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Server;
use Pterodactyl\Jobs\RevokeSftpAccessJob;
use PHPUnit\Framework\Attributes\TestWith;
use GuzzleHttp\Exception\TransferException;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Repositories\Wings\DaemonRevocationRepository;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class RevokeSftpAccessJobTest extends IntegrationTestCase
{
#[TestWith([Server::class, 'server'])]
#[TestWith([Node::class, 'node'])]
public function testUniqueIdBasedOnModelType(string $class, string $key): void
{
$model = $class::factory()->make(['uuid' => 'uuid-1234']);
$job = new RevokeSftpAccessJob('user-1', $model);
$this->assertEquals(
"revoke-sftp:user-1:{$key}:uuid-1234",
$job->uniqueId()
);
}
public function testJobReleasesBackToQueueOnFailure(): void
{
$node = Node::factory()->make(['uuid' => 'uuid-1234']);
$mock = $this->mock(DaemonRevocationRepository::class, function ($mock) {
$mock->expects('setNode->deauthorize')->andThrows(
new DaemonConnectionException(new TransferException('Connection failed'))
);
});
$job = \Mockery::mock(RevokeSftpAccessJob::class, ['user-1', $node])->makePartial();
$job->expects('release')->with(10);
$job->handle($mock);
}
public function testJobDispatchesForNode(): void
{
$node = Node::factory()->make(['uuid' => 'uuid-1234']);
$mock = $this->mock(DaemonRevocationRepository::class, function ($mock) {
$mock->expects('setNode')->andReturnSelf();
$mock->expects('deauthorize')->with('user-1', [])->andReturnUndefined();
});
(new RevokeSftpAccessJob('user-1', $node))->handle($mock);
}
public function testJobDispatchesForIndividualServer(): void
{
$node = Node::factory()->make(['uuid' => 'node-1234']);
$server = Server::factory()->make(['uuid' => 'server-1234'])->setRelation('node', $node);
$mock = $this->mock(DaemonRevocationRepository::class, function ($mock) {
$mock->expects('setNode')->with(\Mockery::on(fn (Node $node) => $node->uuid === 'node-1234'))->andReturnSelf();
$mock->expects('deauthorize')->with('user-1', ['server-1234'])->andReturnUndefined();
});
(new RevokeSftpAccessJob('user-1', $server))->handle($mock);
}
}