diff --git a/tests/Unit/Notifications/VerifierTest.php b/tests/Unit/Notifications/VerifierTest.php index 3db9f0e9..742bd4e8 100644 --- a/tests/Unit/Notifications/VerifierTest.php +++ b/tests/Unit/Notifications/VerifierTest.php @@ -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 { @@ -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'), + ]; + } } diff --git a/tests/Unit/Notifications/_fixtures/special_chars_payload.json b/tests/Unit/Notifications/_fixtures/special_chars_payload.json new file mode 100644 index 00000000..f68c8a6e --- /dev/null +++ b/tests/Unit/Notifications/_fixtures/special_chars_payload.json @@ -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"} diff --git a/tests/Unit/Notifications/_fixtures/standard_payload.json b/tests/Unit/Notifications/_fixtures/standard_payload.json new file mode 100644 index 00000000..06f59212 --- /dev/null +++ b/tests/Unit/Notifications/_fixtures/standard_payload.json @@ -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"} diff --git a/tests/Utils/ReadsFixtures.php b/tests/Utils/ReadsFixtures.php index 3bfb9822..79869ebf 100644 --- a/tests/Utils/ReadsFixtures.php +++ b/tests/Utils/ReadsFixtures.php @@ -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!");