]> BookStack Code Mirror - bookstack/blob - tests/Auth/OpenIdTest.php
Default OpenID display name set to standard value
[bookstack] / tests / Auth / OpenIdTest.php
1 <?php namespace Tests\Auth;
2
3 use Tests\TestCase;
4
5 class OpenIdTest extends TestCase
6 {
7
8     public function setUp(): void
9     {
10         parent::setUp();
11         // Set default config for OpenID Connect
12         config()->set([
13             'auth.method' => 'openid',
14             'auth.defaults.guard' => 'openid',
15             'openid.name' => 'SingleSignOn-Testing',
16             'openid.email_attribute' => 'email',
17             'openid.display_name_attributes' => ['given_name', 'family_name'],
18             'openid.external_id_attribute' => 'uid',
19             'openid.openid_overrides' => null,
20             'openid.openid.clientId' => 'testapp',
21             'openid.openid.clientSecret' => 'testpass',
22             'openid.openid.publicKey' => $this->testCert,
23             'openid.openid.idTokenIssuer' => 'https://openid.local',
24             'openid.openid.urlAuthorize' => 'https://openid.local/auth',
25             'openid.openid.urlAccessToken' => 'https://openid.local/token',
26         ]);
27     }
28
29     public function test_openid_overrides_functions_as_expected()
30     {
31         $json = '{"urlAuthorize": "https://openid.local/custom"}';
32         config()->set(['openid.openid_overrides' => $json]);
33
34         $req = $this->get('/openid/login');
35         $redirect = $req->headers->get('location');
36         $this->assertStringStartsWith('https://openid.local/custom', $redirect, 'Login redirects to SSO location');
37     }
38
39     public function test_login_option_shows_on_login_page()
40     {
41         $req = $this->get('/login');
42         $req->assertSeeText('SingleSignOn-Testing');
43         $req->assertElementExists('form[action$="/openid/login"][method=POST] button');
44     }
45
46     public function test_login()
47     {
48         $req = $this->post('/openid/login');
49         $redirect = $req->headers->get('location');
50
51         $this->assertStringStartsWith('https://openid.local/auth', $redirect, 'Login redirects to SSO location');
52         $this->assertFalse($this->isAuthenticated());
53     }
54
55     public function test_openid_routes_are_only_active_if_openid_enabled()
56     {
57         config()->set(['auth.method' => 'standard']);
58         $getRoutes = ['/logout', '/metadata', '/sls'];
59         foreach ($getRoutes as $route) {
60             $req = $this->get('/openid' . $route);
61             $this->assertPermissionError($req);
62         }
63
64         $postRoutes = ['/login', '/acs'];
65         foreach ($postRoutes as $route) {
66             $req = $this->post('/openid' . $route);
67             $this->assertPermissionError($req);
68         }
69     }
70
71     public function test_forgot_password_routes_inaccessible()
72     {
73         $resp = $this->get('/password/email');
74         $this->assertPermissionError($resp);
75
76         $resp = $this->post('/password/email');
77         $this->assertPermissionError($resp);
78
79         $resp = $this->get('/password/reset/abc123');
80         $this->assertPermissionError($resp);
81
82         $resp = $this->post('/password/reset');
83         $this->assertPermissionError($resp);
84     }
85
86     public function test_standard_login_routes_inaccessible()
87     {
88         $resp = $this->post('/login');
89         $this->assertPermissionError($resp);
90
91         $resp = $this->get('/logout');
92         $this->assertPermissionError($resp);
93     }
94
95     public function test_user_invite_routes_inaccessible()
96     {
97         $resp = $this->get('/register/invite/abc123');
98         $this->assertPermissionError($resp);
99
100         $resp = $this->post('/register/invite/abc123');
101         $this->assertPermissionError($resp);
102     }
103
104     public function test_user_register_routes_inaccessible()
105     {
106         $resp = $this->get('/register');
107         $this->assertPermissionError($resp);
108
109         $resp = $this->post('/register');
110         $this->assertPermissionError($resp);
111     }
112 }