withoutVite(); $this->seed(RolesAndPermissionsSeeder::class); Setting::firstOrCreate([], ['congregazione_nome' => 'Congregazione di prova']); } private function utente(bool $obbligato = false): User { $utente = User::create([ 'name' => 'Mario Rossi', 'email' => 'mario@example.test', 'password' => 'vecchia-password', ]); $utente->forceFill(['password_change_required' => $obbligato])->save(); $utente->assignRole('operatore'); return $utente; } public function test_chi_cambia_di_sua_iniziativa_deve_dare_la_password_attuale(): void { $utente = $this->utente(); $this->actingAs($utente) ->post(route('password.change.store'), [ 'password' => 'nuova-password-1', 'password_confirmation' => 'nuova-password-1', ]) ->assertSessionHasErrors('current_password'); $this->actingAs($utente) ->post(route('password.change.store'), [ 'current_password' => 'sbagliata', 'password' => 'nuova-password-1', 'password_confirmation' => 'nuova-password-1', ]) ->assertSessionHasErrors('current_password'); $this->assertTrue(Hash::check('vecchia-password', $utente->fresh()->password)); $this->actingAs($utente) ->post(route('password.change.store'), [ 'current_password' => 'vecchia-password', 'password' => 'nuova-password-1', 'password_confirmation' => 'nuova-password-1', ]) ->assertRedirect(route('dashboard')); $this->assertTrue(Hash::check('nuova-password-1', $utente->fresh()->password)); } public function test_dopo_un_ripristino_la_password_attuale_non_si_chiede(): void { $utente = $this->utente(obbligato: true); $this->actingAs($utente) ->get(route('password.change')) ->assertOk() ->assertDontSee('name="current_password"', false); $this->actingAs($utente) ->post(route('password.change.store'), [ 'password' => 'nuova-password-1', 'password_confirmation' => 'nuova-password-1', ]) ->assertRedirect(route('dashboard')); $this->assertFalse((bool) $utente->fresh()->password_change_required); } public function test_una_sessione_aperta_altrove_si_chiude_quando_cambia_la_password(): void { $utente = $this->utente(); $hashVecchio = $utente->password; // La sessione "altrove" ha registrato l'impronta della password di allora. $utente->update(['password' => 'cambiata-da-un-altra-parte']); $this->actingAs($utente) ->withSession(['password_hash_web' => $hashVecchio]) ->get(route('dashboard')) ->assertRedirect(route('login')); $this->assertGuest(); } public function test_la_sessione_che_cambia_la_password_resta_aperta(): void { $utente = $this->utente(); $this->actingAs($utente) ->withSession(['password_hash_web' => $utente->password]) ->post(route('password.change.store'), [ 'current_password' => 'vecchia-password', 'password' => 'nuova-password-1', 'password_confirmation' => 'nuova-password-1', ]) ->assertRedirect(route('dashboard')); $this->get(route('dashboard'))->assertOk(); } }