5 use PragmaRX\Google2FA\Google2FA;
8 class MfaConfigurationTest extends TestCase
11 public function test_totp_setup()
13 $editor = $this->getEditor();
14 $this->assertDatabaseMissing('mfa_values', ['user_id' => $editor->id]);
17 $resp = $this->actingAs($editor)->get('/mfa/setup');
18 $resp->assertElementContains('a[href$="/mfa/totp-generate"]', 'Setup');
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');
28 // Validation error, code should remain the same
29 $resp = $this->post('/mfa/totp-confirm', [
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);
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', [
44 $resp->assertRedirect('/mfa/setup');
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');