Repository navigation
Expand file tree
/
Copy pathWebhookVerificationTest.php
More file actions
89 lines (75 loc) · 3.08 KB
/
Copy pathWebhookVerificationTest.php
File metadata and controls
89 lines (75 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
// @oagen-ignore-file
// Hand-maintained tests for the WebhookVerification module (H01, H02).
namespace Tests;
use PHPUnit\Framework\TestCase;
use WorkOS\TestHelper;
use WorkOS\WebhookVerification;
class WebhookVerificationTest extends TestCase
{
use TestHelper;
private const SECRET = 'whsec_test_secret_key';
public function testVerifyEventSuccess(): void
{
$payload = json_encode(['event' => 'user.created', 'data' => ['id' => '123']]);
$timestamp = (string) (time() * 1000); // milliseconds
$expectedSig = hash_hmac('sha256', "{$timestamp}.{$payload}", self::SECRET);
$sigHeader = "t={$timestamp}, v1={$expectedSig}";
$client = $this->createMockClient([]);
$result = $client->webhookVerification()->verifyEvent(
eventBody: $payload,
eventSignature: $sigHeader,
secret: self::SECRET,
);
$this->assertSame('user.created', $result['event']);
$this->assertSame('123', $result['data']['id']);
}
public function testVerifyEventInvalidSignature(): void
{
$payload = '{"event":"test"}';
$timestamp = (string) (time() * 1000);
$sigHeader = "t={$timestamp}, v1=invalidsignature";
$client = $this->createMockClient([]);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Signature hash does not match');
$client->webhookVerification()->verifyEvent(
eventBody: $payload,
eventSignature: $sigHeader,
secret: self::SECRET,
);
}
public function testVerifyEventExpiredTimestamp(): void
{
$payload = '{"event":"test"}';
$timestamp = (string) ((time() - 300) * 1000); // 5 minutes ago
$expectedSig = hash_hmac('sha256', "{$timestamp}.{$payload}", self::SECRET);
$sigHeader = "t={$timestamp}, v1={$expectedSig}";
$client = $this->createMockClient([]);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Timestamp outside the tolerance zone');
$client->webhookVerification()->verifyEvent(
eventBody: $payload,
eventSignature: $sigHeader,
secret: self::SECRET,
);
}
public function testGetTimestampAndSignatureHash(): void
{
$sigHeader = 't=1234567890, v1=abcdef1234567890';
[$timestamp, $hash] = WebhookVerification::getTimestampAndSignatureHash($sigHeader);
$this->assertSame('1234567890', $timestamp);
$this->assertSame('abcdef1234567890', $hash);
}
public function testComputeSignature(): void
{
$expected = hash_hmac('sha256', '12345.payload', 'secret');
$result = WebhookVerification::computeSignature('12345', 'payload', 'secret');
$this->assertSame($expected, $result);
}
public function testWebhookVerificationAccessibleFromClient(): void
{
$client = $this->createMockClient([]);
$this->assertInstanceOf(WebhookVerification::class, $client->webhookVerification());
}
}