3 namespace Tests\Actions;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\DispatchWebhookJob;
7 use BookStack\Activity\Models\Webhook;
8 use BookStack\Activity\Tools\ActivityLogger;
9 use BookStack\Api\ApiToken;
10 use BookStack\Entities\Models\PageRevision;
11 use BookStack\Users\Models\User;
12 use Illuminate\Http\Client\Request;
13 use Illuminate\Support\Facades\Bus;
14 use Illuminate\Support\Facades\Http;
17 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_webhook_runs_for_delete_actions()
53 $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
55 '*' => Http::response('', 500),
58 $user = $this->users->newUser();
59 $resp = $this->asAdmin()->delete($user->getEditUrl());
60 $resp->assertRedirect('/settings/users');
62 /** @var ApiToken $apiToken */
63 $editor = $this->users->editor();
64 $apiToken = ApiToken::factory()->create(['user_id' => $editor]);
65 $resp = $this->delete($editor->getEditUrl('/api-tokens/' . $apiToken->id));
66 $resp->assertRedirect($editor->getEditUrl('#api_tokens'));
69 public function test_failed_webhook_call_logs_error()
71 $logger = $this->withTestLogger();
73 '*' => Http::response('', 500),
75 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
76 $this->assertNull($webhook->last_errored_at);
78 $this->runEvent(ActivityType::ROLE_CREATE);
80 $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with status 500'));
83 $this->assertEquals('Response status from endpoint was 500', $webhook->last_error);
84 $this->assertNotNull($webhook->last_errored_at);
87 public function test_webhook_call_exception_is_caught_and_logged()
89 Http::shouldReceive('asJson')->andThrow(new \Exception('Failed to perform request'));
91 $logger = $this->withTestLogger();
92 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
93 $this->assertNull($webhook->last_errored_at);
95 $this->runEvent(ActivityType::ROLE_CREATE);
97 $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with error "Failed to perform request"'));
100 $this->assertEquals('Failed to perform request', $webhook->last_error);
101 $this->assertNotNull($webhook->last_errored_at);
104 public function test_webhook_uses_ssr_hosts_option_if_set()
106 config()->set('app.ssr_hosts', 'https://*.example.com');
107 $http = Http::fake();
109 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.co.uk'], ['all']);
110 $this->runEvent(ActivityType::ROLE_CREATE);
111 $http->assertNothingSent();
114 $this->assertEquals('The URL does not match the configured allowed SSR hosts', $webhook->last_error);
115 $this->assertNotNull($webhook->last_errored_at);
118 public function test_webhook_call_data_format()
121 '*' => Http::response('', 200),
123 $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
124 $page = $this->entities->page();
125 $editor = $this->users->editor();
127 $this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor);
129 Http::assertSent(function (Request $request) use ($editor, $page, $webhook) {
130 $reqData = $request->data();
132 return $request->isJson()
133 && $reqData['event'] === 'page_update'
134 && $reqData['text'] === ($editor->name . ' updated page "' . $page->name . '"')
135 && is_string($reqData['triggered_at'])
136 && $reqData['triggered_by']['name'] === $editor->name
137 && $reqData['triggered_by_profile_url'] === $editor->getProfileUrl()
138 && $reqData['webhook_id'] === $webhook->id
139 && $reqData['webhook_name'] === $webhook->name
140 && $reqData['url'] === $page->getUrl()
141 && $reqData['related_item']['name'] === $page->name;
145 protected function runEvent(string $event, $detail = '', ?User $user = null)
147 if (is_null($user)) {
148 $user = $this->users->editor();
151 $this->actingAs($user);
153 $activityLogger = $this->app->make(ActivityLogger::class);
154 $activityLogger->add($event, $detail);
157 protected function newWebhook(array $attrs, array $events): Webhook
159 /** @var Webhook $webhook */
160 $webhook = Webhook::factory()->create($attrs);
162 foreach ($events as $event) {
163 $webhook->trackedEvents()->create(['event' => $event]);