mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Refactor tests and code to handle new 2FA methods.
This commit is contained in:
@@ -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);
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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 = [
|
||||
|
@@ -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);
|
||||
|
@@ -1,196 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* AuthenticateTwoFactorTest.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
@@ -1,96 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* RedirectIf2FAAuthenticatedTest.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user