F
Filament5mo 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;
});

});
0 Replies
No replies yetBe the first to reply to this messageJoin