diff --git a/app/Http/Middleware/RedirectIfNotInitializedMiddleware.php b/app/Http/Middleware/RedirectIfNotInitializedMiddleware.php index e5e981f0..e695ff36 100644 --- a/app/Http/Middleware/RedirectIfNotInitializedMiddleware.php +++ b/app/Http/Middleware/RedirectIfNotInitializedMiddleware.php @@ -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; } } diff --git a/tests/Feature/Initialize/MiddlewareTest.php b/tests/Feature/Initialize/MiddlewareTest.php new file mode 100644 index 00000000..5f8b1056 --- /dev/null +++ b/tests/Feature/Initialize/MiddlewareTest.php @@ -0,0 +1,63 @@ +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('/'); + } +}