From f50550d79c9a325b07773bc7087dc360ff7e7d4b Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 25 Jan 2018 20:38:50 +0100 Subject: [PATCH] Fix unit tests --- app/Http/Middleware/AuthenticateTwoFactor.php | 8 +++++- .../Controllers/AttachmentControllerTest.php | 8 ++++-- tests/Unit/Middleware/AuthenticateTest.php | 2 +- .../Middleware/AuthenticateTwoFactorTest.php | 28 +------------------ tests/Unit/Middleware/IsAdminTest.php | 2 +- tests/Unit/Middleware/IsDemoUserTest.php | 17 ++--------- tests/Unit/Middleware/IsSandstormUserTest.php | 2 +- tests/Unit/Middleware/RangeTest.php | 2 +- .../RedirectIf2FAAuthenticatedTest.php | 2 +- .../RedirectIfAuthenticatedTest.php | 2 +- tests/Unit/Middleware/SandstormTest.php | 2 +- 11 files changed, 24 insertions(+), 51 deletions(-) diff --git a/app/Http/Middleware/AuthenticateTwoFactor.php b/app/Http/Middleware/AuthenticateTwoFactor.php index 628f74c3ab..aaa026ee19 100644 --- a/app/Http/Middleware/AuthenticateTwoFactor.php +++ b/app/Http/Middleware/AuthenticateTwoFactor.php @@ -26,7 +26,8 @@ use Closure; use Illuminate\Http\Request; use Log; use Preferences; - +use Auth; +use Session; /** * Class AuthenticateTwoFactor. */ @@ -43,6 +44,11 @@ class AuthenticateTwoFactor */ public function handle(Request $request, Closure $next, $guard = null) { + if (Auth::guard($guard)->guest()) { + + return redirect()->guest('login'); + } + $is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data; $has2faSecret = null !== Preferences::get('twoFactorAuthSecret'); $is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated'); diff --git a/tests/Feature/Controllers/AttachmentControllerTest.php b/tests/Feature/Controllers/AttachmentControllerTest.php index c058da5498..ba7eaa8de5 100644 --- a/tests/Feature/Controllers/AttachmentControllerTest.php +++ b/tests/Feature/Controllers/AttachmentControllerTest.php @@ -123,12 +123,16 @@ class AttachmentControllerTest extends TestCase * @covers \FireflyIII\Http\Controllers\AttachmentController::preview * @covers \FireflyIII\Http\Controllers\AttachmentController::__construct */ - public function testPreview() + public function testView() { + $repository = $this->mock(AttachmentRepositoryInterface::class); + $repository->shouldReceive('exists')->once()->andReturn(true); + $repository->shouldReceive('getContent')->once()->andReturn('This is attachment number one.'); + $journalRepos = $this->mock(JournalRepositoryInterface::class); $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); $this->be($this->user()); - $response = $this->get(route('attachments.preview', [3])); + $response = $this->get(route('attachments.view', [3])); $response->assertStatus(200); } diff --git a/tests/Unit/Middleware/AuthenticateTest.php b/tests/Unit/Middleware/AuthenticateTest.php index a80558f31b..d1936b89e1 100644 --- a/tests/Unit/Middleware/AuthenticateTest.php +++ b/tests/Unit/Middleware/AuthenticateTest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Middleware; use Route; use Symfony\Component\HttpFoundation\Response; diff --git a/tests/Unit/Middleware/AuthenticateTwoFactorTest.php b/tests/Unit/Middleware/AuthenticateTwoFactorTest.php index d863fcb14c..987679bd30 100644 --- a/tests/Unit/Middleware/AuthenticateTwoFactorTest.php +++ b/tests/Unit/Middleware/AuthenticateTwoFactorTest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Middleware; use FireflyIII\Http\Middleware\AuthenticateTwoFactor; use FireflyIII\Models\Preference; @@ -46,32 +46,6 @@ class AuthenticateTwoFactorTest extends TestCase $response->assertRedirect(route('login')); } - /** - * @covers \FireflyIII\Http\Middleware\AuthenticateTwoFactor::handle - */ - public function testMiddlewareAjax() - { - $server = ['HTTP_X-Requested-With' => 'XMLHttpRequest']; - $this->withoutExceptionHandling(); - $response = $this->get('/_test/authenticate', $server); - $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); - } - - /** - * @covers \FireflyIII\Http\Middleware\AuthenticateTwoFactor::handle - */ - public function testMiddlewareBlockedUser() - { - $this->withoutExceptionHandling(); - $user = $this->user(); - $user->blocked = 1; - $this->be($user); - $response = $this->get('/_test/authenticate'); - $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode()); - $response->assertSessionHas('logoutMessage', strval(trans('firefly.block_account_logout'))); - $response->assertRedirect(route('login')); - } - /** * tests for user with no 2FA, should just go to requested page. * diff --git a/tests/Unit/Middleware/IsAdminTest.php b/tests/Unit/Middleware/IsAdminTest.php index 890ad935d8..46b73c6639 100644 --- a/tests/Unit/Middleware/IsAdminTest.php +++ b/tests/Unit/Middleware/IsAdminTest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Middleware; use FireflyIII\Http\Middleware\IsAdmin; use Route; diff --git a/tests/Unit/Middleware/IsDemoUserTest.php b/tests/Unit/Middleware/IsDemoUserTest.php index 5c563c9fbc..51bfefd12d 100644 --- a/tests/Unit/Middleware/IsDemoUserTest.php +++ b/tests/Unit/Middleware/IsDemoUserTest.php @@ -21,9 +21,10 @@ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Middleware; use FireflyIII\Http\Middleware\IsDemoUser; +use FireflyIII\Http\Middleware\StartFireflySession; use Route; use Symfony\Component\HttpFoundation\Response; use Tests\TestCase; @@ -44,19 +45,7 @@ class IsDemoUserTest extends TestCase $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); } - /** - * @covers \FireflyIII\Http\Middleware\IsDemoUser::handle - */ - public function testMiddlewareDemoUser() - { - $this->withoutExceptionHandling(); - $this->be($this->demoUser()); - $response = $this->get('/_test/is-demo'); - $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode()); - $response->assertSessionHas('warning', strval(trans('firefly.not_available_demo_user'))); - $response->assertRedirect(route('index')); - } /** * @covers \FireflyIII\Http\Middleware\IsDemoUser::handle @@ -75,7 +64,7 @@ class IsDemoUserTest extends TestCase { parent::setUp(); - Route::middleware(IsDemoUser::class)->any( + Route::middleware([StartFireflySession::class, IsDemoUser::class])->any( '/_test/is-demo', function () { return 'OK'; } diff --git a/tests/Unit/Middleware/IsSandstormUserTest.php b/tests/Unit/Middleware/IsSandstormUserTest.php index 4d13129d8b..733037d018 100644 --- a/tests/Unit/Middleware/IsSandstormUserTest.php +++ b/tests/Unit/Middleware/IsSandstormUserTest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Middleware; use FireflyIII\Http\Middleware\IsSandStormUser; use Route; diff --git a/tests/Unit/Middleware/RangeTest.php b/tests/Unit/Middleware/RangeTest.php index c064524551..296db2792a 100644 --- a/tests/Unit/Middleware/RangeTest.php +++ b/tests/Unit/Middleware/RangeTest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Middleware; use FireflyIII\Http\Middleware\Range; use Route; diff --git a/tests/Unit/Middleware/RedirectIf2FAAuthenticatedTest.php b/tests/Unit/Middleware/RedirectIf2FAAuthenticatedTest.php index d7c0fdc05c..124cf7a6ac 100644 --- a/tests/Unit/Middleware/RedirectIf2FAAuthenticatedTest.php +++ b/tests/Unit/Middleware/RedirectIf2FAAuthenticatedTest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Middleware; use FireflyIII\Http\Middleware\RedirectIfTwoFactorAuthenticated; use FireflyIII\Models\Preference; diff --git a/tests/Unit/Middleware/RedirectIfAuthenticatedTest.php b/tests/Unit/Middleware/RedirectIfAuthenticatedTest.php index e6b7cd6684..42093c8000 100644 --- a/tests/Unit/Middleware/RedirectIfAuthenticatedTest.php +++ b/tests/Unit/Middleware/RedirectIfAuthenticatedTest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Middleware; use FireflyIII\Http\Middleware\RedirectIfAuthenticated; use Route; diff --git a/tests/Unit/Middleware/SandstormTest.php b/tests/Unit/Middleware/SandstormTest.php index 91dab83d91..a8fe88b950 100644 --- a/tests/Unit/Middleware/SandstormTest.php +++ b/tests/Unit/Middleware/SandstormTest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Middleware; use FireflyIII\Http\Middleware\Sandstorm; use FireflyIII\Models\Role;