Skip to content

Commit

Permalink
Fix Event type detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
ipalaus committed May 19, 2024
1 parent cee0d95 commit 59b25df
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
8 changes: 5 additions & 3 deletions src/ProcessRevenueCatWebhookJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ class ProcessRevenueCatWebhookJob extends ProcessWebhookJob
{
public function handle()
{
if (! isset($this->webhookCall->payload['event_type']) || $this->webhookCall->payload['event_type'] === '') {
$event = $this->webhookCall->payload['event'] ?? null;

if (! $event || ! isset($event['type']) || $event['type'] === '') {
throw WebhookFailed::missingType($this->webhookCall);
}

event("revenuecat-webhooks::{$this->webhookCall->payload['event_type']}", $this->webhookCall);
event("revenuecat-webhooks::{$event['type']}", $this->webhookCall);

$jobClass = $this->determineJobClass($this->webhookCall->payload['event_type']);
$jobClass = $this->determineJobClass($event['type']);

if ($jobClass === '') {
return;
Expand Down
59 changes: 35 additions & 24 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PalauaAndSons\RevenueCatWebhooks\Tests;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Spatie\WebhookClient\Exceptions\InvalidWebhookSignature;
Expand All @@ -19,7 +18,7 @@ public function setUp(): void
Route::revenueCatWebhooks('revenuecat-webhooks');
Route::revenueCatWebhooks('revenuecat-webhooks/{configKey}');

config(['revenuecat-webhooks.jobs' => ['my_type' => DummyJob::class]]);
config(['revenuecat-webhooks.jobs' => ['INITIAL_PURCHASE' => DummyJob::class]]);
cache()->clear();
}

Expand All @@ -29,8 +28,11 @@ public function it_can_handle_a_valid_request()
$this->withoutExceptionHandling();

$payload = [
'event_type' => 'my_type',
'key' => 'value',
'api_version' => '1.0',
'event' => [
'type' => 'INITIAL_PURCHASE',
'key' => 'value',
],
];

$this->postJson('revenuecat-webhooks', $payload)
Expand All @@ -40,16 +42,17 @@ public function it_can_handle_a_valid_request()

$webhookCall = WebhookCall::first();

$this->assertEquals('my_type', $webhookCall->payload['event_type']);
$this->assertEquals('INITIAL_PURCHASE', $webhookCall->payload['event']['type']);
$this->assertEquals($payload, $webhookCall->payload);
$this->assertNull($webhookCall->exception);

Event::assertDispatched('revenuecat-webhooks::my_type', function ($event, $eventPayload) use ($webhookCall) {
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
$this->assertEquals($webhookCall->id, $eventPayload->id);
Event::assertDispatched('revenuecat-webhooks::INITIAL_PURCHASE',
function ($event, $eventPayload) use ($webhookCall) {
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
$this->assertEquals($webhookCall->id, $eventPayload->id);

return true;
});
return true;
});

$this->assertEquals($webhookCall->id, cache('dummyjob')->id);
}
Expand All @@ -66,12 +69,13 @@ public function a_request_with_an_invalid_payload_will_be_logged_but_events_and_

$webhookCall = WebhookCall::first();

$this->assertFalse(isset($webhookCall->payload['event_type']));
$this->assertFalse(isset($webhookCall->payload['event']['type']));
$this->assertEquals(['invalid_payload'], $webhookCall->payload);

$this->assertEquals('Webhook call id `1` did not contain a type. Valid RevenueCat webhook calls should always contain a type.', $webhookCall->exception['message']);
$this->assertEquals('Webhook call id `1` did not contain a type. Valid RevenueCat webhook calls should always contain a type.',
$webhookCall->exception['message']);

Event::assertNotDispatched('revenuecat-webhooks::my_type');
Event::assertNotDispatched('revenuecat-webhooks::INITIAL_PURCHASE');

$this->assertNull(cache('dummyjob'));
}
Expand All @@ -84,8 +88,11 @@ public function it_can_handle_a_valid_request_with_authorization_header()
$this->withoutExceptionHandling();

$payload = [
'event_type' => 'my_type',
'key' => 'value',
'api_version' => '1.0',
'event' => [
'type' => 'INITIAL_PURCHASE',
'key' => 'value',
],
];

$this->postJson('revenuecat-webhooks', $payload, ['Authorization' => 'Bearer ABC'])
Expand All @@ -95,16 +102,17 @@ public function it_can_handle_a_valid_request_with_authorization_header()

$webhookCall = WebhookCall::first();

$this->assertEquals('my_type', $webhookCall->payload['event_type']);
$this->assertEquals('INITIAL_PURCHASE', $webhookCall->payload['event']['type']);
$this->assertEquals($payload, $webhookCall->payload);
$this->assertNull($webhookCall->exception);

Event::assertDispatched('revenuecat-webhooks::my_type', function ($event, $eventPayload) use ($webhookCall) {
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
$this->assertEquals($webhookCall->id, $eventPayload->id);
Event::assertDispatched('revenuecat-webhooks::INITIAL_PURCHASE',
function ($event, $eventPayload) use ($webhookCall) {
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
$this->assertEquals($webhookCall->id, $eventPayload->id);

return true;
});
return true;
});

$this->assertEquals($webhookCall->id, cache('dummyjob')->id);
}
Expand All @@ -118,16 +126,19 @@ public function it_rejects_a_request_with_invalid_authorization_header()
$this->expectException(InvalidWebhookSignature::class);

$payload = [
'event_type' => 'my_type',
'key' => 'value',
'api_version' => '1.0',
'event' => [
'type' => 'INITIAL_PURCHASE',
'key' => 'value',
],
];

$this->postJson('revenuecat-webhooks', $payload, ['Authorization' => 'Invalid Bearer Token'])
->assertStatus(500);

$this->assertCount(0, WebhookCall::get());

Event::assertNotDispatched('revenuecat-webhooks::my_type');
Event::assertNotDispatched('revenuecat-webhooks::INITIAL_PURCHASE');

$this->assertNull(cache('dummyjob'));
}
Expand Down
21 changes: 14 additions & 7 deletions tests/RevenueCatWebhookCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ public function setUp(): void

Event::fake();

config(['revenuecat-webhooks.jobs' => ['my_type' => DummyJob::class]]);
config(['revenuecat-webhooks.jobs' => ['INITIAL_PURCHASE' => DummyJob::class]]);

$this->webhookCall = WebhookCall::create([
'name' => 'revenuecat',
'payload' => ['event_type' => 'my_type', 'name' => 'value'],
'payload' => [
'api_version' => '1.0',
'event' => [
'type' => 'INITIAL_PURCHASE',
'name' => 'value',
],
],
'url' => 'https://example.com/revenuecat-webhooks',
]);

Expand Down Expand Up @@ -68,12 +74,13 @@ public function it_will_dispatch_events_even_when_no_corresponding_job_is_config

$webhookCall = $this->webhookCall;

Event::assertDispatched("revenuecat-webhooks::{$webhookCall->payload['event_type']}", function ($event, $eventPayload) use ($webhookCall) {
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
$this->assertEquals($webhookCall->id, $eventPayload->id);
Event::assertDispatched("revenuecat-webhooks::{$webhookCall->payload['event']['type']}",
function ($event, $eventPayload) use ($webhookCall) {
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
$this->assertEquals($webhookCall->id, $eventPayload->id);

return true;
});
return true;
});

$this->assertNull(cache('dummyjob'));
}
Expand Down

0 comments on commit 59b25df

Please sign in to comment.