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