F
Filament14mo ago
Harry

Trying to Test Password Change Functionality

Hi all - I would love some help figuring out how to write a pest test to test and simulate the flow of a user updating their password after forgetting. Here is the test I have so far, but it's always failling at the assertion of the hash result at the end (suggesting that the password change is not actually being saved to the database). I appreciate any help available. Code is below:
test('password can be reset with valid token', function () {
Notification::fake();

$user = User::factory()->create();
$user->save();

$response = $this
->get(route('filament.app.auth.password-reset.request'));

$response->assertStatus(200);

// Assert we see the email field
$response->assertSee([
'input',
'id="data.email"'
], false);

Livewire::test(RequestPasswordReset::class)
->set('data.email', $user->email)
->call('request')
->assertHasNoErrors();

Notification::assertSentTo($user, ResetPasswordNotification::class, function ($notification) use ($user) {
// Now that we've requested a password reset, we can test the reset password screen with the token received

$this->assertNotNull($notification->token);

$this->get($notification->url)
->assertStatus(200);

$newPassword = 'new-password-x';

Livewire::test(ResetPassword::class)
->set('password', $newPassword)
->set('passwordConfirmation', $newPassword)
->call('resetPassword')
->assertHasNoErrors();

$hashResult = Hash::check($newPassword, $user->refresh()->password);

// Assert that the password has been updated
$this->assertTrue($hashResult);

return true;
});

});
test('password can be reset with valid token', function () {
Notification::fake();

$user = User::factory()->create();
$user->save();

$response = $this
->get(route('filament.app.auth.password-reset.request'));

$response->assertStatus(200);

// Assert we see the email field
$response->assertSee([
'input',
'id="data.email"'
], false);

Livewire::test(RequestPasswordReset::class)
->set('data.email', $user->email)
->call('request')
->assertHasNoErrors();

Notification::assertSentTo($user, ResetPasswordNotification::class, function ($notification) use ($user) {
// Now that we've requested a password reset, we can test the reset password screen with the token received

$this->assertNotNull($notification->token);

$this->get($notification->url)
->assertStatus(200);

$newPassword = 'new-password-x';

Livewire::test(ResetPassword::class)
->set('password', $newPassword)
->set('passwordConfirmation', $newPassword)
->call('resetPassword')
->assertHasNoErrors();

$hashResult = Hash::check($newPassword, $user->refresh()->password);

// Assert that the password has been updated
$this->assertTrue($hashResult);

return true;
});

});
1 Reply
stepanek
stepanek2mo ago
it('can reset a password ', function () { Notification::fake(); $user = User::factory()->create(); $token = app(PasswordBroker::class)->createToken($user); $url = Filament::getResetPasswordUrl($token, $user); $user->notify(new ResetPasswordNotification($token)); Notification::assertSentTo($user, ResetPasswordNotification::class); $this->get($url)->assertOk(); $password = 'password&TEST123!'; livewire(ResetPassword::class, [ 'token' => $token, 'email' => $user->email, ]) ->assertSuccessful() ->set('password', $password) ->set('passwordConfirmation', $password) ->call('resetPassword'); $this->assertTrue(Hash::check($password, $user->fresh()->password)); }); Hey i experienced the same problem and managed to fix it by loading the token and email into the ResetPassword component.
livewire(ResetPassword::class, [ 'token' => $token, 'email' => $user->email, ])...

Did you find this page helpful?