Add guest push notifications and queue alerts

This commit is contained in:
Codex Agent
2025-11-12 20:38:49 +01:00
parent 2c412e3764
commit 574aa47ce7
34 changed files with 1806 additions and 74 deletions

View File

@@ -0,0 +1,66 @@
<?php
namespace Tests\Feature\Api\Event;
use App\Models\Event;
use App\Models\PushSubscription;
use App\Models\Tenant;
use App\Services\EventJoinTokenService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PushSubscriptionApiTest extends TestCase
{
use RefreshDatabase;
public function test_guest_can_register_push_subscription(): void
{
$tenant = Tenant::factory()->create();
$event = Event::factory()->for($tenant)->create(['status' => 'published']);
$token = app(EventJoinTokenService::class)->createToken($event)->plain_token;
$payload = [
'endpoint' => 'https://updates.example.com/push/abc',
'keys' => [
'p256dh' => base64_encode('key'),
'auth' => base64_encode('auth'),
],
];
$response = $this->withHeaders(['X-Device-Id' => 'device-test-1'])
->postJson("/api/v1/events/{$token}/push-subscriptions", $payload);
$response->assertCreated();
$this->assertDatabaseHas('push_subscriptions', [
'event_id' => $event->id,
'device_id' => 'device-test-1',
'endpoint' => $payload['endpoint'],
'status' => 'active',
]);
}
public function test_guest_can_revoke_push_subscription(): void
{
$tenant = Tenant::factory()->create();
$event = Event::factory()->for($tenant)->create(['status' => 'published']);
$token = app(EventJoinTokenService::class)->createToken($event)->plain_token;
$subscription = PushSubscription::factory()
->for($tenant)
->for($event)
->create([
'endpoint' => 'https://updates.example.com/push/cached',
'device_id' => 'device-revoke',
]);
$response = $this->deleteJson("/api/v1/events/{$token}/push-subscriptions", [
'endpoint' => $subscription->endpoint,
]);
$response->assertOk()->assertJsonPath('status', 'revoked');
$this->assertDatabaseHas('push_subscriptions', [
'id' => $subscription->id,
'status' => 'revoked',
]);
}
}

View File

@@ -2,9 +2,11 @@
namespace Tests\Feature\Console;
use App\Enums\GuestNotificationType;
use App\Models\Event;
use App\Models\EventMediaAsset;
use App\Models\MediaStorageTarget;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Facades\Cache;
@@ -24,6 +26,10 @@ class CheckUploadQueuesCommandTest extends TestCase
]);
config()->set('storage-monitor.queue_health.stalled_minutes', 5);
config()->set('storage-monitor.queue_health.cache_minutes', 5);
config()->set('storage-monitor.queue_health.pending_event_threshold', 1);
config()->set('storage-monitor.queue_health.pending_event_minutes', 5);
config()->set('storage-monitor.queue_health.failed_event_threshold', 1);
config()->set('storage-monitor.queue_health.failed_event_minutes', 5);
$manager = Mockery::mock(QueueManager::class);
$connection = Mockery::mock(\Illuminate\Contracts\Queue\Queue::class);
@@ -51,7 +57,8 @@ class CheckUploadQueuesCommandTest extends TestCase
'priority' => 100,
]);
$event = Event::factory()->create();
$tenant = Tenant::factory()->create();
$event = Event::factory()->for($tenant)->create(['status' => 'published']);
$asset = EventMediaAsset::create([
'event_id' => $event->id,
'media_storage_target_id' => $target->id,
@@ -66,6 +73,16 @@ class CheckUploadQueuesCommandTest extends TestCase
'updated_at' => now()->subMinutes(10),
]);
EventMediaAsset::create([
'event_id' => $event->id,
'media_storage_target_id' => $target->id,
'variant' => 'original',
'disk' => 'local-hot',
'path' => 'events/'.$event->id.'/failed.jpg',
'size_bytes' => 256,
'status' => 'failed',
]);
$this->artisan('storage:check-upload-queues')
->expectsOutput('Checked 1 queue(s); 3 alert(s).')
->assertExitCode(0);
@@ -74,5 +91,15 @@ class CheckUploadQueuesCommandTest extends TestCase
$this->assertNotNull($snapshot);
$this->assertSame('critical', $snapshot['queues'][0]['severity']);
$this->assertGreaterThanOrEqual(1, count($snapshot['alerts']));
$this->assertDatabaseHas('guest_notifications', [
'event_id' => $event->id,
'type' => GuestNotificationType::UPLOAD_ALERT->value,
]);
$this->assertDatabaseHas('guest_notifications', [
'event_id' => $event->id,
'type' => GuestNotificationType::SUPPORT_TIP->value,
]);
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace Tests\Feature\Jobs;
use App\Jobs\SendGuestPushNotificationBatch;
use App\Models\Event;
use App\Models\GuestNotification;
use App\Models\PushSubscription;
use App\Models\Tenant;
use App\Services\Push\WebPushDispatcher;
use App\Services\PushSubscriptionService;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Minishlink\WebPush\MessageSentReport;
use Tests\TestCase;
class SendGuestPushNotificationBatchTest extends TestCase
{
use RefreshDatabase;
public function test_job_marks_subscription_as_delivered(): void
{
config()->set('push.enabled', true);
$tenant = Tenant::factory()->create();
$event = Event::factory()->for($tenant)->create(['status' => 'published']);
$notification = GuestNotification::factory()->create([
'tenant_id' => $tenant->id,
'event_id' => $event->id,
]);
$subscription = PushSubscription::factory()
->for($tenant)
->for($event)
->create();
$report = new MessageSentReport(
new Request('POST', 'https://push.example.com'),
new Response(201),
true,
'OK'
);
$dispatcher = new class($report) extends WebPushDispatcher
{
public function __construct(private MessageSentReport $report) {}
public function send(PushSubscription $subscription, array $payload): ?MessageSentReport
{
return $this->report;
}
};
$job = new SendGuestPushNotificationBatch($notification->id, [$subscription->id]);
$job->handle($dispatcher, app(PushSubscriptionService::class));
$this->assertNotNull($subscription->fresh()->last_notified_at);
$this->assertSame('active', $subscription->fresh()->status);
}
public function test_job_revokes_expired_subscription(): void
{
config()->set('push.enabled', true);
$tenant = Tenant::factory()->create();
$event = Event::factory()->for($tenant)->create(['status' => 'published']);
$notification = GuestNotification::factory()->create([
'tenant_id' => $tenant->id,
'event_id' => $event->id,
]);
$subscription = PushSubscription::factory()
->for($tenant)
->for($event)
->create();
$report = new MessageSentReport(
new Request('POST', 'https://push.example.com'),
new Response(410),
false,
'Gone'
);
$dispatcher = new class($report) extends WebPushDispatcher
{
public function __construct(private MessageSentReport $report) {}
public function send(PushSubscription $subscription, array $payload): ?MessageSentReport
{
return $this->report;
}
};
$job = new SendGuestPushNotificationBatch($notification->id, [$subscription->id]);
$job->handle($dispatcher, app(PushSubscriptionService::class));
$this->assertSame('revoked', $subscription->fresh()->status);
}
}