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 BookStack\Entities\Models\Page;
11 use Illuminate\Http\Client\Request;
12 use Illuminate\Support\Facades\Bus;
13 use Illuminate\Support\Facades\Http;
16 class WebhookCallTest extends TestCase
18 public function test_webhook_listening_to_all_called_on_event()
20 $this->newWebhook([], ['all']);
22 $this->runEvent(ActivityType::ROLE_CREATE);
23 Bus::assertDispatched(DispatchWebhookJob::class);
26 public function test_webhook_listening_to_specific_event_called_on_event()
28 $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
30 $this->runEvent(ActivityType::ROLE_UPDATE);
31 Bus::assertDispatched(DispatchWebhookJob::class);
34 public function test_webhook_listening_to_specific_event_not_called_on_other_event()
36 $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
38 $this->runEvent(ActivityType::ROLE_CREATE);
39 Bus::assertNotDispatched(DispatchWebhookJob::class);
42 public function test_inactive_webhook_not_called_on_event()
44 $this->newWebhook(['active' => false], ['all']);
46 $this->runEvent(ActivityType::ROLE_CREATE);
47 Bus::assertNotDispatched(DispatchWebhookJob::class);
50 public function test_failed_webhook_call_logs_error()
52 $logger = $this->withTestLogger();
54 '*' => Http::response('', 500),
56 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
57 $this->assertNull($webhook->last_errored_at);
59 $this->runEvent(ActivityType::ROLE_CREATE);
61 $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with status 500'));
64 $this->assertEquals('Response status from endpoint was 500', $webhook->last_error);
65 $this->assertNotNull($webhook->last_errored_at);
68 public function test_webhook_call_exception_is_caught_and_logged()
70 Http::shouldReceive('asJson')->andThrow(new \Exception('Failed to perform request'));
72 $logger = $this->withTestLogger();
73 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
74 $this->assertNull($webhook->last_errored_at);
76 $this->runEvent(ActivityType::ROLE_CREATE);
78 $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with error "Failed to perform request"'));
81 $this->assertEquals('Failed to perform request', $webhook->last_error);
82 $this->assertNotNull($webhook->last_errored_at);
85 public function test_webhook_call_data_format()
88 '*' => Http::response('', 200),
90 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
91 /** @var Page $page */
92 $page = Page::query()->first();
93 $editor = $this->getEditor();
95 $this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor);
97 Http::assertSent(function (Request $request) use ($editor, $page, $webhook) {
98 $reqData = $request->data();
100 return $request->isJson()
101 && $reqData['event'] === 'page_update'
102 && $reqData['text'] === ($editor->name . ' updated page "' . $page->name . '"')
103 && is_string($reqData['triggered_at'])
104 && $reqData['triggered_by']['name'] === $editor->name
105 && $reqData['triggered_by_profile_url'] === $editor->getProfileUrl()
106 && $reqData['webhook_id'] === $webhook->id
107 && $reqData['webhook_name'] === $webhook->name
108 && $reqData['url'] === $page->getUrl()
109 && $reqData['related_item']['name'] === $page->name;
113 protected function runEvent(string $event, $detail = '', ?User $user = null)
115 if (is_null($user)) {
116 $user = $this->getEditor();
119 $this->actingAs($user);
121 $activityLogger = $this->app->make(ActivityLogger::class);
122 $activityLogger->add($event, $detail);
125 protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
127 /** @var Webhook $webhook */
128 $webhook = Webhook::factory()->create($attrs);
130 foreach ($events as $event) {
131 $webhook->trackedEvents()->create(['event' => $event]);