Add per-customer SMS and Bird credentials to Frontdesk Integrations.
Deploy Ladill Frontdesk / deploy (push) Successful in 45s
Deploy Ladill Frontdesk / deploy (push) Successful in 45s
NotificationDispatcher sends via customer relay and skips Frontdesk wallet debit when tenant keys are configured. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Http\Middleware\EnsurePlatformSession;
|
||||
use App\Models\Branch;
|
||||
use App\Models\Member;
|
||||
use App\Models\MessagingCredential;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FrontdeskMessagingIntegrationsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected User $user;
|
||||
|
||||
protected Organization $organization;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->withoutMiddleware(EnsurePlatformSession::class);
|
||||
|
||||
config(['billing.api_url' => 'https://billing.test']);
|
||||
|
||||
$this->user = User::create([
|
||||
'public_id' => 'test-user-msg-001',
|
||||
'name' => 'Test User',
|
||||
'email' => 'msg@example.com',
|
||||
]);
|
||||
|
||||
$this->organization = Organization::create([
|
||||
'owner_ref' => $this->user->public_id,
|
||||
'name' => 'Test Frontdesk',
|
||||
'slug' => 'test-frontdesk-msg',
|
||||
'settings' => [
|
||||
'onboarded' => true,
|
||||
'plan' => 'pro',
|
||||
'plan_expires_at' => now()->addMonth()->toIso8601String(),
|
||||
],
|
||||
]);
|
||||
|
||||
Member::create([
|
||||
'owner_ref' => $this->user->public_id,
|
||||
'organization_id' => $this->organization->id,
|
||||
'user_ref' => $this->user->public_id,
|
||||
'role' => 'org_admin',
|
||||
]);
|
||||
|
||||
Branch::create([
|
||||
'owner_ref' => $this->user->public_id,
|
||||
'organization_id' => $this->organization->id,
|
||||
'name' => 'Main',
|
||||
'is_active' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_integrations_page_loads_messaging_cards(): void
|
||||
{
|
||||
$this->actingAs($this->user)
|
||||
->get(route('frontdesk.integrations.edit'))
|
||||
->assertOk()
|
||||
->assertSee('Ladill SMS')
|
||||
->assertSee('Ladill Bird')
|
||||
->assertSee('Outbound webhooks');
|
||||
}
|
||||
|
||||
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',
|
||||
]),
|
||||
'*/api/sms/senders' => Http::response([
|
||||
'data' => [['id' => 1, 'sender_id' => 'FrontA', 'is_default' => true]],
|
||||
'default_sender' => 'Ladill',
|
||||
]),
|
||||
]);
|
||||
|
||||
$plain = 'lsk_sms_live_'.str_repeat('a', 32);
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->post(route('frontdesk.integrations.sms.save'), [
|
||||
'sms_api_key' => $plain,
|
||||
'sms_sender_id' => 'FrontA',
|
||||
])
|
||||
->assertRedirect()
|
||||
->assertSessionHas('success');
|
||||
|
||||
$row = MessagingCredential::query()->where('organization_id', $this->organization->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('FrontA', $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('frontdesk.integrations.sms.save'), [
|
||||
'sms_api_key' => 'lsk_sms_live_'.str_repeat('b', 32),
|
||||
'sms_sender_id' => 'FrontA',
|
||||
])
|
||||
->assertRedirect()
|
||||
->assertSessionHas('error');
|
||||
}
|
||||
|
||||
public function test_host_email_requires_bird_credentials(): void
|
||||
{
|
||||
Http::fake([
|
||||
'billing.test/*' => Http::response(['affordable' => true, 'ok' => true]),
|
||||
]);
|
||||
|
||||
$host = \App\Models\Host::create([
|
||||
'owner_ref' => $this->user->public_id,
|
||||
'organization_id' => $this->organization->id,
|
||||
'name' => 'No Bird Host',
|
||||
'email' => 'nobird@example.com',
|
||||
'is_available' => true,
|
||||
]);
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->post(route('frontdesk.visits.store'), [
|
||||
'full_name' => 'Guest',
|
||||
'host_id' => $host->id,
|
||||
'visitor_type' => 'visitor',
|
||||
'policies_accepted' => '1',
|
||||
])
|
||||
->assertRedirect();
|
||||
|
||||
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/api/smtp/send'));
|
||||
}
|
||||
|
||||
public function test_host_email_uses_customer_bird_relay(): void
|
||||
{
|
||||
$plain = 'lsk_live_'.str_repeat('d', 32);
|
||||
MessagingCredential::create([
|
||||
'owner_ref' => $this->user->public_id,
|
||||
'organization_id' => $this->organization->id,
|
||||
'bird_api_key_encrypted' => Crypt::encryptString($plain),
|
||||
'bird_api_key_prefix' => substr($plain, 0, 16),
|
||||
'bird_from_email' => 'frontdesk@example.test',
|
||||
'bird_from_name' => 'Frontdesk',
|
||||
'bird_status' => MessagingCredential::STATUS_VALID,
|
||||
'bird_validated_at' => now(),
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'billing.test/*' => Http::response(['affordable' => true, 'ok' => true]),
|
||||
'*/api/smtp/send' => Http::response(['success' => true, 'message_id' => 'x']),
|
||||
]);
|
||||
|
||||
$host = \App\Models\Host::create([
|
||||
'owner_ref' => $this->user->public_id,
|
||||
'organization_id' => $this->organization->id,
|
||||
'name' => 'Bird Host',
|
||||
'email' => 'host@example.com',
|
||||
'is_available' => true,
|
||||
]);
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->post(route('frontdesk.visits.store'), [
|
||||
'full_name' => 'Guest',
|
||||
'host_id' => $host->id,
|
||||
'visitor_type' => 'visitor',
|
||||
'policies_accepted' => '1',
|
||||
])
|
||||
->assertRedirect();
|
||||
|
||||
Http::assertSent(function ($request) use ($plain) {
|
||||
return str_contains($request->url(), '/api/smtp/send')
|
||||
&& $request->hasHeader('Authorization', 'Bearer '.$plain)
|
||||
&& $request['from'] === 'frontdesk@example.test'
|
||||
&& $request['from_name'] === 'Frontdesk';
|
||||
});
|
||||
|
||||
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/debit'));
|
||||
}
|
||||
|
||||
public function test_host_sms_uses_customer_relay_key(): void
|
||||
{
|
||||
$plain = 'lsk_sms_live_'.str_repeat('c', 32);
|
||||
MessagingCredential::create([
|
||||
'owner_ref' => $this->user->public_id,
|
||||
'organization_id' => $this->organization->id,
|
||||
'sms_api_key_encrypted' => Crypt::encryptString($plain),
|
||||
'sms_api_key_prefix' => substr($plain, 0, 16),
|
||||
'sms_sender_id' => 'FrontA',
|
||||
'sms_status' => MessagingCredential::STATUS_VALID,
|
||||
'sms_validated_at' => now(),
|
||||
]);
|
||||
|
||||
$this->organization->update([
|
||||
'settings' => array_merge($this->organization->settings ?? [], [
|
||||
'notification_channels' => ['sms'],
|
||||
]),
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'billing.test/*' => Http::response(['affordable' => true, 'ok' => true]),
|
||||
'*/api/sms/send' => Http::response(['success' => true, 'message_id' => 1]),
|
||||
]);
|
||||
|
||||
$host = \App\Models\Host::create([
|
||||
'owner_ref' => $this->user->public_id,
|
||||
'organization_id' => $this->organization->id,
|
||||
'name' => 'SMS Host',
|
||||
'phone' => '+233201111111',
|
||||
'is_available' => true,
|
||||
]);
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->post(route('frontdesk.visits.store'), [
|
||||
'full_name' => 'Guest',
|
||||
'host_id' => $host->id,
|
||||
'visitor_type' => 'visitor',
|
||||
'policies_accepted' => '1',
|
||||
])
|
||||
->assertRedirect();
|
||||
|
||||
Http::assertSent(function ($request) use ($plain) {
|
||||
return str_contains($request->url(), '/api/sms/send')
|
||||
&& $request->hasHeader('Authorization', 'Bearer '.$plain)
|
||||
&& $request['sender_id'] === 'FrontA';
|
||||
});
|
||||
|
||||
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/debit'));
|
||||
}
|
||||
}
|
||||
@@ -6,14 +6,14 @@ use App\Http\Middleware\EnsurePlatformSession;
|
||||
use App\Models\Branch;
|
||||
use App\Models\Host;
|
||||
use App\Models\Member;
|
||||
use App\Models\MessagingCredential;
|
||||
use App\Models\NotificationUsage;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use App\Models\Visit;
|
||||
use App\Notifications\FrontdeskAlertNotification;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -25,6 +25,8 @@ class FrontdeskNotificationBillingTest extends TestCase
|
||||
|
||||
protected Organization $organization;
|
||||
|
||||
protected string $birdKey;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
@@ -63,14 +65,26 @@ class FrontdeskNotificationBillingTest extends TestCase
|
||||
'name' => 'HQ',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->birdKey = 'lsk_live_'.str_repeat('a', 32);
|
||||
MessagingCredential::create([
|
||||
'owner_ref' => $this->owner->public_id,
|
||||
'organization_id' => $this->organization->id,
|
||||
'bird_api_key_encrypted' => Crypt::encryptString($this->birdKey),
|
||||
'bird_api_key_prefix' => substr($this->birdKey, 0, 16),
|
||||
'bird_from_email' => 'notify@example.test',
|
||||
'bird_from_name' => 'Notify Org',
|
||||
'bird_status' => MessagingCredential::STATUS_VALID,
|
||||
'bird_validated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_host_email_notification_without_ladill_user_link(): void
|
||||
{
|
||||
Mail::fake();
|
||||
Http::fake([
|
||||
'billing.test/can-afford*' => Http::response(['affordable' => true]),
|
||||
'billing.test/debit' => Http::response(['ok' => true]),
|
||||
'*/api/smtp/send' => Http::response(['success' => true]),
|
||||
]);
|
||||
|
||||
$host = Host::create([
|
||||
@@ -90,7 +104,8 @@ class FrontdeskNotificationBillingTest extends TestCase
|
||||
])
|
||||
->assertRedirect();
|
||||
|
||||
Mail::assertSent(\App\Mail\ContactMessageMail::class);
|
||||
Http::assertSent(fn ($request) => str_contains($request->url(), '/api/smtp/send'));
|
||||
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/debit'));
|
||||
|
||||
$usage = NotificationUsage::where('organization_id', $this->organization->id)->first();
|
||||
$this->assertNotNull($usage);
|
||||
@@ -98,12 +113,12 @@ class FrontdeskNotificationBillingTest extends TestCase
|
||||
$this->assertSame(0, $usage->email_charged_minor);
|
||||
}
|
||||
|
||||
public function test_paid_email_charges_wallet_after_free_allowance(): void
|
||||
public function test_customer_email_skips_wallet_debit_after_free_allowance(): void
|
||||
{
|
||||
Mail::fake();
|
||||
Http::fake([
|
||||
'billing.test/can-afford*' => Http::response(['affordable' => true]),
|
||||
'billing.test/debit' => Http::response(['ok' => true]),
|
||||
'*/api/smtp/send' => Http::response(['success' => true]),
|
||||
]);
|
||||
|
||||
NotificationUsage::create([
|
||||
@@ -132,16 +147,17 @@ class FrontdeskNotificationBillingTest extends TestCase
|
||||
])
|
||||
->assertRedirect();
|
||||
|
||||
Http::assertSent(fn ($request) => str_contains($request->url(), '/debit'));
|
||||
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/debit'));
|
||||
|
||||
$usage = NotificationUsage::where('organization_id', $this->organization->id)->first();
|
||||
$this->assertSame(101, $usage->email_count);
|
||||
$this->assertGreaterThan(0, $usage->email_charged_minor);
|
||||
$this->assertSame(0, $usage->email_charged_minor);
|
||||
}
|
||||
|
||||
public function test_insufficient_balance_skips_host_email(): void
|
||||
public function test_missing_bird_credentials_skips_host_email(): void
|
||||
{
|
||||
Mail::fake();
|
||||
MessagingCredential::query()->where('organization_id', $this->organization->id)->delete();
|
||||
|
||||
Http::fake([
|
||||
'billing.test/can-afford*' => Http::response(['affordable' => false]),
|
||||
]);
|
||||
@@ -169,15 +185,15 @@ class FrontdeskNotificationBillingTest extends TestCase
|
||||
])
|
||||
->assertRedirect();
|
||||
|
||||
Mail::assertNothingSent();
|
||||
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/api/smtp/send'));
|
||||
}
|
||||
|
||||
public function test_kiosk_returns_host_notified_flag(): void
|
||||
{
|
||||
Mail::fake();
|
||||
Http::fake([
|
||||
'billing.test/can-afford*' => Http::response(['affordable' => true]),
|
||||
'billing.test/debit' => Http::response(['ok' => true]),
|
||||
'*/api/smtp/send' => Http::response(['success' => true]),
|
||||
]);
|
||||
|
||||
$host = Host::create([
|
||||
@@ -226,12 +242,12 @@ class FrontdeskNotificationBillingTest extends TestCase
|
||||
->assertSee('Upgrade to Pro');
|
||||
}
|
||||
|
||||
public function test_pro_plan_emails_are_unlimited(): void
|
||||
public function test_pro_plan_emails_record_usage_without_wallet_debit(): void
|
||||
{
|
||||
Mail::fake();
|
||||
Http::fake([
|
||||
'billing.test/can-afford*' => Http::response(['affordable' => true]),
|
||||
'billing.test/debit' => Http::response(['ok' => true]),
|
||||
'*/api/smtp/send' => Http::response(['success' => true]),
|
||||
]);
|
||||
|
||||
$this->organization->update([
|
||||
@@ -264,7 +280,7 @@ class FrontdeskNotificationBillingTest extends TestCase
|
||||
])
|
||||
->assertRedirect();
|
||||
|
||||
Mail::assertSent(\App\Mail\ContactMessageMail::class);
|
||||
Http::assertSent(fn ($request) => str_contains($request->url(), '/api/smtp/send'));
|
||||
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/debit'));
|
||||
|
||||
$usage = NotificationUsage::where('organization_id', $this->organization->id)->first();
|
||||
@@ -275,10 +291,10 @@ class FrontdeskNotificationBillingTest extends TestCase
|
||||
public function test_linked_user_still_gets_in_app_notification(): void
|
||||
{
|
||||
Notification::fake();
|
||||
Mail::fake();
|
||||
Http::fake([
|
||||
'billing.test/can-afford*' => Http::response(['affordable' => true]),
|
||||
'billing.test/debit' => Http::response(['ok' => true]),
|
||||
'*/api/smtp/send' => Http::response(['success' => true]),
|
||||
]);
|
||||
|
||||
$linkedUser = User::create([
|
||||
|
||||
Reference in New Issue
Block a user