Testing your application
The package proves its answers in its own suite. Your application can change them: a custom answer, a sign-in page that shows the status, a form request with more fields. So the package ships the assertion it uses, for your suite to prove the property where it matters.
The assertion
Pushery\FortifyHardening\Testing\AssertsEnumerationSafety provides assertEnumerationSafe(). It
sends the same request once for an identity that has an account and once for one that has none,
runs your own expectation on each response, and then requires the two to be indistinguishable to
the caller.
public function assertEnumerationSafe(
Closure $submit, // sends the request for one identity, returns the TestResponse
string $known, // an identity with an account
string $unknown, // an identity without one
?Closure $expect = null, // your expectation, run on each response
): void
Indistinguishable means the same fingerprint, which compares five things:
| Field | What it is |
|---|---|
status | the HTTP status |
location | the redirect target |
status_ | the status the response flashed to the session |
errors | the validation errors it flashed |
body | the body of a JSON response |
The body of an HTML page is left out, because a page carries a CSRF token of its own on every request.
A JSON error rendered with APP_DEBUG=true carries exception, file, line and trace, which
production never sends, and two requests that both name nothing often fail on different paths
through the code. A body that carries exception and trace is compared without those four, so a
suite that runs in debug compares what a client gets. A field of that name in any other body stays
in the comparison.
What the caller sent, and what Livewire draws at random, are not a difference. The identity a
request sent reads <identity> wherever the response repeats it: in the status line, in an error,
in the redirect target and in the body. A Livewire update response is compared by what its
components say: each snapshot is read as data without its checksum, and the ids Livewire drew for
the components read <component-id>. Everything else stays in the comparison, so a component that
sets a field for the known identity alone still fails.
enumerationFingerprint() is public and static, should you want to compare fingerprints yourself;
pass the identity the request sent as its second argument to get the placeholders. It reads the
session the way Laravel's own session assertions do, so it also works with session.serialization
set to json.
With Pest
use App\Models\User;
use Illuminate\Testing\TestResponse;
use Pushery\FortifyHardening\Testing\AssertsEnumerationSafety;
uses(AssertsEnumerationSafety::class);
it('answers a taken address as it answers a free one', function () {
User::factory()->create(['email' => 'taken@example.com']);
$this->assertEnumerationSafe(
fn (string $email): TestResponse => $this->post('/register', [
'name' => 'Grace',
'email' => $email,
'password' => 'a-long-password',
'password_confirmation' => 'a-long-password',
]),
'taken@example.com',
'free@example.com',
function (TestResponse $response): void {
$response->assertRedirect(route('login'))->assertSessionHasNoErrors();
$this->assertGuest();
},
);
});
With PHPUnit
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Testing\TestResponse;
use Pushery\FortifyHardening\Testing\AssertsEnumerationSafety;
use Tests\TestCase;
class PasswordResetLinkTest extends TestCase
{
use AssertsEnumerationSafety;
public function test_an_unknown_address_reads_like_a_sent_link(): void
{
User::factory()->create(['email' => 'known@example.com']);
$this->assertEnumerationSafe(
fn (string $email): TestResponse => $this->from('/forgot-password')
->post('/forgot-password', ['email' => $email]),
'known@example.com',
'unknown@example.com',
fn (TestResponse $response) => $response->assertSessionHas('status', __('passwords.sent')),
);
}
}
The password reset
A reset needs a token, and the property holds for a wrong one:
$this->assertEnumerationSafe(
fn (string $email): TestResponse => $this->from('/reset-password/a-token')->post('/reset-password', [
'token' => 'not-the-token',
'email' => $email,
'password' => 'a-new-password',
'password_confirmation' => 'a-new-password',
]),
'known@example.com',
'unknown@example.com',
fn (TestResponse $response) => $response->assertSessionHasErrors(['email' => __('passwords.token')]),
);
Waits under a faked sleep
Laravel waits out the minimum duration through Illuminate\Support\Sleep. In a test that calls
Sleep::fake(), each wait is recorded instead of slept, and no time passes.
A failed sign-in records one or two waits of the minimum there, depending on the path it takes and
on the Fortify release. Where Laravel or Fortify already holds a step to the minimum, the package
holds the same step again from outside. In a real request the package's hold finds the minimum
spent and returns at once. Under the fake no time has passed, so it records a second wait. From
Fortify 1.44.0 on, a failure in Fortify's two-factor step or through a callback set with
Fortify::authenticateUsing() records two waits, where an earlier release recorded one. A failure
in a two-factor step your application binds records one on every release.
A test that should keep passing when Fortify is updated expects at least one wait of the minimum,
not exactly one. Sleep::assertSlept() asserts an exact count, so count the waits as they are
recorded:
use Carbon\CarbonInterval;
use Illuminate\Support\Sleep;
it('holds a failed sign-in to the minimum', function () {
Sleep::fake();
$waits = 0;
Sleep::whenFakingSleep(function (CarbonInterval $wait) use (&$waits): void {
if ($wait->totalMicroseconds >= 900_000) {
$waits++;
}
});
$this->post('/login', ['email' => 'nobody@example.com', 'password' => 'a-wrong-password'])
->assertSessionHasErrors('email');
expect($waits)->toBeGreaterThanOrEqual(1);
});
A wait of at least 900 milliseconds counts as the minimum here: the package's one second, less the
time the request took before the wait. If your application sets its own auth.timebox_duration,
compare against that value.
Proving that it can fail
An assertion that has only ever passed proves little. Run it once against an answer that does tell the cases apart, for example with the package's switch for that route turned off, and watch it fail before you rely on it.