Files
fewo-stommer/tests/Feature/TenantServiceGateTest.php
Lars StommerandClaude Opus 5 fd2a7f7ded Dienste wirklich anschließen: zentrale Steuerung statt folgenloser Schalter
Die Schalter aus Phase 3 schrieben nur in eine Tabelle, die niemand las.
Ein gesperrter Portal-Abgleich lief trotzdem weiter. Das ist behoben.

`App\Tenancy\TenantFeatures` ist die eine Stelle, die beantwortet, ob ein
Mandant einen Dienst hat. Sie wird an allen Wegen abgefragt, die zum Dienst
führen:

- `RolePolicy::allows()` – damit greift die Mandanten-Ebene in *jeder*
  Richtlinie des Admins. Die Rolle sagt, was ein Benutzer darf; der Dienst
  sagt, was der Mandant überhaupt hat. Keine Rolle wiegt einen gesperrten
  Dienst auf.
- `Channel::importsOccupations()` – Import und Scheduler.
- Der öffentliche iCal-Feed antwortet mit 404.
- `ProcessPortalMail` und `channels:fetch-mail` für den Postfach-Schnellweg.

`TenantServiceKey::isWired()` trennt Gebautes von Geplantem. Nur verdrahtete
Dienste erscheinen als Schalter; die drei KI-Dienste stehen als Hinweis da,
bis die Anwendung sie abfragt. Eigene API-Schlüssel sind aus dem Betreiber-
Formular verschwunden – sie gehören in den Admin des Mandanten, wenn es
soweit ist (A16).

Neu im Betreiber-Bereich: Seite „System" mit den Grenzen, die für alle
Mandanten gelten (aus der .env, bewusst nicht änderbar), der Mandantenliste
und dem Zugriffsprotokoll. Bisher war dort nur die Mandantenliste.

Die Migration schaltet jedem vorhandenen Mandanten die Portal-Dienste frei,
damit der Umbau laufenden Betrieb nicht stillschweigend stoppt.

240 Feature-Tests grün, davon fünf neue in `TenantServiceGateTest`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-24 09:14:10 +02:00

116 lines
4.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Feature;
use App\Enums\TenantServiceKey;
use App\Enums\UserRole;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\ChannelTask;
use App\Models\PortalEmail;
use App\Models\Tenant;
use App\Models\User;
use App\Tenancy\TenantContext;
use App\Tenancy\TenantFeatures;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Gate;
use Tests\TestCase;
/**
* A service the operator has locked must not be reachable by any route: not by the scheduler, not by the
* public feed, and not by a user whose role would otherwise allow it.
*/
class TenantServiceGateTest extends TestCase
{
use RefreshDatabase;
public function test_a_locked_portal_service_stops_the_import_and_the_feed(): void
{
$tenant = Tenant::factory()->create();
app(TenantContext::class)->set($tenant);
$channel = Channel::factory()->create();
$this->assertTrue($channel->importsOccupations());
$this->get(route('channels.feed', ['token' => $channel->export_token]))->assertOk();
$this->lock($tenant, TenantServiceKey::PortalSync);
$this->assertFalse($channel->fresh()->importsOccupations());
$this->get(route('channels.feed', ['token' => $channel->export_token]))->assertNotFound();
}
public function test_the_sync_command_skips_a_landlord_without_the_service(): void
{
$allowed = Tenant::factory()->create(['name' => 'Mit Dienst']);
$locked = Tenant::factory()->create(['name' => 'Ohne Dienst']);
app(TenantContext::class)->set($allowed);
$allowedChannel = Channel::factory()->create();
app(TenantContext::class)->set($locked);
$lockedChannel = Channel::factory()->create();
$this->lock($locked, TenantServiceKey::PortalSync);
app(TenantContext::class)->forget();
$this->assertTrue($allowedChannel->fresh()->importsOccupations());
$this->assertFalse($lockedChannel->fresh()->importsOccupations());
}
public function test_a_locked_service_closes_the_admin_screens_whatever_the_role_says(): void
{
$tenant = Tenant::factory()->create();
app(TenantContext::class)->set($tenant);
$admin = User::factory()->role(UserRole::Admin)->create();
$this->actingAs($admin);
$this->assertTrue(Gate::forUser($admin)->allows('viewAny', ChannelTask::class));
$this->lock($tenant, TenantServiceKey::PortalSync);
$this->lock($tenant, TenantServiceKey::PortalMail);
$this->assertFalse(Gate::forUser($admin)->allows('viewAny', ChannelTask::class));
$this->assertFalse(Gate::forUser($admin)->allows('viewAny', PortalEmail::class));
// The role itself is untouched – the landlord simply does not have the service.
$this->assertTrue(Gate::forUser($admin)->allows('viewAny', Booking::class));
}
public function test_the_mailbox_fast_path_stays_open_while_only_the_sync_is_locked(): void
{
$tenant = Tenant::factory()->create();
app(TenantContext::class)->set($tenant);
$admin = User::factory()->role(UserRole::Admin)->create();
$this->lock($tenant, TenantServiceKey::PortalSync);
$this->assertTrue(Gate::forUser($admin)->allows('viewAny', PortalEmail::class));
}
public function test_the_system_page_shows_the_limits_and_stays_closed_for_a_landlords_administrator(): void
{
$tenant = Tenant::factory()->create(['name' => 'Beispiel GbR']);
app(TenantContext::class)->set($tenant);
config(['fewo.spam.total_per_hour' => 30, 'fewo.guest_retention_years' => 10]);
$admin = User::factory()->role(UserRole::Admin)->create();
$this->actingAs($admin)->get('/betreiber/system')->assertForbidden();
$admin->forceFill(['is_operator' => true])->saveQuietly();
$this->actingAs($admin)->get('/betreiber/system')
->assertOk()
->assertSee('Beispiel GbR')
->assertSee('30 je Stunde')
->assertSee('10 Jahre');
}
private function lock(Tenant $tenant, TenantServiceKey $service): void
{
$tenant->services()->updateOrCreate(['service' => $service->value], ['is_enabled' => false]);
app(TenantFeatures::class)->forget($tenant);
}
}