trypost/tests/Feature/App/WebhookTest.php

717 lines
23 KiB
PHP
Raw Permalink Normal View History

Add workspace webhooks and drop the unused automation webhook node (#326) * Add workspace webhooks and drop the unused automation webhook node. Give workspaces HMAC-signed outgoing webhooks for the post lifecycle, with retry, auto-pause, replay, and live logs, and keep HTTP Request as the only outbound automation node. * Tighten webhook controller and validation after review. Drop the redundant workspace redirects, prune logs without counting, and validate events/status with Rule::enum. * Move leftover webhook UI copy behind i18n. HTTP status phrases, delete-cancel, and validation attribute names were still English literals. * Build the webhook-paused email through Maizzle. The hand-written Blade skipped the shared layout, header, and footer used by the other mail templates. * Cover real webhook dispatch paths and restyle the webhook pages. * Ask for the shared delete keyword when confirming a webhook delete. The endpoint URL is a poor confirm string; posts and assets already use the common "delete" keyword. * Fix webhook review blockers so CI can go green. Drop leftover French automation keys, stop mutating Inertia log props, and show delivered_at instead of created_at. * Close the remaining webhook review gaps. Keep Echo log updates across infinite scroll, align the channel with the policy, persist log ids across retries, and fail unknown automation nodes without throwing. * Stop webhook delivery after disable and record last sent only on success. Queued jobs now skip paused or disabled endpoints unless the user replays, and changing the URL re-pings it first. * Limit webhooks to owners and admins, and encrypt signing secrets. Members can no longer create or inspect outgoing integrations, and secrets stay encrypted at rest. * Cover webhook secret hiding, skip-ping, and failed-delivery edges. * Send the full post on webhooks after labels and platforms are saved. * Fix webhook payloads for integer media ids and type webhook status. * Split the webhook show page into focused components. * Reset live webhook logs when switching endpoints. * Keep the newest webhook logs at the top after live merges. * Cast media item ids to string without the extra scalar check. * Add post.unscheduled webhooks and put the log id on the envelope. Unscheduling is now a first-class event, and receivers can send the delivery id back so we can find the matching log. * Translate webhook event names in the UI. * Make the webhook show page full-width and stop stacking flash toasts. * Translate remaining webhook UI copy in every locale. * Sign webhook pings and drop author email from the payload. * Send signed webhook tests after create instead of pinging on save. Create and update only block private URLs so the receiver can copy the secret first. The show page then sends a signed webhook.test with an object data envelope. * Polish webhook test UX and always mint the dispatch log id in the job. Keep send-test in the actions menu (its own group) and drop the leftover constructor param so retries reuse the serialized id instead of a caller-supplied one.
2026-09-04 12:43:29 +00:00
<?php
declare(strict_types=1);
use App\Enums\UserWorkspace\Role;
use App\Enums\Webhook\EventType;
use App\Models\User;
use App\Models\Webhook;
use App\Models\WebhookLog;
use App\Models\Workspace;
use App\Services\WebhookService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
beforeEach(function () {
$this->user = User::factory()->create();
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
$mock = Mockery::mock(WebhookService::class);
$mock->shouldReceive('assertEndpointAllowed')->andReturnNull();
$mock->shouldReceive('ping')->andReturnNull();
$this->app->instance(WebhookService::class, $mock);
});
test('guests are redirected to the login page', function () {
$this->get(route('app.webhooks.index'))
->assertRedirect(route('login'));
});
test('authenticated users can view webhooks', function () {
$this->actingAs($this->user)
->get(route('app.webhooks.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('webhooks/Index')
->has('webhooks')
);
});
test('webhook index hides the signing secret', function () {
Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
'signing_secret' => 'whsec_hidden',
]);
$this->actingAs($this->user)
->get(route('app.webhooks.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->has('webhooks', 1, fn ($webhook) => $webhook
->missing('signing_secret')
->etc()
)
);
});
test('authenticated users can create a webhook', function () {
$this->actingAs($this->user)
->post(route('app.webhooks.store'), [
'endpoint' => 'https://example.com/webhooks',
'events' => [EventType::PostPublished->value, EventType::PostFailed->value],
])
->assertRedirect();
$this->assertDatabaseHas('webhooks', [
'workspace_id' => $this->workspace->id,
'endpoint' => 'https://example.com/webhooks',
]);
});
test('generateSigningSecret prefixes a 32 character random string', function () {
$secret = Webhook::generateSigningSecret();
expect($secret)
->toStartWith('whsec_')
->and(strlen($secret))->toBe(38)
->and(Webhook::generateSigningSecret())->not->toBe($secret);
});
test('webhook signing_secret is generated with whsec_ prefix', function () {
$this->actingAs($this->user)
->post(route('app.webhooks.store'), [
'endpoint' => 'https://example.com/webhooks',
'events' => [EventType::PostPublished->value],
]);
$webhook = Webhook::query()->where('workspace_id', $this->workspace->id)->first();
expect($webhook->signing_secret)->toStartWith('whsec_');
});
test('webhook signing secret is encrypted at rest', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
'signing_secret' => 'whsec_test123',
]);
$raw = DB::table('webhooks')->where('id', $webhook->id)->value('signing_secret');
expect($raw)->not->toStartWith('whsec_');
expect($webhook->signing_secret)->toBe('whsec_test123');
});
test('webhook creation does not ping the endpoint', function () {
$mock = Mockery::mock(WebhookService::class);
$mock->shouldReceive('assertEndpointAllowed')->once();
$mock->shouldNotReceive('ping');
$this->app->instance(WebhookService::class, $mock);
$this->actingAs($this->user)
->post(route('app.webhooks.store'), [
'endpoint' => 'https://example.com/webhooks',
'events' => [EventType::PostPublished->value],
])
->assertRedirect();
$this->assertDatabaseHas('webhooks', [
'endpoint' => 'https://example.com/webhooks',
]);
});
test('webhook creation fails when the endpoint is not allowed', function () {
$failingMock = Mockery::mock(WebhookService::class);
$failingMock->shouldReceive('assertEndpointAllowed')
->andThrow(new RuntimeException(__('webhooks.errors.endpoint_not_allowed')));
$this->app->instance(WebhookService::class, $failingMock);
$this->actingAs($this->user)
->post(route('app.webhooks.store'), [
'endpoint' => 'http://127.0.0.1/webhooks',
'events' => [EventType::PostPublished->value],
])
->assertRedirect()
->assertSessionHasErrors('endpoint');
$this->assertDatabaseMissing('webhooks', [
'endpoint' => 'http://127.0.0.1/webhooks',
]);
});
test('re-enabling a webhook resets consecutive failures', function (string $from) {
$webhook = $from === 'paused'
? Webhook::factory()->paused()->create([
'workspace_id' => $this->workspace->id,
])
: Webhook::factory()->disabled()->create([
'workspace_id' => $this->workspace->id,
'consecutive_failures' => 4,
]);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'status' => 'enabled',
])
->assertRedirect();
$webhook->refresh();
expect($webhook->status->value)->toBe('enabled');
expect($webhook->consecutive_failures)->toBe(0);
expect($webhook->paused_at)->toBeNull();
})->with(['paused', 'disabled']);
test('updating an already enabled webhook does not reset consecutive failures', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
'consecutive_failures' => 3,
]);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'status' => 'enabled',
])
->assertRedirect();
expect($webhook->fresh()->consecutive_failures)->toBe(3);
});
Add workspace webhooks and drop the unused automation webhook node (#326) * Add workspace webhooks and drop the unused automation webhook node. Give workspaces HMAC-signed outgoing webhooks for the post lifecycle, with retry, auto-pause, replay, and live logs, and keep HTTP Request as the only outbound automation node. * Tighten webhook controller and validation after review. Drop the redundant workspace redirects, prune logs without counting, and validate events/status with Rule::enum. * Move leftover webhook UI copy behind i18n. HTTP status phrases, delete-cancel, and validation attribute names were still English literals. * Build the webhook-paused email through Maizzle. The hand-written Blade skipped the shared layout, header, and footer used by the other mail templates. * Cover real webhook dispatch paths and restyle the webhook pages. * Ask for the shared delete keyword when confirming a webhook delete. The endpoint URL is a poor confirm string; posts and assets already use the common "delete" keyword. * Fix webhook review blockers so CI can go green. Drop leftover French automation keys, stop mutating Inertia log props, and show delivered_at instead of created_at. * Close the remaining webhook review gaps. Keep Echo log updates across infinite scroll, align the channel with the policy, persist log ids across retries, and fail unknown automation nodes without throwing. * Stop webhook delivery after disable and record last sent only on success. Queued jobs now skip paused or disabled endpoints unless the user replays, and changing the URL re-pings it first. * Limit webhooks to owners and admins, and encrypt signing secrets. Members can no longer create or inspect outgoing integrations, and secrets stay encrypted at rest. * Cover webhook secret hiding, skip-ping, and failed-delivery edges. * Send the full post on webhooks after labels and platforms are saved. * Fix webhook payloads for integer media ids and type webhook status. * Split the webhook show page into focused components. * Reset live webhook logs when switching endpoints. * Keep the newest webhook logs at the top after live merges. * Cast media item ids to string without the extra scalar check. * Add post.unscheduled webhooks and put the log id on the envelope. Unscheduling is now a first-class event, and receivers can send the delivery id back so we can find the matching log. * Translate webhook event names in the UI. * Make the webhook show page full-width and stop stacking flash toasts. * Translate remaining webhook UI copy in every locale. * Sign webhook pings and drop author email from the payload. * Send signed webhook tests after create instead of pinging on save. Create and update only block private URLs so the receiver can copy the secret first. The show page then sends a signed webhook.test with an object data envelope. * Polish webhook test UX and always mint the dispatch log id in the job. Keep send-test in the actions menu (its own group) and drop the leftover constructor param so retries reuse the serialized id instead of a caller-supplied one.
2026-09-04 12:43:29 +00:00
test('webhook endpoint is required', function () {
$this->actingAs($this->user)
->post(route('app.webhooks.store'), [
'events' => [EventType::PostPublished->value],
])
->assertSessionHasErrors('endpoint');
expect(session('errors')->first('endpoint'))
->toContain(__('webhooks.create.endpoint'));
});
test('webhook endpoint must be a valid url', function () {
$this->actingAs($this->user)
->post(route('app.webhooks.store'), [
'endpoint' => 'not-a-url',
'events' => [EventType::PostPublished->value],
])
->assertSessionHasErrors('endpoint');
});
test('webhook events are required', function () {
$this->actingAs($this->user)
->post(route('app.webhooks.store'), [
'endpoint' => 'https://example.com/webhooks',
'events' => [],
])
->assertSessionHasErrors('events');
});
test('webhook rejects wildcard events', function () {
$this->actingAs($this->user)
->post(route('app.webhooks.store'), [
'endpoint' => 'https://example.com/webhooks',
'events' => ['*'],
])
->assertSessionHasErrors('events.0');
});
test('webhook accepts the post.unscheduled event', function () {
$this->actingAs($this->user)
->post(route('app.webhooks.store'), [
'endpoint' => 'https://example.com/webhooks',
'events' => [EventType::PostUnscheduled->value],
])
->assertRedirect();
$webhook = Webhook::query()->where('workspace_id', $this->workspace->id)->first();
expect($webhook->events)->toEqual([EventType::PostUnscheduled->value]);
});
test('webhook rejects invalid event names', function () {
$this->actingAs($this->user)
->post(route('app.webhooks.store'), [
'endpoint' => 'https://example.com/webhooks',
'events' => ['foo.bar'],
])
->assertSessionHasErrors('events.0');
});
test('webhook show includes full log payload and response body', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$log = WebhookLog::factory()->create([
'webhook_id' => $webhook->id,
'payload' => [
'id' => 'post-1',
'type' => EventType::PostPublished->value,
'data' => ['content' => 'Hello'],
],
'response_body' => 'OK',
]);
$this->actingAs($this->user)
->get(route('app.webhooks.show', $webhook))
->assertOk()
->assertInertia(fn ($page) => $page
->component('webhooks/Show')
->has('logs.data', 1)
->where('logs.data.0.id', $log->id)
->where('logs.data.0.payload.id', 'post-1')
->where('logs.data.0.payload.data.content', 'Hello')
->where('logs.data.0.response_body', 'OK')
);
});
Add workspace webhooks and drop the unused automation webhook node (#326) * Add workspace webhooks and drop the unused automation webhook node. Give workspaces HMAC-signed outgoing webhooks for the post lifecycle, with retry, auto-pause, replay, and live logs, and keep HTTP Request as the only outbound automation node. * Tighten webhook controller and validation after review. Drop the redundant workspace redirects, prune logs without counting, and validate events/status with Rule::enum. * Move leftover webhook UI copy behind i18n. HTTP status phrases, delete-cancel, and validation attribute names were still English literals. * Build the webhook-paused email through Maizzle. The hand-written Blade skipped the shared layout, header, and footer used by the other mail templates. * Cover real webhook dispatch paths and restyle the webhook pages. * Ask for the shared delete keyword when confirming a webhook delete. The endpoint URL is a poor confirm string; posts and assets already use the common "delete" keyword. * Fix webhook review blockers so CI can go green. Drop leftover French automation keys, stop mutating Inertia log props, and show delivered_at instead of created_at. * Close the remaining webhook review gaps. Keep Echo log updates across infinite scroll, align the channel with the policy, persist log ids across retries, and fail unknown automation nodes without throwing. * Stop webhook delivery after disable and record last sent only on success. Queued jobs now skip paused or disabled endpoints unless the user replays, and changing the URL re-pings it first. * Limit webhooks to owners and admins, and encrypt signing secrets. Members can no longer create or inspect outgoing integrations, and secrets stay encrypted at rest. * Cover webhook secret hiding, skip-ping, and failed-delivery edges. * Send the full post on webhooks after labels and platforms are saved. * Fix webhook payloads for integer media ids and type webhook status. * Split the webhook show page into focused components. * Reset live webhook logs when switching endpoints. * Keep the newest webhook logs at the top after live merges. * Cast media item ids to string without the extra scalar check. * Add post.unscheduled webhooks and put the log id on the envelope. Unscheduling is now a first-class event, and receivers can send the delivery id back so we can find the matching log. * Translate webhook event names in the UI. * Make the webhook show page full-width and stop stacking flash toasts. * Translate remaining webhook UI copy in every locale. * Sign webhook pings and drop author email from the payload. * Send signed webhook tests after create instead of pinging on save. Create and update only block private URLs so the receiver can copy the secret first. The show page then sends a signed webhook.test with an object data envelope. * Polish webhook test UX and always mint the dispatch log id in the job. Keep send-test in the actions menu (its own group) and drop the leftover constructor param so retries reuse the serialized id instead of a caller-supplied one.
2026-09-04 12:43:29 +00:00
test('authenticated users can view a webhook with signing_secret exposed', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
'signing_secret' => 'whsec_test123',
]);
$this->actingAs($this->user)
->get(route('app.webhooks.show', $webhook))
->assertOk()
->assertInertia(fn ($page) => $page
->component('webhooks/Show')
->has('webhook')
->has('logs')
->where('webhook.signing_secret', 'whsec_test123')
);
});
test('authenticated users can update a webhook endpoint', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'endpoint' => 'https://updated.com/hook',
])
->assertRedirect()
->assertSessionHas('flash.banner', __('webhooks.flash.updated'));
$this->assertDatabaseHas('webhooks', [
'id' => $webhook->id,
'endpoint' => 'https://updated.com/hook',
]);
});
test('updating a webhook endpoint does not ping the new url', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
'endpoint' => 'https://old.example.com/hook',
]);
$mock = Mockery::mock(WebhookService::class);
$mock->shouldReceive('assertEndpointAllowed')->once()->with('https://updated.com/hook');
$mock->shouldNotReceive('ping');
$this->app->instance(WebhookService::class, $mock);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'endpoint' => 'https://updated.com/hook',
])
->assertRedirect();
});
test('updating a webhook endpoint fails when the endpoint is not allowed', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
'endpoint' => 'https://old.example.com/hook',
]);
$failingMock = Mockery::mock(WebhookService::class);
$failingMock->shouldReceive('assertEndpointAllowed')
->andThrow(new RuntimeException(__('webhooks.errors.endpoint_not_allowed')));
$this->app->instance(WebhookService::class, $failingMock);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'endpoint' => 'http://127.0.0.1/webhooks',
])
->assertRedirect()
->assertSessionHasErrors('endpoint');
$webhook->refresh();
expect($webhook->endpoint)->toBe('https://old.example.com/hook');
});
test('updating a webhook without changing the endpoint does not ping', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
'endpoint' => 'https://same.example.com/hook',
]);
$mock = Mockery::mock(WebhookService::class);
$mock->shouldNotReceive('assertEndpointAllowed');
$mock->shouldNotReceive('ping');
$this->app->instance(WebhookService::class, $mock);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'endpoint' => 'https://same.example.com/hook',
'events' => [EventType::PostPublished->value],
])
->assertRedirect();
});
test('updating webhook status does not ping the endpoint', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$mock = Mockery::mock(WebhookService::class);
$mock->shouldNotReceive('assertEndpointAllowed');
$mock->shouldNotReceive('ping');
$this->app->instance(WebhookService::class, $mock);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'status' => 'disabled',
])
->assertRedirect();
$webhook->refresh();
expect($webhook->status->value)->toBe('disabled');
});
test('authenticated users can update webhook events', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
'events' => [EventType::PostCreated->value],
]);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'events' => [EventType::PostCreated->value, EventType::PostFailed->value],
])
->assertRedirect();
$webhook->refresh();
expect($webhook->events)->toEqual([
EventType::PostCreated->value,
EventType::PostFailed->value,
]);
});
test('authenticated users can update webhook status', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'status' => 'disabled',
])
->assertRedirect();
$webhook->refresh();
expect($webhook->status->value)->toBe('disabled');
});
test('update webhook rejects invalid status', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'status' => 'invalid',
])
->assertSessionHasErrors('status');
});
test('update webhook rejects paused status from the user', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'status' => 'paused',
])
->assertSessionHasErrors('status');
});
test('update webhook rejects wildcard events', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'events' => ['*'],
])
->assertSessionHasErrors('events.0');
});
test('authenticated users can delete a webhook', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$this->actingAs($this->user)
->delete(route('app.webhooks.destroy', $webhook))
->assertRedirect(route('app.webhooks.index'));
$this->assertDatabaseMissing('webhooks', [
'id' => $webhook->id,
]);
});
test('deleting a webhook also deletes its logs', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$log = WebhookLog::factory()->create([
'webhook_id' => $webhook->id,
]);
$this->actingAs($this->user)
->delete(route('app.webhooks.destroy', $webhook))
->assertRedirect();
$this->assertDatabaseMissing('webhook_logs', [
'id' => $log->id,
]);
});
test('users cannot view webhooks from other workspaces', function () {
$otherUser = User::factory()->create();
$otherWorkspace = Workspace::factory()->create(['user_id' => $otherUser->id]);
$webhook = Webhook::factory()->create([
'workspace_id' => $otherWorkspace->id,
]);
$this->actingAs($this->user)
->get(route('app.webhooks.show', $webhook))
->assertForbidden();
});
test('users cannot update webhooks from other workspaces', function () {
$otherUser = User::factory()->create();
$otherWorkspace = Workspace::factory()->create(['user_id' => $otherUser->id]);
$webhook = Webhook::factory()->create([
'workspace_id' => $otherWorkspace->id,
]);
$this->actingAs($this->user)
->put(route('app.webhooks.update', $webhook), [
'endpoint' => 'https://hacker.com/steal',
])
->assertForbidden();
});
test('authenticated users can send a signed test event', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
'endpoint' => 'https://example.com/hook',
]);
$mock = Mockery::mock(WebhookService::class);
$mock->shouldReceive('ping')->once()->with($webhook->endpoint, $webhook->signing_secret);
$this->app->instance(WebhookService::class, $mock);
$this->actingAs($this->user)
->post(route('app.webhooks.send-test', $webhook))
->assertRedirect()
->assertSessionHas('flash.banner', __('webhooks.flash.tested'));
});
test('sending a test event flashes the error when ping fails', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$failingMock = Mockery::mock(WebhookService::class);
$failingMock->shouldReceive('ping')->andThrow(new RuntimeException('Connection refused'));
$this->app->instance(WebhookService::class, $failingMock);
$this->actingAs($this->user)
->post(route('app.webhooks.send-test', $webhook))
->assertRedirect()
->assertSessionHas('flash.banner', 'Connection refused')
->assertSessionHas('flash.bannerStyle', 'danger');
});
test('users cannot send a test event for other workspaces webhooks', function () {
$otherUser = User::factory()->create();
$otherWorkspace = Workspace::factory()->create(['user_id' => $otherUser->id]);
$webhook = Webhook::factory()->create([
'workspace_id' => $otherWorkspace->id,
]);
$this->actingAs($this->user)
->post(route('app.webhooks.send-test', $webhook))
->assertForbidden();
});
test('authenticated users can rotate a webhook signing secret', function () {
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$originalSecret = $webhook->signing_secret;
$this->actingAs($this->user)
->post(route('app.webhooks.rotate-secret', $webhook))
->assertRedirect()
->assertSessionHas('flash.banner', __('webhooks.flash.secret_rotated'));
$webhook->refresh();
expect($webhook->signing_secret)
->not->toBe($originalSecret)
->toStartWith('whsec_');
$raw = DB::table('webhooks')->where('id', $webhook->id)->value('signing_secret');
expect($raw)->not->toStartWith('whsec_');
});
test('users cannot rotate signing secret for other workspaces webhooks', function () {
$otherUser = User::factory()->create();
$otherWorkspace = Workspace::factory()->create(['user_id' => $otherUser->id]);
$webhook = Webhook::factory()->create([
'workspace_id' => $otherWorkspace->id,
]);
$this->actingAs($this->user)
->post(route('app.webhooks.rotate-secret', $webhook))
->assertForbidden();
});
test('users cannot delete webhooks from other workspaces', function () {
$otherUser = User::factory()->create();
$otherWorkspace = Workspace::factory()->create(['user_id' => $otherUser->id]);
$webhook = Webhook::factory()->create([
'workspace_id' => $otherWorkspace->id,
]);
$this->actingAs($this->user)
->delete(route('app.webhooks.destroy', $webhook))
->assertForbidden();
});
test('workspace admins can manage webhooks', function () {
$admin = teammateForWebhookWorkspace(Role::Admin);
$this->actingAs($admin)
->get(route('app.webhooks.index'))
->assertOk();
$this->actingAs($admin)
->post(route('app.webhooks.store'), [
'endpoint' => 'https://admin.example.com/webhooks',
'events' => [EventType::PostPublished->value],
])
->assertRedirect();
$webhook = Webhook::query()->where('endpoint', 'https://admin.example.com/webhooks')->first();
expect($webhook)->not->toBeNull();
$this->actingAs($admin)
->get(route('app.webhooks.show', $webhook))
->assertOk();
$this->actingAs($admin)
->put(route('app.webhooks.update', $webhook), [
'status' => 'disabled',
])
->assertRedirect();
$this->actingAs($admin)
->post(route('app.webhooks.send-test', $webhook))
->assertRedirect();
$this->actingAs($admin)
->post(route('app.webhooks.rotate-secret', $webhook))
->assertRedirect();
$log = WebhookLog::factory()->create([
'webhook_id' => $webhook->id,
]);
Queue::fake();
$this->actingAs($admin)
->post(route('app.webhooks.replay', [$webhook, $log]))
->assertRedirect();
$this->actingAs($admin)
->delete(route('app.webhooks.destroy', $webhook))
->assertRedirect(route('app.webhooks.index'));
});
test('members and viewers cannot manage webhooks', function (Role $role) {
$user = teammateForWebhookWorkspace($role);
$webhook = Webhook::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$log = WebhookLog::factory()->create([
'webhook_id' => $webhook->id,
]);
$this->actingAs($user)
->get(route('app.webhooks.index'))
->assertForbidden();
$this->actingAs($user)
->get(route('app.webhooks.show', $webhook))
->assertForbidden();
$this->actingAs($user)
->post(route('app.webhooks.store'), [
'endpoint' => 'https://member.example.com/webhooks',
'events' => [EventType::PostPublished->value],
])
->assertForbidden();
$this->actingAs($user)
->put(route('app.webhooks.update', $webhook), [
'endpoint' => 'https://stolen.example.com/hook',
])
->assertForbidden();
$this->actingAs($user)
->post(route('app.webhooks.send-test', $webhook))
->assertForbidden();
$this->actingAs($user)
->post(route('app.webhooks.rotate-secret', $webhook))
->assertForbidden();
$this->actingAs($user)
->post(route('app.webhooks.replay', [$webhook, $log]))
->assertForbidden();
$this->actingAs($user)
->delete(route('app.webhooks.destroy', $webhook))
->assertForbidden();
$this->assertDatabaseHas('webhooks', [
'id' => $webhook->id,
'endpoint' => $webhook->endpoint,
]);
$this->assertDatabaseMissing('webhooks', [
'endpoint' => 'https://member.example.com/webhooks',
]);
})->with([
Role::Member,
Role::Viewer,
]);
function teammateForWebhookWorkspace(Role $role): User
{
$user = User::factory()->create(['account_id' => test()->user->account_id]);
test()->workspace->members()->attach($user->id, ['role' => $role->value]);
$user->update(['current_workspace_id' => test()->workspace->id]);
return $user->fresh();
}