Files
coolify/tests/Feature/CloudProviderTokenShowTest.php
T
Andras Bacsai 88efc0716e fix: enforce resource team isolation and preserve ClickHouse data
Scope resource permissions and related records to the owning team across
team switches, cloning, deployments, and resource operations.

Merge ClickHouse volume mounts, preserve anonymous data volumes on stop,
and require adopting current data before starting or restarting.

Fix file storage deletion checks and add regression coverage.
2026-10-05 14:23:04 +02:00

88 lines
3.2 KiB
PHP

<?php
use App\Livewire\Server\CloudProviderToken\Show;
use App\Models\AuditEvent;
use App\Models\CloudProviderToken;
use App\Models\InstanceSettings;
use App\Models\Server;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Once;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
if (! InstanceSettings::query()->whereKey(0)->exists()) {
$settings = new InstanceSettings;
$settings->id = 0;
$settings->save();
}
Once::flush();
$this->team = Team::factory()->create();
$this->user = User::factory()->create();
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
session(['currentTeam' => $this->team]);
$this->actingAs($this->user);
});
test('server cloud provider token cards show token descriptions', function () {
$server = Server::factory()->create([
'team_id' => $this->team->id,
'hetzner_server_id' => 12345,
]);
CloudProviderToken::factory()->create([
'team_id' => $this->team->id,
'provider' => 'hetzner',
'name' => 'Production Hetzner',
'description' => 'Used for production servers in the EU region.',
]);
Livewire::test(Show::class, ['server_uuid' => $server->uuid])
->assertSee('Production Hetzner')
->assertSee('Used for production servers in the EU region.');
});
describe('after the user switches the session team', function () {
beforeEach(function () {
$this->withoutDefer();
Http::fake(['*' => Http::response(['servers' => []])]);
$this->otherTeam = Team::factory()->create();
$this->otherTeam->members()->attach($this->user->id, ['role' => 'owner']);
$this->server = Server::factory()->create(['team_id' => $this->team->id]);
$this->sameTeamToken = CloudProviderToken::factory()->create(['team_id' => $this->team->id, 'provider' => 'hetzner']);
$this->otherTeamToken = CloudProviderToken::factory()->create(['team_id' => $this->otherTeam->id, 'provider' => 'hetzner']);
$this->component = Livewire::test(Show::class, ['server_uuid' => $this->server->uuid]);
session(['currentTeam' => $this->otherTeam]);
});
test('lists only tokens of the server team', function () {
$tokenIds = $this->component->call('loadTokens')->get('cloudProviderTokens')->pluck('id')->all();
expect($tokenIds)->toBe([$this->sameTeamToken->id]);
});
test('rejects a token of the session team', function () {
$this->component->call('setCloudProviderToken', $this->otherTeamToken->id)
->assertDispatched('error', 'You are not allowed to use this token.');
expect($this->server->fresh()->cloud_provider_token_id)->toBeNull();
});
test('accepts a token of the server team and audits it for the server team', function () {
$this->component->call('setCloudProviderToken', $this->sameTeamToken->id)
->assertDispatched('success');
expect($this->server->fresh()->cloud_provider_token_id)->toBe($this->sameTeamToken->id)
->and(AuditEvent::query()->where('event', 'ui.server.cloud_token_assigned')->sole()->team_id)->toBe($this->team->id);
});
});