Ein neu angelegter Mandant war eine Sackgasse. Er bekam keine Website, also keinen Host, also keine Adresse – und der Host wird im Mandanten-Admin eingetragen, den er ohne Adresse nicht erreicht. „Im Admin öffnen" half nicht: ohne Domain landete es beim Mandanten des aktuellen Hosts. Einsprungsadressen gehören jetzt dem Betreiber, weil Webspace und Hosting ihm gehören und ein Mandant nicht die Adresse eines anderen auf sich zeigen lassen darf. Im Mandanten-Formular: Websites mit ihren Adressen. Beim Anlegen entstehen Website, Host-Einträge und eine leere Startseite – ohne Startseite antwortet die Website mit 404. Im Mandanten-Admin sind die Adressen nur noch sichtbar, nicht änderbar. Die Domain beim Hoster auf das Verzeichnis zu legen bleibt ein Schritt von Hand. Gesperrter Mandant: statt 404 ein Hinweis mit Status 503, Text je Mandant einstellbar. 503 hält die Seiten im Suchindex, weil die Sperre vorübergehend ist. Behoben: `URL::forceRootUrl(APP_URL)` zwang **alle** Links auf einen festen Host. Mit einem Mandanten unsichtbar; mit zwei Domains hätte jeder interne Link der zweiten Website auf die erste gezeigt. Der Ursprung ist jetzt der Host der aufgelösten Website. Neu: systemweite Einstellungen sind im Betreiber-Bereich änderbar (Löschfristen, Bot-Schutz, Flutgrenze, Sicherungen, Portal-Fristen, Versandweg, SMTP, harte Mail-Umleitung, Tokens). Leer heißt: der Wert der .env gilt wieder – und zwar sofort, nicht erst beim nächsten Start. Geheimnisse liegen verschlüsselt. Datenbank-Zugang und APP_KEY bleiben in der .env, weil sie gebraucht werden, um die Datenbank zu öffnen und das Gespeicherte zu entschlüsseln. 253 Feature-Tests grün. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
212 lines
7.4 KiB
PHP
212 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Enums\UserRole;
|
|
use App\Filament\Operator\Tenants\Pages\ListTenants;
|
|
use App\Filament\Resources\Bookings\BookingResource;
|
|
use App\Filament\Resources\Users\Pages\CreateUser;
|
|
use App\Filament\Resources\Users\Pages\ListUsers;
|
|
use App\Filament\Resources\Users\UserResource;
|
|
use App\Models\Booking;
|
|
use App\Models\Property;
|
|
use App\Models\Site;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Tenancy\TenantContext;
|
|
use Filament\Actions\Testing\TestAction;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Livewire\Livewire;
|
|
use Tests\TestCase;
|
|
|
|
class TenantAccessTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_a_member_cannot_open_another_landlords_booking(): void
|
|
{
|
|
[$ours, $theirs] = $this->twoLandlords();
|
|
$admin = $this->memberOf($ours);
|
|
$foreign = Booking::factory()->for(Property::factory()->create(['tenant_id' => $theirs->id]))->create();
|
|
|
|
$this->actingAs($admin);
|
|
|
|
$this->get(BookingResource::getUrl('view', ['record' => $foreign]))->assertNotFound();
|
|
$this->get(BookingResource::getUrl('index'))->assertOk()->assertDontSee($foreign->reference);
|
|
}
|
|
|
|
public function test_a_landlord_admin_sees_only_their_own_members(): void
|
|
{
|
|
[$ours, $theirs] = $this->twoLandlords();
|
|
$admin = $this->memberOf($ours);
|
|
$foreign = $this->memberOf($theirs, 'fremd@example.com');
|
|
|
|
$this->actingAs($admin);
|
|
|
|
$this->get(UserResource::getUrl('index'))
|
|
->assertOk()
|
|
->assertSee($admin->email)
|
|
->assertDontSee($foreign->email);
|
|
}
|
|
|
|
public function test_an_existing_account_can_be_added_without_changing_its_password(): void
|
|
{
|
|
[$ours, $theirs] = $this->twoLandlords();
|
|
$admin = $this->memberOf($ours);
|
|
$shared = $this->memberOf($theirs, 'geteilt@example.com');
|
|
$shared->forceFill(['password' => Hash::make('altes-passwort-123')])->saveQuietly();
|
|
|
|
$this->actingAs($admin);
|
|
app(TenantContext::class)->set($ours);
|
|
|
|
Livewire::test(CreateUser::class)
|
|
->fillForm([
|
|
'name' => 'Geteilt',
|
|
'email' => 'geteilt@example.com',
|
|
'role' => UserRole::Content->value,
|
|
'password' => 'neues-passwort-123',
|
|
])
|
|
->call('create')
|
|
->assertHasNoFormErrors();
|
|
|
|
$shared->refresh();
|
|
$this->assertTrue(Hash::check('altes-passwort-123', $shared->password));
|
|
$this->assertSame(UserRole::Content, $shared->roleOn($ours->id));
|
|
$this->assertSame(UserRole::Admin, $shared->roleOn($theirs->id));
|
|
$this->assertSame(1, User::query()->where('email', 'geteilt@example.com')->count());
|
|
}
|
|
|
|
public function test_password_and_two_factor_of_a_shared_account_stay_with_the_operator(): void
|
|
{
|
|
[$ours, $theirs] = $this->twoLandlords();
|
|
$admin = $this->memberOf($ours);
|
|
$shared = $this->memberOf($ours, 'geteilt@example.com');
|
|
$shared->tenants()->attach($theirs->id, ['role' => UserRole::Rentals->value]);
|
|
$operator = $this->memberOf($ours, 'betrieb@example.com');
|
|
$operator->forceFill(['is_operator' => true])->saveQuietly();
|
|
|
|
$this->assertFalse($admin->mayResetCredentials($shared));
|
|
$this->assertTrue($operator->mayResetCredentials($shared));
|
|
$this->assertFalse($shared->fresh()->is_operator);
|
|
|
|
$shared->update(['is_operator' => true]);
|
|
$this->assertFalse($shared->fresh()->is_operator);
|
|
|
|
$this->actingAs($admin);
|
|
|
|
Livewire::test(ListUsers::class)
|
|
->assertActionHidden(TestAction::make('resetTwoFactor')->table($shared));
|
|
|
|
$this->assertNotNull($shared->fresh()->getAppAuthenticationSecret());
|
|
|
|
$this->actingAs($operator);
|
|
|
|
Livewire::test(ListUsers::class)
|
|
->callAction(TestAction::make('resetTwoFactor')->table($shared));
|
|
|
|
$this->assertNull($shared->fresh()->getAppAuthenticationSecret());
|
|
}
|
|
|
|
public function test_the_membership_role_counts_inside_that_landlord(): void
|
|
{
|
|
$tenant = Tenant::factory()->create();
|
|
app(TenantContext::class)->set($tenant);
|
|
$user = User::factory()->create();
|
|
$user->tenants()->updateExistingPivot($tenant->id, ['role' => UserRole::Rentals->value]);
|
|
$user->forceFill(['role' => UserRole::Admin])->saveQuietly();
|
|
|
|
app(TenantContext::class)->set($tenant);
|
|
|
|
$this->assertTrue($user->hasRole(UserRole::Rentals));
|
|
$this->assertFalse($user->hasRole(UserRole::Admin));
|
|
}
|
|
|
|
public function test_a_suspended_landlord_is_closed_except_for_the_operator(): void
|
|
{
|
|
$tenant = Tenant::factory()->create(['is_active' => false]);
|
|
Site::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'domain' => 'localhost',
|
|
'is_default' => true,
|
|
]);
|
|
app(TenantContext::class)->set($tenant);
|
|
$admin = User::factory()->create();
|
|
|
|
// Visitors and the landlord's own people see a notice, not a dead link; 503 keeps the pages in the
|
|
// search index, because a suspended landlord is a temporary state.
|
|
$this->get('/')->assertStatus(503)->assertSee('nicht erreichbar');
|
|
$this->actingAs($admin)->get('/admin')->assertStatus(503);
|
|
|
|
$admin->forceFill(['is_operator' => true])->saveQuietly();
|
|
|
|
$this->actingAs($admin)->get('/admin')->assertOk();
|
|
}
|
|
|
|
public function test_only_the_operator_opens_the_operator_panel_and_the_visit_is_recorded(): void
|
|
{
|
|
$tenant = Tenant::factory()->create();
|
|
app(TenantContext::class)->set($tenant);
|
|
$admin = User::factory()->create();
|
|
|
|
$this->actingAs($admin)->get('/betreiber')->assertForbidden();
|
|
|
|
$admin->forceFill(['is_operator' => true])->saveQuietly();
|
|
|
|
$this->actingAs($admin)->get('/betreiber')->assertOk()->assertSee('Mandanten');
|
|
|
|
Livewire::test(ListTenants::class)
|
|
->callAction(TestAction::make('openAdmin')->table($tenant));
|
|
|
|
$this->assertDatabaseHas('tenant_access_logs', [
|
|
'tenant_id' => $tenant->id,
|
|
'user_id' => $admin->id,
|
|
]);
|
|
}
|
|
|
|
public function test_promote_operator_keeps_the_membership(): void
|
|
{
|
|
$tenant = Tenant::factory()->create();
|
|
app(TenantContext::class)->set($tenant);
|
|
$user = User::factory()->role(UserRole::Rentals)->create();
|
|
|
|
$this->artisan('users:promote-operator', ['email' => $user->email])->assertSuccessful();
|
|
|
|
$user->refresh();
|
|
$this->assertTrue($user->is_operator);
|
|
$this->assertSame(UserRole::Rentals, $user->roleOn($tenant->id));
|
|
$this->assertNull($user->otherTenantAdminUrl());
|
|
}
|
|
|
|
/**
|
|
* @return array{0: Tenant, 1: Tenant}
|
|
*/
|
|
private function twoLandlords(): array
|
|
{
|
|
$ours = Tenant::factory()->create();
|
|
$theirs = Tenant::factory()->create();
|
|
|
|
Site::factory()->create([
|
|
'tenant_id' => $ours->id,
|
|
'domain' => 'localhost',
|
|
'is_default' => false,
|
|
]);
|
|
Site::factory()->create([
|
|
'tenant_id' => $theirs->id,
|
|
'domain' => 'andere.test',
|
|
'is_default' => false,
|
|
]);
|
|
|
|
return [$ours, $theirs];
|
|
}
|
|
|
|
private function memberOf(Tenant $tenant, ?string $email = null): User
|
|
{
|
|
app(TenantContext::class)->set($tenant);
|
|
|
|
return User::factory()->create(array_filter([
|
|
'email' => $email,
|
|
]));
|
|
}
|
|
}
|