2021-12-12 14:01:50 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Actions;
|
|
|
|
|
|
|
|
use BookStack\Actions\ActivityLogger;
|
|
|
|
use BookStack\Actions\ActivityType;
|
|
|
|
use BookStack\Actions\DispatchWebhookJob;
|
|
|
|
use BookStack\Actions\Webhook;
|
|
|
|
use BookStack\Auth\User;
|
|
|
|
use Illuminate\Http\Client\Request;
|
|
|
|
use Illuminate\Support\Facades\Bus;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class WebhookCallTest extends TestCase
|
|
|
|
{
|
|
|
|
public function test_webhook_listening_to_all_called_on_event()
|
|
|
|
{
|
|
|
|
$this->newWebhook([], ['all']);
|
|
|
|
Bus::fake();
|
|
|
|
$this->runEvent(ActivityType::ROLE_CREATE);
|
|
|
|
Bus::assertDispatched(DispatchWebhookJob::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_webhook_listening_to_specific_event_called_on_event()
|
|
|
|
{
|
|
|
|
$this->newWebhook([], [ActivityType::ROLE_UPDATE]);
|
|
|
|
Bus::fake();
|
|
|
|
$this->runEvent(ActivityType::ROLE_UPDATE);
|
|
|
|
Bus::assertDispatched(DispatchWebhookJob::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_webhook_listening_to_specific_event_not_called_on_other_event()
|
|
|
|
{
|
|
|
|
$this->newWebhook([], [ActivityType::ROLE_UPDATE]);
|
|
|
|
Bus::fake();
|
|
|
|
$this->runEvent(ActivityType::ROLE_CREATE);
|
|
|
|
Bus::assertNotDispatched(DispatchWebhookJob::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_inactive_webhook_not_called_on_event()
|
|
|
|
{
|
|
|
|
$this->newWebhook(['active' => false], ['all']);
|
|
|
|
Bus::fake();
|
|
|
|
$this->runEvent(ActivityType::ROLE_CREATE);
|
|
|
|
Bus::assertNotDispatched(DispatchWebhookJob::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_failed_webhook_call_logs_error()
|
|
|
|
{
|
|
|
|
$logger = $this->withTestLogger();
|
|
|
|
Http::fake([
|
|
|
|
'*' => Http::response('', 500),
|
|
|
|
]);
|
2022-01-03 14:42:48 -05:00
|
|
|
$webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
|
|
|
|
$this->assertNull($webhook->last_errored_at);
|
2021-12-12 14:01:50 -05:00
|
|
|
|
|
|
|
$this->runEvent(ActivityType::ROLE_CREATE);
|
|
|
|
|
|
|
|
$this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with status 500'));
|
2022-01-03 14:42:48 -05:00
|
|
|
|
|
|
|
$webhook->refresh();
|
|
|
|
$this->assertEquals('Response status from endpoint was 500', $webhook->last_error);
|
|
|
|
$this->assertNotNull($webhook->last_errored_at);
|
2021-12-12 14:01:50 -05:00
|
|
|
}
|
|
|
|
|
2022-01-03 13:37:05 -05:00
|
|
|
public function test_webhook_call_exception_is_caught_and_logged()
|
|
|
|
{
|
|
|
|
Http::shouldReceive('asJson')->andThrow(new \Exception('Failed to perform request'));
|
|
|
|
|
|
|
|
$logger = $this->withTestLogger();
|
2022-01-03 14:42:48 -05:00
|
|
|
$webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
|
|
|
|
$this->assertNull($webhook->last_errored_at);
|
2022-01-03 13:37:05 -05:00
|
|
|
|
|
|
|
$this->runEvent(ActivityType::ROLE_CREATE);
|
|
|
|
|
|
|
|
$this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with error "Failed to perform request"'));
|
2022-01-03 14:42:48 -05:00
|
|
|
|
|
|
|
$webhook->refresh();
|
|
|
|
$this->assertEquals('Failed to perform request', $webhook->last_error);
|
|
|
|
$this->assertNotNull($webhook->last_errored_at);
|
2022-01-03 13:37:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-12 14:01:50 -05:00
|
|
|
public function test_webhook_call_data_format()
|
|
|
|
{
|
|
|
|
Http::fake([
|
|
|
|
'*' => Http::response('', 200),
|
|
|
|
]);
|
|
|
|
$webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2023-01-21 06:08:34 -05:00
|
|
|
$editor = $this->users->editor();
|
2021-12-12 14:01:50 -05:00
|
|
|
|
|
|
|
$this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor);
|
|
|
|
|
2021-12-18 06:43:05 -05:00
|
|
|
Http::assertSent(function (Request $request) use ($editor, $page, $webhook) {
|
2021-12-12 14:01:50 -05:00
|
|
|
$reqData = $request->data();
|
2021-12-18 06:43:05 -05:00
|
|
|
|
2021-12-12 14:01:50 -05:00
|
|
|
return $request->isJson()
|
|
|
|
&& $reqData['event'] === 'page_update'
|
|
|
|
&& $reqData['text'] === ($editor->name . ' updated page "' . $page->name . '"')
|
|
|
|
&& is_string($reqData['triggered_at'])
|
|
|
|
&& $reqData['triggered_by']['name'] === $editor->name
|
|
|
|
&& $reqData['triggered_by_profile_url'] === $editor->getProfileUrl()
|
|
|
|
&& $reqData['webhook_id'] === $webhook->id
|
|
|
|
&& $reqData['webhook_name'] === $webhook->name
|
|
|
|
&& $reqData['url'] === $page->getUrl()
|
|
|
|
&& $reqData['related_item']['name'] === $page->name;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function runEvent(string $event, $detail = '', ?User $user = null)
|
|
|
|
{
|
|
|
|
if (is_null($user)) {
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->editor();
|
2021-12-12 14:01:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->actingAs($user);
|
|
|
|
|
|
|
|
$activityLogger = $this->app->make(ActivityLogger::class);
|
|
|
|
$activityLogger->add($event, $detail);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
|
|
|
|
{
|
|
|
|
/** @var Webhook $webhook */
|
|
|
|
$webhook = Webhook::factory()->create($attrs);
|
|
|
|
|
|
|
|
foreach ($events as $event) {
|
|
|
|
$webhook->trackedEvents()->create(['event' => $event]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $webhook;
|
|
|
|
}
|
2021-12-18 06:43:05 -05:00
|
|
|
}
|