227 lines
9.1 KiB
PHP
227 lines
9.1 KiB
PHP
<?php
|
|
|
|
use App\Enums\EnvironmentAttachmentRole;
|
|
use App\Enums\ServiceCategory;
|
|
use App\Enums\ServiceType;
|
|
use App\Models\Application;
|
|
use App\Models\Environment;
|
|
use App\Models\Organisation;
|
|
use App\Models\Service;
|
|
use App\Models\User;
|
|
use Inertia\Testing\AssertableInertia;
|
|
|
|
it('shows the managed attachment create page for an environment', function () {
|
|
$user = User::factory()->create();
|
|
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
|
|
$application = Application::factory()->for($organisation)->create();
|
|
$environment = Environment::factory()->for($application)->create();
|
|
Service::factory()->for($environment)->create([
|
|
'organisation_id' => $organisation->id,
|
|
'name' => 'postgres',
|
|
'category' => ServiceCategory::DATABASE,
|
|
'type' => ServiceType::POSTGRES,
|
|
'version' => '18',
|
|
'version_track' => '18',
|
|
'driver_name' => 'postgres.18',
|
|
]);
|
|
|
|
$response = $this->actingAs($user)->get(route('environment-attachments.create', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
]));
|
|
|
|
$response->assertOk();
|
|
$response->assertInertia(fn (AssertableInertia $page) => $page
|
|
->component('environment-attachments/Create', false)
|
|
->has('services', 1)
|
|
->where('roles.0', EnvironmentAttachmentRole::DATABASE->value));
|
|
});
|
|
|
|
it('stores a managed attachment and generated environment variables', function () {
|
|
$user = User::factory()->create();
|
|
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
|
|
$application = Application::factory()->for($organisation)->create(['name' => 'Billing API']);
|
|
$environment = Environment::factory()->for($application)->create(['name' => 'production']);
|
|
$service = Service::factory()->for($environment)->create([
|
|
'organisation_id' => $organisation->id,
|
|
'name' => 'postgres',
|
|
'category' => ServiceCategory::DATABASE,
|
|
'type' => ServiceType::POSTGRES,
|
|
'version' => '18',
|
|
'version_track' => '18',
|
|
'driver_name' => 'postgres.18',
|
|
]);
|
|
|
|
$response = $this->actingAs($user)->post(route('environment-attachments.store', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
]), [
|
|
'service_id' => $service->id,
|
|
'role' => EnvironmentAttachmentRole::DATABASE->value,
|
|
'name' => 'billing_api',
|
|
'is_primary' => true,
|
|
]);
|
|
|
|
$response->assertRedirect(route('environments.show', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
]));
|
|
|
|
expect($environment->attachments()->where('service_id', $service->id)->exists())->toBeTrue()
|
|
->and($environment->variables()->where('key', 'DB_CONNECTION')->first()->value)->toBe('pgsql')
|
|
->and($service->slices()->where('name', 'billing_api')->exists())->toBeTrue();
|
|
});
|
|
|
|
it('stores gateway route configuration on caddy attachments', function () {
|
|
$user = User::factory()->create();
|
|
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
|
|
$application = Application::factory()->for($organisation)->create(['name' => 'Billing API']);
|
|
$environment = Environment::factory()->for($application)->create(['name' => 'production']);
|
|
$service = Service::factory()->for($environment)->create([
|
|
'organisation_id' => $organisation->id,
|
|
'name' => 'gateway',
|
|
'category' => ServiceCategory::GATEWAY,
|
|
'type' => ServiceType::CADDY,
|
|
'version' => '2',
|
|
'version_track' => '2',
|
|
'driver_name' => 'caddy.2',
|
|
]);
|
|
|
|
$this->actingAs($user)->post(route('environment-attachments.store', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
]), [
|
|
'service_id' => $service->id,
|
|
'role' => EnvironmentAttachmentRole::GATEWAY->value,
|
|
'name' => 'billing',
|
|
'domain' => 'billing.example.com',
|
|
'path_prefix' => '/app',
|
|
'tls_enabled' => true,
|
|
'is_primary' => true,
|
|
])->assertRedirect(route('environments.show', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
]));
|
|
|
|
$slice = $service->slices()->where('type', 'route')->firstOrFail();
|
|
|
|
expect($slice->config['domain'])->toBe('billing.example.com')
|
|
->and($slice->config['path_prefix'])->toBe('/app')
|
|
->and($slice->config['tls_enabled'])->toBeTrue();
|
|
|
|
$attachment = $environment->attachments()->firstOrFail();
|
|
|
|
$this->actingAs($user)->put(route('environment-attachments.update', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
'attachment' => $attachment->id,
|
|
]), [
|
|
'role' => EnvironmentAttachmentRole::GATEWAY->value,
|
|
'env_prefix' => null,
|
|
'is_primary' => true,
|
|
'domain' => 'www.example.com',
|
|
'path_prefix' => '/',
|
|
'tls_enabled' => false,
|
|
'certificate_status' => 'disabled',
|
|
])->assertRedirect(route('environments.show', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
]));
|
|
|
|
expect($slice->refresh()->config)->toMatchArray([
|
|
'domain' => 'www.example.com',
|
|
'path_prefix' => '/',
|
|
'tls_enabled' => false,
|
|
'certificate_status' => 'disabled',
|
|
]);
|
|
});
|
|
|
|
it('manages gateway routes through dedicated route pages', function () {
|
|
$user = User::factory()->create();
|
|
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
|
|
$application = Application::factory()->for($organisation)->create(['name' => 'Billing API']);
|
|
$environment = Environment::factory()->for($application)->create(['name' => 'production']);
|
|
$service = Service::factory()->for($environment)->create([
|
|
'organisation_id' => $organisation->id,
|
|
'name' => 'gateway',
|
|
'category' => ServiceCategory::GATEWAY,
|
|
'type' => ServiceType::CADDY,
|
|
'version' => '2',
|
|
'version_track' => '2',
|
|
'driver_name' => 'caddy.2',
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->get(route('gateway.routes.create', [$organisation, $application, $environment]))
|
|
->assertOk()
|
|
->assertInertia(fn (AssertableInertia $page) => $page
|
|
->component('gateway-routes/Create', false)
|
|
->has('services', 1)
|
|
->where('services.0.id', $service->id));
|
|
|
|
$this->actingAs($user)
|
|
->post(route('gateway.routes.store', [$organisation, $application, $environment]), [
|
|
'service_id' => $service->id,
|
|
'name' => 'billing',
|
|
'domain' => 'billing.example.com',
|
|
'path_prefix' => '/app',
|
|
'tls_enabled' => true,
|
|
])
|
|
->assertRedirect(route('gateway.routes.index', [$organisation, $application, $environment]));
|
|
|
|
$attachment = $environment->attachments()->with('serviceSlice')->firstOrFail();
|
|
|
|
expect($attachment->role)->toBe(EnvironmentAttachmentRole::GATEWAY)
|
|
->and($attachment->serviceSlice->config)->toMatchArray([
|
|
'domain' => 'billing.example.com',
|
|
'path_prefix' => '/app',
|
|
'tls_enabled' => true,
|
|
'certificate_status' => 'pending',
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->get(route('gateway.routes.index', [$organisation, $application, $environment]))
|
|
->assertOk()
|
|
->assertInertia(fn (AssertableInertia $page) => $page
|
|
->component('gateway-routes/Index', false)
|
|
->has('routes', 1)
|
|
->where('routes.0.id', $attachment->id)
|
|
->where('routes.0.service_slice.config.domain', 'billing.example.com'));
|
|
|
|
$this->actingAs($user)
|
|
->get(route('gateway.routes.edit', [$organisation, $application, $environment, $attachment]))
|
|
->assertOk()
|
|
->assertInertia(fn (AssertableInertia $page) => $page
|
|
->component('gateway-routes/Edit', false)
|
|
->where('routeAttachment.id', $attachment->id));
|
|
|
|
$this->actingAs($user)
|
|
->put(route('gateway.routes.update', [$organisation, $application, $environment, $attachment]), [
|
|
'domain' => 'www.example.com',
|
|
'path_prefix' => '/',
|
|
'tls_enabled' => false,
|
|
'certificate_status' => 'disabled',
|
|
])
|
|
->assertRedirect(route('gateway.routes.index', [$organisation, $application, $environment]));
|
|
|
|
expect($attachment->serviceSlice->refresh()->config)->toMatchArray([
|
|
'domain' => 'www.example.com',
|
|
'path_prefix' => '/',
|
|
'tls_enabled' => false,
|
|
'certificate_status' => 'disabled',
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->delete(route('gateway.routes.destroy', [$organisation, $application, $environment, $attachment]))
|
|
->assertRedirect(route('gateway.routes.index', [$organisation, $application, $environment]));
|
|
|
|
expect($environment->attachments()->exists())->toBeFalse();
|
|
});
|