68 lines
2.9 KiB
PHP
68 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Models;
|
|
|
|
use App\Enums\NotificationChannel;
|
|
use App\Enums\NotificationEventType;
|
|
use App\Http\Resources\NotificationActionResource;
|
|
use App\Models\NotificationAction;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tests\TestCase;
|
|
|
|
class NotificationActionTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/**
|
|
* Ensure webhook notification actions encrypt their delivery URL and cast enum fields.
|
|
*/
|
|
public function test_factory_creates_an_encrypted_webhook_notification_action(): void
|
|
{
|
|
$notificationAction = NotificationAction::factory()->webhook()->create([
|
|
'delivery_url' => 'https://hooks.example.test/kanbangit/core-status',
|
|
]);
|
|
|
|
$storedDeliveryUrl = DB::table('notification_actions')
|
|
->where('id', $notificationAction->id)
|
|
->value('delivery_url');
|
|
|
|
$this->assertNotSame('https://hooks.example.test/kanbangit/core-status', $storedDeliveryUrl);
|
|
$this->assertSame('https://hooks.example.test/kanbangit/core-status', $notificationAction->fresh()->delivery_url);
|
|
$this->assertSame(NotificationEventType::TicketStatusChanged, $notificationAction->fresh()->event_type);
|
|
$this->assertSame(NotificationChannel::Webhook, $notificationAction->fresh()->channel);
|
|
$this->assertSame([], $notificationAction->fresh()->channel_configuration);
|
|
$this->assertSame([], $notificationAction->fresh()->filters ?? []);
|
|
}
|
|
|
|
/**
|
|
* Ensure the Discord factory state creates message-driven Discord actions.
|
|
*/
|
|
public function test_factory_can_create_discord_notification_actions(): void
|
|
{
|
|
$notificationAction = NotificationAction::factory()->discord()->create();
|
|
|
|
$this->assertSame(NotificationChannel::Discord, $notificationAction->fresh()->channel);
|
|
$this->assertNotNull($notificationAction->fresh()->message_template);
|
|
$this->assertStringStartsWith('https://discord.com/api/webhooks/', $notificationAction->fresh()->delivery_url);
|
|
}
|
|
|
|
/**
|
|
* Ensure notification actions belong to a project and keep secret URLs out of serialized output.
|
|
*/
|
|
public function test_notification_actions_belong_to_projects_and_hide_delivery_urls(): void
|
|
{
|
|
$notificationAction = NotificationAction::factory()->create();
|
|
$resource = (new NotificationActionResource($notificationAction->fresh()))
|
|
->toArray(Request::create('/'));
|
|
|
|
$this->assertTrue($notificationAction->project()->exists());
|
|
$this->assertTrue($notificationAction->project->is($notificationAction->fresh()->project));
|
|
$this->assertArrayNotHasKey('delivery_url', $notificationAction->fresh()->toArray());
|
|
$this->assertArrayNotHasKey('delivery_url', $resource);
|
|
$this->assertTrue($resource['has_delivery_url']);
|
|
$this->assertSame([], $resource['filters']);
|
|
}
|
|
}
|