Files
keystone/tests/Feature/ApplicationControllerTest.php
Harry Bayliss 5b977c1f41
Some checks failed
CI / Lint (push) Failing after 22s
CI / Tests (push) Failing after 33s
wowowowowo
2026-05-28 15:15:41 +01:00

193 lines
7.9 KiB
PHP

<?php
use App\Actions\Applications\GenerateDeployKey;
use App\Enums\OperationKind;
use App\Enums\OperationStatus;
use App\Enums\RepositoryType;
use App\Models\Application;
use App\Models\Environment;
use App\Models\Organisation;
use App\Models\SourceProvider;
use App\Models\User;
use Illuminate\Support\Facades\Process;
use Inertia\Testing\AssertableInertia;
it('lists applications with environments as the primary deployment surface', function () {
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$application = Application::factory()->for($organisation)->create();
Environment::factory()->for($application)->create(['name' => 'production']);
$response = $this->actingAs($user)->get(route('applications.index', [
'organisation' => $organisation->id,
]));
$response->assertOk();
$response->assertInertia(fn (AssertableInertia $page) => $page
->component('applications/Index', false)
->has('applications.0.environments', 1));
});
it('shows an application with environments services and attachments', 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(['name' => 'production']);
$environment->services()->create(\App\Models\Service::factory()->make([
'organisation_id' => $organisation->id,
'desired_revision' => 'abc123',
'current_image_digest' => 'sha256:current',
])->toArray());
$environment->operations()->create([
'kind' => OperationKind::ENVIRONMENT_DEPLOY,
'status' => OperationStatus::COMPLETED,
'finished_at' => now(),
]);
$response = $this->actingAs($user)->get(route('applications.show', [
'organisation' => $organisation->id,
'application' => $application->id,
]));
$response->assertOk();
$response->assertInertia(fn (AssertableInertia $page) => $page
->component('applications/Show', false)
->has('application.environments', 1)
->has('application.environments.0.services', 1)
->where('application.environments.0.services.0.desired_revision', 'abc123')
->where('application.environments.0.services.0.current_image_digest', 'sha256:current')
->has('application.environments.0.operations', 1));
});
it('shows the create application page', function () {
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$response = $this->actingAs($user)->get(route('applications.create', [
'organisation' => $organisation->id,
]));
$response->assertOk();
$response->assertInertia(fn (AssertableInertia $page) => $page
->component('applications/Create', false));
});
it('stores an application with a deploy key and default laravel environment', function () {
$this->app->bind(GenerateDeployKey::class, fn () => new class extends GenerateDeployKey
{
public function execute(Application $application, ?array $keyPair = null): Application
{
return parent::execute($application, [
'public' => 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITestPublicKey keystone',
'private' => "-----BEGIN OPENSSH PRIVATE KEY-----\ntest\n-----END OPENSSH PRIVATE KEY-----",
'fingerprint' => 'SHA256:test',
]);
}
});
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$sourceProvider = SourceProvider::query()->create([
'organisation_id' => $organisation->id,
'name' => 'GitHub',
'type' => 'github',
'url' => 'https://github.com',
]);
$response = $this->actingAs($user)->post(route('applications.store', [
'organisation' => $organisation->id,
]), [
'name' => 'Billing API',
'source_provider_id' => $sourceProvider->id,
'repository_type' => RepositoryType::GIT->value,
'repository_url' => 'git@example.com:org/billing-api.git',
'default_branch' => 'main',
'environment_name' => 'production',
]);
$application = Application::query()->where('name', 'Billing API')->firstOrFail();
$response->assertRedirect(route('applications.show', [
'organisation' => $organisation->id,
'application' => $application->id,
]));
expect($application->deploy_key_public)->toStartWith('ssh-ed25519')
->and($application->source_provider_id)->toBe($sourceProvider->id)
->and($application->repository_type)->toBe(RepositoryType::GIT)
->and($application->environments()->where('name', 'production')->exists())->toBeTrue()
->and($application->environments()->first()->services()->where('name', 'web')->exists())->toBeTrue();
});
it('validates application repository type', function () {
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$this->actingAs($user)->post(route('applications.store', [
'organisation' => $organisation->id,
]), [
'name' => 'Billing API',
'repository_type' => 'svn',
'repository_url' => 'git@example.com:org/billing-api.git',
'default_branch' => 'main',
'environment_name' => 'production',
])->assertInvalid(['repository_type']);
});
it('verifies repository access for an application deploy key', function () {
Process::fake([
'*' => Process::result(output: "abc123\trefs/heads/main\n"),
]);
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$application = Application::factory()->for($organisation)->create([
'repository_url' => 'git@example.com:org/repo.git',
'default_branch' => 'main',
]);
app(GenerateDeployKey::class)->execute($application, [
'public' => 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITestPublicKey keystone',
'private' => "-----BEGIN OPENSSH PRIVATE KEY-----\ntest\n-----END OPENSSH PRIVATE KEY-----",
'fingerprint' => 'SHA256:test',
]);
$response = $this->actingAs($user)->post(route('applications.verify-repository', [
'organisation' => $organisation->id,
'application' => $application->id,
]));
$response->assertRedirect();
expect($application->refresh()->deploy_key_installed_at)->not->toBeNull();
});
it('rotates an application deploy key and clears verification state', function () {
$this->app->bind(GenerateDeployKey::class, fn () => new class extends GenerateDeployKey
{
public function execute(Application $application, ?array $keyPair = null): Application
{
return parent::execute($application, [
'public' => 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIRotatedPublicKey keystone',
'private' => "-----BEGIN OPENSSH PRIVATE KEY-----\nrotated\n-----END OPENSSH PRIVATE KEY-----",
'fingerprint' => 'SHA256:rotated',
]);
}
});
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$application = Application::factory()->for($organisation)->create([
'deploy_key_public' => 'old-public-key',
'deploy_key_private' => 'old-private-key',
'deploy_key_fingerprint' => 'SHA256:old',
'deploy_key_installed_at' => now(),
]);
$this->actingAs($user)
->post(route('applications.deploy-key.rotate', [$organisation, $application]))
->assertRedirect();
expect($application->refresh()->deploy_key_public)->toContain('IRotatedPublicKey')
->and($application->deploy_key_fingerprint)->toBe('SHA256:rotated')
->and($application->deploy_key_installed_at)->toBeNull();
});