3 namespace Tests\Actions;
5 use BookStack\Actions\ActivityLogger;
6 use BookStack\Actions\ActivityType;
7 use BookStack\Actions\DispatchWebhookJob;
8 use BookStack\Actions\Webhook;
9 use BookStack\Auth\User;
10 use Illuminate\Http\Client\Request;
11 use Illuminate\Support\Facades\Bus;
12 use Illuminate\Support\Facades\Http;
15 class WebhookCallTest extends TestCase
17 public function test_webhook_listening_to_all_called_on_event()
19 $this->newWebhook([], ['all']);
21 $this->runEvent(ActivityType::ROLE_CREATE);
22 Bus::assertDispatched(DispatchWebhookJob::class);
25 public function test_webhook_listening_to_specific_event_called_on_event()
27 $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
29 $this->runEvent(ActivityType::ROLE_UPDATE);
30 Bus::assertDispatched(DispatchWebhookJob::class);
33 public function test_webhook_listening_to_specific_event_not_called_on_other_event()
35 $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
37 $this->runEvent(ActivityType::ROLE_CREATE);
38 Bus::assertNotDispatched(DispatchWebhookJob::class);
41 public function test_inactive_webhook_not_called_on_event()
43 $this->newWebhook(['active' => false], ['all']);
45 $this->runEvent(ActivityType::ROLE_CREATE);
46 Bus::assertNotDispatched(DispatchWebhookJob::class);
49 public function test_failed_webhook_call_logs_error()
51 $logger = $this->withTestLogger();
53 '*' => Http::response('', 500),
55 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
56 $this->assertNull($webhook->last_errored_at);
58 $this->runEvent(ActivityType::ROLE_CREATE);
60 $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with status 500'));
63 $this->assertEquals('Response status from endpoint was 500', $webhook->last_error);
64 $this->assertNotNull($webhook->last_errored_at);
67 public function test_webhook_call_exception_is_caught_and_logged()
69 Http::shouldReceive('asJson')->andThrow(new \Exception('Failed to perform request'));
71 $logger = $this->withTestLogger();
72 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
73 $this->assertNull($webhook->last_errored_at);
75 $this->runEvent(ActivityType::ROLE_CREATE);
77 $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with error "Failed to perform request"'));
80 $this->assertEquals('Failed to perform request', $webhook->last_error);
81 $this->assertNotNull($webhook->last_errored_at);
84 public function test_webhook_call_data_format()
87 '*' => Http::response('', 200),
89 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
90 $page = $this->entities->page();
91 $editor = $this->getEditor();
93 $this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor);
95 Http::assertSent(function (Request $request) use ($editor, $page, $webhook) {
96 $reqData = $request->data();
98 return $request->isJson()
99 && $reqData['event'] === 'page_update'
100 && $reqData['text'] === ($editor->name . ' updated page "' . $page->name . '"')
101 && is_string($reqData['triggered_at'])
102 && $reqData['triggered_by']['name'] === $editor->name
103 && $reqData['triggered_by_profile_url'] === $editor->getProfileUrl()
104 && $reqData['webhook_id'] === $webhook->id
105 && $reqData['webhook_name'] === $webhook->name
106 && $reqData['url'] === $page->getUrl()
107 && $reqData['related_item']['name'] === $page->name;
111 protected function runEvent(string $event, $detail = '', ?User $user = null)
113 if (is_null($user)) {
114 $user = $this->getEditor();
117 $this->actingAs($user);
119 $activityLogger = $this->app->make(ActivityLogger::class);
120 $activityLogger->add($event, $detail);
123 protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
125 /** @var Webhook $webhook */
126 $webhook = Webhook::factory()->create($attrs);
128 foreach ($events as $event) {
129 $webhook->trackedEvents()->create(['event' => $event]);