From 631849039834fe714a0dd10ea2d412edd0ee0812 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 30 Aug 2022 23:30:08 +0200 Subject: [PATCH] Add initiailize middleware --- .../RedirectIfNotInitializedMiddleware.php | 8 ++- tests/Feature/Initialize/MiddlewareTest.php | 63 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/Initialize/MiddlewareTest.php 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('/'); + } +}