-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathWebhookControllerTest.php
256 lines (205 loc) · 10 KB
/
WebhookControllerTest.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php declare(strict_types=1);
/*
* (c) shopware AG <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swag\PayPal\Test\Webhook;
use Monolog\Handler\TestHandler;
use Monolog\Level;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
use Shopware\Core\System\SystemConfig\SystemConfigDefinition;
use Shopware\Core\Test\TestDefaults;
use Swag\PayPal\Test\Mock\Repositories\DefinitionInstanceRegistryMock;
use Swag\PayPal\Test\Mock\Setting\Service\SystemConfigServiceMock;
use Swag\PayPal\Test\Mock\Webhook\WebhookServiceMock;
use Swag\PayPal\Test\Webhook\_fixtures\WebhookDataFixture;
use Swag\PayPal\Webhook\WebhookController;
use Swag\PayPal\Webhook\WebhookService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* @internal
*/
#[Package('checkout')]
class WebhookControllerTest extends TestCase
{
use IntegrationTestBehaviour;
public const THROW_PAYPAL_API_EXCEPTION = 'executeWebhookThrowsPayPalApiException';
public const THROW_WEBHOOK_EXCEPTION = 'executeWebhookThrowsWebhookException';
public const THROW_GENERAL_EXCEPTION = 'executeWebhookThrowsGeneralException';
public const THROW_NOT_FOUND_EXCEPTION = 'executeWebhookThrowsNotFoundException';
public const EMPTY_TOKEN = 'emptyExecuteToken';
private WebhookController $controller;
private TestHandler $logger;
protected function setUp(): void
{
$definitionRegistry = new DefinitionInstanceRegistryMock([], $this->createMock(ContainerInterface::class));
$systemConfigRepo = $definitionRegistry->getRepository(
(new SystemConfigDefinition())->getEntityName()
);
$this->logger = new TestHandler();
$this->controller = new WebhookController(
new Logger('testlogger', [$this->logger]),
new WebhookServiceMock(SystemConfigServiceMock::createWithoutCredentials()),
$systemConfigRepo
);
}
public function testStatusWebhook(): void
{
$jsonResponse = $this->controller->statusWebhook(TestDefaults::SALES_CHANNEL);
$content = $jsonResponse->getContent();
static::assertNotFalse($content);
$result = \json_decode($content, true);
static::assertEqualsCanonicalizing(['result' => WebhookService::STATUS_WEBHOOK_VALID], $result);
}
public function testRegisterWebhook(): void
{
$jsonResponse = $this->controller->registerWebhook(TestDefaults::SALES_CHANNEL);
$content = $jsonResponse->getContent();
static::assertNotFalse($content);
$result = \json_decode($content, true);
static::assertEqualsCanonicalizing(['result' => WebhookService::WEBHOOK_CREATED], $result);
}
public function testDeregisterWebhook(): void
{
$jsonResponse = $this->controller->deregisterWebhook(TestDefaults::SALES_CHANNEL);
$content = $jsonResponse->getContent();
static::assertNotFalse($content);
$result = \json_decode($content, true);
// no action because no Webhook ID is defined by default
static::assertEqualsCanonicalizing(['result' => WebhookService::NO_WEBHOOK_ACTION_REQUIRED], $result);
}
public function testExecuteWebhook(): void
{
$context = Context::createDefaultContext();
$request = $this->createRequestWithWebhookData();
$response = $this->controller->executeWebhook($request, $context);
static::assertSame(Response::HTTP_OK, $response->getStatusCode());
static::assertTrue(
$this->logger->hasRecordThatContains('Webhook successfully executed', Level::Info),
'Expected "Webhook successfully executed" log entry not found',
);
}
public function testExecuteWebhookThrowsPayPalApiException(): void
{
$context = Context::createDefaultContext();
$context->addExtension(self::THROW_PAYPAL_API_EXCEPTION, new ArrayStruct());
$request = $this->createRequestWithWebhookData();
$response = $this->controller->executeWebhook($request, $context);
static::assertSame('An error occurred during execution of webhook', $response->getContent());
static::assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
static::assertTrue(
$this->logger->hasRecordThatContains('testPayPalApiExceptionMessage', Level::Error),
'Expected "testPayPalApiExceptionMessage" log entry not found',
);
}
public function testExecuteWebhookThrowsWebhookException(): void
{
$context = Context::createDefaultContext();
$context->addExtension(self::THROW_WEBHOOK_EXCEPTION, new ArrayStruct());
$request = $this->createRequestWithWebhookData();
$response = $this->controller->executeWebhook($request, $context);
static::assertSame('An error occurred during execution of webhook', $response->getContent());
static::assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
static::assertTrue(
$this->logger->hasRecordThatContains('testWebhookExceptionMessage', Level::Error),
'Expected "testWebhookExceptionMessage" log entry not found',
);
}
public function testExecuteWebhookThrowsGeneralException(): void
{
$context = Context::createDefaultContext();
$context->addExtension(self::THROW_GENERAL_EXCEPTION, new ArrayStruct());
$request = $this->createRequestWithWebhookData();
$response = $this->controller->executeWebhook($request, $context);
static::assertSame('An error occurred during execution of webhook', $response->getContent());
static::assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
static::assertTrue(
$this->logger->hasRecordThatContains('testGeneralExceptionMessage', Level::Error),
'Expected "testGeneralExceptionMessage" log entry not found',
);
}
public function testExecuteWebhookEmptyToken(): void
{
$context = Context::createDefaultContext();
$context->addExtension(self::EMPTY_TOKEN, new ArrayStruct());
$request = $this->createRequestWithWebhookData();
$response = $this->controller->executeWebhook($request, $context);
static::assertSame('Shopware token is invalid', $response->getContent());
static::assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
static::assertTrue(
$this->logger->hasRecordThatContains('Shopware token of webhook "{webhookId}" is invalid', Level::Warning),
'Expected invalid-shopware-token log entry not found',
);
}
public function testExecuteWebhookEmptyTokenSent(): void
{
$context = Context::createDefaultContext();
$request = new Request();
$response = $this->controller->executeWebhook($request, $context);
static::assertSame('Shopware token is invalid', $response->getContent());
static::assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
static::assertEquals([], $this->logger->getRecords());
}
public function testExecuteWebhookInvalidToken(): void
{
$context = Context::createDefaultContext();
$request = new Request(
[WebhookService::PAYPAL_WEBHOOK_TOKEN_NAME => 'invalid-token']
);
$response = $this->controller->executeWebhook($request, $context);
static::assertSame('Shopware token is invalid', $response->getContent());
static::assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
static::assertEquals([], $this->logger->getRecords());
}
public function testExecuteWebhookInvalidTokenWithId(): void
{
$context = Context::createDefaultContext();
$request = $this->createRequestWithWebhookData();
$request->query->set(WebhookService::PAYPAL_WEBHOOK_TOKEN_NAME, 'invalid-token');
$response = $this->controller->executeWebhook($request, $context);
static::assertSame('Shopware token is invalid', $response->getContent());
static::assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
static::assertTrue(
$this->logger->hasRecordThatContains('Shopware token of webhook "{webhookId}" is invalid', Level::Warning),
'Expected invalid-shopware-token log entry not found',
);
}
public function testExecuteWebhookNoData(): void
{
$context = Context::createDefaultContext();
$request = new Request(
[WebhookService::PAYPAL_WEBHOOK_TOKEN_NAME => WebhookServiceTest::ALREADY_EXISTING_WEBHOOK_EXECUTE_TOKEN]
);
$this->expectException(BadRequestHttpException::class);
$this->expectExceptionMessage('No webhook data sent');
$this->controller->executeWebhook($request, $context);
}
public function testExecuteWebhookHandlerNotFound(): void
{
$context = Context::createDefaultContext();
$context->addExtension(self::THROW_NOT_FOUND_EXCEPTION, new ArrayStruct());
$request = $this->createRequestWithWebhookData();
$response = $this->controller->executeWebhook($request, $context);
static::assertSame(Response::HTTP_OK, $response->getStatusCode());
static::assertTrue(
$this->logger->hasRecordThatContains('No webhook handler found', Level::Info),
'Expected "No webhook handler found" log entry not found',
);
}
private function createRequestWithWebhookData(): Request
{
return new Request(
[WebhookService::PAYPAL_WEBHOOK_TOKEN_NAME => WebhookServiceTest::ALREADY_EXISTING_WEBHOOK_EXECUTE_TOKEN],
WebhookDataFixture::get()
);
}
}