CluPilotCloud/tests/Feature/RescueTunnelTest.php

74 lines
2.4 KiB
PHP

<?php
use App\Livewire\Admin\Settings;
use App\Models\Operator;
use App\Services\Deployment\UpdateChannel;
use Illuminate\Support\Facades\File;
use Livewire\Livewire;
/**
* `deploy/rescue-tunnel.sh` stand in zwei Runbooks als Handarbeit. Es läuft
* ohnehin als Dienstbenutzer und tut nichts Zerstörerisches — der Agent kann
* es also fahren wie jede andere Anfrage.
*/
beforeEach(function () {
File::ensureDirectoryExists(storage_path('app/deploy'));
File::delete(File::glob(storage_path('app/deploy/*')));
});
afterEach(function () {
File::deleteDirectory(storage_path('app/deploy'));
});
it('puts a rescue request in the same mailbox as every other kind', function () {
// Kein zweiter Weg: dieselbe Datei, derselbe Agent, derselbe Lauf.
expect(app(UpdateChannel::class)->requestRescueTunnel('chef@example.com'))->toBeTrue();
$request = json_decode(File::get(storage_path('app/deploy/update-request.json')), true);
expect($request['kind'])->toBe('rescue-tunnel')
->and($request['requested_by'])->toBe('chef@example.com');
});
it('takes only one request at a time', function () {
$channel = app(UpdateChannel::class);
expect($channel->requestRescueTunnel('chef@example.com'))->toBeTrue()
->and($channel->requestRescueTunnel('chef@example.com'))->toBeFalse();
});
it('rescues the tunnel from the console', function () {
$owner = Operator::factory()->role('Owner')->create();
Livewire::actingAs($owner, 'operator')
->test(Settings::class)
->call('rescueTunnel');
expect(File::exists(storage_path('app/deploy/update-request.json')))->toBeTrue();
});
it('refuses to rescue without site.manage', function () {
// Eine Livewire-Aktion ist ein oeffentlicher Endpunkt.
$staff = Operator::factory()->create();
Livewire::actingAs($staff, 'operator')
->test(Settings::class)
->call('rescueTunnel')
->assertForbidden();
});
it('reads the outcome without falling over when it is rubbish', function () {
File::put(storage_path('app/deploy/rescue-last-run.json'), 'kein json {');
expect(app(UpdateChannel::class)->state()['rescue_last_run'])->toBeNull();
});
it('agent knows the kind', function () {
// Der Agent muss die Art kennen, sonst liegt die Anfrage bis zum Ablauf
// im Postkasten und niemand erfaehrt warum.
expect(File::get(base_path('deploy/update-agent.sh')))
->toContain('rescue-tunnel')
->toContain('rescue-tunnel.sh');
});