From 3bb81bb5cebba17d25c2bee527134c0a4d38cc1b Mon Sep 17 00:00:00 2001 From: DariusIII Date: Fri, 10 Apr 2026 16:54:08 +0200 Subject: [PATCH] Fix redirect issues that came from laravel --- bootstrap/app.php | 6 +- tests/Feature/AuthRedirectMiddlewareTest.php | 117 +++++++++++++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/AuthRedirectMiddlewareTest.php diff --git a/bootstrap/app.php b/bootstrap/app.php index 4641fcd0d..467663009 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -44,8 +44,10 @@ return Application::configure(basePath: dirname(__DIR__)) }, ) ->withMiddleware(function (Middleware $middleware) { - $middleware->redirectGuestsTo(fn () => route('login')); - $middleware->redirectUsersTo('/'); + $middleware->redirectTo( + guests: fn () => route('login'), + users: '/', + ); $middleware->validateCsrfTokens(except: [ 'failed', diff --git a/tests/Feature/AuthRedirectMiddlewareTest.php b/tests/Feature/AuthRedirectMiddlewareTest.php new file mode 100644 index 000000000..193d50730 --- /dev/null +++ b/tests/Feature/AuthRedirectMiddlewareTest.php @@ -0,0 +1,117 @@ + + */ + private array $originalEnvironment = []; + + public function createApplication() + { + $this->databasePath = sys_get_temp_dir().'/nntmux-auth-redirect-test.sqlite'; + + $this->originalEnvironment = [ + 'APP_ENV' => getenv('APP_ENV'), + 'DB_CONNECTION' => getenv('DB_CONNECTION'), + 'DB_DATABASE' => getenv('DB_DATABASE'), + ]; + + if (file_exists($this->databasePath)) { + unlink($this->databasePath); + } + + $pdo = new PDO('sqlite:'.$this->databasePath); + $pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)'); + $pdo->exec("INSERT INTO settings (name, value) VALUES + ('categorizeforeign', '0'), + ('catwebdl', '0'), + ('title', 'NNTmux Test'), + ('home_link', '/')"); + + $this->setEnvironmentValue('APP_ENV', 'testing'); + $this->setEnvironmentValue('DB_CONNECTION', 'sqlite'); + $this->setEnvironmentValue('DB_DATABASE', $this->databasePath); + + $app = require __DIR__.'/../../bootstrap/app.php'; + + $app->make(Kernel::class)->bootstrap(); + + return $app; + } + + protected function setUp(): void + { + parent::setUp(); + + config([ + 'database.default' => 'sqlite', + 'database.connections.sqlite.database' => $this->databasePath, + 'app.key' => 'base64:'.base64_encode(random_bytes(32)), + ]); + + Cache::flush(); + } + + protected function tearDown(): void + { + if ($this->databasePath !== '' && file_exists($this->databasePath)) { + unlink($this->databasePath); + } + + parent::tearDown(); + + foreach ($this->originalEnvironment as $key => $value) { + $this->setEnvironmentValue($key, $value === false ? null : $value); + } + } + + public function test_guest_is_redirected_to_login_for_auth_middleware_routes(): void + { + $this->get(route('verification.notice')) + ->assertRedirect(route('login')); + } + + public function test_json_guest_requests_receive_unauthorized_instead_of_html_redirects(): void + { + $this->getJson(route('verification.notice')) + ->assertUnauthorized(); + } + + public function test_authenticated_users_are_redirected_away_from_guest_routes(): void + { + $this->actingAs(new GenericUser([ + 'id' => 1, + 'email' => 'member@example.test', + 'password' => 'test-password', + ])) + ->get(route('login')) + ->assertRedirect('/'); + } + + private function setEnvironmentValue(string $key, ?string $value): void + { + if ($value === null) { + putenv($key); + unset($_ENV[$key], $_SERVER[$key]); + + return; + } + + putenv($key.'='.$value); + $_ENV[$key] = $value; + $_SERVER[$key] = $value; + } +}