From 62b5cf04ad7652206b72c4400df6c1c82e7772f7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 4 Aug 2019 10:27:37 +0200 Subject: [PATCH] Refactor tests and code to handle new 2FA methods. --- app/Http/Controllers/Admin/UserController.php | 8 +- .../Controllers/Auth/TwoFactorController.php | 64 +----- app/Http/Middleware/AuthenticateTwoFactor.php | 90 -------- .../RedirectIfTwoFactorAuthenticated.php | 59 ------ app/Repositories/User/UserRepository.php | 23 +- app/Validation/AccountValidator.php | 3 - .../Controllers/Admin/UserControllerTest.php | 9 - .../Auth/TwoFactorControllerTest.php | 96 --------- .../Controllers/ProfileControllerTest.php | 27 +-- tests/TestCase.php | 3 - .../Middleware/AuthenticateTwoFactorTest.php | 196 ------------------ .../RedirectIf2FAAuthenticatedTest.php | 96 --------- 12 files changed, 12 insertions(+), 662 deletions(-) delete mode 100644 app/Http/Middleware/AuthenticateTwoFactor.php delete mode 100644 app/Http/Middleware/RedirectIfTwoFactorAuthenticated.php delete mode 100644 tests/Unit/Middleware/AuthenticateTwoFactorTest.php delete mode 100644 tests/Unit/Middleware/RedirectIf2FAAuthenticatedTest.php diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 9634556cd8..001eec6061 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -125,16 +125,10 @@ class UserController extends Controller $users = $this->repository->all(); // add meta stuff. - die('the 2FA references here should be refactored.'); $users->each( function (User $user) { - $list = ['twoFactorAuthEnabled', 'twoFactorAuthSecret']; - $preferences = app('preferences')->getArrayForUser($user, $list); $user->isAdmin = $this->repository->hasRole($user, 'owner'); - $is2faEnabled = 1 === $preferences['twoFactorAuthEnabled']; - $has2faSecret = null !== $preferences['twoFactorAuthSecret']; - $user->has2FA = ($is2faEnabled && $has2faSecret); - $user->prefs = $preferences; + $user->has2FA = null !== $user->mfa_secret; } ); diff --git a/app/Http/Controllers/Auth/TwoFactorController.php b/app/Http/Controllers/Auth/TwoFactorController.php index b754e4b13a..b211cb78f3 100644 --- a/app/Http/Controllers/Auth/TwoFactorController.php +++ b/app/Http/Controllers/Auth/TwoFactorController.php @@ -39,6 +39,8 @@ class TwoFactorController extends Controller { /** * @param Request $request + * + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function submitMFA(Request $request) { @@ -119,40 +121,6 @@ class TwoFactorController extends Controller Preferences::set('mfa_history', $newHistory); } - /** - * Show 2FA screen. - * - * @param Request $request - * - * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View - * - * @throws FireflyException - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - */ - public function index(Request $request) - { - die('this auth controller must be refactored.'); - $user = auth()->user(); - - // to make sure the validator in the next step gets the secret, we push it in session - $secretPreference = app('preferences')->get('twoFactorAuthSecret', null); - $secret = null === $secretPreference ? null : $secretPreference->data; - $title = (string)trans('firefly.two_factor_title'); - - // make sure the user has two factor configured: - $has2FA = app('preferences')->get('twoFactorAuthEnabled', false)->data; - if (null === $has2FA || false === $has2FA) { - return redirect(route('index')); - } - - if ('' === (string)$secret) { - throw new FireflyException('Your two factor authentication secret is empty, which it cannot be at this point. Please check the log files.'); - } - $request->session()->flash('two-factor-secret', $secret); - - return view('auth.two-factor', compact('user', 'title')); - } - /** * What to do if 2FA lost? * @@ -174,34 +142,6 @@ class TwoFactorController extends Controller return view('auth.lost-two-factor', compact('user', 'siteOwner', 'title')); } - /** - * Submit 2FA code. - * - * @param TokenFormRequest $request - * @param CookieJar $cookieJar - * - * @return mixed - */ - public function postIndex(TokenFormRequest $request, CookieJar $cookieJar) - { - // wants to remember session? - $remember = $request->session()->get('remember_login') ?? false; - - $minutes = config('session.lifetime'); - if (true === $remember) { - // set cookie with a long lifetime (30 days) - $minutes = 43200; - } - $cookie = $cookieJar->make( - 'twoFactorAuthenticated', 'true', $minutes, config('session.path'), config('session.domain'), config('session.secure'), config('session.http_only') - ); - - // whatever the case, forget about it: - $request->session()->forget('remember_login'); - - return redirect(route('home'))->withCookie($cookie); - } - /** * Each MFA history has a timestamp and a code, saving the MFA entries for 5 minutes. So if the * submitted MFA code has been submitted in the last 5 minutes, it won't work despite being valid. diff --git a/app/Http/Middleware/AuthenticateTwoFactor.php b/app/Http/Middleware/AuthenticateTwoFactor.php deleted file mode 100644 index c2a5064e2f..0000000000 --- a/app/Http/Middleware/AuthenticateTwoFactor.php +++ /dev/null @@ -1,90 +0,0 @@ -. - */ -/** @noinspection PhpMethodParametersCountMismatchInspection */ -declare(strict_types=1); - -namespace FireflyIII\Http\Middleware; - -use Closure; -use Illuminate\Contracts\Auth\Factory as Auth; -use Log; - -/** - * Class AuthenticateTwoFactor. - */ -class AuthenticateTwoFactor -{ - /** - * The authentication factory instance. - * - * @var \Illuminate\Contracts\Auth\Factory - */ - protected $auth; - - /** - * Create a new middleware instance. - * - * @param \Illuminate\Contracts\Auth\Factory $auth - * - * @return void - */ - public function __construct(Auth $auth) - { - $this->auth = $auth; - } - - - /** - * Handle 2FA request. - * - * @param $request - * @param Closure $next - * - * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|mixed - * @throws \Psr\Container\NotFoundExceptionInterface - * @throws \Psr\Container\ContainerExceptionInterface - * - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - */ - public function handle($request, Closure $next) - { - die('this middleware is deprecated.'); - /** @noinspection PhpUndefinedMethodInspection */ - if ($this->auth->guest()) { - return response()->redirectTo(route('login')); - } - - - $is2faEnabled = app('preferences')->get('twoFactorAuthEnabled', false)->data; - $has2faSecret = null !== app('preferences')->get('twoFactorAuthSecret'); - /** @noinspection PhpUndefinedMethodInspection */ - $is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated'); - - if ($is2faEnabled && $has2faSecret && !$is2faAuthed) { - Log::debug('Does not seem to be 2 factor authed, redirect.'); - - return response()->redirectTo(route('two-factor.index')); - } - - return $next($request); - } - -} diff --git a/app/Http/Middleware/RedirectIfTwoFactorAuthenticated.php b/app/Http/Middleware/RedirectIfTwoFactorAuthenticated.php deleted file mode 100644 index 8f971e12b8..0000000000 --- a/app/Http/Middleware/RedirectIfTwoFactorAuthenticated.php +++ /dev/null @@ -1,59 +0,0 @@ -. - */ -declare(strict_types=1); - -namespace FireflyIII\Http\Middleware; - -use Closure; -use Illuminate\Support\Facades\Auth; - -/** - * Class RedirectIfTwoFactorAuthenticated. - */ -class RedirectIfTwoFactorAuthenticated -{ - /** - * Handle an incoming request. - * - * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @param string|null $guard - * - * @return mixed - * - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - */ - public function handle($request, Closure $next, $guard = null) - { - die('this middleware is deprecated.'); - if (Auth::guard($guard)->check()) { - $is2faEnabled = app('preferences')->get('twoFactorAuthEnabled', false)->data; - $has2faSecret = null !== app('preferences')->get('twoFactorAuthSecret'); - $is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated'); - - if ($is2faEnabled && $has2faSecret && $is2faAuthed) { - return response()->redirectTo(route('index')); - } - } - - return $next($request); - } -} diff --git a/app/Repositories/User/UserRepository.php b/app/Repositories/User/UserRepository.php index 5519e61867..4ca7f388f3 100644 --- a/app/Repositories/User/UserRepository.php +++ b/app/Repositories/User/UserRepository.php @@ -245,21 +245,14 @@ class UserRepository implements UserRepositoryInterface $return = []; // two factor: - die('this method references 2FA and must be refactored.'); - $is2faEnabled = app('preferences')->getForUser($user, 'twoFactorAuthEnabled', false)->data; - $has2faSecret = null !== app('preferences')->getForUser($user, 'twoFactorAuthSecret'); - $return['has_2fa'] = false; - if ($is2faEnabled && $has2faSecret) { - $return['has_2fa'] = true; - } - - $return['is_admin'] = $this->hasRole($user, 'owner'); - $return['blocked'] = 1 === (int)$user->blocked; - $return['blocked_code'] = $user->blocked_code; - $return['accounts'] = $user->accounts()->count(); - $return['journals'] = $user->transactionJournals()->count(); - $return['transactions'] = $user->transactions()->count(); - $return['attachments'] = $user->attachments()->count(); + $return['has_2fa'] = $user->mfa_secret !== null; + $return['is_admin'] = $this->hasRole($user, 'owner'); + $return['blocked'] = 1 === (int)$user->blocked; + $return['blocked_code'] = $user->blocked_code; + $return['accounts'] = $user->accounts()->count(); + $return['journals'] = $user->transactionJournals()->count(); + $return['transactions'] = $user->transactions()->count(); + $return['attachments'] = $user->attachments()->count(); $return['attachments_size'] = $user->attachments()->sum('size'); $return['bills'] = $user->bills()->count(); $return['categories'] = $user->categories()->count(); diff --git a/app/Validation/AccountValidator.php b/app/Validation/AccountValidator.php index 2ca4843d42..b5f3d42e4c 100644 --- a/app/Validation/AccountValidator.php +++ b/app/Validation/AccountValidator.php @@ -162,9 +162,6 @@ class AccountValidator case TransactionType::RECONCILIATION: $result = $this->validateReconciliationSource($accountId); break; - //case TransactionType::OPENING_BALANCE: - //case TransactionType::RECONCILIATION: - // die(sprintf('Cannot handle type "%s"', $this->transactionType)); } return $result; diff --git a/tests/Feature/Controllers/Admin/UserControllerTest.php b/tests/Feature/Controllers/Admin/UserControllerTest.php index f7cc0fe571..abfdd737e1 100644 --- a/tests/Feature/Controllers/Admin/UserControllerTest.php +++ b/tests/Feature/Controllers/Admin/UserControllerTest.php @@ -102,19 +102,10 @@ class UserControllerTest extends TestCase */ public function testIndex(): void { - die('this test references old 2FA code.'); $repository = $this->mock(UserRepositoryInterface::class); $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->times(3)->andReturn(true); $user = $this->user(); $repository->shouldReceive('all')->andReturn(new Collection([$user])); - - Preferences::shouldReceive('getArrayForUser')->atLeast()->once()->andReturn( - [ - 'twoFactorAuthEnabled' => false, - 'twoFactorAuthSecret' => null, - ] - ); - $this->mockDefaultSession(); $this->be($user); diff --git a/tests/Feature/Controllers/Auth/TwoFactorControllerTest.php b/tests/Feature/Controllers/Auth/TwoFactorControllerTest.php index a75c9ed4f0..ac633b01ea 100644 --- a/tests/Feature/Controllers/Auth/TwoFactorControllerTest.php +++ b/tests/Feature/Controllers/Auth/TwoFactorControllerTest.php @@ -42,114 +42,18 @@ class TwoFactorControllerTest extends TestCase Log::info(sprintf('Now in %s.', get_class($this))); } - /** - * @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController - */ - public function testIndex(): void - { - die('this test references old 2FA code.'); - $this->mockDefaultConfiguration(); - $this->be($this->user()); - - - $truePref = new Preference; - $truePref->data = true; - $secretPreference = new Preference; - $secretPreference->data = 'JZMES376Z6YXY4QZ'; - $langPreference = new Preference; - $langPreference->data = 'en_US'; - - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($truePref)->twice(); - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference)->once(); - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference)->once(); - Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($langPreference); - - $response = $this->get(route('two-factor.index')); - $response->assertStatus(200); - } - - /** - * @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController - */ - public function testIndexNo2FA(): void - { - die('this test references old 2FA code.'); - $this->be($this->user()); - - $falsePreference = new Preference; - $falsePreference->data = false; - $langPreference = new Preference; - $langPreference->data = 'en_US'; - - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($falsePreference)->twice(); - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn(null)->once(); - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn(null)->once(); - Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($langPreference); - - $response = $this->get(route('two-factor.index')); - $response->assertStatus(302); - $response->assertRedirect(route('index')); - } - - /** - * @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController - */ - public function testIndexNoSecret(): void - { - die('this test references old 2FA code.'); - $this->be($this->user()); - - $truePref = new Preference; - $truePref->data = true; - $secretPreference = new Preference; - $secretPreference->data = ''; - $langPreference = new Preference; - $langPreference->data = 'en_US'; - - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($truePref)->twice(); - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference)->once(); - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference)->once(); - Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($langPreference); - - $response = $this->get(route('two-factor.index')); - $response->assertStatus(500); - } - /** * @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController */ public function testLostTwoFactor(): void { - die('this test references old 2FA code.'); $this->be($this->user()); - - $truePreference = new Preference; - $truePreference->data = true; - $secretPreference = new Preference; - $secretPreference->data = 'JZMES376Z6YXY4QZ'; $langPreference = new Preference; $langPreference->data = 'en_US'; - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($truePreference); - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference); - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference); Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($langPreference); $response = $this->get(route('two-factor.lost')); $response->assertStatus(200); } - - /** - * @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController - */ - public function testPostIndex(): void - { - $data = ['code' => '123456']; - Google2FA::shouldReceive('verifyKey')->andReturn(true)->once(); - $this->session(['remember_login' => true]); - - $this->be($this->user()); - $response = $this->post(route('two-factor.post'), $data); - $response->assertStatus(302); - } } diff --git a/tests/Feature/Controllers/ProfileControllerTest.php b/tests/Feature/Controllers/ProfileControllerTest.php index c38d6dff58..bb741460a9 100644 --- a/tests/Feature/Controllers/ProfileControllerTest.php +++ b/tests/Feature/Controllers/ProfileControllerTest.php @@ -167,14 +167,7 @@ class ProfileControllerTest extends TestCase $this->mockDefaultSession(); // mock stuff $userRepos = $this->mock(UserRepositoryInterface::class); - $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->atLeast()->once()->andReturn(false); - - die('the references in this test to 2FA preferences must be refactored.'); - Preferences::shouldReceive('delete')->withArgs(['twoFactorAuthEnabled'])->atLeast()->once(); - Preferences::shouldReceive('delete')->withArgs(['twoFactorAuthSecret'])->atLeast()->once(); - - $this->be($this->user()); $response = $this->get(route('profile.delete-code')); $response->assertStatus(302); @@ -211,17 +204,6 @@ class ProfileControllerTest extends TestCase $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->times(1)->andReturn(false); Preferences::shouldReceive('set')->once()->withArgs(['twoFactorAuthEnabled', 1]); - //Preferences::shouldReceive('lastActivity')->once(); - - die('the references in this test to 2FA preferences must be refactored.'); - $pref = new Preference; - $pref->data = false; - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->atLeast()->once()->andReturn($pref); - - $pref = new Preference; - $pref->data = 'super-secret'; - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->atLeast()->once()->andReturn($pref); - $view = new Preference; $view->data = '1M'; @@ -231,10 +213,6 @@ class ProfileControllerTest extends TestCase $lang->data = 'en_US'; Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($lang)->atLeast()->once(); -// $pref = new Preference; -// $pref->data = 'EUR'; -// Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'currencyPreference', 'EUR'])->atLeast()->once()->andReturn($pref); - $list = new Preference; $list->data = 50; Preferences::shouldReceive('get')->withArgs(['list-length', 10])->andReturn($list)->atLeast()->once(); @@ -439,6 +417,7 @@ class ProfileControllerTest extends TestCase */ public function testPostCode(): void { + $this->mock(UserRepositoryInterface::class); Log::info(sprintf('Now in test %s.', __METHOD__)); $this->mockDefaultSession(); @@ -449,11 +428,7 @@ class ProfileControllerTest extends TestCase $this->withoutMiddleware(); $this->session(['two-factor-secret' => $secret]); - die('the references in this test to 2FA preferences must be refactored.'); - Preferences::shouldReceive('set')->withArgs(['twoFactorAuthEnabled', 1])->once(); - Preferences::shouldReceive('set')->withArgs(['twoFactorAuthSecret', $secret])->once(); Preferences::shouldReceive('mark')->once(); - Google2FA::shouldReceive('verifyKey')->withArgs([$secret, $key])->andReturn(true); $data = [ diff --git a/tests/TestCase.php b/tests/TestCase.php index b0eee779ff..821928f173 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -451,9 +451,6 @@ abstract class TestCase extends BaseTestCase $list = new Preference; $list->data = 50; - die('the references in this test to 2FA preferences must be refactored.'); - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($false); - Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturnNull(); Preferences::shouldReceive('get')->withArgs(['viewRange', Mockery::any()])->andReturn($view); Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($lang); Preferences::shouldReceive('get')->withArgs(['list-length', 10])->andReturn($list); diff --git a/tests/Unit/Middleware/AuthenticateTwoFactorTest.php b/tests/Unit/Middleware/AuthenticateTwoFactorTest.php deleted file mode 100644 index 18d14781dc..0000000000 --- a/tests/Unit/Middleware/AuthenticateTwoFactorTest.php +++ /dev/null @@ -1,196 +0,0 @@ -. - */ - -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')); - } -} diff --git a/tests/Unit/Middleware/RedirectIf2FAAuthenticatedTest.php b/tests/Unit/Middleware/RedirectIf2FAAuthenticatedTest.php deleted file mode 100644 index c8bb097646..0000000000 --- a/tests/Unit/Middleware/RedirectIf2FAAuthenticatedTest.php +++ /dev/null @@ -1,96 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace Tests\Unit\Middleware; - -use FireflyIII\Http\Middleware\RedirectIfTwoFactorAuthenticated; -use FireflyIII\Models\Preference; -use Log; -use Preferences; -use Route; -use Symfony\Component\HttpFoundation\Response; -use Tests\TestCase; - -/** - * Class RedirectIf2FAAuthenticatedTest - */ -class RedirectIf2FAAuthenticatedTest extends TestCase -{ - /** - * Set up test - */ - public function setUp(): void - { - parent::setUp(); - Log::info(sprintf('Now in %s.', get_class($this))); - Route::middleware(RedirectIfTwoFactorAuthenticated::class)->any( - '/_test/authenticate', function () { - return 'OK'; - } - ); - } - - /** - * @covers \FireflyIII\Http\Middleware\RedirectIfTwoFactorAuthenticated - */ - public function testMiddleware(): void - { - $response = $this->get('/_test/authenticate'); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); - } - - /** - * @covers \FireflyIII\Http\Middleware\RedirectIfTwoFactorAuthenticated - */ - public function testMiddlewareAuthenticated(): void - { - die('this test references old 2FA code.'); - // 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']; - - $this->be($this->user()); - $response = $this->call('GET', '/_test/authenticate', [], $cookie); - $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode()); - $response->assertRedirect(route('index')); - } - - /** - * @covers \FireflyIII\Http\Middleware\RedirectIfTwoFactorAuthenticated - */ - public function testMiddlewareLightAuth(): void - { - $this->be($this->user()); - $response = $this->get('/_test/authenticate'); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); - } -}