Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Backup/BackupAuthorizationTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Backup;34use Carbon\CarbonImmutable;5use Pterodactyl\Models\Backup;6use Pterodactyl\Models\Subuser;7use Pterodactyl\Services\Backups\DeleteBackupService;8use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;910class BackupAuthorizationTest extends ClientApiIntegrationTestCase11{12#[\PHPUnit\Framework\Attributes\DataProvider('methodDataProvider')]13public function testAccessToAServersBackupIsRestrictedProperly(string $method, string $endpoint)14{15// The API $user is the owner of $server1.16[$user, $server1] = $this->generateTestAccount();17// Will be a subuser of $server2.18$server2 = $this->createServerModel();19// And as no access to $server3.20$server3 = $this->createServerModel();2122// Set the API $user as a subuser of server 2, but with no permissions23// to do anything with the backups for that server.24Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);2526$backup1 = Backup::factory()->create(['server_id' => $server1->id, 'completed_at' => CarbonImmutable::now()]);27$backup2 = Backup::factory()->create(['server_id' => $server2->id, 'completed_at' => CarbonImmutable::now()]);28$backup3 = Backup::factory()->create(['server_id' => $server3->id, 'completed_at' => CarbonImmutable::now()]);2930$this->instance(DeleteBackupService::class, $mock = \Mockery::mock(DeleteBackupService::class));3132if ($method === 'DELETE') {33$mock->expects('handle')->andReturnUndefined();34}3536// This is the only valid call for this test, accessing the backup for the same37// server that the API user is the owner of.38$this->actingAs($user)->json($method, $this->link($server1, '/backups/' . $backup1->uuid . $endpoint))39->assertStatus($method === 'DELETE' ? 204 : 200);4041// This request fails because the backup is valid for that server but the user42// making the request is not authorized to perform that action.43$this->actingAs($user)->json($method, $this->link($server2, '/backups/' . $backup2->uuid . $endpoint))->assertForbidden();4445// Both of these should report a 404 error due to the backup being linked to46// servers that are not the same as the server in the request, or are assigned47// to a server for which the user making the request has no access to.48$this->actingAs($user)->json($method, $this->link($server1, '/backups/' . $backup2->uuid . $endpoint))->assertNotFound();49$this->actingAs($user)->json($method, $this->link($server1, '/backups/' . $backup3->uuid . $endpoint))->assertNotFound();50$this->actingAs($user)->json($method, $this->link($server2, '/backups/' . $backup3->uuid . $endpoint))->assertNotFound();51$this->actingAs($user)->json($method, $this->link($server3, '/backups/' . $backup3->uuid . $endpoint))->assertNotFound();52}5354public static function methodDataProvider(): array55{56return [57['GET', ''],58['GET', '/download'],59['DELETE', ''],60];61}62}636465