Merge remote-tracking branch 'origin/main' into theme/seebad
Deploy / test (push) Failing after 2s
Deploy / deploy (push) Skipped

This commit is contained in:
Lars committed 2026-10-23 12:33:33 +02:00
commit b3114cdfe3
93 files changed
+2564 -114

No files matched your search

+3
View File
@@ -8,6 +8,7 @@
use App\Models\Property;
use App\Models\Season;
use App\Models\SpecialOffer;
use App\Models\Tenant;
use App\Pricing\AdvertisedOffer;
use App\Pricing\OfferShowcase;
use Carbon\CarbonImmutable;
@@ -105,7 +106,9 @@ public function test_gap_below_minimum_stay_is_not_advertised_unless_the_offer_w
public function test_offers_section_shows_the_advertised_offers_on_the_website(): void
{
$this->travelTo($this->now);
$unusedLandlord = $this->property->tenant_id;
$this->property->delete();
Tenant::query()->whereKey($unusedLandlord)->delete();
$this->seed([PropertySeeder::class, SiteSeeder::class]);
SpecialOffer::factory()->for(Property::query()->sole())->create(['name' => 'Ab 3 Nächten −10 %', 'min_nights' => 3, 'discount_value' => 10]);
+1 -1
View File
@@ -67,7 +67,7 @@ public function test_request_creates_a_held_reservation_and_sends_quote_and_noti
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->assertSame('2026-10-24 10:00', $booking->hold_expires_at->format('Y-m-d H:i'));
$this->assertSame(3 * 6500 + 10800, $booking->total);
$this->assertSame('2026-'.str_pad((string) $booking->id, 4, '0', STR_PAD_LEFT), $booking->reference);
$this->assertSame('2026-0001', $booking->reference);
$this->assertSame('anna@example.com', $booking->guest->email);
Mail::assertSent(ReservationReceived::class, fn (ReservationReceived $mail): bool => $mail->hasTo('anna@example.com'));
Mail::assertSent(NewReservation::class, fn (NewReservation $mail): bool => $mail->hasTo(config('fewo.notification_email')));
+209
View File
@@ -0,0 +1,209 @@
<?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();
$this->get('/')->assertNotFound();
$this->actingAs($admin)->get('/admin')->assertNotFound();
$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,
]));
}
}
+97
View File
@@ -0,0 +1,97 @@
<?php
namespace Tests\Feature;
use App\Actions\CreateReservation;
use App\Models\Booking;
use App\Models\Guest;
use App\Models\Property;
use App\Models\Tenant;
use App\Pricing\StayRequest;
use App\Tenancy\TenantContext;
use App\Tenancy\TenantContextMissing;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class TenantIsolationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
Mail::fake();
}
public function test_the_same_email_address_is_a_separate_guest_for_each_landlord(): void
{
$this->seed(PropertySeeder::class);
$stommer = Tenant::query()->where('slug', 'stommer')->firstOrFail();
$other = Tenant::factory()->create();
$foreign = Guest::factory()->create([
'tenant_id' => $other->id,
'email' => 'anna@example.com',
'first_name' => 'Bert',
]);
$booking = app(CreateReservation::class)->handle(
Property::withoutGlobalScope('tenant')->where('tenant_id', $stommer->id)->firstOrFail(),
new StayRequest(CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10'), 2),
['first_name' => 'Anna', 'last_name' => 'Muster', 'email' => 'anna@example.com'],
);
$this->assertNotSame($foreign->id, $booking->guest_id);
$this->assertSame('Anna', $booking->guest->first_name);
$this->assertSame($stommer->id, $booking->guest->tenant_id);
app(TenantContext::class)->set($stommer);
$this->assertSame(1, Guest::query()->where('email', 'anna@example.com')->count());
app(TenantContext::class)->set($other);
$this->assertSame('Bert', Guest::query()->where('email', 'anna@example.com')->firstOrFail()->first_name);
}
public function test_booking_numbers_continue_per_landlord_and_start_again_for_the_next_one(): void
{
$firstLandlord = Tenant::factory()->create();
$secondLandlord = Tenant::factory()->create();
$firstProperty = Property::factory()->create(['tenant_id' => $firstLandlord->id]);
$existing = Booking::factory()->for($firstProperty)->create();
$existing->forceFill(['reference' => '2026-0042'])->saveQuietly();
$next = Booking::factory()->for($firstProperty)->create();
$other = Booking::factory()->for(Property::factory()->create(['tenant_id' => $secondLandlord->id]))->create();
$this->assertSame('2026-0043', $next->reference);
$this->assertSame('2026-0001', $other->reference);
$this->assertSame($firstLandlord->id, $next->tenant_id);
$this->assertSame($secondLandlord->id, $other->tenant_id);
}
public function test_a_query_fails_when_a_landlord_is_required_and_none_is_current(): void
{
$context = app(TenantContext::class);
$context->forget();
$context->requirePresence();
$this->expectException(TenantContextMissing::class);
Guest::query()->count();
}
public function test_the_seeded_apartment_belongs_to_stommer(): void
{
$this->seed(PropertySeeder::class);
$property = Property::withoutGlobalScope('tenant')->sole();
$this->assertSame('stommer', $property->tenant->slug);
$this->assertSame($property->tenant_id, $property->seasons()->withoutGlobalScope('tenant')->firstOrFail()->tenant_id);
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
namespace Tests\Feature;
use App\Actions\SendOffer;
use App\Models\Booking;
use App\Models\Page;
use App\Models\Property;
use App\Models\Site;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class WebsiteTenancyTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->withoutVite();
}
public function test_one_landlord_still_answers_on_any_address(): void
{
$site = $this->website('Einzige Wohnung');
$this->get('http://fremd.test/')->assertOk()->assertSee('Einzige Wohnung');
$this->assertSame($site->id, Site::query()->where('is_default', true)->value('id'));
}
public function test_a_second_landlord_is_reached_only_on_their_own_address(): void
{
$this->website('Nordhaus', 'nordhaus.test');
$this->website('Südheim', 'suedheim.test');
$this->get('http://nordhaus.test/')->assertOk()->assertSee('Nordhaus')->assertDontSee('Südheim');
$this->get('http://suedheim.test/')->assertOk()->assertSee('Südheim')->assertDontSee('Nordhaus');
$this->get('http://unbekannt.test/')->assertNotFound();
}
public function test_the_offer_link_uses_the_website_address(): void
{
$site = $this->website('Nordhaus', 'nordhaus.test');
$property = Property::factory()->create(['site_id' => $site->id]);
$booking = Booking::factory()->for($property)->create([
'hold_expires_at' => now()->addDay(),
'offer_version' => 1,
]);
$url = app(SendOffer::class)->offerUrl($booking);
$this->assertStringStartsWith('https://nordhaus.test/angebot/', $url);
$this->assertStringContainsString('signature=', $url);
}
private function website(string $name, ?string $domain = null): Site
{
$tenant = Tenant::factory()->create();
$site = Site::factory()->create([
'tenant_id' => $tenant->id,
'name' => $name,
'domain' => $domain,
'is_default' => $domain === null,
]);
Page::factory()->create([
'site_id' => $site->id,
'slug' => Page::HOME,
'title' => $name,
'sections' => [],
]);
return $site;
}
}