139 lines
4.7 KiB
PHP
139 lines
4.7 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\Network;
|
|
use App\Models\Organisation;
|
|
use App\Models\Provider;
|
|
use App\Models\Server;
|
|
use App\Models\Service;
|
|
use App\Models\ServiceReplica;
|
|
use App\Models\ServiceSlice;
|
|
use App\Models\User;
|
|
use Inertia\Testing\AssertableInertia;
|
|
|
|
use function Pest\Laravel\actingAs;
|
|
|
|
beforeEach(function () {
|
|
$this->user = User::factory()->create();
|
|
actingAs($this->user);
|
|
});
|
|
|
|
it('returns the environment show inertia view', function () {
|
|
$organisation = Organisation::factory()->create();
|
|
$application = Application::factory()->create([
|
|
'organisation_id' => $organisation->id,
|
|
]);
|
|
$environment = Environment::factory()->create([
|
|
'application_id' => $application->id,
|
|
]);
|
|
|
|
$response = $this->get(route('environments.show', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
]));
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertInertia(fn (AssertableInertia $page) => $page
|
|
->component('environments/Show', false));
|
|
});
|
|
|
|
it('404s when the environment does not belong to the application', function () {
|
|
$organisation = Organisation::factory()->create();
|
|
$application = Application::factory()->create([
|
|
'organisation_id' => $organisation->id,
|
|
]);
|
|
$otherApplication = Application::factory()->create([
|
|
'organisation_id' => $organisation->id,
|
|
]);
|
|
$environment = Environment::factory()->create([
|
|
'application_id' => $otherApplication->id,
|
|
]);
|
|
|
|
$response = $this->get(route('environments.show', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
]));
|
|
|
|
$response->assertNotFound();
|
|
});
|
|
|
|
it('renders caddyfile previews for gateway attachments', function () {
|
|
$organisation = Organisation::factory()->create();
|
|
$application = Application::factory()->create([
|
|
'organisation_id' => $organisation->id,
|
|
]);
|
|
$environment = Environment::factory()->create([
|
|
'application_id' => $application->id,
|
|
]);
|
|
$gateway = 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',
|
|
]);
|
|
$web = Service::factory()->for($environment)->create([
|
|
'organisation_id' => $organisation->id,
|
|
'name' => 'web',
|
|
'category' => ServiceCategory::APPLICATION,
|
|
'type' => ServiceType::LARAVEL,
|
|
'version' => 'php-8.4',
|
|
'version_track' => 'php-8.4',
|
|
'driver_name' => 'laravel.php-8.4',
|
|
'process_roles' => ['web'],
|
|
]);
|
|
$provider = Provider::factory()->forOrganisation($organisation)->create();
|
|
$network = Network::query()->create([
|
|
'organisation_id' => $organisation->id,
|
|
'provider_id' => $provider->id,
|
|
'name' => 'private',
|
|
'ip_range' => '10.0.0.0/16',
|
|
]);
|
|
$server = Server::factory()
|
|
->forOrganisation($organisation->id)
|
|
->forProvider((string) $provider->id)
|
|
->forNetwork((string) $network->id)
|
|
->create();
|
|
ServiceReplica::factory()->for($web)->for($server)->create([
|
|
'internal_host' => 'web-1',
|
|
'internal_port' => 8080,
|
|
]);
|
|
$slice = ServiceSlice::factory()->for($gateway)->for($environment)->create([
|
|
'name' => 'billing',
|
|
'type' => 'route',
|
|
'config' => [
|
|
'domain' => 'billing.example.com',
|
|
'path_prefix' => '/app',
|
|
'tls_enabled' => false,
|
|
],
|
|
]);
|
|
$attachment = $environment->attachments()->create([
|
|
'service_id' => $gateway->id,
|
|
'service_slice_id' => $slice->id,
|
|
'role' => EnvironmentAttachmentRole::GATEWAY,
|
|
'is_primary' => true,
|
|
]);
|
|
|
|
$response = $this->get(route('environments.show', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
]));
|
|
|
|
$response->assertOk()
|
|
->assertInertia(fn (AssertableInertia $page) => $page
|
|
->component('environments/Show', false)
|
|
->where('gatewayRoutePreviews.0.attachment_id', $attachment->id)
|
|
->where('gatewayRoutePreviews.0.caddyfile', fn (string $caddyfile): bool => str_contains($caddyfile, 'http://billing.example.com {')
|
|
&& str_contains($caddyfile, 'handle_path /app* {')
|
|
&& str_contains($caddyfile, 'reverse_proxy web-1:8080')));
|
|
});
|