X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/f27d0d5aebf4bc88a72e6769c2d6b23b6bb2ceb9..refs/pull/4467/head:/tests/Actions/WebhookCallTest.php diff --git a/tests/Actions/WebhookCallTest.php b/tests/Actions/WebhookCallTest.php index 958d33d9d..fc49a524e 100644 --- a/tests/Actions/WebhookCallTest.php +++ b/tests/Actions/WebhookCallTest.php @@ -2,12 +2,13 @@ namespace Tests\Actions; -use BookStack\Actions\ActivityLogger; -use BookStack\Actions\ActivityType; -use BookStack\Actions\DispatchWebhookJob; -use BookStack\Actions\Webhook; -use BookStack\Auth\User; -use BookStack\Entities\Models\Page; +use BookStack\Activity\ActivityType; +use BookStack\Activity\DispatchWebhookJob; +use BookStack\Activity\Models\Webhook; +use BookStack\Activity\Tools\ActivityLogger; +use BookStack\Api\ApiToken; +use BookStack\Entities\Models\PageRevision; +use BookStack\Users\Models\User; use Illuminate\Http\Client\Request; use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Http; @@ -15,7 +16,6 @@ use Tests\TestCase; class WebhookCallTest extends TestCase { - public function test_webhook_listening_to_all_called_on_event() { $this->newWebhook([], ['all']); @@ -48,17 +48,57 @@ class WebhookCallTest extends TestCase Bus::assertNotDispatched(DispatchWebhookJob::class); } + public function test_webhook_runs_for_delete_actions() + { + $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']); + Http::fake([ + '*' => Http::response('', 500), + ]); + + $user = $this->users->newUser(); + $resp = $this->asAdmin()->delete($user->getEditUrl()); + $resp->assertRedirect('/settings/users'); + + /** @var ApiToken $apiToken */ + $editor = $this->users->editor(); + $apiToken = ApiToken::factory()->create(['user_id' => $editor]); + $resp = $this->delete($editor->getEditUrl('/api-tokens/' . $apiToken->id)); + $resp->assertRedirect($editor->getEditUrl('#api_tokens')); + } + public function test_failed_webhook_call_logs_error() { $logger = $this->withTestLogger(); Http::fake([ '*' => Http::response('', 500), ]); - $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']); + $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']); + $this->assertNull($webhook->last_errored_at); $this->runEvent(ActivityType::ROLE_CREATE); $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with status 500')); + + $webhook->refresh(); + $this->assertEquals('Response status from endpoint was 500', $webhook->last_error); + $this->assertNotNull($webhook->last_errored_at); + } + + public function test_webhook_call_exception_is_caught_and_logged() + { + Http::shouldReceive('asJson')->andThrow(new \Exception('Failed to perform request')); + + $logger = $this->withTestLogger(); + $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']); + $this->assertNull($webhook->last_errored_at); + + $this->runEvent(ActivityType::ROLE_CREATE); + + $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with error "Failed to perform request"')); + + $webhook->refresh(); + $this->assertEquals('Failed to perform request', $webhook->last_error); + $this->assertNotNull($webhook->last_errored_at); } public function test_webhook_call_data_format() @@ -67,14 +107,14 @@ class WebhookCallTest extends TestCase '*' => Http::response('', 200), ]); $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']); - /** @var Page $page */ - $page = Page::query()->first(); - $editor = $this->getEditor(); + $page = $this->entities->page(); + $editor = $this->users->editor(); $this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor); - Http::assertSent(function(Request $request) use ($editor, $page, $webhook) { + Http::assertSent(function (Request $request) use ($editor, $page, $webhook) { $reqData = $request->data(); + return $request->isJson() && $reqData['event'] === 'page_update' && $reqData['text'] === ($editor->name . ' updated page "' . $page->name . '"') @@ -88,11 +128,10 @@ class WebhookCallTest extends TestCase }); } - protected function runEvent(string $event, $detail = '', ?User $user = null) { if (is_null($user)) { - $user = $this->getEditor(); + $user = $this->users->editor(); } $this->actingAs($user); @@ -101,7 +140,7 @@ class WebhookCallTest extends TestCase $activityLogger->add($event, $detail); } - protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook + protected function newWebhook(array $attrs, array $events): Webhook { /** @var Webhook $webhook */ $webhook = Webhook::factory()->create($attrs); @@ -112,5 +151,4 @@ class WebhookCallTest extends TestCase return $webhook; } - -} \ No newline at end of file +}