Operating mode in the admin (test mode sends all mail to a test recipient until switched live), bot protection for booking requests, mail rendering tests, mail:test command

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lars StommerandClaude Opus 5.5 committed 2026-10-22 23:01:44 +02:00
1 parent fdcb68d578
commit d018c97be4
22 files changed
+698 -23

No files matched your search

+3
View File
@@ -22,6 +22,9 @@ protected function setUp(): void
{
parent::setUp();
// These tests submit the form instantly; the bot time trap is tested in SpamProtectionTest.
config(['fewo.spam.min_seconds' => 0]);
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed([PropertySeeder::class, SiteSeeder::class]);
Mail::fake();
+3
View File
@@ -47,6 +47,9 @@ protected function setUp(): void
{
parent::setUp();
// These tests submit the form instantly; the bot time trap is tested in SpamProtectionTest.
config(['fewo.spam.min_seconds' => 0]);
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed(PropertySeeder::class);
$this->property = Property::sole();
+3
View File
@@ -40,6 +40,9 @@ protected function setUp(): void
{
parent::setUp();
// These tests submit the form instantly; the bot time trap is tested in SpamProtectionTest.
config(['fewo.spam.min_seconds' => 0]);
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed(PropertySeeder::class);
$this->property = Property::sole();
+128
View File
@@ -0,0 +1,128 @@
<?php
namespace Tests\Feature;
use App\Actions\AcceptOffer;
use App\Actions\CreateReservation;
use App\Actions\SendOffer;
use App\Enums\PortalEmailStatus;
use App\Enums\PortalEmailType;
use App\Mail\ArrivalInformation;
use App\Mail\BookingConfirmed;
use App\Mail\BookingDeclined;
use App\Mail\ChannelConflict;
use App\Mail\ChannelSyncAlert;
use App\Mail\ChannelTasksNotification;
use App\Mail\MailboxAlert;
use App\Mail\MarketingConsentRequest;
use App\Mail\NewReservation;
use App\Mail\NewReview;
use App\Mail\OfferAnswered;
use App\Mail\OfferMade;
use App\Mail\PaymentReminder;
use App\Mail\PortalMailNotification;
use App\Mail\ReservationExpired;
use App\Mail\ReservationReceived;
use App\Mail\ReviewRequest;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\ChannelTask;
use App\Models\PortalEmail;
use App\Models\Property;
use App\Models\Review;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
/**
* Every e-mail of the system is built completely (subject, body, attachments) in the state in which it is
* really sent – so a broken template is found here and not by a guest. Delivery itself is a server matter.
*/
class MailRenderingTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed(PropertySeeder::class);
Mail::fake();
Storage::fake('local');
}
public function test_every_mail_along_the_booking_path_renders_in_its_state(): void
{
$booking = app(CreateReservation::class)->handle(
Property::sole(),
new StayRequest(CarbonImmutable::parse('2026-12-01'), CarbonImmutable::parse('2026-12-05'), 2),
['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'erika@example.com', 'street' => 'Seeweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn'],
'Wir kommen mit Hund – ist das in Ordnung?',
sendMails: false,
);
$this->assertRenders(new ReservationReceived($booking), 'vorgemerkt');
$this->assertRenders(new NewReservation($booking), 'Erika Muster');
$send = app(SendOffer::class);
$booking = $send->handle($booking, ['price_adjustment' => -1500, 'offer_message' => 'Hund ist willkommen.'], sendMail: false);
$this->assertRenders(new OfferMade($booking, $send->offerUrl($booking)), 'Angebot ansehen und annehmen', attachments: 1);
$this->assertRenders(new ReservationExpired($booking), 'Angebot abgelaufen');
$this->assertRenders(new BookingDeclined($booking), 'Erika');
app(AcceptOffer::class)->handle($booking, $booking->offer_version);
$booking->refresh();
$this->assertRenders(new OfferAnswered($booking, accepted: true), 'Angebot angenommen');
$this->assertRenders(new OfferAnswered($booking, accepted: false), 'abgelehnt');
$this->assertRenders(new BookingConfirmed($booking), 'Sie haben unser Angebot', attachments: 1);
foreach ([PaymentReminder::DEPOSIT_OVERDUE, PaymentReminder::BALANCE_REQUEST, PaymentReminder::BALANCE_OVERDUE] as $kind) {
$this->assertRenders(new PaymentReminder($booking, $kind), 'Erika Muster');
}
$this->assertRenders(new ArrivalInformation($booking), 'Erika');
$this->assertRenders(new ReviewRequest($booking), 'Erika');
}
public function test_directly_confirmed_booking_and_other_notifications_render(): void
{
$booking = Booking::factory()->for(Property::sole())->create(['confirmed_at' => now(), 'deposit_due_on' => now()->addWeek()->toDateString()]);
$this->assertRenders(new BookingConfirmed($booking), 'verbindlich zu bestätigen');
$channel = Channel::factory()->for(Property::sole())->create(['name' => 'FeWo-direkt']);
$this->assertRenders(new ChannelSyncAlert($channel), 'FeWo-direkt');
$this->assertRenders(new ChannelConflict($channel, ['14.11.–17.11.2026 überschneidet Buchung 2026-0001']), 'überschneidet');
$this->assertRenders(new ChannelTasksNotification([ChannelTask::factory()->for($channel)->create()]), 'FeWo-direkt');
$this->assertRenders(new MailboxAlert('Login fehlgeschlagen'), 'Login fehlgeschlagen');
$this->assertRenders(new MarketingConsentRequest($booking->guest), $booking->guest->first_name);
$this->assertRenders(new NewReview(Review::factory()->create(['booking_id' => $booking->id, 'title' => 'Wunderbar'])), 'Wunderbar');
$this->assertRenders(new PortalMailNotification(PortalEmail::query()->create([
'channel_id' => $channel->id, 'message_id' => 'm1@portal', 'from' => 'noreply@portal.example', 'subject' => 'Neue Buchung',
'received_at' => now(), 'type' => PortalEmailType::Booking, 'status' => PortalEmailStatus::Blocked, 'starts_on' => '2026-11-14', 'ends_on' => '2026-11-17',
])), 'Neue Buchung');
}
public function test_test_mail_command_sends_and_respects_the_redirect(): void
{
config(['fewo.mail_redirect_to' => 'test@example.com']);
$this->artisan('mail:test', ['to' => 'gast@example.com'])
->expectsOutputToContain('Umleitung aktiv')
->assertSuccessful();
}
private function assertRenders(Mailable $mail, string $expected, int $attachments = 0): void
{
$html = $mail->render();
$this->assertNotEmpty($mail->envelope()->subject, $mail::class.' needs a subject');
$this->assertStringContainsString(e($expected), $html, $mail::class);
if (method_exists($mail, 'attachments')) {
$this->assertCount($attachments, $mail->attachments(), $mail::class.' attachments');
}
}
}
+78
View File
@@ -0,0 +1,78 @@
<?php
namespace Tests\Feature;
use App\Enums\UserRole;
use App\Filament\Pages\Operations;
use App\Models\User;
use App\Support\Settings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Mail\MailManager;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Tests\TestCase;
class OperationsModeTest extends TestCase
{
use RefreshDatabase;
public function test_new_installations_start_in_test_mode_with_the_notification_address(): void
{
config(['fewo.mail_redirect_to' => null, 'fewo.notification_email' => 'fewo@example.com']);
$this->assertFalse(Settings::isLive());
$this->assertSame('fewo@example.com', Settings::mailRedirect());
}
public function test_in_test_mode_every_mail_goes_to_the_test_recipient_only(): void
{
config(['fewo.mail_redirect_to' => null, 'mail.default' => 'array']);
Settings::save([Settings::MODE => Settings::MODE_TEST, Settings::TEST_RECIPIENT => 'test@example.com']);
Mail::raw('Hallo', fn ($message) => $message->to('gast@example.com')->cc('vermieterin@example.com')->subject('Angebot'));
$sent = app(MailManager::class)->mailer('array')->getSymfonyTransport()->messages()->last();
$this->assertSame(['test@example.com'], array_map(fn ($address) => $address->getAddress(), $sent->getEnvelope()->getRecipients()));
}
public function test_switched_live_mails_reach_their_real_recipients(): void
{
config(['fewo.mail_redirect_to' => null, 'mail.default' => 'array']);
Settings::save([Settings::MODE => Settings::MODE_LIVE]);
Mail::raw('Hallo', fn ($message) => $message->to('gast@example.com')->subject('Angebot'));
$sent = app(MailManager::class)->mailer('array')->getSymfonyTransport()->messages()->last();
$this->assertSame(['gast@example.com'], array_map(fn ($address) => $address->getAddress(), $sent->getEnvelope()->getRecipients()));
$this->assertNull(Settings::mailRedirect());
}
public function test_server_override_always_wins(): void
{
config(['fewo.mail_redirect_to' => 'server@example.com']);
Settings::save([Settings::MODE => Settings::MODE_LIVE]);
$this->assertSame('server@example.com', Settings::mailRedirect());
}
public function test_administrator_switches_the_mode_in_the_admin_and_sees_the_banner(): void
{
config(['fewo.mail_redirect_to' => null]);
$this->actingAs(User::factory()->create());
$this->get(Operations::getUrl())->assertOk()->assertSee('Testbetrieb:');
Livewire::test(Operations::class)
->set('data.mode', Settings::MODE_TEST)
->set('data.test_recipient', 'lars@example.com')
->call('save')
->assertHasNoErrors();
$this->assertSame('lars@example.com', Settings::mailRedirect());
Livewire::test(Operations::class)->set('data.mode', Settings::MODE_LIVE)->call('save')->assertNotified('Scharf geschaltet');
$this->assertTrue(Settings::isLive());
$this->actingAs(User::factory()->role(UserRole::Rentals)->create());
$this->get(Operations::getUrl())->assertForbidden();
}
}
+83
View File
@@ -0,0 +1,83 @@
<?php
namespace Tests\Feature;
use App\Livewire\BookingRequest;
use App\Models\Booking;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Tests\TestCase;
class SpamProtectionTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed(PropertySeeder::class);
Mail::fake();
}
public function test_form_sent_faster_than_a_human_is_ignored_silently(): void
{
$this->filled('anna@example.com')->call('submit')->assertHasNoErrors();
$this->assertSame(0, Booking::count());
}
public function test_honeypot_request_is_ignored(): void
{
$form = $this->filled('anna@example.com')->set('website', 'https://spam.example');
$this->travel(10)->seconds();
$form->call('submit');
$this->assertSame(0, Booking::count());
}
public function test_human_request_after_a_few_seconds_is_accepted(): void
{
$form = $this->filled('anna@example.com');
$this->travel(10)->seconds();
$form->call('submit')->assertHasNoErrors()->assertSee('Vielen Dank, Anna!');
$this->assertSame(1, Booking::count());
}
public function test_one_email_address_cannot_hold_many_dates_at_once(): void
{
foreach ([['2026-11-07', '2026-11-10'], ['2026-11-14', '2026-11-17'], ['2026-11-21', '2026-11-24']] as [$arrival, $departure]) {
$form = $this->filled('anna@example.com', $arrival, $departure);
$this->travel(10)->seconds();
$form->call('submit');
}
$this->assertSame(2, Booking::count());
}
public function test_links_in_names_or_many_links_in_the_message_are_rejected(): void
{
$form = $this->filled('anna@example.com')->set('firstName', 'Cheap https://pills.example')->set('message', 'see www.a.example and https://b.example');
$this->travel(10)->seconds();
$form->call('submit')->assertHasErrors(['firstName', 'message']);
$this->assertSame(0, Booking::count());
}
private function filled(string $email, string $arrival = '2026-11-07', string $departure = '2026-11-10'): Testable
{
return Livewire::test(BookingRequest::class)
->set('arrival', $arrival)
->set('departure', $departure)
->set('firstName', 'Anna')
->set('lastName', 'Muster')
->set('email', $email)
->set('privacyAccepted', true);
}
}