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 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
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')]),
);
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.