auth tests
This commit is contained in:
66
backend/tests/Concerns/CreatesUsers.php
Normal file
66
backend/tests/Concerns/CreatesUsers.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Concerns;
|
||||
|
||||
use App\Models\Lists\UserRole;
|
||||
use App\Models\User;
|
||||
|
||||
/**
|
||||
* Helper per costruire utenti nei vari stati del setup, senza ripetere ovunque
|
||||
* lo stesso array di attributi. La factory di default crea un utente verificato
|
||||
* ma con must_change_password=true (default di colonna) → setup incompleto: qui
|
||||
* forniamo scorciatoie per gli stati che servono ai test (operativo, admin,
|
||||
* di sistema, setup incompleto, non verificato).
|
||||
*/
|
||||
trait CreatesUsers
|
||||
{
|
||||
/** Ruolo applicativo, riusato se già presente (idempotente in-test). */
|
||||
protected function role(string $name): UserRole
|
||||
{
|
||||
return UserRole::firstOrCreate(['name' => $name], ['description' => $name.' role']);
|
||||
}
|
||||
|
||||
/** Utente pienamente operativo: verificato, setup completo, ruolo User. */
|
||||
protected function operationalUser(array $overrides = []): User
|
||||
{
|
||||
return User::factory()->create(array_merge([
|
||||
'role_id' => $this->role(UserRole::USER)->id,
|
||||
'email_verified_at' => now(),
|
||||
'must_change_password' => false,
|
||||
'two_factor_setup_completed_at' => now(),
|
||||
], $overrides));
|
||||
}
|
||||
|
||||
/** Operativo con ruolo Admin → supera tier.admin / IsAdmin. */
|
||||
protected function adminUser(array $overrides = []): User
|
||||
{
|
||||
return $this->operationalUser(array_merge([
|
||||
'role_id' => $this->role(UserRole::ADMIN)->id,
|
||||
], $overrides));
|
||||
}
|
||||
|
||||
/** Utente di sistema (destinatario dei contenuti riassegnati, non eliminabile). */
|
||||
protected function systemUser(array $overrides = []): User
|
||||
{
|
||||
return $this->operationalUser(array_merge([
|
||||
'is_system' => true,
|
||||
'role_id' => $this->role(UserRole::ADMIN)->id,
|
||||
], $overrides));
|
||||
}
|
||||
|
||||
/** Autenticato ma setup incompleto (deve cambiare password). */
|
||||
protected function setupIncompleteUser(array $overrides = []): User
|
||||
{
|
||||
return $this->operationalUser(array_merge([
|
||||
'must_change_password' => true,
|
||||
], $overrides));
|
||||
}
|
||||
|
||||
/** Setup completo ma email non verificata → blocco di `verified`. */
|
||||
protected function unverifiedUser(array $overrides = []): User
|
||||
{
|
||||
return $this->operationalUser(array_merge([
|
||||
'email_verified_at' => null,
|
||||
], $overrides));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user