]> BookStack Code Mirror - bookstack/blob - tests/Actions/WebhookCallTest.php
Added copy considerations
[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     public function test_webhook_listening_to_all_called_on_event()
19     {
20         $this->newWebhook([], ['all']);
21         Bus::fake();
22         $this->runEvent(ActivityType::ROLE_CREATE);
23         Bus::assertDispatched(DispatchWebhookJob::class);
24     }
25
26     public function test_webhook_listening_to_specific_event_called_on_event()
27     {
28         $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
29         Bus::fake();
30         $this->runEvent(ActivityType::ROLE_UPDATE);
31         Bus::assertDispatched(DispatchWebhookJob::class);
32     }
33
34     public function test_webhook_listening_to_specific_event_not_called_on_other_event()
35     {
36         $this->newWebhook([], [ActivityType::ROLE_UPDATE]);
37         Bus::fake();
38         $this->runEvent(ActivityType::ROLE_CREATE);
39         Bus::assertNotDispatched(DispatchWebhookJob::class);
40     }
41
42     public function test_inactive_webhook_not_called_on_event()
43     {
44         $this->newWebhook(['active' => false], ['all']);
45         Bus::fake();
46         $this->runEvent(ActivityType::ROLE_CREATE);
47         Bus::assertNotDispatched(DispatchWebhookJob::class);
48     }
49
50     public function test_failed_webhook_call_logs_error()
51     {
52         $logger = $this->withTestLogger();
53         Http::fake([
54             '*' => Http::response('', 500),
55         ]);
56         $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
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
63     public function test_webhook_call_data_format()
64     {
65         Http::fake([
66             '*' => Http::response('', 200),
67         ]);
68         $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
69         /** @var Page $page */
70         $page = Page::query()->first();
71         $editor = $this->getEditor();
72
73         $this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor);
74
75         Http::assertSent(function (Request $request) use ($editor, $page, $webhook) {
76             $reqData = $request->data();
77
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     protected function runEvent(string $event, $detail = '', ?User $user = null)
92     {
93         if (is_null($user)) {
94             $user = $this->getEditor();
95         }
96
97         $this->actingAs($user);
98
99         $activityLogger = $this->app->make(ActivityLogger::class);
100         $activityLogger->add($event, $detail);
101     }
102
103     protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
104     {
105         /** @var Webhook $webhook */
106         $webhook = Webhook::factory()->create($attrs);
107
108         foreach ($events as $event) {
109             $webhook->trackedEvents()->create(['event' => $event]);
110         }
111
112         return $webhook;
113     }
114 }