2021-06-13 17:26:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Services\Backups;
|
|
|
|
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Pterodactyl\Models\Backup;
|
|
|
|
use GuzzleHttp\Exception\ClientException;
|
|
|
|
use Pterodactyl\Extensions\Backups\BackupManager;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Pterodactyl\Extensions\Filesystem\S3Filesystem;
|
2021-06-13 17:26:47 +00:00
|
|
|
use Pterodactyl\Services\Backups\DeleteBackupService;
|
|
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
|
|
|
|
use Pterodactyl\Exceptions\Service\Backup\BackupLockedException;
|
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
|
|
|
|
|
|
|
class DeleteBackupServiceTest extends IntegrationTestCase
|
|
|
|
{
|
|
|
|
public function testLockedBackupCannotBeDeleted()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
$backup = Backup::factory()->create([
|
|
|
|
'server_id' => $server->id,
|
|
|
|
'is_locked' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->expectException(BackupLockedException::class);
|
|
|
|
|
|
|
|
$this->app->make(DeleteBackupService::class)->handle($backup);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailedBackupThatIsLockedCanBeDeleted()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
$backup = Backup::factory()->create([
|
|
|
|
'server_id' => $server->id,
|
|
|
|
'is_locked' => true,
|
|
|
|
'is_successful' => false,
|
|
|
|
]);
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$mock = $this->mock(DaemonBackupRepository::class);
|
|
|
|
$mock->expects('setServer->delete')->with($backup)->andReturn(new Response());
|
2021-06-13 17:26:47 +00:00
|
|
|
|
|
|
|
$this->app->make(DeleteBackupService::class)->handle($backup);
|
|
|
|
|
|
|
|
$backup->refresh();
|
|
|
|
|
|
|
|
$this->assertNotNull($backup->deleted_at);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExceptionThrownDueToMissingBackupIsIgnored()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
$backup = Backup::factory()->create(['server_id' => $server->id]);
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$mock = $this->mock(DaemonBackupRepository::class);
|
|
|
|
$mock->expects('setServer->delete')->with($backup)->andThrow(
|
2021-06-13 17:26:47 +00:00
|
|
|
new DaemonConnectionException(
|
|
|
|
new ClientException('', new Request('DELETE', '/'), new Response(404))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->app->make(DeleteBackupService::class)->handle($backup);
|
|
|
|
|
|
|
|
$backup->refresh();
|
|
|
|
|
|
|
|
$this->assertNotNull($backup->deleted_at);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExceptionIsThrownIfNot404()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
$backup = Backup::factory()->create(['server_id' => $server->id]);
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$mock = $this->mock(DaemonBackupRepository::class);
|
|
|
|
$mock->expects('setServer->delete')->with($backup)->andThrow(
|
2021-06-13 17:26:47 +00:00
|
|
|
new DaemonConnectionException(
|
|
|
|
new ClientException('', new Request('DELETE', '/'), new Response(500))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectException(DaemonConnectionException::class);
|
|
|
|
|
|
|
|
$this->app->make(DeleteBackupService::class)->handle($backup);
|
|
|
|
|
|
|
|
$backup->refresh();
|
|
|
|
|
|
|
|
$this->assertNull($backup->deleted_at);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testS3ObjectCanBeDeleted()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
$backup = Backup::factory()->create([
|
|
|
|
'disk' => Backup::ADAPTER_AWS_S3,
|
|
|
|
'server_id' => $server->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$manager = $this->mock(BackupManager::class);
|
2022-10-14 16:59:20 +00:00
|
|
|
$adapter = $this->mock(S3Filesystem::class);
|
|
|
|
|
|
|
|
$manager->expects('adapter')->with(Backup::ADAPTER_AWS_S3)->andReturn($adapter);
|
|
|
|
|
|
|
|
$adapter->expects('getBucket')->andReturn('foobar');
|
|
|
|
$adapter->expects('getClient->deleteObject')->with([
|
2021-06-13 17:26:47 +00:00
|
|
|
'Bucket' => 'foobar',
|
|
|
|
'Key' => sprintf('%s/%s.tar.gz', $server->uuid, $backup->uuid),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->app->make(DeleteBackupService::class)->handle($backup);
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$this->assertSoftDeleted($backup);
|
2021-06-13 17:26:47 +00:00
|
|
|
}
|
|
|
|
}
|