Add initiailize middleware

This commit is contained in:
philipp lang 2022-08-30 23:30:08 +02:00
parent 09336537e4
commit 6318490398
2 changed files with 70 additions and 1 deletions

View File

@ -2,6 +2,8 @@
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use App\Setting\NamiSettings;
use Closure;
class RedirectIfNotInitializedMiddleware
@ -17,6 +19,10 @@ class RedirectIfNotInitializedMiddleware
*/
public function handle($request, Closure $next)
{
if ($this->initialized() && request()->routeIs(['initialize.form'])) {
return redirect()->to(RouteServiceProvider::HOME);
}
if (!$this->shouldRedirect()) {
return $next($request);
}
@ -35,6 +41,6 @@ class RedirectIfNotInitializedMiddleware
public function initialized(): bool
{
return \App\Fee::count() > 0;
return 0 !== app(NamiSettings::class)->default_group_id;
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace Tests\Feature\Initialize;
use App\Setting\NamiSettings;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class MiddlewareTest extends TestCase
{
use DatabaseTransactions;
public function testItRedirectsToInitializeRouteWhenNotInitialized(): void
{
$this->login();
$response = $this->get('/');
$response->assertRedirect('/initialize');
}
public function testItDoesntRedirctIfUserIsGuest(): void
{
$response = $this->get('/');
$response->assertRedirect('/login');
}
public function testItDoesntRedirectToInitializeRoute(): void
{
$this->login();
$response = $this->get('/initialize');
$response->assertStatus(200);
}
public function testItDoesntRedirectWhenAlreadyInitialized(): void
{
NamiSettings::fake([
'mglnr' => 333,
'password' => 'secret',
'default_group_id' => 555,
]);
$this->login();
$response = $this->get('/');
$response->assertStatus(200);
}
public function testItRedirectsHomeWhenAlreadyInitialized(): void
{
NamiSettings::fake([
'mglnr' => 333,
'password' => 'secret',
'default_group_id' => 555,
]);
$this->login();
$response = $this->get('/initialize');
$response->assertRedirect('/');
}
}