]> BookStack Code Mirror - bookstack/blobdiff - tests/Auth/MfaConfigurationTest.php
Added test for logical-theme-system command registration
[bookstack] / tests / Auth / MfaConfigurationTest.php
index 63a5fa4ddcdaace437216d12b976d0a5bad75db6..fab94817c4b651d6c1bd022c737984c0b742f915 100644 (file)
@@ -4,13 +4,13 @@ namespace Tests\Auth;
 
 use BookStack\Actions\ActivityType;
 use BookStack\Auth\Access\Mfa\MfaValue;
+use BookStack\Auth\Role;
 use BookStack\Auth\User;
 use PragmaRX\Google2FA\Google2FA;
 use Tests\TestCase;
 
 class MfaConfigurationTest extends TestCase
 {
-
     public function test_totp_setup()
     {
         $editor = $this->getEditor();
@@ -37,10 +37,12 @@ class MfaConfigurationTest extends TestCase
         $resp->assertSee('The provided code is not valid or has expired.');
         $revisitSvg = $resp->getElementHtml('#main-content .card svg');
         $this->assertTrue($svg === $revisitSvg);
+        $secret = decrypt(session()->get('mfa-setup-totp-secret'));
+
+        $resp->assertSee("?secret={$secret}&issuer=BookStack&algorithm=SHA1&digits=6&period=30");
 
         // Successful confirmation
         $google2fa = new Google2FA();
-        $secret = decrypt(session()->get('mfa-setup-totp-secret'));
         $otp = $google2fa->getCurrentOtp($secret);
         $resp = $this->post('/mfa/totp/confirm', [
             'code' => $otp,
@@ -54,7 +56,7 @@ class MfaConfigurationTest extends TestCase
 
         $this->assertDatabaseHas('mfa_values', [
             'user_id' => $editor->id,
-            'method' => 'totp',
+            'method'  => 'totp',
         ]);
         $this->assertFalse(session()->has('mfa-setup-totp-secret'));
         $value = MfaValue::query()->where('user_id', '=', $editor->id)
@@ -79,7 +81,7 @@ class MfaConfigurationTest extends TestCase
         $codes = decrypt(session()->get('mfa-setup-backup-codes'));
         // Check code format
         $this->assertCount(16, $codes);
-        $this->assertEquals(16*11, strlen(implode('', $codes)));
+        $this->assertEquals(16 * 11, strlen(implode('', $codes)));
         // Check download link
         $resp->assertSee(base64_encode(implode("\n\n", $codes)));
 
@@ -94,7 +96,7 @@ class MfaConfigurationTest extends TestCase
 
         $this->assertDatabaseHas('mfa_values', [
             'user_id' => $editor->id,
-            'method' => 'backup_codes',
+            'method'  => 'backup_codes',
         ]);
         $this->assertFalse(session()->has('mfa-setup-backup-codes'));
         $value = MfaValue::query()->where('user_id', '=', $editor->id)
@@ -153,10 +155,10 @@ class MfaConfigurationTest extends TestCase
         MfaValue::upsertWithValue($admin, MfaValue::METHOD_TOTP, 'test');
         $this->assertEquals(1, $admin->mfaValues()->count());
         $resp = $this->actingAs($admin)->get('/mfa/setup');
-        $resp->assertElementExists('form[action$="/mfa/remove/totp"]');
+        $resp->assertElementExists('form[action$="/mfa/totp/remove"]');
 
-        $resp = $this->delete("/mfa/remove/totp");
-        $resp->assertRedirect("/mfa/setup");
+        $resp = $this->delete('/mfa/totp/remove');
+        $resp->assertRedirect('/mfa/setup');
         $resp = $this->followRedirects($resp);
         $resp->assertSee('Multi-factor method successfully removed');
 
@@ -164,4 +166,21 @@ class MfaConfigurationTest extends TestCase
         $this->assertEquals(0, $admin->mfaValues()->count());
     }
 
-}
\ No newline at end of file
+    public function test_totp_setup_url_shows_correct_user_when_setup_forced_upon_login()
+    {
+        $admin = $this->getAdmin();
+        /** @var Role $role */
+        $role = $admin->roles()->first();
+        $role->mfa_enforced = true;
+        $role->save();
+
+        $resp = $this->post('/login', ['email' => $admin->email, 'password' => 'password']);
+        $this->assertFalse(auth()->check());
+        $resp->assertRedirect('/mfa/verify');
+
+        $resp = $this->get('/mfa/totp/generate');
+        $resp->assertSeeText('Mobile App Setup');
+        $resp->assertDontSee('otpauth://totp/BookStack:guest%40example.com', false);
+        $resp->assertSee('otpauth://totp/BookStack:admin%40admin.com', false);
+    }
+}