forgetDrivers(); app()->forgetInstance('session.store'); test()->flushSession(); } /** * Sign the admin in on a database session, use it once as an admin would, and return its ID. */ function signedInAdminSessionId(User $admin): string { config(['session.driver' => 'database']); $sessionId = test()->post(route('login.store'), ['email' => $admin->email, 'password' => 'Old-Password-1!']) ->assertRedirect() ->getCookie(config('session.cookie')) ->getValue(); forgetRequestState(); test()->withCookie(config('session.cookie'), $sessionId)->get(route('admin.dashboard'))->assertOk(); forgetRequestState(); return $sessionId; } test('password can be updated', function () { $user = User::factory()->create([ 'password' => Hash::make('password'), ]); $this->actingAs($user); $response = Livewire::test('pages::settings.password') ->set('current_password', 'password') ->set('password', 'new-password') ->set('password_confirmation', 'new-password') ->call('updatePassword'); $response->assertHasNoErrors() ->assertDispatched('toast', type: 'success', title: 'Saved.'); expect(Hash::check('new-password', $user->refresh()->password))->toBeTrue(); }); test('correct password must be provided to update password', function () { $user = User::factory()->create([ 'password' => Hash::make('password'), ]); $this->actingAs($user); $response = Livewire::test('pages::settings.password') ->set('current_password', 'wrong-password') ->set('password', 'new-password') ->set('password_confirmation', 'new-password') ->call('updatePassword'); $response->assertHasErrors(['current_password']); }); test('changing the password signs the user\'s other sessions out', function () { $admin = User::factory()->admin()->create(['password' => Hash::make('Old-Password-1!')]); $sessionId = signedInAdminSessionId($admin); Livewire::actingAs($admin)->test('pages::settings.password') ->set('current_password', 'Old-Password-1!') ->set('password', 'New-Password-2!xY') ->set('password_confirmation', 'New-Password-2!xY') ->call('updatePassword') ->assertHasNoErrors(); forgetRequestState(); $this->withCookie(config('session.cookie'), $sessionId) ->get(route('admin.dashboard')) ->assertRedirect(route('login')); }); test('resetting the password signs the user\'s sessions out', function () { $admin = User::factory()->admin()->create(['password' => Hash::make('Old-Password-1!')]); $sessionId = signedInAdminSessionId($admin); // The reset comes from a browser that is not signed in, without the session's cookie. $this->defaultCookies = []; $this->post(route('password.update'), [ 'token' => Password::broker()->createToken($admin), 'email' => $admin->email, 'password' => 'New-Password-3!xY', 'password_confirmation' => 'New-Password-3!xY', ])->assertSessionHasNoErrors(); forgetRequestState(); $this->withCookie(config('session.cookie'), $sessionId) ->get(route('admin.dashboard')) ->assertRedirect(route('login')); }); test('the session that changes the password stays signed in', function () { $admin = User::factory()->admin()->create(['password' => Hash::make('Old-Password-1!')]); $sessionId = signedInAdminSessionId($admin); preg_match('/wire:snapshot="([^"]+)"/', $this->withCookie(config('session.cookie'), $sessionId)->get(route('user-password.edit'))->assertOk()->getContent(), $matches); forgetRequestState(); $this->withCookie(config('session.cookie'), $sessionId) ->withCredentials() ->withHeaders(['X-Livewire' => '1']) ->postJson(app('livewire')->getUpdateUri(), [ 'components' => [[ 'snapshot' => htmlspecialchars_decode($matches[1], ENT_QUOTES), 'updates' => ['current_password' => 'Old-Password-1!', 'password' => 'New-Password-2!xY', 'password_confirmation' => 'New-Password-2!xY'], 'calls' => [['method' => 'updatePassword', 'params' => [], 'path' => '']], ]], ]) ->assertOk(); expect(Hash::check('New-Password-2!xY', $admin->refresh()->password))->toBeTrue(); forgetRequestState(); $this->withCookie(config('session.cookie'), $sessionId) ->get(route('admin.dashboard')) ->assertOk(); });