Files
keystone/tests/Feature/ApplicationControllerTest.php

123 lines
4.9 KiB
PHP

<?php
use App\Actions\Applications\GenerateDeployKey;
use App\Models\Application;
use App\Models\Environment;
use App\Models\Organisation;
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,
])->toArray());
$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));
});
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]);
$response = $this->actingAs($user)->post(route('applications.store', [
'organisation' => $organisation->id,
]), [
'name' => 'Billing API',
'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->environments()->where('name', 'production')->exists())->toBeTrue()
->and($application->environments()->first()->services()->where('name', 'web')->exists())->toBeTrue();
});
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();
});