Improve backup handling

This commit is contained in:
robert dennis
2026-06-28 17:51:42 -04:00
parent 913b354aff
commit ace8effd07
2 changed files with 71 additions and 1 deletions
@@ -203,7 +203,7 @@ class BackupController extends ClientApiController
throw new BadRequestHttpException('This server is not currently in a state that allows for a backup to be restored.');
}
if (!$backup->is_successful && is_null($backup->completed_at)) {
if (!$backup->is_successful || is_null($backup->completed_at)) {
throw new BadRequestHttpException('This backup cannot be restored at this time: not completed or failed.');
}
@@ -0,0 +1,70 @@
<?php
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Backup;
use Mockery\MockInterface;
use Carbon\CarbonImmutable;
use Illuminate\Http\Response;
use Pterodactyl\Models\Backup;
use Pterodactyl\Models\Permission;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
class RestoreBackupTest extends ClientApiIntegrationTestCase
{
private MockInterface $repository;
public function setUp(): void
{
parent::setUp();
$this->repository = $this->mock(DaemonBackupRepository::class);
}
public function testBackupCanBeRestored()
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_BACKUP_RESTORE]);
/** @var Backup $backup */
$backup = Backup::factory()->create(['server_id' => $server->id]);
$this->repository->expects('setServer->restore')->with(
\Mockery::on(function ($value) use ($backup) {
return $value instanceof Backup && $value->uuid === $backup->uuid;
}),
null,
true,
)->andReturn(new GuzzleResponse());
$this->actingAs($user)->postJson($this->link($backup, 'restore'), ['truncate' => true])
->assertStatus(Response::HTTP_NO_CONTENT);
}
#[\PHPUnit\Framework\Attributes\DataProvider('invalidBackupDataProvider')]
public function testBackupCannotBeRestoredUntilSuccessfulAndComplete(bool $isSuccessful, bool $isCompleted)
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_BACKUP_RESTORE]);
/** @var Backup $backup */
$backup = Backup::factory()->create([
'server_id' => $server->id,
'is_successful' => $isSuccessful,
'completed_at' => $isCompleted ? CarbonImmutable::now() : null,
]);
$this->repository->shouldNotReceive('setServer');
$this->actingAs($user)->postJson($this->link($backup, 'restore'), ['truncate' => true])
->assertStatus(Response::HTTP_BAD_REQUEST);
}
public static function invalidBackupDataProvider(): array
{
return [
'failed completed' => [false, true],
'failed incomplete' => [false, false],
'successful incomplete' => [true, false],
];
}
}