|
| 1 | +<?php |
| 2 | +namespace Square\Tests; |
| 3 | + |
| 4 | +use Square\Utils\WebhooksHelper; |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +class WebhooksHelperTest extends TestCase |
| 8 | +{ |
| 9 | + private $requestBody = '{"merchant_id":"MLEFBHHSJGVHD","type":"webhooks.test_notification","event_id":"ac3ac95b-f97d-458c-a6e6-18981597e05f","created_at":"2022-07-13T20:30:59.037339943Z","data":{"type":"webhooks","id":"bc368e64-01aa-407e-b46e-3231809b1129"}}'; |
| 10 | + private $signatureHeader = 'GF4YkrJgGBDZ9NIYbNXBnMzqb2HoL4RW/S6vkZ9/2N4='; |
| 11 | + private $signatureKey = 'Ibxx_5AKakO-3qeNVR61Dw'; |
| 12 | + private $notificationUrl = 'https://webhook.site/679a4f3a-dcfa-49ee-bac5-9d0edad886b9'; |
| 13 | + |
| 14 | + public function testSignatureValidationPass(): void |
| 15 | + { |
| 16 | + $isValid = WebhooksHelper::isValidWebhookEventSignature( |
| 17 | + $this->requestBody, |
| 18 | + $this->signatureHeader, |
| 19 | + $this->signatureKey, |
| 20 | + $this->notificationUrl |
| 21 | + ); |
| 22 | + $this->assertTrue($isValid); |
| 23 | + } |
| 24 | + |
| 25 | + public function testSignatureValidationFailsOnNotificationUrlMismatch(): void |
| 26 | + { |
| 27 | + $isValid = WebhooksHelper::isValidWebhookEventSignature( |
| 28 | + $this->requestBody, |
| 29 | + $this->signatureHeader, |
| 30 | + $this->signatureKey, |
| 31 | + 'https://webhook.site/79a4f3a-dcfa-49ee-bac5-9d0edad886b9' |
| 32 | + ); |
| 33 | + $this->assertFalse($isValid); |
| 34 | + } |
| 35 | + |
| 36 | + public function testSignatureValidationFailsOnInvalidSignatureKey(): void |
| 37 | + { |
| 38 | + $isValid = WebhooksHelper::isValidWebhookEventSignature( |
| 39 | + $this->requestBody, |
| 40 | + $this->signatureHeader, |
| 41 | + 'MCmhFRxGX82xMwg5FsaoQA', |
| 42 | + $this->notificationUrl |
| 43 | + ); |
| 44 | + $this->assertFalse($isValid); |
| 45 | + } |
| 46 | + |
| 47 | + public function testSignatureValidationFailsOnInvalidSignatureHeader(): void |
| 48 | + { |
| 49 | + $isValid = WebhooksHelper::isValidWebhookEventSignature( |
| 50 | + $this->requestBody, |
| 51 | + '1z2n3DEJJiUPKcPzQo1ftvbxGdw=', |
| 52 | + $this->signatureKey, |
| 53 | + $this->notificationUrl |
| 54 | + ); |
| 55 | + $this->assertFalse($isValid); |
| 56 | + } |
| 57 | + |
| 58 | + public function testSignatureValidationFailsOnRequestBodyMismatch(): void |
| 59 | + { |
| 60 | + $requestBody = '{"merchant_id":"MLEFBHHSJGVHD","type":"webhooks.test_notification","event_id":"ac3ac95b-f97d-458c-a6e6-18981597e05f","created_at":"2022-06-13T20:30:59.037339943Z","data":{"type":"webhooks","id":"bc368e64-01aa-407e-b46e-3231809b1129"}}'; |
| 61 | + $isValid = WebhooksHelper::isValidWebhookEventSignature( |
| 62 | + $requestBody, |
| 63 | + $this->signatureHeader, |
| 64 | + $this->signatureKey, |
| 65 | + $this->notificationUrl |
| 66 | + ); |
| 67 | + $this->assertFalse($isValid); |
| 68 | + } |
| 69 | + |
| 70 | + public function testSignatureValidationFailsOnRequestBodyFormatted(): void |
| 71 | + { |
| 72 | + $requestBody = '{ |
| 73 | + "merchant_id": "MLEFBHHSJGVHD", |
| 74 | + "type": "webhooks.test_notification", |
| 75 | + "event_id": "ac3ac95b-f97d-458c-a6e6-18981597e05f", |
| 76 | + "created_at": "2022-07-13T20:30:59.037339943Z", |
| 77 | + "data": { |
| 78 | + "type": "webhooks", |
| 79 | + "id": "bc368e64-01aa-407e-b46e-3231809b1129" |
| 80 | + } |
| 81 | + }'; |
| 82 | + $isValid = WebhooksHelper::isValidWebhookEventSignature( |
| 83 | + $requestBody, |
| 84 | + $this->signatureHeader, |
| 85 | + $this->signatureKey, |
| 86 | + $this->notificationUrl |
| 87 | + ); |
| 88 | + $this->assertFalse($isValid); |
| 89 | + } |
| 90 | + |
| 91 | + public function testThrowsErrorOnEmptySignatureKey(): void |
| 92 | + { |
| 93 | + $this->expectExceptionMessage('signatureKey is null or empty'); |
| 94 | + WebhooksHelper::isValidWebhookEventSignature( |
| 95 | + $this->requestBody, |
| 96 | + $this->signatureHeader, |
| 97 | + '', |
| 98 | + $this->notificationUrl |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + public function testThrowsErrorOnSignatureKeyNotConfigured(): void |
| 103 | + { |
| 104 | + $this->expectException(\TypeError::class); |
| 105 | + |
| 106 | + WebhooksHelper::isValidWebhookEventSignature( |
| 107 | + $this->requestBody, |
| 108 | + $this->signatureHeader, |
| 109 | + null, |
| 110 | + $this->notificationUrl |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + public function testThrowsErrorOnEmptyNotificationUrl(): void |
| 115 | + { |
| 116 | + $this->expectExceptionMessage('notificationUrl is null or empty'); |
| 117 | + WebhooksHelper::isValidWebhookEventSignature( |
| 118 | + $this->requestBody, |
| 119 | + $this->signatureHeader, |
| 120 | + $this->signatureKey, |
| 121 | + '' |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + public function testThrowsErrorOnNotificationUrlNotConfigured(): void |
| 126 | + { |
| 127 | + $this->expectException(\TypeError::class); |
| 128 | + |
| 129 | + WebhooksHelper::isValidWebhookEventSignature( |
| 130 | + $this->requestBody, |
| 131 | + $this->signatureHeader, |
| 132 | + $this->signatureKey, |
| 133 | + null |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
0 commit comments