2021-01-19 21:20:55 -08:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Tests\Integration\Api\Client\Server\Database;
|
2021-01-19 21:20:55 -08:00
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\Database;
|
|
|
|
|
use App\Models\DatabaseHost;
|
2025-09-24 13:34:19 +02:00
|
|
|
use App\Models\Subuser;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Services\Databases\DatabaseManagementService;
|
|
|
|
|
use App\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
|
2025-01-30 16:39:17 -05:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2021-01-19 21:20:55 -08:00
|
|
|
|
|
|
|
|
class DatabaseAuthorizationTest extends ClientApiIntegrationTestCase
|
|
|
|
|
{
|
2025-01-30 16:39:17 -05:00
|
|
|
#[DataProvider('methodDataProvider')]
|
2025-02-25 14:22:07 +01:00
|
|
|
public function test_access_to_a_servers_databases_is_restricted_properly(string $method, string $endpoint): void
|
2021-01-19 21:20:55 -08:00
|
|
|
{
|
|
|
|
|
// The API $user is the owner of $server1.
|
|
|
|
|
[$user, $server1] = $this->generateTestAccount();
|
|
|
|
|
// Will be a subuser of $server2.
|
|
|
|
|
$server2 = $this->createServerModel();
|
|
|
|
|
// And as no access to $server3.
|
|
|
|
|
$server3 = $this->createServerModel();
|
|
|
|
|
|
2021-01-23 12:09:16 -08:00
|
|
|
$host = DatabaseHost::factory()->create([]);
|
2021-01-19 21:20:55 -08:00
|
|
|
|
|
|
|
|
// Set the API $user as a subuser of server 2, but with no permissions
|
|
|
|
|
// to do anything with the databases for that server.
|
2021-01-23 12:09:16 -08:00
|
|
|
Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);
|
2021-01-19 21:20:55 -08:00
|
|
|
|
2021-01-23 12:09:16 -08:00
|
|
|
$database1 = Database::factory()->create(['server_id' => $server1->id, 'database_host_id' => $host->id]);
|
|
|
|
|
$database2 = Database::factory()->create(['server_id' => $server2->id, 'database_host_id' => $host->id]);
|
|
|
|
|
$database3 = Database::factory()->create(['server_id' => $server3->id, 'database_host_id' => $host->id]);
|
2021-01-19 21:20:55 -08:00
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
$this
|
2025-09-06 22:57:11 +02:00
|
|
|
->mock(DatabaseManagementService::class)
|
|
|
|
|
->expects($method === 'POST' ? 'rotatePassword' : 'delete')
|
2022-10-14 10:59:20 -06:00
|
|
|
->andReturn($method === 'POST' ? 'foo' : null);
|
2021-01-19 21:20:55 -08:00
|
|
|
|
|
|
|
|
// This is the only valid call for this test, accessing the database for the same
|
|
|
|
|
// server that the API user is the owner of.
|
2024-05-30 00:41:44 +02:00
|
|
|
$this->actingAs($user)->json($method, $this->link($server1, '/databases/' . $database1->id . $endpoint))
|
2021-01-19 21:20:55 -08:00
|
|
|
->assertStatus($method === 'DELETE' ? 204 : 200);
|
|
|
|
|
|
|
|
|
|
// This request fails because the database is valid for that server but the user
|
|
|
|
|
// making the request is not authorized to perform that action.
|
2024-05-30 00:41:44 +02:00
|
|
|
$this->actingAs($user)->json($method, $this->link($server2, '/databases/' . $database2->id . $endpoint))->assertForbidden();
|
2021-01-19 21:20:55 -08:00
|
|
|
|
|
|
|
|
// Both of these should report a 404 error due to the database being linked to
|
|
|
|
|
// servers that are not the same as the server in the request, or are assigned
|
|
|
|
|
// to a server for which the user making the request has no access to.
|
2024-05-30 00:41:44 +02:00
|
|
|
$this->actingAs($user)->json($method, $this->link($server1, '/databases/' . $database2->id . $endpoint))->assertNotFound();
|
|
|
|
|
$this->actingAs($user)->json($method, $this->link($server1, '/databases/' . $database3->id . $endpoint))->assertNotFound();
|
|
|
|
|
$this->actingAs($user)->json($method, $this->link($server2, '/databases/' . $database3->id . $endpoint))->assertNotFound();
|
|
|
|
|
$this->actingAs($user)->json($method, $this->link($server3, '/databases/' . $database3->id . $endpoint))->assertNotFound();
|
2021-01-19 21:20:55 -08:00
|
|
|
}
|
|
|
|
|
|
2023-02-23 12:30:16 -07:00
|
|
|
public static function methodDataProvider(): array
|
2021-01-19 21:20:55 -08:00
|
|
|
{
|
|
|
|
|
return [
|
2021-01-23 12:09:16 -08:00
|
|
|
['POST', '/rotate-password'],
|
|
|
|
|
['DELETE', ''],
|
2021-01-19 21:20:55 -08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|