Files
ladill-events/tests/Feature/EventsMessagingIntegrationsTest.php
T
isaaccladandCursor 407f1364fb
Deploy Ladill Events / deploy (push) Successful in 44s
Use generic copy on messaging Integrations screens.
Drop wallet/brand marketing language so credential setup reads as neutral configuration.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-12 18:40:05 +00:00

244 lines
8.4 KiB
PHP

<?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('SMS')
->assertSee('Email')
->assertSee('credentials for outbound messaging');
}
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;
}
}