Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature;
use Proxynth\Larawebhook\Processing\Application\Commands\ReplayWebhookCommand;
use Proxynth\Larawebhook\Processing\Application\UseCases\ReplayWebhook;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;
use Proxynth\Larawebhook\Shared\Domain\Enums\WebhookService;
use RuntimeException;
use Throwable;
Expand All @@ -31,6 +32,7 @@ public function __construct(
private readonly GetWebhookLogDetails $getWebhookLogDetails,
private readonly ReplayWebhook $replayWebhook,
private readonly WebhookSecretResolver $secretResolver,
private readonly EventBus $eventBus,
) {}

public function index(Request $request): JsonResponse
Expand Down Expand Up @@ -80,6 +82,8 @@ public function replay(WebhookLog $log): JsonResponse
signature: $this->extractSignatureFromPayload($log),
));

$this->eventBus->dispatchMany($result->events);

return response()->json([
'success' => $result->log->status === 'success',
'message' => $result->log->status === 'success'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature;
use Proxynth\Larawebhook\Processing\Application\Ports\RetryPolicyResolver;
use Proxynth\Larawebhook\Processing\Infrastructure\Laravel\Jobs\RetryWebhookJob;
use Proxynth\Larawebhook\Shared\Application\EventBus;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;
use Proxynth\Larawebhook\Shared\Domain\Enums\WebhookService;
use Symfony\Component\HttpFoundation\Response;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature;
use Proxynth\Larawebhook\Processing\Application\Commands\RetryWebhookCommand;
use Proxynth\Larawebhook\Processing\Application\UseCases\RetryWebhook;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;

class RetryWebhookJob implements ShouldQueue
{
Expand Down Expand Up @@ -42,7 +43,7 @@ public function __construct(
*
* @throws WebhookException
*/
public function handle(RetryWebhook $retryWebhook): void
public function handle(RetryWebhook $retryWebhook, EventBus $eventBus): void
{
$result = $retryWebhook->handle(new RetryWebhookCommand(
payload: $this->payload,
Expand All @@ -55,6 +56,8 @@ public function handle(RetryWebhook $retryWebhook): void
idempotencyKey: $this->idempotencyKey,
));

$eventBus->dispatchMany($result->events);

if (! $result->shouldRetry) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Proxynth\Larawebhook\Shared\Application;
namespace Proxynth\Larawebhook\Shared\Application\Ports;

use Proxynth\Larawebhook\Shared\Domain\Events\DomainEvent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus;

use Illuminate\Contracts\Events\Dispatcher;
use Proxynth\Larawebhook\Shared\Application\EventBus;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;
use Proxynth\Larawebhook\Shared\Domain\Events\DomainEvent;

final readonly class LaravelEventBus implements EventBus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
use Proxynth\Larawebhook\Processing\Infrastructure\Idempotency\DefaultIdempotencyResolver;
use Proxynth\Larawebhook\Processing\Infrastructure\Persistence\EloquentProcessedWebhookRecorder;
use Proxynth\Larawebhook\Processing\Infrastructure\Persistence\EloquentReplayableWebhookRepository;
use Proxynth\Larawebhook\Shared\Application\EventBus;
use Proxynth\Larawebhook\Shared\Application\Larawebhook;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;
use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus\LaravelEventBus;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand Down
12 changes: 12 additions & 0 deletions tests/ArchTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

declare(strict_types=1);
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Facades\Event;

arch('it will not use debugging functions')
->expect(['var_dump', 'dd', 'dump', 'ray'])
Expand Down Expand Up @@ -116,3 +118,13 @@
arch('application layer does not call config helper')
->expect('config')
->not->toBeUsedIn($pureApplications);

arch('application use cases do not depend on laravel event dispatcher')
->expect([
'Proxynth\Larawebhook\Ingestion\Application',
'Proxynth\Larawebhook\Processing\Application',
'Proxynth\Larawebhook\Audit\Application',
])->not->toUse([
Dispatcher::class,
Event::class,
]);
2 changes: 1 addition & 1 deletion tests/Fakes/Shared/FakeEventBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Proxynth\Larawebhook\Tests\Fakes\Shared;

use Proxynth\Larawebhook\Shared\Application\EventBus;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;
use Proxynth\Larawebhook\Shared\Domain\Events\DomainEvent;

final class FakeEventBus implements EventBus
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/Http/Controllers/API/WebhookLogControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

use Illuminate\Events\Dispatcher;
use Proxynth\Larawebhook\Audit\Domain\Events\WebhookLogged;
use Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Persistence\Models\WebhookLog;
use Proxynth\Larawebhook\Processing\Domain\Events\WebhookReplayed;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;
use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus\LaravelEventBus;
use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\Providers\LarawebhookServiceProvider;

beforeEach(function () {
Expand Down Expand Up @@ -263,3 +268,26 @@
expect($response->json('message'))->toContain('Error replaying webhook:');
expect($response->json('message'))->toContain('Database connection lost');
});

it('dispatches replay domain events', function () {
Event::fake();

app()->instance(
EventBus::class,
new LaravelEventBus(app(Dispatcher::class)),
);

$log = WebhookLog::factory()->create([
'service' => 'stripe',
'event' => 'payment_intent.succeeded',
'payload' => ['type' => 'payment_intent.succeeded', 'data' => ['amount' => 1000]],
'status' => 'failed',
'attempt' => 0,
]);

$this->postJson("/api/larawebhook/logs/{$log->id}/replay");

Event::assertDispatched(WebhookReplayed::class);
Event::assertDispatched(WebhookLogged::class);

});
134 changes: 132 additions & 2 deletions tests/Feature/Middleware/ValidateWebhookMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

declare(strict_types=1);

use Illuminate\Events\Dispatcher;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Route;
use Proxynth\Larawebhook\Audit\Domain\Events\WebhookLogged;
use Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Persistence\Models\WebhookLog;
use Proxynth\Larawebhook\Ingestion\Domain\Events\WebhookReceived;
use Proxynth\Larawebhook\Ingestion\Domain\Events\WebhookRejected;
use Proxynth\Larawebhook\Ingestion\Domain\Events\WebhookValidated;
use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature;
use Proxynth\Larawebhook\Processing\Infrastructure\Laravel\Jobs\RetryWebhookJob;
use Proxynth\Larawebhook\Processing\Infrastructure\Persistence\Models\ProcessedWebhookEvent;
use Proxynth\Larawebhook\Shared\Application\EventBus;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;
use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus\LaravelEventBus;
use Proxynth\Larawebhook\Tests\Fakes\Shared\FakeEventBus;

beforeEach(function () {
Expand Down Expand Up @@ -1144,3 +1145,132 @@
expect(WebhookLog::query()->count())->toBe(2)
->and(ProcessedWebhookEvent::query()->count())->toBe(1);
});

it('dispatches domain events after successful webhook receive', function () {
Event::fake([
WebhookReceived::class,
WebhookValidated::class,
WebhookLogged::class,
]);

app()->instance(
EventBus::class,
new LaravelEventBus(app(Dispatcher::class)),
);

$payload = json_encode([
'action' => 'opened',
'pull_request' => [
'id' => 123,
],
], JSON_THROW_ON_ERROR);

$signature = githubSignature($payload, 'test_github_secret');

$this->call(
'POST',
'test-github-webhook',
[],
[],
[],
[
'HTTP_X_HUB_SIGNATURE_256' => $signature->value(),
'HTTP_X_GITHUB_EVENT' => 'ping',
'HTTP_X_GITHUB_DELIVERY' => 'delivery_123',
'CONTENT_TYPE' => 'application/json',
],
$payload
)->assertOk();

Event::assertDispatched(WebhookReceived::class);
Event::assertDispatched(WebhookValidated::class);
Event::assertDispatched(WebhookLogged::class);
});

it('dispatches domain events after rejected webhook receive', function () {
Event::fake();

config()->set('larawebhook.retries.async', false);

app()->instance(
EventBus::class,
new LaravelEventBus(app(Dispatcher::class)),
);

$payload = json_encode([
'action' => 'opened',
'pull_request' => [
'id' => 123,
],
], JSON_THROW_ON_ERROR);

$this->call(
'POST',
'test-github-webhook',
[],
[],
[],
[
'HTTP_X_HUB_SIGNATURE_256' => 'sha1=invalid_signature',
'HTTP_X_GITHUB_EVENT' => 'ping',
'HTTP_X_GITHUB_DELIVERY' => 'delivery_123',
'CONTENT_TYPE' => 'application/json',
],
$payload
);

Event::assertDispatched(WebhookReceived::class);
Event::assertDispatched(WebhookRejected::class);
Event::assertDispatched(WebhookLogged::class);
Event::assertNotDispatched(WebhookValidated::class);
});

it('does not dispatch receive events for already processed webhook', function () {
Event::fake();

$payload = json_encode([
'action' => 'opened',
'pull_request' => [
'id' => 123,
],
], JSON_THROW_ON_ERROR);

$signature = githubSignature($payload, 'test_github_secret');

$this->call(
'POST',
'test-github-webhook',
[],
[],
[],
[
'HTTP_X_HUB_SIGNATURE_256' => $signature->value(),
'HTTP_X_GITHUB_EVENT' => 'ping',
'HTTP_X_GITHUB_DELIVERY' => 'delivery_123',
'CONTENT_TYPE' => 'application/json',
],
$payload
);

$this->call(
'POST',
'test-github-webhook',
[],
[],
[],
[
'HTTP_X_HUB_SIGNATURE_256' => $signature->value(),
'HTTP_X_GITHUB_EVENT' => 'ping',
'HTTP_X_GITHUB_DELIVERY' => 'delivery_123',
'CONTENT_TYPE' => 'application/json',
],
$payload
)->assertOk()
->assertJson([
'status' => 'already_processed',
]);

Event::assertNotDispatched(WebhookReceived::class);
Event::assertNotDispatched(WebhookValidated::class);
Event::assertNotDispatched(WebhookLogged::class);
});
40 changes: 37 additions & 3 deletions tests/Unit/Jobs/RetryWebhookJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
declare(strict_types=1);

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Facades\Queue;
use Proxynth\Larawebhook\Audit\Domain\Events\WebhookLogged;
use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature;
use Proxynth\Larawebhook\Processing\Application\Data\RetryPolicy;
use Proxynth\Larawebhook\Processing\Application\Ports\RetryPolicyResolver;
use Proxynth\Larawebhook\Processing\Application\UseCases\RetryWebhook;
use Proxynth\Larawebhook\Processing\Domain\Events\WebhookProcessingFailed;
use Proxynth\Larawebhook\Processing\Infrastructure\Laravel\Jobs\RetryWebhookJob;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;
use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus\LaravelEventBus;
use Proxynth\Larawebhook\Tests\Fakes\Processing\FakeRetryPolicyResolver;

beforeEach(function () {
Expand Down Expand Up @@ -83,7 +89,7 @@
idempotencyKey: 'delivery_123',
);

$job->handle(app(RetryWebhook::class));
$job->handle(app(RetryWebhook::class), app(EventBus::class));

Queue::assertNothingPushed();
});
Expand All @@ -102,7 +108,7 @@
idempotencyKey: 'delivery_123',
);

$job->handle(app(RetryWebhook::class));
$job->handle(app(RetryWebhook::class), app(EventBus::class));

Queue::assertPushed(RetryWebhookJob::class, function (RetryWebhookJob $queuedJob) {
return $queuedJob->attempt() === 1
Expand Down Expand Up @@ -134,8 +140,36 @@
idempotencyKey: 'delivery_123',
);

$job->handle(app(RetryWebhook::class));
$job->handle(app(RetryWebhook::class), app(EventBus::class));

Queue::assertNothingPushed();
});
});

it('dispatches domain events produced by retry webhook', function () {
Event::fake();

app()->instance(
EventBus::class,
new LaravelEventBus(app(Dispatcher::class)),
);

$job = new RetryWebhookJob(
payload: '{"action":"opened"}',
signature: Signature::fromString('sha256=invalid'),
service: 'github',
event: 'opened',
secret: 'github_secret',
attempt: 1,
externalId: 'delivery_123',
idempotencyKey: 'delivery_123',
);

$job->handle(
app(RetryWebhook::class),
app(EventBus::class),
);

Event::assertDispatched(WebhookProcessingFailed::class);
Event::assertDispatched(WebhookLogged::class);
});
2 changes: 1 addition & 1 deletion tests/Unit/LarawebhookServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Proxynth\Larawebhook\Ingestion\Application\Ports\WebhookServiceMetadataResolver;
use Proxynth\Larawebhook\Processing\Application\Ports\ReplayableWebhookRepository;
use Proxynth\Larawebhook\Processing\Application\UseCases\RetryWebhook;
use Proxynth\Larawebhook\Shared\Application\EventBus;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;
use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus\LaravelEventBus;
use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\Providers\LarawebhookServiceProvider;

Expand Down