presseportale/tests/Feature/ActivityLogTest.php
Kevin Adametz 5dfd622141 security: Andere Geräte abmelden, IP-weites Login-Limit, dauerhafte Sperrmarke, Passwortregeln, CSP im Melde-Modus
Restliche Phase 4 aus dem Sicherheitsaudit 07.10.2026:

- Sicherheitsseite: „Andere Geräte abmelden“ rotiert remember_token und
  beendet alle anderen Sessions, die aktuelle bleibt (nicht während einer
  Impersonation).
- LoginIpThrottle: 20 Fehlversuche je IP in 15 Minuten über alle Konten.
  Gezählt über das Failed-Event, damit Livewire-Login und Fortifys
  POST /login denselben Zähler teilen; Erfolg setzt ihn nicht zurück.
- Sperrmarke für Sessions in users.sessions_revoked_at_ms statt nur im
  Cache — übersteht cache:clear. Die alte Cache-Marke zählt übergangsweise
  mit und kann im nächsten Release entfallen.
- Neue und geänderte Passwörter: mindestens 12 Zeichen, in Produktion
  zusätzlich uncompromised() (HIBP, k-Anonymität). Bestehende bleiben gültig.
- Content-Security-Policy-Report-Only in Produktion; Meldungen über
  /csp-report nach storage/logs/csp-*.log, je Meldung höchstens einmal pro
  Stunde, ohne Query und tokenartige Pfadsegmente.

Migration: users.sessions_revoked_at_ms (users hat FULLTEXT, 25 MB, Sekunden).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 13:02:49 +00:00

156 lines
5.9 KiB
PHP

<?php
use App\Enums\ActivityType;
use App\Enums\PressReleaseStatus;
use App\Listeners\RecordAuthActivity;
use App\Models\ActivityEvent;
use App\Models\ContactMessage;
use App\Models\PressRelease;
use App\Models\User;
use App\Models\WithdrawalRequest;
use App\Services\Billing\StripeCheckoutService;
use App\Services\PressRelease\PressReleaseService;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Auth\Events\Login;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;
use Tests\TestCase;
test('a self registration is recorded in the activity log', function () {
/** @var TestCase $this */
Notification::fake();
Livewire::test('auth.register')
->set('name', 'Verlauf Tester')
->set('email', 'verlauf@example.com')
->set('password', 'sicheres-passwort-2026')
->set('password_confirmation', 'sicheres-passwort-2026')
->set('terms_accepted', true)
->call('register');
$event = ActivityEvent::query()->where('type', ActivityType::UserRegistered->value)->sole();
expect($event->meta['way'])->toBe('form')
->and($event->description)->toContain('verlauf@example.com')
->and($event->user_id)->not->toBeNull();
});
test('a login is recorded with its method', function () {
$user = User::factory()->create(['is_active' => true]);
request()->attributes->set(RecordAuthActivity::AUTH_METHOD_ATTRIBUTE, 'magic-link');
event(new Login('web', $user, false));
$event = ActivityEvent::query()->where('type', ActivityType::UserLoggedIn->value)->sole();
expect($event->meta['method'])->toBe('magic-link')
->and($event->description)->toContain('Magic-Link')
->and($event->user_id)->toBe($user->id);
});
test('a press release status change is recorded', function () {
Queue::fake();
$pressRelease = PressRelease::factory()->create(['status' => PressReleaseStatus::Draft]);
app(PressReleaseService::class)->submitForReview($pressRelease);
$event = ActivityEvent::query()->where('type', ActivityType::PressReleaseStatus->value)->first();
expect($event)->not->toBeNull()
->and($event->description)->toContain($pressRelease->title)
->and($event->subject_id)->toBe($pressRelease->id)
->and($event->actor_label)->toBe('System');
});
test('form submissions are recorded via the observer', function () {
ContactMessage::factory()->create([
'name' => 'Max Beispiel',
'email' => 'max@example.com',
]);
WithdrawalRequest::factory()->create([
'name' => 'Erika Beispiel',
'email' => 'erika@example.com',
]);
expect(ActivityEvent::query()->where('type', ActivityType::ContactRequest->value)->sole()->description)
->toContain('max@example.com');
expect(ActivityEvent::query()->where('type', ActivityType::WithdrawalRequest->value)->sole()->description)
->toContain('erika@example.com');
});
test('the activity report page lists events and filters by type', function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
$admin = User::factory()->create(['is_active' => true]);
$admin->assignRole('admin');
ActivityEvent::query()->create([
'type' => ActivityType::ContactRequest->value,
'occurred_at' => now(),
'actor_label' => 'Gast',
'description' => 'Kontaktanfrage „Testthema“ von Gast (gast@example.com).',
]);
ActivityEvent::query()->create([
'type' => ActivityType::UserLoggedIn->value,
'occurred_at' => now(),
'user_id' => $admin->id,
'actor_label' => $admin->name,
'description' => 'Admin hat sich angemeldet — Passwort.',
]);
$this->actingAs($admin);
$this->get(route('admin.reports.activity'))->assertOk();
Livewire::test('admin.reports.activity')
->assertSee('Kontaktanfrage „Testthema“')
->assertSee('hat sich angemeldet')
->set('typeFilter', ActivityType::ContactRequest->value)
->assertSee('Kontaktanfrage „Testthema“')
->assertDontSee('hat sich angemeldet');
});
test('a sent mail is recorded and linked to the recipient user', function () {
$user = User::factory()->create(['email' => 'empfaenger@example.com']);
Mail::raw('Inhalt', function ($message) {
$message->to('empfaenger@example.com')->subject('Willkommen bei pressekonto');
});
$event = ActivityEvent::query()->where('type', ActivityType::MailSent->value)->sole();
expect($event->description)->toContain('Willkommen bei pressekonto')
->and($event->description)->toContain('empfaenger@example.com')
->and($event->user_id)->toBe($user->id)
->and($event->meta['to'])->toBe(['empfaenger@example.com'])
->and($event->meta['subject'])->toBe('Willkommen bei pressekonto');
});
test('a mail to an unknown address is recorded with the address as actor label', function () {
Mail::raw('Inhalt', function ($message) {
$message->to('extern@example.com')->subject('Externe Info');
});
$event = ActivityEvent::query()->where('type', ActivityType::MailSent->value)->sole();
expect($event->user_id)->toBeNull()
->and($event->actor_label)->toBe('extern@example.com');
});
test('a checkout redirect is recorded in the activity log', function () {
$user = User::factory()->create(['name' => 'Kauf Karla']);
app(StripeCheckoutService::class)
->recordCheckoutStarted($user, 'Abo „Business" (monatlich)', ['kind' => 'subscription', 'interval' => 'monthly']);
$event = ActivityEvent::query()->where('type', ActivityType::CheckoutStarted->value)->sole();
expect($event->user_id)->toBe($user->id)
->and($event->description)->toContain('zur Zahlung weitergeleitet')
->and($event->description)->toContain('Abo „Business" (monatlich)')
->and($event->meta['kind'])->toBe('subscription');
});