Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Add tests for the RequirePassword middleware #30217

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions tests/Integration/Auth/Middleware/RequirePasswordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Illuminate\Tests\Integration\Auth\Middleware;

use Illuminate\Auth\Middleware\RequirePassword;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Http\Response;
use Illuminate\Session\Middleware\StartSession;
use Orchestra\Testbench\TestCase;

class RequirePasswordTest extends TestCase
{
public function testUserSeesTheWantedPageIfThePasswordWasRecentlyConfirmed()
{
$this->withoutExceptionHandling();

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('test-route', function (): Response {
return new Response('foobar');
})->middleware([StartSession::class, RequirePassword::class]);

$response = $this->withSession(['auth.password_confirmed_at' => time()])->get('test-route');

$response->assertOk();
$response->assertSeeText('foobar');
}

public function testUserIsRedirectedToThePasswordConfirmRouteIfThePasswordWasNotRecentlyConfirmed()
{
$this->withoutExceptionHandling();

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('password-confirm', function (): Response {
return new Response('foo');
})->name('password.confirm');

$router->get('test-route', function (): Response {
return new Response('foobar');
})->middleware([StartSession::class, RequirePassword::class]);

$response = $this->withSession(['auth.password_confirmed_at' => time() - 10801])->get('test-route');

$response->assertStatus(302);
$response->assertRedirect($this->app->make(UrlGenerator::class)->route('password.confirm'));
}

public function testUserIsRedirectedToACustomRouteIfThePasswordWasNotRecentlyConfirmedAndTheCustomRouteIsSpecified()
{
$this->withoutExceptionHandling();

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('confirm', function (): Response {
return new Response('foo');
})->name('my-password.confirm');

$router->get('test-route', function (): Response {
return new Response('foobar');
})->middleware([StartSession::class, RequirePassword::class.':my-password.confirm']);

$response = $this->withSession(['auth.password_confirmed_at' => time() - 10801])->get('test-route');

$response->assertStatus(302);
$response->assertRedirect($this->app->make(UrlGenerator::class)->route('my-password.confirm'));
}

public function testAuthPasswordTimeoutIsConfigurable()
{
$this->withoutExceptionHandling();

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('password-confirm', function (): Response {
return new Response('foo');
})->name('password.confirm');

$router->get('test-route', function (): Response {
return new Response('foobar');
})->middleware([StartSession::class, RequirePassword::class]);

$this->app->make(Repository::class)->set('auth.password_timeout', 500);

$response = $this->withSession(['auth.password_confirmed_at' => time() - 495])->get('test-route');

$response->assertOk();
$response->assertSeeText('foobar');

$response = $this->withSession(['auth.password_confirmed_at' => time() - 501])->get('test-route');

$response->assertStatus(302);
$response->assertRedirect($this->app->make(UrlGenerator::class)->route('password.confirm'));
}
}