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
19 public function test_webhook_listening_to_all_called_on_event()
21 $this->newWebhook([], ['all']);
23 $this->runEvent(ActivityType::ROLE_CREATE);
24 Bus::assertDispatched(DispatchWebhookJob::class);
27 public function test_webhook_listening_to_specific_event_called_on_event()
29 $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
31 $this->runEvent(ActivityType::ROLE_UPDATE);
32 Bus::assertDispatched(DispatchWebhookJob::class);
35 public function test_webhook_listening_to_specific_event_not_called_on_other_event()
37 $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
39 $this->runEvent(ActivityType::ROLE_CREATE);
40 Bus::assertNotDispatched(DispatchWebhookJob::class);
43 public function test_inactive_webhook_not_called_on_event()
45 $this->newWebhook(['active' => false], ['all']);
47 $this->runEvent(ActivityType::ROLE_CREATE);
48 Bus::assertNotDispatched(DispatchWebhookJob::class);
51 public function test_failed_webhook_call_logs_error()
53 $logger = $this->withTestLogger();
55 '*' => Http::response('', 500),
57 $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
59 $this->runEvent(ActivityType::ROLE_CREATE);
61 $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with status 500'));
64 public function test_webhook_call_data_format()
67 '*' => Http::response('', 200),
69 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
70 /** @var Page $page */
71 $page = Page::query()->first();
72 $editor = $this->getEditor();
74 $this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor);
76 Http::assertSent(function(Request $request) use ($editor, $page, $webhook) {
77 $reqData = $request->data();
78 return $request->isJson()
79 && $reqData['event'] === 'page_update'
80 && $reqData['text'] === ($editor->name . ' updated page "' . $page->name . '"')
81 && is_string($reqData['triggered_at'])
82 && $reqData['triggered_by']['name'] === $editor->name
83 && $reqData['triggered_by_profile_url'] === $editor->getProfileUrl()
84 && $reqData['webhook_id'] === $webhook->id
85 && $reqData['webhook_name'] === $webhook->name
86 && $reqData['url'] === $page->getUrl()
87 && $reqData['related_item']['name'] === $page->name;
92 protected function runEvent(string $event, $detail = '', ?User $user = null)
95 $user = $this->getEditor();
98 $this->actingAs($user);
100 $activityLogger = $this->app->make(ActivityLogger::class);
101 $activityLogger->add($event, $detail);
104 protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
106 /** @var Webhook $webhook */
107 $webhook = Webhook::factory()->create($attrs);
109 foreach ($events as $event) {
110 $webhook->trackedEvents()->create(['event' => $event]);