. */ declare(strict_types=1); namespace Tests\Unit\Middleware; use FireflyIII\Http\Middleware\AuthenticateTwoFactor; use FireflyIII\Models\Preference; use Log; use Preferences; use Route; use Symfony\Component\HttpFoundation\Response; use Tests\TestCase; /** * Class AuthenticateTwoFactorTest */ class AuthenticateTwoFactorTest extends TestCase { /** * Set up test */ public function setUp(): void { parent::setUp(); Log::info(sprintf('Now in %s.', get_class($this))); Route::middleware(AuthenticateTwoFactor::class)->any( '/_test/authenticate', function () { return 'OK'; } ); } /** * @covers \FireflyIII\Http\Middleware\AuthenticateTwoFactor */ public function testMiddleware(): void { $this->withoutExceptionHandling(); $response = $this->get('/_test/authenticate'); $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode()); $response->assertRedirect(route('login')); } /** * tests for user with no 2FA, should just go to requested page. * * 2FA enabled: false * 2FA secret : false * cookie : false * * * @covers \FireflyIII\Http\Middleware\AuthenticateTwoFactor */ public function testMiddlewareNoTwoFA(): void { die('this test references old 2FA code.'); $this->withoutExceptionHandling(); $user = $this->user(); $user->blocked = 0; $this->be($user); // pref for has 2fa is false $preference = new Preference; $preference->data = false; Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->once()->andReturn($preference); // pref for no twoFactorAuthSecret Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->once()->andReturn(null); // no cookie $cookie = []; $response = $this->call('GET', '/_test/authenticate', [], $cookie); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); } /** * tests for user with 2FA and secret and cookie. Continue to page. * * 2FA enabled: true * 2FA secret : 'abcde' * cookie : false * * * @covers \FireflyIII\Http\Middleware\AuthenticateTwoFactor */ public function testMiddlewareTwoFAAuthed(): void { die('this test references old 2FA code.'); $this->withoutExceptionHandling(); $user = $this->user(); $user->blocked = 0; $this->be($user); // pref for has 2fa is true $preference = new Preference; $preference->data = true; Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->once()->andReturn($preference); // pref for twoFactorAuthSecret $secret = new Preference; $secret->data = 'SomeSecret'; Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->once()->andReturn($secret); // no cookie $cookie = ['twoFactorAuthenticated' => 'true']; $response = $this->call('GET', '/_test/authenticate', [], $cookie); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); } /** * tests for user with 2FA but no secret. 2FA is not fired. * * 2FA enabled: true * 2FA secret : false * cookie : false * * * @covers \FireflyIII\Http\Middleware\AuthenticateTwoFactor */ public function testMiddlewareTwoFANoSecret(): void { die('this test references old 2FA code.'); $this->withoutExceptionHandling(); $user = $this->user(); $user->blocked = 0; $this->be($user); // pref for has 2fa is true $preference = new Preference; $preference->data = true; Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->once()->andReturn($preference); // pref for no twoFactorAuthSecret Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->once()->andReturn(null); // no cookie $cookie = []; $response = $this->call('GET', '/_test/authenticate', [], $cookie); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); } /** * tests for user with 2FA and secret. 2FA is checked * * 2FA enabled: true * 2FA secret : 'abcde' * cookie : false * * * @covers \FireflyIII\Http\Middleware\AuthenticateTwoFactor */ public function testMiddlewareTwoFASecret(): void { die('this test references old 2FA code.'); $this->withoutExceptionHandling(); $user = $this->user(); $user->blocked = 0; $this->be($user); // pref for has 2fa is true $preference = new Preference; $preference->data = true; Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->once()->andReturn($preference); // pref for twoFactorAuthSecret $secret = new Preference; $secret->data = 'SomeSecret'; Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->once()->andReturn($secret); // no cookie $cookie = []; $response = $this->call('GET', '/_test/authenticate', [], $cookie); $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode()); $response->assertRedirect(route('two-factor.index')); } }