diff --git a/app/Http/Controllers/Api/Client/Servers/BackupController.php b/app/Http/Controllers/Api/Client/Servers/BackupController.php index 723f3c916..2db02b30d 100644 --- a/app/Http/Controllers/Api/Client/Servers/BackupController.php +++ b/app/Http/Controllers/Api/Client/Servers/BackupController.php @@ -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.'); } diff --git a/tests/Integration/Api/Client/Server/Backup/RestoreBackupTest.php b/tests/Integration/Api/Client/Server/Backup/RestoreBackupTest.php new file mode 100644 index 000000000..c9ac72136 --- /dev/null +++ b/tests/Integration/Api/Client/Server/Backup/RestoreBackupTest.php @@ -0,0 +1,70 @@ +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], + ]; + } +}