Add per-customer SMS and Bird Integrations for event messaging.
Deploy Ladill Events / deploy (push) Successful in 28s

Attendee and speaker comms use encrypted tenant relay credentials and skip platform-key fallback and Bird wallet double-debit.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-07-12 17:05:54 +00:00
co-authored by Cursor
parent 13b2bd9702
commit 6e42cd1cb2
21 changed files with 1095 additions and 80 deletions
+23 -13
View File
@@ -2,12 +2,13 @@
namespace Tests\Feature;
use App\Models\MessagingCredential;
use App\Models\QrCode;
use App\Models\User;
use App\Services\Events\EventSpeakerInviteService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class EventSpeakerInviteTest extends TestCase
@@ -20,17 +21,25 @@ class EventSpeakerInviteTest extends TestCase
$this->withoutMiddleware(\App\Http\Middleware\EnsurePlatformSession::class);
config([
'mail.from.address' => 'noreply@ladill.com',
'mail.from.name' => 'Ladill',
'events.service_api_keys.meet' => 'test-meet-key',
]);
Mail::fake();
Http::fake([
config('billing.api_url').'/balance*' => Http::response(['balance_minor' => 50000]),
config('billing.api_url').'/can-afford*' => Http::response(['affordable' => true]),
config('billing.api_url').'/debit' => Http::response(['success' => true]),
'*/api/smtp/send' => Http::response(['success' => true, 'message_id' => 'x']),
]);
}
private function seedBirdCredentials(User $owner): void
{
$plain = 'lsk_live_'.str_repeat('f', 32);
MessagingCredential::create([
'owner_ref' => $owner->public_id,
'bird_api_key_encrypted' => Crypt::encryptString($plain),
'bird_api_key_prefix' => substr($plain, 0, 16),
'bird_from_email' => 'events@example.test',
'bird_from_name' => 'Events',
'bird_status' => MessagingCredential::STATUS_VALID,
'bird_validated_at' => now(),
]);
}
@@ -76,6 +85,7 @@ class EventSpeakerInviteTest extends TestCase
public function test_manual_speaker_invite_sends_email_and_records_metadata(): void
{
$owner = User::factory()->create(['public_id' => 'usr_sp_manual']);
$this->seedBirdCredentials($owner);
$event = QrCode::create([
'user_id' => $owner->id,
@@ -103,7 +113,7 @@ class EventSpeakerInviteTest extends TestCase
->assertRedirect(route('speakers.show', $event))
->assertSessionHas('success');
Http::assertSent(fn ($request) => str_contains($request->url(), '/debit'));
Http::assertSent(fn ($request) => str_contains($request->url(), '/api/smtp/send'));
$event->refresh();
$speaker = $event->content()['speakers'][0];
@@ -115,6 +125,7 @@ class EventSpeakerInviteTest extends TestCase
public function test_programme_save_invites_assigned_speaker_hosts(): void
{
$owner = User::factory()->create(['public_id' => 'usr_sp_prog']);
$this->seedBirdCredentials($owner);
$programme = QrCode::create([
'user_id' => $owner->id,
@@ -166,7 +177,7 @@ class EventSpeakerInviteTest extends TestCase
app(EventSpeakerInviteService::class)->inviteProgrammeHosts($programme, $owner);
Http::assertSent(fn ($request) => str_contains($request->url(), '/debit'));
Http::assertSent(fn ($request) => str_contains($request->url(), '/api/smtp/send'));
$event->refresh();
$speaker = $event->content()['speakers'][0];
@@ -232,8 +243,6 @@ class EventSpeakerInviteTest extends TestCase
public function test_manual_speaker_invite_reports_when_email_not_configured(): void
{
config(['mail.from.address' => '']);
$owner = User::factory()->create(['public_id' => 'usr_sp_no_smtp']);
$event = QrCode::create([
@@ -266,6 +275,7 @@ class EventSpeakerInviteTest extends TestCase
public function test_manual_speaker_invite_can_be_resent_after_programme_invite(): void
{
$owner = User::factory()->create(['public_id' => 'usr_sp_resend']);
$this->seedBirdCredentials($owner);
$event = QrCode::create([
'user_id' => $owner->id,
@@ -296,6 +306,6 @@ class EventSpeakerInviteTest extends TestCase
->assertRedirect(route('speakers.show', $event))
->assertSessionHas('success');
Http::assertSent(fn ($request) => str_contains($request->url(), '/debit'));
Http::assertSent(fn ($request) => str_contains($request->url(), '/api/smtp/send'));
}
}
@@ -0,0 +1,242 @@
<?php
namespace Tests\Feature;
use App\Http\Middleware\EnsurePlatformSession;
use App\Models\MessagingCredential;
use App\Models\QrCode;
use App\Models\QrEventRegistration;
use App\Models\User;
use App\Services\Events\EventCommsService;
use App\Services\Events\EventEmailService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class EventsMessagingIntegrationsTest extends TestCase
{
use RefreshDatabase;
protected User $user;
protected function setUp(): void
{
parent::setUp();
$this->withoutMiddleware(EnsurePlatformSession::class);
$this->user = User::factory()->create([
'public_id' => 'test-user-msg-001',
'name' => 'Test User',
'email' => 'msg@example.com',
]);
}
public function test_integrations_page_loads(): void
{
$this->actingAs($this->user)
->get(route('account.integrations'))
->assertOk()
->assertSee('Ladill SMS')
->assertSee('Ladill Bird');
}
public function test_saving_sms_credentials_encrypts_key(): void
{
Http::fake([
'*/api/sms/me' => Http::response([
'api_key_prefix' => 'lsk_sms_live_abcd',
'service_id' => 1,
'status' => 'active',
'brand_name' => 'Events',
]),
'*/api/sms/senders' => Http::response([
'data' => [['id' => 1, 'sender_id' => 'EventsA', 'is_default' => true]],
'default_sender' => 'Ladill',
]),
]);
$plain = 'lsk_sms_live_'.str_repeat('a', 32);
$this->actingAs($this->user)
->post(route('account.integrations.sms.save'), [
'sms_api_key' => $plain,
'sms_sender_id' => 'EventsA',
])
->assertRedirect()
->assertSessionHas('success');
$row = MessagingCredential::query()->where('owner_ref', $this->user->public_id)->first();
$this->assertNotNull($row);
$this->assertNotSame($plain, $row->sms_api_key_encrypted);
$this->assertSame($plain, Crypt::decryptString($row->sms_api_key_encrypted));
$this->assertSame(MessagingCredential::STATUS_VALID, $row->sms_status);
$this->assertSame('EventsA', $row->sms_sender_id);
}
public function test_invalid_sms_key_is_rejected(): void
{
Http::fake([
'*/api/sms/me' => Http::response(['error' => 'Invalid or missing API key.'], 401),
]);
$this->actingAs($this->user)
->post(route('account.integrations.sms.save'), [
'sms_api_key' => 'lsk_sms_live_'.str_repeat('b', 32),
'sms_sender_id' => 'EventsA',
])
->assertRedirect()
->assertSessionHas('error');
}
public function test_attendee_comms_requires_credentials(): void
{
$event = $this->createEventWithAttendee();
$this->actingAs($this->user)
->post(route('events.attendees.share-comms', $event), [
'mode' => EventCommsService::MODE_JOIN,
])
->assertRedirect()
->assertSessionHas('error');
}
public function test_attendee_sms_uses_customer_relay_key(): void
{
$plain = 'lsk_sms_live_'.str_repeat('c', 32);
MessagingCredential::create([
'owner_ref' => $this->user->public_id,
'sms_api_key_encrypted' => Crypt::encryptString($plain),
'sms_api_key_prefix' => substr($plain, 0, 16),
'sms_sender_id' => 'EventsA',
'sms_status' => MessagingCredential::STATUS_VALID,
'sms_validated_at' => now(),
]);
Http::fake([
'*/api/sms/send' => Http::response(['success' => true, 'message_id' => 1]),
]);
$event = $this->createEventWithAttendee(phoneOnly: true);
$this->actingAs($this->user)
->post(route('events.attendees.share-comms', $event), [
'mode' => EventCommsService::MODE_JOIN,
])
->assertRedirect()
->assertSessionHas('success');
Http::assertSent(function ($request) use ($plain) {
return str_contains($request->url(), '/api/sms/send')
&& $request->hasHeader('Authorization', 'Bearer '.$plain)
&& $request['sender_id'] === 'EventsA';
});
}
public function test_attendee_email_uses_customer_bird_key(): void
{
$plain = 'lsk_live_'.str_repeat('d', 32);
MessagingCredential::create([
'owner_ref' => $this->user->public_id,
'bird_api_key_encrypted' => Crypt::encryptString($plain),
'bird_api_key_prefix' => substr($plain, 0, 16),
'bird_from_email' => 'events@example.test',
'bird_from_name' => 'Events',
'bird_status' => MessagingCredential::STATUS_VALID,
'bird_validated_at' => now(),
]);
Http::fake([
'*/api/smtp/send' => Http::response(['success' => true, 'message_id' => 'x']),
]);
$event = $this->createEventWithAttendee(emailOnly: true);
$this->actingAs($this->user)
->post(route('events.attendees.share-comms', $event), [
'mode' => EventCommsService::MODE_JOIN,
])
->assertRedirect()
->assertSessionHas('success');
Http::assertSent(function ($request) use ($plain) {
return str_contains($request->url(), '/api/smtp/send')
&& $request->hasHeader('Authorization', 'Bearer '.$plain)
&& $request['from'] === 'events@example.test'
&& $request['from_name'] === 'Events';
});
}
public function test_registration_confirmation_email_uses_customer_bird_key(): void
{
$plain = 'lsk_live_'.str_repeat('e', 32);
MessagingCredential::create([
'owner_ref' => $this->user->public_id,
'bird_api_key_encrypted' => Crypt::encryptString($plain),
'bird_api_key_prefix' => substr($plain, 0, 16),
'bird_from_email' => 'events@example.test',
'bird_from_name' => 'Events',
'bird_status' => MessagingCredential::STATUS_VALID,
'bird_validated_at' => now(),
]);
Http::fake([
'*/api/smtp/send' => Http::response(['success' => true, 'message_id' => 'x']),
]);
$sent = app(EventEmailService::class)->sendRegistrationConfirmation(
$this->user->public_id,
'ada@example.com',
'Demo event',
'BADGE123',
'https://meet.test/join',
'Ada Lovelace',
$this->user->email,
$this->user->name,
);
$this->assertTrue($sent);
Http::assertSent(function ($request) use ($plain) {
return str_contains($request->url(), '/api/smtp/send')
&& $request->hasHeader('Authorization', 'Bearer '.$plain);
});
}
private function createEventWithAttendee(bool $emailOnly = false, bool $phoneOnly = false): QrCode
{
$event = QrCode::create([
'user_id' => $this->user->id,
'short_code' => 'evt-msg-'.uniqid(),
'type' => QrCode::TYPE_EVENT,
'label' => 'Messaging test event',
'payload' => [
'content' => [
'name' => 'Messaging test event',
'format' => 'virtual',
'virtual_sessions' => [
['join_url' => 'https://meet.test/join-room'],
],
],
'style' => [],
],
'is_active' => true,
]);
QrEventRegistration::create([
'qr_code_id' => $event->id,
'user_id' => $this->user->id,
'reference' => 'QRE-'.strtoupper(uniqid()),
'badge_code' => 'BADGE'.random_int(1000, 9999),
'tier_name' => 'General',
'amount_minor' => 0,
'currency' => 'GHS',
'attendee_name' => 'Ada Lovelace',
'attendee_email' => $phoneOnly ? null : 'ada@example.com',
'attendee_phone' => $emailOnly ? null : '+233201111111',
'status' => QrEventRegistration::STATUS_CONFIRMED,
]);
return $event;
}
}