1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
<?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 testItCanGenerateDefinitionViaStaticMethod()
{
$signature = (string) RequirePassword::using('route.name');
$this->assertSame('Illuminate\Auth\Middleware\RequirePassword:route.name', $signature);
$signature = (string) RequirePassword::using('route.name', 100);
$this->assertSame('Illuminate\Auth\Middleware\RequirePassword:route.name,100', $signature);
$signature = (string) RequirePassword::using(passwordTimeoutSeconds: 100);
$this->assertSame('Illuminate\Auth\Middleware\RequirePassword:,100', $signature);
}
public function testUserSeesTheWantedPageIfThePasswordWasRecentlyConfirmed()
{
$this->withoutExceptionHandling();
/** @var \Illuminate\Contracts\Routing\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 \Illuminate\Contracts\Routing\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 \Illuminate\Contracts\Routing\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 \Illuminate\Contracts\Routing\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'));
}
}
|