|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Api; |
| 4 | + |
| 5 | +use BookStack\App\SystemApiController; |
| 6 | +use BookStack\Exceptions\PrettyException; |
| 7 | +use Tests\TestCase; |
| 8 | + |
| 9 | +class ApiErrorTest extends TestCase |
| 10 | +{ |
| 11 | + use TestsApi; |
| 12 | + |
| 13 | + public function test_exception_detail_only_shown_in_debug_mode() |
| 14 | + { |
| 15 | + $mockController = $this->partialMock(SystemApiController::class); |
| 16 | + $mockController->shouldReceive('read')->andThrow(\InvalidArgumentException::class, 'Potentially sensitive data', 500); |
| 17 | + |
| 18 | + $resp = $this->actingAsApiEditor()->get('/api/system'); |
| 19 | + $resp->assertStatus(500); |
| 20 | + $resp->assertDontSee('Potentially sensitive data', false); |
| 21 | + $resp->assertJsonPath('error.message', 'An error occurred'); |
| 22 | + |
| 23 | + config(['app.debug' => true]); |
| 24 | + |
| 25 | + $resp = $this->actingAsApiEditor()->get('/api/system'); |
| 26 | + $resp->assertStatus(500); |
| 27 | + $resp->assertJsonPath('error.message', 'Potentially sensitive data'); |
| 28 | + } |
| 29 | + |
| 30 | + public function test_exception_message_when_model_not_found() |
| 31 | + { |
| 32 | + $resp = $this->actingAsApiEditor()->get('/api/books/123456789'); |
| 33 | + $resp->assertStatus(404); |
| 34 | + $resp->assertSee('The requested resource could not be found.', false); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_pretty_exception_messages_are_provided_in_non_debug_mode() |
| 38 | + { |
| 39 | + $mockController = $this->partialMock(SystemApiController::class); |
| 40 | + $exception = new PrettyException('Mr Error is here!'); |
| 41 | + $exception->setSubtitle('Oh no!'); |
| 42 | + $exception->setDetails('Something has really gone wrong'); |
| 43 | + $mockController->shouldReceive('read')->andThrow($exception); |
| 44 | + |
| 45 | + $resp = $this->actingAsApiEditor()->get('/api/system'); |
| 46 | + $resp->assertStatus(500); |
| 47 | + $resp->assertJson([ |
| 48 | + 'error' => [ |
| 49 | + 'message' => 'Mr Error is here!. Oh no!. Something has really gone wrong.' |
| 50 | + ] |
| 51 | + ]); |
| 52 | + } |
| 53 | +} |
0 commit comments