]> BookStack Code Mirror - bookstack/blob - tests/Auth/MfaConfigurationTest.php
Covered TOTP setup with testing
[bookstack] / tests / Auth / MfaConfigurationTest.php
1 <?php
2
3 namespace Tests\Auth;
4
5 use PragmaRX\Google2FA\Google2FA;
6 use Tests\TestCase;
7
8 class MfaConfigurationTest extends TestCase
9 {
10
11     public function test_totp_setup()
12     {
13         $editor = $this->getEditor();
14         $this->assertDatabaseMissing('mfa_values', ['user_id' => $editor->id]);
15
16         // Setup page state
17         $resp = $this->actingAs($editor)->get('/mfa/setup');
18         $resp->assertElementContains('a[href$="/mfa/totp-generate"]', 'Setup');
19
20         // Generate page access
21         $resp = $this->get('/mfa/totp-generate');
22         $resp->assertSee('Mobile App Setup');
23         $resp->assertSee('Verify Setup');
24         $resp->assertElementExists('form[action$="/mfa/totp-confirm"] button');
25         $this->assertSessionHas('mfa-setup-totp-secret');
26         $svg = $resp->getElementHtml('#main-content .card svg');
27
28         // Validation error, code should remain the same
29         $resp = $this->post('/mfa/totp-confirm', [
30             'code' => 'abc123',
31         ]);
32         $resp->assertRedirect('/mfa/totp-generate');
33         $resp = $this->followRedirects($resp);
34         $resp->assertSee('The provided code is not valid or has expired.');
35         $revisitSvg = $resp->getElementHtml('#main-content .card svg');
36         $this->assertTrue($svg === $revisitSvg);
37
38         // Successful confirmation
39         $google2fa = new Google2FA();
40         $otp = $google2fa->getCurrentOtp(decrypt(session()->get('mfa-setup-totp-secret')));
41         $resp = $this->post('/mfa/totp-confirm', [
42             'code' => $otp,
43         ]);
44         $resp->assertRedirect('/mfa/setup');
45
46         // Confirmation of setup
47         $resp = $this->followRedirects($resp);
48         $resp->assertSee('Multi-factor method successfully configured');
49         $resp->assertElementContains('a[href$="/mfa/totp-generate"]', 'Reconfigure');
50     }
51
52 }