-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventsTest.php
More file actions
186 lines (158 loc) · 6.67 KB
/
EventsTest.php
File metadata and controls
186 lines (158 loc) · 6.67 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace Tests\Services;
use Increase\Client;
use Increase\Core\Exceptions\WebhookException;
use Increase\Core\Util;
use Increase\Events\Event;
use Increase\Page;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use StandardWebhooks\Webhook;
/**
* @internal
*/
#[CoversNothing]
final class EventsTest extends TestCase
{
protected Client $client;
protected function setUp(): void
{
parent::setUp();
$testUrl = Util::getenv('TEST_API_BASE_URL') ?: 'http://127.0.0.1:4010';
$client = new Client(apiKey: 'My API Key', baseUrl: $testUrl);
$this->client = $client;
}
#[Test]
public function testRetrieve(): void
{
$result = $this->client->events->retrieve(
'event_001dzz0r20rzr4zrhrr1364hy80'
);
// @phpstan-ignore-next-line method.alreadyNarrowedType
$this->assertInstanceOf(Event::class, $result);
}
#[Test]
public function testList(): void
{
$page = $this->client->events->list();
// @phpstan-ignore-next-line method.alreadyNarrowedType
$this->assertInstanceOf(Page::class, $page);
if ($item = $page->getItems()[0] ?? null) {
// @phpstan-ignore-next-line method.alreadyNarrowedType
$this->assertInstanceOf(Event::class, $item);
}
}
#[Test]
public function testUnwrap(): void
{
$payload = '{"id":"event_001dzz0r20rzr4zrhrr1364hy80","associated_object_id":"account_in71c4amph0vgo2qllky","associated_object_type":"account","category":"account.created","created_at":"2020-01-31T23:59:59Z","type":"event"}';
$this->client->events->unwrap($payload);
// unwrap successful if not error thrown, increment assertion count to avoid risky test warning
$this->addToAssertionCount(1);
}
#[Test]
public function testUnwrapBadJson(): void
{
$this->expectException(WebhookException::class);
$badPayload = 'not a json string';
$this->client->events->unwrap($badPayload);
}
#[Test]
public function testUnwrapWithVerification(): void
{
$payload = '{"id":"event_001dzz0r20rzr4zrhrr1364hy80","associated_object_id":"account_in71c4amph0vgo2qllky","associated_object_type":"account","category":"account.created","created_at":"2020-01-31T23:59:59Z","type":"event"}';
$secret = 'whsec_c2VjcmV0Cg==';
$rawSecret = "secret\n";
$webhook = new Webhook($secret);
$messageId = '1';
$timestamp = time();
$signature = $webhook->sign($messageId, $timestamp, $payload);
/** @var array<string, list<string>> $headers */
$headers = [
'webhook-signature' => [$signature],
'webhook-id' => [$messageId],
'webhook-timestamp' => [(string) $timestamp],
];
$this->client->events->unwrap($payload, $headers, $rawSecret);
// unwrap successful if not error thrown, increment assertion count to avoid risky test warning
$this->addToAssertionCount(1);
}
#[Test]
public function testUnwrapWrongKey(): void
{
$this->expectException(WebhookException::class);
$payload = '{"id":"event_001dzz0r20rzr4zrhrr1364hy80","associated_object_id":"account_in71c4amph0vgo2qllky","associated_object_type":"account","category":"account.created","created_at":"2020-01-31T23:59:59Z","type":"event"}';
$secret = 'whsec_c2VjcmV0Cg==';
$rawSecret = "secret\n";
$webhook = new Webhook($secret);
$messageId = '1';
$timestamp = time();
$signature = $webhook->sign($messageId, $timestamp, $payload);
/** @var array<string, list<string>> $headers */
$headers = [
'webhook-signature' => [$signature],
'webhook-id' => [$messageId],
'webhook-timestamp' => [(string) $timestamp],
];
$wrongKey = 'whsec_aaaaaaaaaa';
$this->client->events->unwrap($payload, $headers, $wrongKey);
}
#[Test]
public function testUnwrapBadSignature(): void
{
$this->expectException(WebhookException::class);
$payload = '{"id":"event_001dzz0r20rzr4zrhrr1364hy80","associated_object_id":"account_in71c4amph0vgo2qllky","associated_object_type":"account","category":"account.created","created_at":"2020-01-31T23:59:59Z","type":"event"}';
$secret = 'whsec_c2VjcmV0Cg==';
$rawSecret = "secret\n";
$webhook = new Webhook($secret);
$messageId = '1';
$timestamp = time();
$badSig = $webhook->sign($messageId, $timestamp, 'some other payload');
/** @var array<string, list<string>> $headers */
$headers = [
'webhook-signature' => [$badSig],
'webhook-id' => [$messageId],
'webhook-timestamp' => [(string) $timestamp],
];
$this->client->events->unwrap($payload, $headers, $rawSecret);
}
#[Test]
public function testUnwrapOldTimestamp(): void
{
$this->expectException(WebhookException::class);
$payload = '{"id":"event_001dzz0r20rzr4zrhrr1364hy80","associated_object_id":"account_in71c4amph0vgo2qllky","associated_object_type":"account","category":"account.created","created_at":"2020-01-31T23:59:59Z","type":"event"}';
$secret = 'whsec_c2VjcmV0Cg==';
$rawSecret = "secret\n";
$webhook = new Webhook($secret);
$messageId = '1';
$timestamp = time();
$signature = $webhook->sign($messageId, $timestamp, $payload);
/** @var array<string, list<string>> $headers */
$headers = [
'webhook-signature' => [$signature],
'webhook-id' => [$messageId],
'webhook-timestamp' => ['5'],
];
$this->client->events->unwrap($payload, $headers, $rawSecret);
}
#[Test]
public function testUnwrapWrongMessageID(): void
{
$this->expectException(WebhookException::class);
$payload = '{"id":"event_001dzz0r20rzr4zrhrr1364hy80","associated_object_id":"account_in71c4amph0vgo2qllky","associated_object_type":"account","category":"account.created","created_at":"2020-01-31T23:59:59Z","type":"event"}';
$secret = 'whsec_c2VjcmV0Cg==';
$rawSecret = "secret\n";
$webhook = new Webhook($secret);
$messageId = '1';
$timestamp = time();
$signature = $webhook->sign($messageId, $timestamp, $payload);
/** @var array<string, list<string>> $headers */
$headers = [
'webhook-signature' => [$signature],
'webhook-id' => ['wrong'],
'webhook-timestamp' => [(string) $timestamp],
];
$this->client->events->unwrap($payload, $headers, $rawSecret);
}
}