Files
keystone/tests/Feature/RegistryControllerTest.php

163 lines
5.9 KiB
PHP

<?php
use App\Enums\RegistryType;
use App\Models\Application;
use App\Models\BuildArtifact;
use App\Models\Environment;
use App\Models\Organisation;
use App\Models\User;
use Inertia\Testing\AssertableInertia;
it('shows the create registry page', function () {
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$response = $this->actingAs($user)->get(route('registries.create', [
'organisation' => $organisation->id,
]));
$response->assertOk();
$response->assertInertia(fn (AssertableInertia $page) => $page
->component('registries/Create', false)
->where('registryTypes.0', RegistryType::GENERIC->value)
->where('registryTypes', fn ($types) => ! in_array(RegistryType::MANAGED->value, $types->all(), true)));
});
it('lists registries for an organisation', function () {
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$organisation->registries()->create([
'name' => 'GHCR',
'type' => RegistryType::GHCR,
'url' => 'ghcr.io/example',
'credentials' => ['username' => 'keystone', 'password' => 'secret'],
]);
$this->actingAs($user)
->get(route('registries.index', $organisation))
->assertOk()
->assertInertia(fn (AssertableInertia $page) => $page
->component('registries/Index', false)
->has('registries', 1)
->where('registries.0.name', 'GHCR'));
});
it('stores a registry for multi-server build artifacts', function () {
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$response = $this->actingAs($user)->post(route('registries.store', [
'organisation' => $organisation->id,
]), [
'name' => 'GHCR',
'type' => RegistryType::GHCR->value,
'url' => 'ghcr.io/example/',
'username' => 'keystone',
'password' => 'secret',
]);
$response->assertRedirect(route('organisations.show', [
'organisation' => $organisation->id,
]));
$registry = $organisation->registries()->firstOrFail();
expect($registry->name)->toBe('GHCR')
->and($registry->type)->toBe(RegistryType::GHCR)
->and($registry->url)->toBe('ghcr.io/example')
->and($registry->credentials)->toMatchArray([
'username' => 'keystone',
'password' => 'secret',
]);
});
it('rejects user-created managed registries and scheme-prefixed registry urls', function (array $overrides, string $field) {
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$this->actingAs($user)->post(route('registries.store', [
'organisation' => $organisation->id,
]), [
'name' => 'Registry',
'type' => RegistryType::GHCR->value,
'url' => 'ghcr.io/example',
'username' => 'keystone',
'password' => 'secret',
...$overrides,
])->assertSessionHasErrors($field);
})->with([
'managed type' => [['type' => RegistryType::MANAGED->value], 'type'],
'url scheme' => [['url' => 'https://registry.example.com'], 'url'],
]);
it('does not delete managed registries through the user registry controller', function () {
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$registry = $organisation->registries()->create([
'name' => 'Managed',
'type' => RegistryType::MANAGED,
'url' => 'registry.example.com',
'credentials' => ['username' => 'keystone-build', 'password' => 'secret'],
]);
$this->actingAs($user)
->delete(route('registries.destroy', [$organisation, $registry]))
->assertForbidden();
expect($registry->fresh())->not->toBeNull();
});
it('does not update managed registries through the user registry controller', function () {
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$registry = $organisation->registries()->create([
'name' => 'Managed',
'type' => RegistryType::MANAGED,
'url' => 'registry.example.com',
'credentials' => ['username' => 'keystone-build', 'password' => 'secret'],
]);
$this->actingAs($user)
->put(route('registries.update', [$organisation, $registry]), [
'name' => 'GHCR',
'type' => RegistryType::GHCR->value,
'url' => 'ghcr.io/example',
'username' => 'keystone',
'password' => 'secret',
])
->assertForbidden();
expect($registry->refresh()->type)->toBe(RegistryType::MANAGED)
->and($registry->name)->toBe('Managed');
});
it('shows registry usage from published build artifacts', function () {
$user = User::factory()->create();
$organisation = Organisation::factory()->create(['owner_id' => $user->id]);
$registry = $organisation->registries()->create([
'name' => 'GHCR',
'type' => RegistryType::GHCR,
'url' => 'ghcr.io/example',
'credentials' => ['username' => 'keystone', 'password' => 'secret'],
]);
$application = Application::factory()->for($organisation)->create();
$environment = Environment::factory()->for($application)->create();
BuildArtifact::query()->create([
'environment_id' => $environment->id,
'commit_sha' => 'abc123',
'image_tag' => 'app:abc123',
'registry_ref' => 'ghcr.io/example/app:abc123',
]);
$this->actingAs($user)
->get(route('registries.show', [$organisation, $registry]))
->assertOk()
->assertInertia(fn (AssertableInertia $page) => $page
->component('registries/Show', false)
->where('registry.id', $registry->id)
->where('artifactCount', 1)
->where('environmentCount', 1)
->has('artifacts.data', 1));
});