| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-05 22:44:58 +08:00
										 |  |  | namespace Activity; | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Activity\ActivityType; | 
					
						
							|  |  |  | use BookStack\Activity\DispatchWebhookJob; | 
					
						
							|  |  |  | use BookStack\Activity\Models\Webhook; | 
					
						
							|  |  |  | use BookStack\Activity\Tools\ActivityLogger; | 
					
						
							| 
									
										
										
										
											2023-07-12 23:16:12 +08:00
										 |  |  | use BookStack\Api\ApiToken; | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Users\Models\User; | 
					
						
							| 
									
										
										
										
											2023-09-08 21:16:09 +08:00
										 |  |  | use GuzzleHttp\Exception\ConnectException; | 
					
						
							|  |  |  | use GuzzleHttp\Psr7\Response; | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  | use Illuminate\Support\Facades\Bus; | 
					
						
							|  |  |  | use Tests\TestCase; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class WebhookCallTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function test_webhook_listening_to_all_called_on_event() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->newWebhook([], ['all']); | 
					
						
							|  |  |  |         Bus::fake(); | 
					
						
							|  |  |  |         $this->runEvent(ActivityType::ROLE_CREATE); | 
					
						
							|  |  |  |         Bus::assertDispatched(DispatchWebhookJob::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_webhook_listening_to_specific_event_called_on_event() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->newWebhook([], [ActivityType::ROLE_UPDATE]); | 
					
						
							|  |  |  |         Bus::fake(); | 
					
						
							|  |  |  |         $this->runEvent(ActivityType::ROLE_UPDATE); | 
					
						
							|  |  |  |         Bus::assertDispatched(DispatchWebhookJob::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_webhook_listening_to_specific_event_not_called_on_other_event() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->newWebhook([], [ActivityType::ROLE_UPDATE]); | 
					
						
							|  |  |  |         Bus::fake(); | 
					
						
							|  |  |  |         $this->runEvent(ActivityType::ROLE_CREATE); | 
					
						
							|  |  |  |         Bus::assertNotDispatched(DispatchWebhookJob::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_inactive_webhook_not_called_on_event() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->newWebhook(['active' => false], ['all']); | 
					
						
							|  |  |  |         Bus::fake(); | 
					
						
							|  |  |  |         $this->runEvent(ActivityType::ROLE_CREATE); | 
					
						
							|  |  |  |         Bus::assertNotDispatched(DispatchWebhookJob::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-12 23:16:12 +08:00
										 |  |  |     public function test_webhook_runs_for_delete_actions() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-09-08 21:16:09 +08:00
										 |  |  |         // This test must not fake the queue/bus since this covers an issue
 | 
					
						
							|  |  |  |         // around handling and serialization of items now deleted from the database.
 | 
					
						
							| 
									
										
										
										
											2023-10-19 21:18:42 +08:00
										 |  |  |         $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']); | 
					
						
							| 
									
										
										
										
											2023-09-08 21:16:09 +08:00
										 |  |  |         $this->mockHttpClient([new Response(500)]); | 
					
						
							| 
									
										
										
										
											2023-07-12 23:16:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $user = $this->users->newUser(); | 
					
						
							|  |  |  |         $resp = $this->asAdmin()->delete($user->getEditUrl()); | 
					
						
							|  |  |  |         $resp->assertRedirect('/settings/users'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var ApiToken $apiToken */ | 
					
						
							|  |  |  |         $editor = $this->users->editor(); | 
					
						
							|  |  |  |         $apiToken = ApiToken::factory()->create(['user_id' => $editor]); | 
					
						
							| 
									
										
										
										
											2023-10-19 21:18:42 +08:00
										 |  |  |         $this->delete($apiToken->getUrl())->assertRedirect(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $webhook->refresh(); | 
					
						
							|  |  |  |         $this->assertEquals('Response status from endpoint was 500', $webhook->last_error); | 
					
						
							| 
									
										
										
										
											2023-07-12 23:16:12 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  |     public function test_failed_webhook_call_logs_error() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $logger = $this->withTestLogger(); | 
					
						
							| 
									
										
										
										
											2023-09-08 21:16:09 +08:00
										 |  |  |         $this->mockHttpClient([new Response(500)]); | 
					
						
							| 
									
										
										
										
											2022-01-04 03:42:48 +08:00
										 |  |  |         $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']); | 
					
						
							|  |  |  |         $this->assertNull($webhook->last_errored_at); | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->runEvent(ActivityType::ROLE_CREATE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with status 500')); | 
					
						
							| 
									
										
										
										
											2022-01-04 03:42:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $webhook->refresh(); | 
					
						
							|  |  |  |         $this->assertEquals('Response status from endpoint was 500', $webhook->last_error); | 
					
						
							|  |  |  |         $this->assertNotNull($webhook->last_errored_at); | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 02:37:05 +08:00
										 |  |  |     public function test_webhook_call_exception_is_caught_and_logged() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-09-08 21:16:09 +08:00
										 |  |  |         $this->mockHttpClient([new ConnectException('Failed to perform request', new \GuzzleHttp\Psr7\Request('GET', ''))]); | 
					
						
							| 
									
										
										
										
											2022-01-04 02:37:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $logger = $this->withTestLogger(); | 
					
						
							| 
									
										
										
										
											2022-01-04 03:42:48 +08:00
										 |  |  |         $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']); | 
					
						
							|  |  |  |         $this->assertNull($webhook->last_errored_at); | 
					
						
							| 
									
										
										
										
											2022-01-04 02:37:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->runEvent(ActivityType::ROLE_CREATE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with error "Failed to perform request"')); | 
					
						
							| 
									
										
										
										
											2022-01-04 03:42:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $webhook->refresh(); | 
					
						
							|  |  |  |         $this->assertEquals('Failed to perform request', $webhook->last_error); | 
					
						
							|  |  |  |         $this->assertNotNull($webhook->last_errored_at); | 
					
						
							| 
									
										
										
										
											2022-01-04 02:37:05 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-27 03:13:37 +08:00
										 |  |  |     public function test_webhook_uses_ssr_hosts_option_if_set() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         config()->set('app.ssr_hosts', 'https://*.example.com'); | 
					
						
							| 
									
										
										
										
											2023-09-08 21:16:09 +08:00
										 |  |  |         $responses = $this->mockHttpClient(); | 
					
						
							| 
									
										
										
										
											2023-08-27 03:13:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.co.uk'], ['all']); | 
					
						
							|  |  |  |         $this->runEvent(ActivityType::ROLE_CREATE); | 
					
						
							| 
									
										
										
										
											2023-09-08 21:16:09 +08:00
										 |  |  |         $this->assertEquals(0, $responses->requestCount()); | 
					
						
							| 
									
										
										
										
											2023-08-27 03:13:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $webhook->refresh(); | 
					
						
							|  |  |  |         $this->assertEquals('The URL does not match the configured allowed SSR hosts', $webhook->last_error); | 
					
						
							|  |  |  |         $this->assertNotNull($webhook->last_errored_at); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  |     public function test_webhook_call_data_format() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-09-08 21:16:09 +08:00
										 |  |  |         $responses = $this->mockHttpClient([new Response(200, [], '')]); | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  |         $webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']); | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $editor = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 21:16:09 +08:00
										 |  |  |         $request = $responses->latestRequest(); | 
					
						
							|  |  |  |         $reqData = json_decode($request->getBody(), true); | 
					
						
							|  |  |  |         $this->assertEquals('page_update', $reqData['event']); | 
					
						
							|  |  |  |         $this->assertEquals(($editor->name . ' updated page "' . $page->name . '"'), $reqData['text']); | 
					
						
							|  |  |  |         $this->assertIsString($reqData['triggered_at']); | 
					
						
							|  |  |  |         $this->assertEquals($editor->name, $reqData['triggered_by']['name']); | 
					
						
							|  |  |  |         $this->assertEquals($editor->getProfileUrl(), $reqData['triggered_by_profile_url']); | 
					
						
							|  |  |  |         $this->assertEquals($webhook->id, $reqData['webhook_id']); | 
					
						
							|  |  |  |         $this->assertEquals($webhook->name, $reqData['webhook_name']); | 
					
						
							|  |  |  |         $this->assertEquals($page->getUrl(), $reqData['url']); | 
					
						
							|  |  |  |         $this->assertEquals($page->name, $reqData['related_item']['name']); | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function runEvent(string $event, $detail = '', ?User $user = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_null($user)) { | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |             $user = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->actingAs($user); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $activityLogger = $this->app->make(ActivityLogger::class); | 
					
						
							|  |  |  |         $activityLogger->add($event, $detail); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-12 23:16:12 +08:00
										 |  |  |     protected function newWebhook(array $attrs, array $events): Webhook | 
					
						
							| 
									
										
										
										
											2021-12-13 03:01:50 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @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
										 |  |  | } |