]> BookStack Code Mirror - bookstack/blob - tests/Actions/WebhookCallTest.php
7ca190200eb9286aad40fd34d2ecc2f77ada71cb
[bookstack] / tests / Actions / WebhookCallTest.php
1 <?php
2
3 namespace Tests\Actions;
4
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;
13 use Tests\TestCase;
14
15 class WebhookCallTest extends TestCase
16 {
17     public function test_webhook_listening_to_all_called_on_event()
18     {
19         $this->newWebhook([], ['all']);
20         Bus::fake();
21         $this->runEvent(ActivityType::ROLE_CREATE);
22         Bus::assertDispatched(DispatchWebhookJob::class);
23     }
24
25     public function test_webhook_listening_to_specific_event_called_on_event()
26     {
27         $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
28         Bus::fake();
29         $this->runEvent(ActivityType::ROLE_UPDATE);
30         Bus::assertDispatched(DispatchWebhookJob::class);
31     }
32
33     public function test_webhook_listening_to_specific_event_not_called_on_other_event()
34     {
35         $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
36         Bus::fake();
37         $this->runEvent(ActivityType::ROLE_CREATE);
38         Bus::assertNotDispatched(DispatchWebhookJob::class);
39     }
40
41     public function test_inactive_webhook_not_called_on_event()
42     {
43         $this->newWebhook(['active' => false], ['all']);
44         Bus::fake();
45         $this->runEvent(ActivityType::ROLE_CREATE);
46         Bus::assertNotDispatched(DispatchWebhookJob::class);
47     }
48
49     public function test_failed_webhook_call_logs_error()
50     {
51         $logger = $this->withTestLogger();
52         Http::fake([
53             '*' => Http::response('', 500),
54         ]);
55         $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
56         $this->assertNull($webhook->last_errored_at);
57
58         $this->runEvent(ActivityType::ROLE_CREATE);
59
60         $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with status 500'));
61
62         $webhook->refresh();
63         $this->assertEquals('Response status from endpoint was 500', $webhook->last_error);
64         $this->assertNotNull($webhook->last_errored_at);
65     }
66
67     public function test_webhook_call_exception_is_caught_and_logged()
68     {
69         Http::shouldReceive('asJson')->andThrow(new \Exception('Failed to perform request'));
70
71         $logger = $this->withTestLogger();
72         $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
73         $this->assertNull($webhook->last_errored_at);
74
75         $this->runEvent(ActivityType::ROLE_CREATE);
76
77         $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with error "Failed to perform request"'));
78
79         $webhook->refresh();
80         $this->assertEquals('Failed to perform request', $webhook->last_error);
81         $this->assertNotNull($webhook->last_errored_at);
82     }
83
84     public function test_webhook_call_data_format()
85     {
86         Http::fake([
87             '*' => Http::response('', 200),
88         ]);
89         $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
90         $page = $this->entities->page();
91         $editor = $this->getEditor();
92
93         $this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor);
94
95         Http::assertSent(function (Request $request) use ($editor, $page, $webhook) {
96             $reqData = $request->data();
97
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;
108         });
109     }
110
111     protected function runEvent(string $event, $detail = '', ?User $user = null)
112     {
113         if (is_null($user)) {
114             $user = $this->getEditor();
115         }
116
117         $this->actingAs($user);
118
119         $activityLogger = $this->app->make(ActivityLogger::class);
120         $activityLogger->add($event, $detail);
121     }
122
123     protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
124     {
125         /** @var Webhook $webhook */
126         $webhook = Webhook::factory()->create($attrs);
127
128         foreach ($events as $event) {
129             $webhook->trackedEvents()->create(['event' => $event]);
130         }
131
132         return $webhook;
133     }
134 }