| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Actions; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Actions\ActivityType; | 
					
						
							|  |  |  | use BookStack\Actions\Webhook; | 
					
						
							|  |  |  | use Tests\TestCase; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class WebhookManagementTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function test_index_view() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $webhook = $this->newWebhook([ | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  |             'name'     => 'My awesome webhook', | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |             'endpoint' => 'https://example.com/donkey/webhook', | 
					
						
							|  |  |  |         ], ['all']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asAdmin()->get('/settings/webhooks'); | 
					
						
							|  |  |  |         $resp->assertOk(); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('a[href$="/settings/webhooks/create"]', 'Create New Webhook'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementContains('a[href="' . $webhook->getUrl() . '"]', $webhook->name); | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |         $resp->assertSee($webhook->endpoint); | 
					
						
							|  |  |  |         $resp->assertSee('All system events'); | 
					
						
							| 
									
										
										
										
											2021-12-13 01:39:06 +08:00
										 |  |  |         $resp->assertSee('Active'); | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_create_view() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $resp = $this->asAdmin()->get('/settings/webhooks/create'); | 
					
						
							|  |  |  |         $resp->assertOk(); | 
					
						
							|  |  |  |         $resp->assertSee('Create New Webhook'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('form[action$="/settings/webhooks/create"] button', 'Save Webhook'); | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_store() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $resp = $this->asAdmin()->post('/settings/webhooks/create', [ | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  |             'name'     => 'My first webhook', | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |             'endpoint' => 'https://example.com/webhook', | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  |             'events'   => ['all'], | 
					
						
							|  |  |  |             'active'   => 'true', | 
					
						
							| 
									
										
										
										
											2022-01-04 03:51:13 +08:00
										 |  |  |             'timeout'  => 4, | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp->assertRedirect('/settings/webhooks'); | 
					
						
							|  |  |  |         $this->assertActivityExists(ActivityType::WEBHOOK_CREATE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->followRedirects($resp); | 
					
						
							|  |  |  |         $resp->assertSee('Webhook successfully created'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseHas('webhooks', [ | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  |             'name'     => 'My first webhook', | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |             'endpoint' => 'https://example.com/webhook', | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  |             'active'   => true, | 
					
						
							| 
									
										
										
										
											2022-01-04 03:51:13 +08:00
										 |  |  |             'timeout'  => 4, | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Webhook $webhook */ | 
					
						
							|  |  |  |         $webhook = Webhook::query()->where('name', '=', 'My first webhook')->first(); | 
					
						
							|  |  |  |         $this->assertDatabaseHas('webhook_tracked_events', [ | 
					
						
							|  |  |  |             'webhook_id' => $webhook->id, | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  |             'event'      => 'all', | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_edit_view() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $webhook = $this->newWebhook(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asAdmin()->get('/settings/webhooks/' . $webhook->id); | 
					
						
							|  |  |  |         $resp->assertOk(); | 
					
						
							|  |  |  |         $resp->assertSee('Edit Webhook'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('form[action="' . $webhook->getUrl() . '"] button', 'Save Webhook'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementContains('a[href="' . $webhook->getUrl('/delete') . '"]', 'Delete Webhook'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementExists('input[type="checkbox"][value="all"][name="events[]"]'); | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_update() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $webhook = $this->newWebhook(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asAdmin()->put('/settings/webhooks/' . $webhook->id, [ | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  |             'name'     => 'My updated webhook', | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |             'endpoint' => 'https://example.com/updated-webhook', | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  |             'events'   => [ActivityType::PAGE_CREATE, ActivityType::PAGE_UPDATE], | 
					
						
							|  |  |  |             'active'   => 'true', | 
					
						
							| 
									
										
										
										
											2022-01-06 20:18:11 +08:00
										 |  |  |             'timeout'  => 5, | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertRedirect('/settings/webhooks'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->followRedirects($resp); | 
					
						
							|  |  |  |         $resp->assertSee('Webhook successfully updated'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseHas('webhooks', [ | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  |             'id'       => $webhook->id, | 
					
						
							|  |  |  |             'name'     => 'My updated webhook', | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |             'endpoint' => 'https://example.com/updated-webhook', | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  |             'active'   => true, | 
					
						
							| 
									
										
										
										
											2022-01-04 03:51:13 +08:00
										 |  |  |             'timeout'  => 5, | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $trackedEvents = $webhook->trackedEvents()->get(); | 
					
						
							|  |  |  |         $this->assertCount(2, $trackedEvents); | 
					
						
							|  |  |  |         $this->assertEquals(['page_create', 'page_update'], $trackedEvents->pluck('event')->values()->all()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertActivityExists(ActivityType::WEBHOOK_UPDATE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_delete_view() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $webhook = $this->newWebhook(['name' => 'Webhook to delete']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asAdmin()->get('/settings/webhooks/' . $webhook->id . '/delete'); | 
					
						
							|  |  |  |         $resp->assertOk(); | 
					
						
							|  |  |  |         $resp->assertSee('Delete Webhook'); | 
					
						
							|  |  |  |         $resp->assertSee('This will fully delete this webhook, with the name \'Webhook to delete\', from the system.'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('form[action$="/settings/webhooks/' . $webhook->id . '"]', 'Delete'); | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_destroy() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $webhook = $this->newWebhook(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asAdmin()->delete('/settings/webhooks/' . $webhook->id); | 
					
						
							|  |  |  |         $resp->assertRedirect('/settings/webhooks'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->followRedirects($resp); | 
					
						
							|  |  |  |         $resp->assertSee('Webhook successfully deleted'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseMissing('webhooks', ['id' => $webhook->id]); | 
					
						
							|  |  |  |         $this->assertDatabaseMissing('webhook_tracked_events', ['webhook_id' => $webhook->id]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertActivityExists(ActivityType::WEBHOOK_DELETE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_settings_manage_permission_required_for_webhook_routes() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $editor = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  |         $this->actingAs($editor); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $routes = [ | 
					
						
							|  |  |  |             ['GET', '/settings/webhooks'], | 
					
						
							|  |  |  |             ['GET', '/settings/webhooks/create'], | 
					
						
							|  |  |  |             ['POST', '/settings/webhooks/create'], | 
					
						
							|  |  |  |             ['GET', '/settings/webhooks/1'], | 
					
						
							|  |  |  |             ['PUT', '/settings/webhooks/1'], | 
					
						
							|  |  |  |             ['DELETE', '/settings/webhooks/1'], | 
					
						
							|  |  |  |             ['GET', '/settings/webhooks/1/delete'], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($routes as [$method, $endpoint]) { | 
					
						
							|  |  |  |             $resp = $this->call($method, $endpoint); | 
					
						
							|  |  |  |             $this->assertPermissionError($resp); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->permissions->grantUserRolePermissions($editor, ['settings-manage']); | 
					
						
							| 
									
										
										
										
											2021-12-10 22:54:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ($routes as [$method, $endpoint]) { | 
					
						
							|  |  |  |             $resp = $this->call($method, $endpoint); | 
					
						
							|  |  |  |             $this->assertNotPermissionError($resp); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Webhook $webhook */ | 
					
						
							|  |  |  |         $webhook = Webhook::factory()->create($attrs); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($events as $event) { | 
					
						
							|  |  |  |             $webhook->trackedEvents()->create(['event' => $event]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $webhook; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-18 19:43:05 +08:00
										 |  |  | } |