mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 18:41:08 +00:00
Improve testing of middleware.
This commit is contained in:
@@ -89,6 +89,9 @@ class Range
|
|||||||
View::share('listLength', $pref);
|
View::share('listLength', $pref);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
private function configureView()
|
private function configureView()
|
||||||
{
|
{
|
||||||
$pref = Preferences::get('language', config('firefly.default_language', 'en_US'));
|
$pref = Preferences::get('language', config('firefly.default_language', 'en_US'));
|
||||||
@@ -103,7 +106,7 @@ class Range
|
|||||||
|
|
||||||
// send error to view if could not set money format
|
// send error to view if could not set money format
|
||||||
if (false === $moneyResult) {
|
if (false === $moneyResult) {
|
||||||
View::share('invalidMonetaryLocale', true);
|
View::share('invalidMonetaryLocale', true); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
// save some formats:
|
// save some formats:
|
||||||
|
@@ -43,7 +43,7 @@ class RedirectIfAuthenticated
|
|||||||
public function handle($request, Closure $next, $guard = null)
|
public function handle($request, Closure $next, $guard = null)
|
||||||
{
|
{
|
||||||
if (Auth::guard($guard)->check()) {
|
if (Auth::guard($guard)->check()) {
|
||||||
return redirect('/home');
|
return redirect(route('index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $next($request);
|
return $next($request);
|
||||||
|
@@ -44,13 +44,14 @@ class RedirectIfTwoFactorAuthenticated
|
|||||||
{
|
{
|
||||||
if (Auth::guard($guard)->check()) {
|
if (Auth::guard($guard)->check()) {
|
||||||
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data;
|
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data;
|
||||||
|
|
||||||
$has2faSecret = null !== Preferences::get('twoFactorAuthSecret');
|
$has2faSecret = null !== Preferences::get('twoFactorAuthSecret');
|
||||||
|
|
||||||
// grab 2auth information from cookie.
|
// grab 2auth information from cookie.
|
||||||
$is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated');
|
$is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated');
|
||||||
|
|
||||||
if ($is2faEnabled && $has2faSecret && $is2faAuthed) {
|
if ($is2faEnabled && $has2faSecret && $is2faAuthed) {
|
||||||
return redirect('/');
|
return redirect(route('index'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -25,10 +25,10 @@ namespace FireflyIII\Http\Middleware;
|
|||||||
use Auth;
|
use Auth;
|
||||||
use Closure;
|
use Closure;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\Role;
|
|
||||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Log;
|
||||||
use View;
|
use View;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,6 +63,7 @@ class Sandstorm
|
|||||||
/** @var UserRepositoryInterface $repository */
|
/** @var UserRepositoryInterface $repository */
|
||||||
$repository = app(UserRepositoryInterface::class);
|
$repository = app(UserRepositoryInterface::class);
|
||||||
$userId = strval($request->header('X-Sandstorm-User-Id'));
|
$userId = strval($request->header('X-Sandstorm-User-Id'));
|
||||||
|
Log::debug(sprintf('Sandstorm user ID is "%s"', $userId));
|
||||||
$count = $repository->count();
|
$count = $repository->count();
|
||||||
|
|
||||||
// if there already is one user in this instance, we assume this is
|
// if there already is one user in this instance, we assume this is
|
||||||
@@ -72,7 +73,7 @@ class Sandstorm
|
|||||||
// and any other differences there may be between these users.
|
// and any other differences there may be between these users.
|
||||||
if (1 === $count && strlen($userId) > 0) {
|
if (1 === $count && strlen($userId) > 0) {
|
||||||
// login as first user user.
|
// login as first user user.
|
||||||
$user = User::first();
|
$user = $repository->first();
|
||||||
Auth::guard($guard)->login($user);
|
Auth::guard($guard)->login($user);
|
||||||
View::share('SANDSTORM_ANON', false);
|
View::share('SANDSTORM_ANON', false);
|
||||||
|
|
||||||
@@ -92,7 +93,7 @@ class Sandstorm
|
|||||||
// create new user.
|
// create new user.
|
||||||
$email = $userId . '@firefly';
|
$email = $userId . '@firefly';
|
||||||
/** @var User $user */
|
/** @var User $user */
|
||||||
$user = User::create(
|
$user = $repository->store(
|
||||||
[
|
[
|
||||||
'email' => $email,
|
'email' => $email,
|
||||||
'password' => str_random(16),
|
'password' => str_random(16),
|
||||||
@@ -101,9 +102,10 @@ class Sandstorm
|
|||||||
Auth::guard($guard)->login($user);
|
Auth::guard($guard)->login($user);
|
||||||
|
|
||||||
// also make the user an admin
|
// also make the user an admin
|
||||||
$admin = Role::where('name', 'owner')->first();
|
$repository->attachRole($user, 'owner');
|
||||||
$user->attachRole($admin);
|
|
||||||
$user->save();
|
// share value.
|
||||||
|
View::share('SANDSTORM_ANON', false);
|
||||||
|
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
@@ -116,6 +118,14 @@ class Sandstorm
|
|||||||
throw new FireflyException('Your Firefly III installation has more than one user, which is weird.');
|
throw new FireflyException('Your Firefly III installation has more than one user, which is weird.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// if in Sandstorm, user logged in, still must check if user is anon.
|
||||||
|
$userId = strval($request->header('X-Sandstorm-User-Id'));
|
||||||
|
if (strlen($userId) === 0) {
|
||||||
|
View::share('SANDSTORM_ANON', true);
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
View::share('SANDSTORM_ANON', false);
|
||||||
|
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
@@ -168,6 +168,16 @@ class UserRepository implements UserRepositoryInterface
|
|||||||
return User::where('email', $email)->first();
|
return User::where('email', $email)->first();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first user in the DB. Generally only works when there is just one.
|
||||||
|
*
|
||||||
|
* @return null|User
|
||||||
|
*/
|
||||||
|
public function first(): ?User
|
||||||
|
{
|
||||||
|
return User::first();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return basic user information.
|
* Return basic user information.
|
||||||
*
|
*
|
||||||
@@ -225,6 +235,23 @@ class UserRepository implements UserRepositoryInterface
|
|||||||
return $user->hasRole($role);
|
return $user->hasRole($role);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return User
|
||||||
|
*/
|
||||||
|
public function store(array $data): User
|
||||||
|
{
|
||||||
|
$password = bcrypt($data['password'] ?? app('str')->random(16));
|
||||||
|
|
||||||
|
return User::create(
|
||||||
|
[
|
||||||
|
'email' => $data['email'],
|
||||||
|
'password' => $password,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param User $user
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
|
@@ -37,6 +37,20 @@ interface UserRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function all(): Collection;
|
public function all(): Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first user in the DB. Generally only works when there is just one.
|
||||||
|
*
|
||||||
|
* @return null|User
|
||||||
|
*/
|
||||||
|
public function first(): ?User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return User
|
||||||
|
*/
|
||||||
|
public function store(array $data): User;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gives a user a role.
|
* Gives a user a role.
|
||||||
*
|
*
|
||||||
|
2
resources/views/test/test.twig
Normal file
2
resources/views/test/test.twig
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
list-length: {{ listLength }}
|
||||||
|
sandstorm-anon: {% if SANDSTORM_ANON %}true{% else %}false{% endif %}
|
@@ -88,7 +88,17 @@ abstract class TestCase extends BaseTestCase
|
|||||||
/**
|
/**
|
||||||
* @return User
|
* @return User
|
||||||
*/
|
*/
|
||||||
public function emptyUser()
|
public function demoUser(): User
|
||||||
|
{
|
||||||
|
$user = User::find(4);
|
||||||
|
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return User
|
||||||
|
*/
|
||||||
|
public function emptyUser(): User
|
||||||
{
|
{
|
||||||
$user = User::find(2);
|
$user = User::find(2);
|
||||||
|
|
||||||
@@ -98,7 +108,7 @@ abstract class TestCase extends BaseTestCase
|
|||||||
/**
|
/**
|
||||||
* @return User
|
* @return User
|
||||||
*/
|
*/
|
||||||
public function user()
|
public function user(): User
|
||||||
{
|
{
|
||||||
$user = User::find(1);
|
$user = User::find(1);
|
||||||
|
|
||||||
|
94
tests/Unit/Middleware/IsAdminTest.php
Normal file
94
tests/Unit/Middleware/IsAdminTest.php
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* IsAdminTest.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\Helpers;
|
||||||
|
|
||||||
|
use FireflyIII\Http\Middleware\IsAdmin;
|
||||||
|
use Route;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class IsAdminTest
|
||||||
|
*/
|
||||||
|
class IsAdminTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\IsAdmin::handle
|
||||||
|
*/
|
||||||
|
public function testMiddleware()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$response = $this->get('/_test/is-admin');
|
||||||
|
$this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode());
|
||||||
|
$response->assertRedirect(route('login'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\IsAdmin::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareAjax()
|
||||||
|
{
|
||||||
|
$server = ['HTTP_X-Requested-With' => 'XMLHttpRequest'];
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$response = $this->get('/_test/is-admin', $server);
|
||||||
|
$this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\IsAdmin::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareOwner()
|
||||||
|
{
|
||||||
|
$this->be($this->user());
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$response = $this->get('/_test/is-admin');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\IsAdmin::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareNotOwner()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$this->be($this->emptyUser());
|
||||||
|
$response = $this->get('/_test/is-admin');
|
||||||
|
$this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode());
|
||||||
|
$response->assertRedirect(route('home'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up test
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Route::middleware(IsAdmin::class)->any(
|
||||||
|
'/_test/is-admin', function () {
|
||||||
|
return 'OK';
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
84
tests/Unit/Middleware/IsDemoUserTest.php
Normal file
84
tests/Unit/Middleware/IsDemoUserTest.php
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* IsDemoUserTest.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\Helpers;
|
||||||
|
|
||||||
|
use FireflyIII\Http\Middleware\IsDemoUser;
|
||||||
|
use Route;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class IsDemoUserTest
|
||||||
|
*/
|
||||||
|
class IsDemoUserTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\IsDemoUser::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareNotAuthenticated()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$response = $this->get('/_test/is-demo');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\IsDemoUser::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareAuthenticated()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$this->be($this->user());
|
||||||
|
$response = $this->get('/_test/is-demo');
|
||||||
|
$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'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up test
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Route::middleware(IsDemoUser::class)->any(
|
||||||
|
'/_test/is-demo', function () {
|
||||||
|
return 'OK';
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
87
tests/Unit/Middleware/IsSandstormUserTest.php
Normal file
87
tests/Unit/Middleware/IsSandstormUserTest.php
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* IsSandstormUserTest.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\Helpers;
|
||||||
|
|
||||||
|
use FireflyIII\Http\Middleware\IsDemoUser;
|
||||||
|
use FireflyIII\Http\Middleware\IsSandStormUser;
|
||||||
|
use Route;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class IsSandstormUserTest
|
||||||
|
*/
|
||||||
|
class IsSandstormUserTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\IsSandStormUser::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareNotAuthenticated()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$response = $this->get('/_test/is-sandstorm');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\IsSandStormUser::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareNotSandStorm()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$this->be($this->user());
|
||||||
|
$response = $this->get('/_test/is-sandstorm');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\IsSandStormUser::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareSandstorm()
|
||||||
|
{
|
||||||
|
putenv('SANDSTORM=1');
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$this->be($this->user());
|
||||||
|
$response = $this->get('/_test/is-sandstorm');
|
||||||
|
|
||||||
|
$this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode());
|
||||||
|
$response->assertSessionHas('warning', strval(trans('firefly.sandstorm_not_available')));
|
||||||
|
$response->assertRedirect(route('index'));
|
||||||
|
putenv('SANDSTORM=0');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up test
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Route::middleware(IsSandStormUser::class)->any(
|
||||||
|
'/_test/is-sandstorm', function () {
|
||||||
|
return 'OK';
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
80
tests/Unit/Middleware/RangeTest.php
Normal file
80
tests/Unit/Middleware/RangeTest.php
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* RangeTest.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\Helpers;
|
||||||
|
|
||||||
|
use FireflyIII\Http\Middleware\Range;
|
||||||
|
use Route;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class RangeTest
|
||||||
|
*/
|
||||||
|
class RangeTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Range::handle
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Range::__construct
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Range::configureList
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Range::configureView
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Range::setRange
|
||||||
|
*/
|
||||||
|
public function testMiddlewareAuthenticated()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$this->be($this->user());
|
||||||
|
$response = $this->get('/_test/range');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
|
||||||
|
// view has list length
|
||||||
|
$response->assertSeeText('list-length: 10');
|
||||||
|
|
||||||
|
// assert some session stuff?
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Range::handle
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Range::__construct
|
||||||
|
*/
|
||||||
|
public function testMiddlewareNotAuthenticated()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$response = $this->get('/_test/range');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up test
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Route::middleware(Range::class)->any(
|
||||||
|
'/_test/range', function () {
|
||||||
|
return view('test.test');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
94
tests/Unit/Middleware/RedirectIf2FAAuthenticatedTest.php
Normal file
94
tests/Unit/Middleware/RedirectIf2FAAuthenticatedTest.php
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
<?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\Helpers;
|
||||||
|
|
||||||
|
use FireflyIII\Http\Middleware\RedirectIfTwoFactorAuthenticated;
|
||||||
|
use FireflyIII\Models\Preference;
|
||||||
|
use Preferences;
|
||||||
|
use Route;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class RedirectIf2FAAuthenticatedTest
|
||||||
|
*/
|
||||||
|
class RedirectIf2FAAuthenticatedTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\RedirectIfTwoFactorAuthenticated::handle
|
||||||
|
*/
|
||||||
|
public function testMiddleware()
|
||||||
|
{
|
||||||
|
$response = $this->get('/_test/authenticate');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\RedirectIfTwoFactorAuthenticated::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareAuthenticated()
|
||||||
|
{
|
||||||
|
// 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::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareLightAuth()
|
||||||
|
{
|
||||||
|
$this->be($this->user());
|
||||||
|
$response = $this->get('/_test/authenticate');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up test
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Route::middleware(RedirectIfTwoFactorAuthenticated::class)->any(
|
||||||
|
'/_test/authenticate', function () {
|
||||||
|
return 'OK';
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
69
tests/Unit/Middleware/RedirectIfAuthenticatedTest.php
Normal file
69
tests/Unit/Middleware/RedirectIfAuthenticatedTest.php
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* RedirectIfAuthenticatedTest.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\Helpers;
|
||||||
|
|
||||||
|
use FireflyIII\Http\Middleware\RedirectIfAuthenticated;
|
||||||
|
use Route;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class RedirectIfAuthenticatedTest
|
||||||
|
*/
|
||||||
|
class RedirectIfAuthenticatedTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\RedirectIfAuthenticated::handle
|
||||||
|
*/
|
||||||
|
public function testMiddleware()
|
||||||
|
{
|
||||||
|
$response = $this->get('/_test/authenticate');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\RedirectIfAuthenticated::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareAuthenticated()
|
||||||
|
{
|
||||||
|
$this->be($this->user());
|
||||||
|
$response = $this->get('/_test/authenticate');
|
||||||
|
$this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode());
|
||||||
|
$response->assertRedirect(route('index'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up test
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Route::middleware(RedirectIfAuthenticated::class)->any(
|
||||||
|
'/_test/authenticate', function () {
|
||||||
|
return 'OK';
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
178
tests/Unit/Middleware/SandstormTest.php
Normal file
178
tests/Unit/Middleware/SandstormTest.php
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* SandstormTest.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\Helpers;
|
||||||
|
|
||||||
|
use FireflyIII\Http\Middleware\Sandstorm;
|
||||||
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||||
|
use Route;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class RangeTest
|
||||||
|
*/
|
||||||
|
class SandstormTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareAnonEmpty()
|
||||||
|
{
|
||||||
|
putenv('SANDSTORM=1');
|
||||||
|
|
||||||
|
$repository = $this->mock(UserRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('count')->once()->andReturn(0);
|
||||||
|
|
||||||
|
$response = $this->get('/_test/sandstorm');
|
||||||
|
$this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode());
|
||||||
|
$response->assertSee('The first visit to a new Firefly III administration cannot be by a guest user.');
|
||||||
|
|
||||||
|
putenv('SANDSTORM=0');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareAnonUser()
|
||||||
|
{
|
||||||
|
putenv('SANDSTORM=1');
|
||||||
|
|
||||||
|
$repository = $this->mock(UserRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('count')->once()->andReturn(1);
|
||||||
|
|
||||||
|
$response = $this->get('/_test/sandstorm');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
$response->assertSee('sandstorm-anon: true');
|
||||||
|
|
||||||
|
putenv('SANDSTORM=0');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareLoggedIn()
|
||||||
|
{
|
||||||
|
putenv('SANDSTORM=1');
|
||||||
|
|
||||||
|
$this->be($this->user());
|
||||||
|
$response = $this->get('/_test/sandstorm', ['X-Sandstorm-User-Id' => 'abcd']);
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
$response->assertSee('sandstorm-anon: false');
|
||||||
|
|
||||||
|
putenv('SANDSTORM=0');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareAnonLoggedIn()
|
||||||
|
{
|
||||||
|
putenv('SANDSTORM=1');
|
||||||
|
|
||||||
|
$this->be($this->user());
|
||||||
|
$response = $this->get('/_test/sandstorm');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
$response->assertSee('sandstorm-anon: true');
|
||||||
|
|
||||||
|
putenv('SANDSTORM=0');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareMultiUser()
|
||||||
|
{
|
||||||
|
putenv('SANDSTORM=1');
|
||||||
|
|
||||||
|
$repository = $this->mock(UserRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('count')->once()->andReturn(2);
|
||||||
|
|
||||||
|
$response = $this->get('/_test/sandstorm');
|
||||||
|
$this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode());
|
||||||
|
$response->assertSee('Your Firefly III installation has more than one user, which is weird.');
|
||||||
|
|
||||||
|
putenv('SANDSTORM=0');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareNoUser()
|
||||||
|
{
|
||||||
|
putenv('SANDSTORM=1');
|
||||||
|
|
||||||
|
$repository = $this->mock(UserRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('count')->once()->andReturn(0);
|
||||||
|
$repository->shouldReceive('store')->once()->andReturn($this->user());
|
||||||
|
$repository->shouldReceive('attachRole')->once()->andReturn(true);
|
||||||
|
|
||||||
|
$response = $this->get('/_test/sandstorm', ['X-Sandstorm-User-Id' => 'abcd']);
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
$response->assertSee('sandstorm-anon: false');
|
||||||
|
|
||||||
|
putenv('SANDSTORM=0');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareNotSandstorm()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$response = $this->get('/_test/sandstorm');
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
|
||||||
|
*/
|
||||||
|
public function testMiddlewareOneUser()
|
||||||
|
{
|
||||||
|
putenv('SANDSTORM=1');
|
||||||
|
|
||||||
|
$repository = $this->mock(UserRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('count')->once()->andReturn(1);
|
||||||
|
$repository->shouldReceive('first')->once()->andReturn($this->user());
|
||||||
|
|
||||||
|
$response = $this->get('/_test/sandstorm', ['X-Sandstorm-User-Id' => 'abcd']);
|
||||||
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||||
|
$response->assertSee('sandstorm-anon: false');
|
||||||
|
|
||||||
|
putenv('SANDSTORM=0');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up test
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Route::middleware(Sandstorm::class)->any(
|
||||||
|
'/_test/sandstorm', function () {
|
||||||
|
return view('test.test');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user