]> BookStack Code Mirror - bookstack/blob - tests/Actions/WebhookCallTest.php
Aligned notification capitalisation
[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 BookStack\Entities\Models\Page;
11 use Illuminate\Http\Client\Request;
12 use Illuminate\Support\Facades\Bus;
13 use Illuminate\Support\Facades\Http;
14 use Tests\TestCase;
15
16 class WebhookCallTest extends TestCase
17 {
18
19     public function test_webhook_listening_to_all_called_on_event()
20     {
21         $this->newWebhook([], ['all']);
22         Bus::fake();
23         $this->runEvent(ActivityType::ROLE_CREATE);
24         Bus::assertDispatched(DispatchWebhookJob::class);
25     }
26
27     public function test_webhook_listening_to_specific_event_called_on_event()
28     {
29         $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
30         Bus::fake();
31         $this->runEvent(ActivityType::ROLE_UPDATE);
32         Bus::assertDispatched(DispatchWebhookJob::class);
33     }
34
35     public function test_webhook_listening_to_specific_event_not_called_on_other_event()
36     {
37         $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
38         Bus::fake();
39         $this->runEvent(ActivityType::ROLE_CREATE);
40         Bus::assertNotDispatched(DispatchWebhookJob::class);
41     }
42
43     public function test_inactive_webhook_not_called_on_event()
44     {
45         $this->newWebhook(['active' => false], ['all']);
46         Bus::fake();
47         $this->runEvent(ActivityType::ROLE_CREATE);
48         Bus::assertNotDispatched(DispatchWebhookJob::class);
49     }
50
51     public function test_failed_webhook_call_logs_error()
52     {
53         $logger = $this->withTestLogger();
54         Http::fake([
55             '*' => Http::response('', 500),
56         ]);
57         $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
58
59         $this->runEvent(ActivityType::ROLE_CREATE);
60
61         $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with status 500'));
62     }
63
64     public function test_webhook_call_data_format()
65     {
66         Http::fake([
67             '*' => Http::response('', 200),
68         ]);
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();
73
74         $this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor);
75
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;
88         });
89     }
90
91
92     protected function runEvent(string $event, $detail = '', ?User $user = null)
93     {
94         if (is_null($user)) {
95             $user = $this->getEditor();
96         }
97
98         $this->actingAs($user);
99
100         $activityLogger = $this->app->make(ActivityLogger::class);
101         $activityLogger->add($event, $detail);
102     }
103
104     protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
105     {
106         /** @var Webhook $webhook */
107         $webhook = Webhook::factory()->create($attrs);
108
109         foreach ($events as $event) {
110             $webhook->trackedEvents()->create(['event' => $event]);
111         }
112
113         return $webhook;
114     }
115
116 }