]> BookStack Code Mirror - bookstack/blob - tests/Activity/WebhookManagementTest.php
fix tests namespace definition
[bookstack] / tests / Activity / WebhookManagementTest.php
1 <?php
2
3 namespace Tests\Activity;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\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         $this->withHtml($resp)->assertElementContains('a[href$="/settings/webhooks/create"]', 'Create New Webhook');
21         $this->withHtml($resp)->assertElementContains('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         $this->withHtml($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             'timeout'  => 4,
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             'timeout'  => 4,
56         ]);
57
58         /** @var Webhook $webhook */
59         $webhook = Webhook::query()->where('name', '=', 'My first webhook')->first();
60         $this->assertDatabaseHas('webhook_tracked_events', [
61             'webhook_id' => $webhook->id,
62             'event'      => 'all',
63         ]);
64     }
65
66     public function test_edit_view()
67     {
68         $webhook = $this->newWebhook();
69
70         $resp = $this->asAdmin()->get('/settings/webhooks/' . $webhook->id);
71         $resp->assertOk();
72         $resp->assertSee('Edit Webhook');
73         $this->withHtml($resp)->assertElementContains('form[action="' . $webhook->getUrl() . '"] button', 'Save Webhook');
74         $this->withHtml($resp)->assertElementContains('a[href="' . $webhook->getUrl('/delete') . '"]', 'Delete Webhook');
75         $this->withHtml($resp)->assertElementExists('input[type="checkbox"][value="all"][name="events[]"]');
76     }
77
78     public function test_update()
79     {
80         $webhook = $this->newWebhook();
81
82         $resp = $this->asAdmin()->put('/settings/webhooks/' . $webhook->id, [
83             'name'     => 'My updated webhook',
84             'endpoint' => 'https://example.com/updated-webhook',
85             'events'   => [ActivityType::PAGE_CREATE, ActivityType::PAGE_UPDATE],
86             'active'   => 'true',
87             'timeout'  => 5,
88         ]);
89         $resp->assertRedirect('/settings/webhooks');
90
91         $resp = $this->followRedirects($resp);
92         $resp->assertSee('Webhook successfully updated');
93
94         $this->assertDatabaseHas('webhooks', [
95             'id'       => $webhook->id,
96             'name'     => 'My updated webhook',
97             'endpoint' => 'https://example.com/updated-webhook',
98             'active'   => true,
99             'timeout'  => 5,
100         ]);
101
102         $trackedEvents = $webhook->trackedEvents()->get();
103         $this->assertCount(2, $trackedEvents);
104         $this->assertEquals(['page_create', 'page_update'], $trackedEvents->pluck('event')->values()->all());
105
106         $this->assertActivityExists(ActivityType::WEBHOOK_UPDATE);
107     }
108
109     public function test_delete_view()
110     {
111         $webhook = $this->newWebhook(['name' => 'Webhook to delete']);
112
113         $resp = $this->asAdmin()->get('/settings/webhooks/' . $webhook->id . '/delete');
114         $resp->assertOk();
115         $resp->assertSee('Delete Webhook');
116         $resp->assertSee('This will fully delete this webhook, with the name \'Webhook to delete\', from the system.');
117         $this->withHtml($resp)->assertElementContains('form[action$="/settings/webhooks/' . $webhook->id . '"]', 'Delete');
118     }
119
120     public function test_destroy()
121     {
122         $webhook = $this->newWebhook();
123
124         $resp = $this->asAdmin()->delete('/settings/webhooks/' . $webhook->id);
125         $resp->assertRedirect('/settings/webhooks');
126
127         $resp = $this->followRedirects($resp);
128         $resp->assertSee('Webhook successfully deleted');
129
130         $this->assertDatabaseMissing('webhooks', ['id' => $webhook->id]);
131         $this->assertDatabaseMissing('webhook_tracked_events', ['webhook_id' => $webhook->id]);
132
133         $this->assertActivityExists(ActivityType::WEBHOOK_DELETE);
134     }
135
136     public function test_settings_manage_permission_required_for_webhook_routes()
137     {
138         $editor = $this->users->editor();
139         $this->actingAs($editor);
140
141         $routes = [
142             ['GET', '/settings/webhooks'],
143             ['GET', '/settings/webhooks/create'],
144             ['POST', '/settings/webhooks/create'],
145             ['GET', '/settings/webhooks/1'],
146             ['PUT', '/settings/webhooks/1'],
147             ['DELETE', '/settings/webhooks/1'],
148             ['GET', '/settings/webhooks/1/delete'],
149         ];
150
151         foreach ($routes as [$method, $endpoint]) {
152             $resp = $this->call($method, $endpoint);
153             $this->assertPermissionError($resp);
154         }
155
156         $this->permissions->grantUserRolePermissions($editor, ['settings-manage']);
157
158         foreach ($routes as [$method, $endpoint]) {
159             $resp = $this->call($method, $endpoint);
160             $this->assertNotPermissionError($resp);
161         }
162     }
163
164     protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
165     {
166         /** @var Webhook $webhook */
167         $webhook = Webhook::factory()->create($attrs);
168
169         foreach ($events as $event) {
170             $webhook->trackedEvents()->create(['event' => $event]);
171         }
172
173         return $webhook;
174     }
175 }