Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add improved test coverage for webhook verification #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions tests/Unit/Notifications/VerifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use Paddle\SDK\Notifications\PaddleSignature;
use Paddle\SDK\Notifications\Secret;
use Paddle\SDK\Notifications\Verifier;
use Paddle\SDK\Tests\Utils\ReadsFixtures;
use PHPUnit\Framework\TestCase;

class VerifierTest extends TestCase
{
use ReadsFixtures;

/** @test */
public function it_verifies_a_signature(): void
{
Expand All @@ -34,4 +37,41 @@ public function it_verifies_a_signature(): void

self::assertTrue((new Verifier(null))->verify($request, ...$secrets));
}

/**
* @test
*
* @dataProvider verifyPayloadProvider
*/
public function it_verifies_payloads(string $signature, string $secret, string $payload): void
{
$request = Psr17FactoryDiscovery::findRequestFactory()
->createRequest('POST', '/paddle/notifications')
->withHeader(
PaddleSignature::HEADER,
$signature,
)
->withBody(
Psr17FactoryDiscovery::findStreamFactory()->createStream($payload),
);

self::assertTrue((new Verifier(null))->verify($request, new Secret($secret)));
}

public static function verifyPayloadProvider(): \Generator
{
$secret = 'pdl_ntfset_01hs0t3tw21j988db1pam5xg8m_GrOWLNef+vmtjJYq4mSnHNzvc8uWoJ1I';

yield 'Standard Payload' => [
'ts=1710498758;h1=558bf93944dbeb4790c7a8af6cb2ea435c8ca9c8396aafc1a4e37424ac132744',
$secret,
self::readRawJsonFixture('standard_payload'),
];

yield 'Special Characters' => [
'ts=1710498288;h1=38219b3ba1578184e625c29d201a27cd25bd69e2ec9c3ab87dcb90d87ff73a41',
$secret,
self::readRawJsonFixture('special_chars_payload'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"id":"ctm_01hs0t94g83hs5jdw282ztqa4z","name":"\u00dc\u00c4\u00c5\u00e5\u00d6\u00dc\u00c4\u00c5\u00e5\u00d6","email":"blackhole+verification1@paddle.com","locale":"en","status":"active","created_at":"2024-03-15T10:24:48.136Z","updated_at":"2024-03-15T10:24:48.136Z","custom_data":null,"import_meta":null,"marketing_consent":false},"event_id":"evt_01hs0t94tjvtv62azjmkbeysz7","event_type":"customer.created","occurred_at":"2024-03-15T10:24:48.466759Z","notification_id":"ntf_01hs0t94ws0ay8keh5t4rq70ht"}
1 change: 1 addition & 0 deletions tests/Unit/Notifications/_fixtures/standard_payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"id":"ctm_01hs0tqf76sxmp7ba5e4mw1sc8","name":"John Doe","email":"blackhole+verification2@paddle.com","locale":"en","status":"active","created_at":"2024-03-15T10:32:37.862Z","updated_at":"2024-03-15T10:32:37.862Z","custom_data":null,"import_meta":null,"marketing_consent":false},"event_id":"evt_01hs0tqfme2xwb2hvwv87p8y3w","event_type":"customer.created","occurred_at":"2024-03-15T10:32:38.286848Z","notification_id":"ntf_01hs0tqfrhgkyp39x4wyvy7h6n"}
2 changes: 1 addition & 1 deletion tests/Utils/ReadsFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function readRawJsonFixture(string $fixture, string|null $basePath
continue;
}

return file_get_contents($file);
return trim(file_get_contents($file));
}

throw new \InvalidArgumentException("Fixture '{$fixture}' not found!");
Expand Down
Loading