]> BookStack Code Mirror - bookstack/blob - tests/Api/ApiDocsTest.php
Merge branch 'v21.05.x'
[bookstack] / tests / Api / ApiDocsTest.php
1 <?php
2
3 namespace Tests\Api;
4
5 use BookStack\Auth\User;
6 use Tests\TestCase;
7
8 class ApiDocsTest extends TestCase
9 {
10     use TestsApi;
11
12     protected $endpoint = '/api/docs';
13
14     public function test_docs_page_not_visible_to_normal_viewers()
15     {
16         $viewer = $this->getViewer();
17         $resp = $this->actingAs($viewer)->get($this->endpoint);
18         $resp->assertStatus(403);
19
20         $resp = $this->actingAsApiEditor()->get($this->endpoint);
21         $resp->assertStatus(200);
22     }
23
24     public function test_docs_page_returns_view_with_docs_content()
25     {
26         $resp = $this->actingAsApiEditor()->get($this->endpoint);
27         $resp->assertStatus(200);
28         $resp->assertSee(url('/api/docs.json'));
29         $resp->assertSee('Show a JSON view of the API docs data.');
30         $resp->assertHeader('Content-Type', 'text/html; charset=UTF-8');
31     }
32
33     public function test_docs_json_endpoint_returns_json()
34     {
35         $resp = $this->actingAsApiEditor()->get($this->endpoint . '.json');
36         $resp->assertStatus(200);
37         $resp->assertHeader('Content-Type', 'application/json');
38         $resp->assertJson([
39             'docs' => [[
40                 'name' => 'docs-display',
41                 'uri'  => 'api/docs',
42             ]],
43         ]);
44     }
45
46     public function test_docs_page_visible_by_public_user_if_given_permission()
47     {
48         $this->setSettings(['app-public' => true]);
49         $guest = User::getDefault();
50
51         $this->startSession();
52         $resp = $this->get('/api/docs');
53         $resp->assertStatus(403);
54
55         $this->giveUserPermissions($guest, ['access-api']);
56
57         $resp = $this->get('/api/docs');
58         $resp->assertStatus(200);
59     }
60 }