Loheide-eu-mitarbeiterplatt.../tests/Feature/Update/UpdateEndpointsTest.php
Friederich Loheide 43244d751a Self-Updater und Lizenzprüfung
Umsetzung von docs/specs/self-updater.md für Laravel:
- Installierter Stand als SHA in storage/.version, Fortschritt in storage/.update-progress,
  selbstheilender Wartungsmodus in storage/.maintenance
- Update-Client je Kanal (stable/development), nur HTTPS, begrenzte Redirects,
  ZIP-Download per Stream mit PK- und SHA-256-Prüfung
- Entpacken mit zweistufigem ZIP-Slip-Schutz, Übernahme mit geschützten Pfaden
  (atomar je Datei, nie löschen), Migrationen, Cache- und OPcache-Reset
- Lizenzspeicher außerhalb der DB, Geräte-ID als Zufallszahl, rotierendes Gerätetoken
  wird bei jeder Antwort (auch Fehler und Downloads) sofort gespeichert
- Lizenzfehler mit verständlichen Meldungen – sie sperren nie das Portal
- Seite „Aktualisierung“ mit Kanal, Suche, Changelog-Vorschau, Fortschritt,
  Migrationen, Änderungsprotokoll, Lizenz und Datenschutzhinweis
- Wartungs-Middleware (503 + Retry-After) und Auto-Migration per Fingerabdruck
- Audit-Log für Updates, Kanalwechsel und Lizenzänderungen
- Version 0.2.0

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 06:02:19 +00:00

142 lines
5.5 KiB
PHP

<?php
namespace Tests\Feature\Update;
use App\Models\User;
use App\Support\Update\Channels;
use App\Support\Update\Filesystem;
use App\Support\Update\InstalledVersion;
use App\Support\Update\MaintenanceFlag;
use App\Support\Update\UpdateProgress;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class UpdateEndpointsTest extends TestCase
{
use RefreshDatabase;
private string $dir;
protected function setUp(): void
{
parent::setUp();
$this->dir = sys_get_temp_dir().'/inhaus-ep-'.bin2hex(random_bytes(4));
mkdir($this->dir);
$this->app->instance(MaintenanceFlag::class, new MaintenanceFlag($this->dir.'/.maintenance'));
$this->app->instance(UpdateProgress::class, new UpdateProgress($this->dir.'/.progress'));
$this->app->instance(InstalledVersion::class, new InstalledVersion($this->dir.'/.version'));
}
protected function tearDown(): void
{
Filesystem::deleteDirectory($this->dir);
parent::tearDown();
}
public function test_update_endpoints_are_admin_only(): void
{
$this->postJson('/admin/update/check')->assertUnauthorized();
$user = User::factory()->create();
foreach (['check', 'install', 'channel', 'migrations'] as $endpoint) {
$this->actingAs($user)->postJson("/admin/update/{$endpoint}")->assertForbidden();
}
$this->actingAs($user)->getJson('/admin/update/progress')->assertForbidden();
$this->actingAs($user)->get('/admin/aktualisierung')->assertForbidden();
}
public function test_maintenance_mode_shows_503_but_keeps_update_pages_reachable(): void
{
$admin = User::factory()->admin()->create();
app(MaintenanceFlag::class)->activate(300);
$this->actingAs($admin)->get('/')
->assertStatus(503)
->assertHeader('Retry-After')
->assertSee('Das Portal wird gerade aktualisiert');
$this->actingAs($admin)->getJson('/admin/update/progress')->assertOk()->assertJson(['step' => 'idle']);
$this->actingAs($admin)->get('/admin/aktualisierung')->assertOk();
$this->get('/up')->assertOk();
app(MaintenanceFlag::class)->deactivate();
$this->actingAs($admin)->get('/')->assertOk();
}
public function test_first_check_of_fresh_installation_remembers_latest_sha(): void
{
Http::fake(['*' => Http::response(['has_update' => false, 'latest_sha' => str_repeat('d', 40), 'version' => '0.2.0'])]);
$this->actingAs(User::factory()->admin()->create())
->postJson('/admin/update/check')
->assertOk()
->assertJson(['ok' => true, 'result' => ['has_update' => false, 'changelog' => []]]);
Http::assertSent(fn ($request) => ! str_contains($request->url(), 'current_sha'));
$this->assertSame(str_repeat('d', 40), app(InstalledVersion::class)->current());
}
public function test_check_shows_changelog_preview_without_commit_texts(): void
{
app(InstalledVersion::class)->save(str_repeat('a', 40));
Http::fake(['*' => Http::response([
'has_update' => true,
'latest_sha' => str_repeat('b', 40),
'version' => '0.3.0',
'versions_behind' => 4,
'changelog_preview' => [
'entries' => [['version' => '0.3.0', 'heading' => '0.3.0', 'text' => "- Neue **Umfragen**\n- <script>x</script>"]],
'complete' => false,
],
])]);
$response = $this->actingAs(User::factory()->admin()->create())
->postJson('/admin/update/check')
->assertOk()
->assertJsonPath('result.has_update', true)
->assertJsonPath('result.version', '0.3.0')
->assertJsonPath('result.latest_short', 'bbbbbbb')
->assertJsonPath('result.versions_behind', 4)
->assertJsonPath('result.changelog_complete', false);
Http::assertSent(fn ($request) => str_contains($request->url(), 'current_sha='.str_repeat('a', 40)));
$this->assertStringContainsString('<strong>Umfragen</strong>', $response->json('result.changelog.0.html'));
$this->assertStringNotContainsString('<script>', $response->json('result.changelog.0.html'));
}
public function test_channel_can_be_changed_and_unknown_channels_are_rejected(): void
{
$admin = User::factory()->admin()->create();
$this->actingAs($admin)->postJson('/admin/update/channel', ['channel' => 'development'])->assertOk();
$this->assertSame('development', app(Channels::class)->active());
$this->actingAs($admin)->postJson('/admin/update/channel', ['channel' => 'nightly'])->assertUnprocessable();
}
public function test_unconfigured_channel_gives_clear_message(): void
{
Http::fake(['*' => Http::response(['code' => 'unknown_tool', 'error' => 'nicht konfiguriert'], 404)]);
$this->actingAs(User::factory()->admin()->create())
->postJson('/admin/update/check')
->assertOk()
->assertJsonPath('code', 'unknown_tool')
->assertJsonPath('message', 'Dieser Update-Kanal ist beim Hersteller noch nicht eingerichtet. Bitte den Kanal „Stabil“ verwenden.');
}
public function test_install_is_refused_while_another_run_is_active(): void
{
app(UpdateProgress::class)->set(50, 'extract', 'läuft');
$this->actingAs(User::factory()->admin()->create())
->postJson('/admin/update/install')
->assertStatus(409);
}
}