Add notification action foundation
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Models;
|
||||
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationDeliveryStatus;
|
||||
use App\Models\NotificationDeliveryLog;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class NotificationDeliveryLogTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure delivery log factories create related notification actions with consistent casts.
|
||||
*/
|
||||
public function test_delivery_log_factory_creates_a_related_notification_action(): void
|
||||
{
|
||||
$deliveryLog = NotificationDeliveryLog::factory()->create();
|
||||
|
||||
$this->assertTrue($deliveryLog->notificationAction()->exists());
|
||||
$this->assertTrue($deliveryLog->notificationAction->is($deliveryLog->fresh()->notificationAction));
|
||||
$this->assertSame(NotificationChannel::Webhook, $deliveryLog->fresh()->channel);
|
||||
$this->assertSame(NotificationDeliveryStatus::Pending, $deliveryLog->fresh()->delivery_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure delivery logs can represent every supported delivery state.
|
||||
*/
|
||||
public function test_delivery_logs_support_pending_succeeded_failed_and_retryable_statuses(): void
|
||||
{
|
||||
NotificationDeliveryLog::factory()->pending()->create();
|
||||
NotificationDeliveryLog::factory()->succeeded()->create();
|
||||
NotificationDeliveryLog::factory()->failed()->create();
|
||||
NotificationDeliveryLog::factory()->retryable()->create();
|
||||
|
||||
$this->assertSame(
|
||||
[
|
||||
NotificationDeliveryStatus::Pending,
|
||||
NotificationDeliveryStatus::Succeeded,
|
||||
NotificationDeliveryStatus::Failed,
|
||||
NotificationDeliveryStatus::Retryable,
|
||||
],
|
||||
NotificationDeliveryLog::query()->orderBy('id')->get()->pluck('delivery_status')->all(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure delivery logs cast metadata and attempt information for later delivery tracking.
|
||||
*/
|
||||
public function test_delivery_logs_cast_metadata_and_attempt_information(): void
|
||||
{
|
||||
$deliveryLog = NotificationDeliveryLog::factory()->succeeded()->create([
|
||||
'payload_metadata' => [
|
||||
'project_key' => 'CORE',
|
||||
'ticket_key' => 'CORE-27',
|
||||
],
|
||||
])->fresh();
|
||||
|
||||
$this->assertSame('CORE-27', $deliveryLog->payload_metadata['ticket_key']);
|
||||
$this->assertSame(204, $deliveryLog->http_status);
|
||||
$this->assertNotNull($deliveryLog->last_attempted_at);
|
||||
$this->assertNotNull($deliveryLog->delivered_at);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Notifications;
|
||||
|
||||
use App\Actions\Notifications\DeleteProjectNotificationAction;
|
||||
use App\Actions\Notifications\ListProjectNotificationActions;
|
||||
use App\Actions\Notifications\StoreProjectNotificationAction;
|
||||
use App\Actions\Notifications\UpdateProjectNotificationAction;
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationEventType;
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProjectNotificationActionManagementTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure notification actions can be created and listed for a single project.
|
||||
*/
|
||||
public function test_notification_actions_can_be_created_and_listed_for_a_project(): void
|
||||
{
|
||||
$storeAction = app(StoreProjectNotificationAction::class);
|
||||
$listAction = app(ListProjectNotificationActions::class);
|
||||
$project = Project::factory()->create();
|
||||
$otherProject = Project::factory()->create();
|
||||
|
||||
$storeAction->handle($project, [
|
||||
'name' => 'Discord alerts',
|
||||
'enabled' => false,
|
||||
'event_type' => NotificationEventType::TicketStatusChanged,
|
||||
'channel' => NotificationChannel::Discord,
|
||||
'channel_configuration' => ['mentions' => ['ops']],
|
||||
'message_template' => '$ticket_key changed to $status_to',
|
||||
'delivery_url' => 'https://discord.com/api/webhooks/123456/discord-alerts',
|
||||
]);
|
||||
|
||||
$webhookAction = $storeAction->handle($project, [
|
||||
'name' => 'Webhook alerts',
|
||||
'enabled' => true,
|
||||
'event_type' => NotificationEventType::TicketStatusChanged,
|
||||
'channel' => NotificationChannel::Webhook,
|
||||
'channel_configuration' => ['source' => 'n8n'],
|
||||
'message_template' => null,
|
||||
'delivery_url' => 'https://hooks.example.test/kanbangit/webhook-alerts',
|
||||
]);
|
||||
|
||||
NotificationAction::factory()->for($otherProject)->create([
|
||||
'name' => 'Foreign project webhook',
|
||||
]);
|
||||
|
||||
$listedActions = $listAction->handle($project);
|
||||
|
||||
$this->assertModelExists($webhookAction);
|
||||
$this->assertCount(2, $listedActions);
|
||||
$this->assertSame(['Discord alerts', 'Webhook alerts'], $listedActions->pluck('name')->all());
|
||||
$this->assertSame(['mentions' => ['ops']], $listedActions->first()->channel_configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure notification actions can be updated without overwriting their saved secret URL.
|
||||
*/
|
||||
public function test_notification_actions_can_be_updated_without_replacing_the_existing_delivery_url(): void
|
||||
{
|
||||
$updateAction = app(UpdateProjectNotificationAction::class);
|
||||
$project = Project::factory()->create();
|
||||
$notificationAction = NotificationAction::factory()->discord()->for($project)->create([
|
||||
'delivery_url' => 'https://discord.com/api/webhooks/123456/original-secret',
|
||||
'message_template' => '$ticket_key moved to $status_to',
|
||||
]);
|
||||
|
||||
$updatedAction = $updateAction->handle($project, $notificationAction, [
|
||||
'enabled' => false,
|
||||
'message_template' => '$ticket_key moved from $status_from to $status_to',
|
||||
'channel_configuration' => ['mentions' => ['support']],
|
||||
'delivery_url' => '',
|
||||
]);
|
||||
|
||||
$this->assertFalse($updatedAction->enabled);
|
||||
$this->assertSame(
|
||||
'$ticket_key moved from $status_from to $status_to',
|
||||
$updatedAction->message_template,
|
||||
);
|
||||
$this->assertSame(['mentions' => ['support']], $updatedAction->channel_configuration);
|
||||
$this->assertSame(
|
||||
'https://discord.com/api/webhooks/123456/original-secret',
|
||||
$updatedAction->delivery_url,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure notification actions can replace their delivery URL when a new secret is submitted.
|
||||
*/
|
||||
public function test_notification_actions_can_replace_delivery_urls(): void
|
||||
{
|
||||
$updateAction = app(UpdateProjectNotificationAction::class);
|
||||
$project = Project::factory()->create();
|
||||
$notificationAction = NotificationAction::factory()->for($project)->create([
|
||||
'delivery_url' => 'https://hooks.example.test/kanbangit/original-secret',
|
||||
]);
|
||||
|
||||
$updatedAction = $updateAction->handle($project, $notificationAction, [
|
||||
'delivery_url' => 'https://hooks.example.test/kanbangit/replaced-secret',
|
||||
'name' => 'Primary webhook',
|
||||
]);
|
||||
|
||||
$this->assertSame('Primary webhook', $updatedAction->name);
|
||||
$this->assertSame('https://hooks.example.test/kanbangit/replaced-secret', $updatedAction->delivery_url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure notification actions can be deleted from their owning project.
|
||||
*/
|
||||
public function test_notification_actions_can_be_deleted_from_their_project(): void
|
||||
{
|
||||
$deleteAction = app(DeleteProjectNotificationAction::class);
|
||||
$project = Project::factory()->create();
|
||||
$notificationAction = NotificationAction::factory()->for($project)->create();
|
||||
|
||||
$deleteAction->handle($project, $notificationAction);
|
||||
|
||||
$this->assertModelMissing($notificationAction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure project-scoped actions reject notification actions from other projects.
|
||||
*/
|
||||
public function test_project_scoped_management_rejects_foreign_notification_actions(): void
|
||||
{
|
||||
$updateAction = app(UpdateProjectNotificationAction::class);
|
||||
$project = Project::factory()->create();
|
||||
$otherProject = Project::factory()->create();
|
||||
$notificationAction = NotificationAction::factory()->for($otherProject)->create();
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
|
||||
$updateAction->handle($project, $notificationAction, [
|
||||
'name' => 'Should not update',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user