]> BookStack Code Mirror - bookstack/blob - tests/Actions/WebhookManagementTest.php
Added webhook_call_before theme event hook
[bookstack] / tests / Actions / WebhookManagementTest.php
1 <?php
2
3 namespace Tests\Actions;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\Webhook;
7 use Tests\TestCase;
8
9 class WebhookManagementTest extends TestCase
10 {
11     public function test_index_view()
12     {
13         $webhook = $this->newWebhook([
14             'name'     => 'My awesome webhook',
15             'endpoint' => 'https://example.com/donkey/webhook',
16         ], ['all']);
17
18         $resp = $this->asAdmin()->get('/settings/webhooks');
19         $resp->assertOk();
20         $resp->assertElementContains('a[href$="/settings/webhooks/create"]', 'Create New Webhook');
21         $resp->assertElementExists('a[href="' . $webhook->getUrl() . '"]', $webhook->name);
22         $resp->assertSee($webhook->endpoint);
23         $resp->assertSee('All system events');
24         $resp->assertSee('Active');
25     }
26
27     public function test_create_view()
28     {
29         $resp = $this->asAdmin()->get('/settings/webhooks/create');
30         $resp->assertOk();
31         $resp->assertSee('Create New Webhook');
32         $resp->assertElementContains('form[action$="/settings/webhooks/create"] button', 'Save Webhook');
33     }
34
35     public function test_store()
36     {
37         $resp = $this->asAdmin()->post('/settings/webhooks/create', [
38             'name'     => 'My first webhook',
39             'endpoint' => 'https://example.com/webhook',
40             'events'   => ['all'],
41             'active'   => 'true',
42         ]);
43
44         $resp->assertRedirect('/settings/webhooks');
45         $this->assertActivityExists(ActivityType::WEBHOOK_CREATE);
46
47         $resp = $this->followRedirects($resp);
48         $resp->assertSee('Webhook successfully created');
49
50         $this->assertDatabaseHas('webhooks', [
51             'name'     => 'My first webhook',
52             'endpoint' => 'https://example.com/webhook',
53             'active'   => true,
54         ]);
55
56         /** @var Webhook $webhook */
57         $webhook = Webhook::query()->where('name', '=', 'My first webhook')->first();
58         $this->assertDatabaseHas('webhook_tracked_events', [
59             'webhook_id' => $webhook->id,
60             'event'      => 'all',
61         ]);
62     }
63
64     public function test_edit_view()
65     {
66         $webhook = $this->newWebhook();
67
68         $resp = $this->asAdmin()->get('/settings/webhooks/' . $webhook->id);
69         $resp->assertOk();
70         $resp->assertSee('Edit Webhook');
71         $resp->assertElementContains('form[action="' . $webhook->getUrl() . '"] button', 'Save Webhook');
72         $resp->assertElementContains('a[href="' . $webhook->getUrl('/delete') . '"]', 'Delete Webhook');
73         $resp->assertElementExists('input[type="checkbox"][value="all"][name="events[]"]');
74     }
75
76     public function test_update()
77     {
78         $webhook = $this->newWebhook();
79
80         $resp = $this->asAdmin()->put('/settings/webhooks/' . $webhook->id, [
81             'name'     => 'My updated webhook',
82             'endpoint' => 'https://example.com/updated-webhook',
83             'events'   => [ActivityType::PAGE_CREATE, ActivityType::PAGE_UPDATE],
84             'active'   => 'true',
85         ]);
86         $resp->assertRedirect('/settings/webhooks');
87
88         $resp = $this->followRedirects($resp);
89         $resp->assertSee('Webhook successfully updated');
90
91         $this->assertDatabaseHas('webhooks', [
92             'id'       => $webhook->id,
93             'name'     => 'My updated webhook',
94             'endpoint' => 'https://example.com/updated-webhook',
95             'active'   => true,
96         ]);
97
98         $trackedEvents = $webhook->trackedEvents()->get();
99         $this->assertCount(2, $trackedEvents);
100         $this->assertEquals(['page_create', 'page_update'], $trackedEvents->pluck('event')->values()->all());
101
102         $this->assertActivityExists(ActivityType::WEBHOOK_UPDATE);
103     }
104
105     public function test_delete_view()
106     {
107         $webhook = $this->newWebhook(['name' => 'Webhook to delete']);
108
109         $resp = $this->asAdmin()->get('/settings/webhooks/' . $webhook->id . '/delete');
110         $resp->assertOk();
111         $resp->assertSee('Delete Webhook');
112         $resp->assertSee('This will fully delete this webhook, with the name \'Webhook to delete\', from the system.');
113         $resp->assertElementContains('form[action$="/settings/webhooks/' . $webhook->id . '"]', 'Delete');
114     }
115
116     public function test_destroy()
117     {
118         $webhook = $this->newWebhook();
119
120         $resp = $this->asAdmin()->delete('/settings/webhooks/' . $webhook->id);
121         $resp->assertRedirect('/settings/webhooks');
122
123         $resp = $this->followRedirects($resp);
124         $resp->assertSee('Webhook successfully deleted');
125
126         $this->assertDatabaseMissing('webhooks', ['id' => $webhook->id]);
127         $this->assertDatabaseMissing('webhook_tracked_events', ['webhook_id' => $webhook->id]);
128
129         $this->assertActivityExists(ActivityType::WEBHOOK_DELETE);
130     }
131
132     public function test_settings_manage_permission_required_for_webhook_routes()
133     {
134         $editor = $this->getEditor();
135         $this->actingAs($editor);
136
137         $routes = [
138             ['GET', '/settings/webhooks'],
139             ['GET', '/settings/webhooks/create'],
140             ['POST', '/settings/webhooks/create'],
141             ['GET', '/settings/webhooks/1'],
142             ['PUT', '/settings/webhooks/1'],
143             ['DELETE', '/settings/webhooks/1'],
144             ['GET', '/settings/webhooks/1/delete'],
145         ];
146
147         foreach ($routes as [$method, $endpoint]) {
148             $resp = $this->call($method, $endpoint);
149             $this->assertPermissionError($resp);
150         }
151
152         $this->giveUserPermissions($editor, ['settings-manage']);
153
154         foreach ($routes as [$method, $endpoint]) {
155             $resp = $this->call($method, $endpoint);
156             $this->assertNotPermissionError($resp);
157         }
158     }
159
160     protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
161     {
162         /** @var Webhook $webhook */
163         $webhook = Webhook::factory()->create($attrs);
164
165         foreach ($events as $event) {
166             $webhook->trackedEvents()->create(['event' => $event]);
167         }
168
169         return $webhook;
170     }
171 }