feat(authorization): refactor role management to use RoleCode enum, update migrations, seeders, and tests

This commit is contained in:
2026-07-31 08:32:00 -03:00
parent a087a511e0
commit b7e1332b8c
11 changed files with 82 additions and 151 deletions

View File

@@ -2,14 +2,10 @@
namespace Tests\Feature\Auth;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Models\Role;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Authorization\Enums\RoleCode;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use LogicException;
use Tests\TestCase;
class UserAuthorizationRelationsTest extends TestCase
@@ -28,79 +24,9 @@ class UserAuthorizationRelationsTest extends TestCase
{
$user = User::factory()->create();
$this->assertSame('user', $user->rol_codigo);
$this->assertSame(RoleCode::User->value, $user->rol_codigo);
$this->assertNull($user->tenant_codigo);
$this->assertSame('user', $user->role->codigo);
$this->assertSame(RoleCode::User->value, $user->role->codigo);
$this->assertNull($user->tenant);
}
public function test_a_tenant_admin_belongs_to_its_role_and_tenant(): void
{
$tenant = $this->createTenant();
$user = User::factory()->create([
'rol_codigo' => 'tenant_admin',
'tenant_codigo' => $tenant->codigo,
]);
$this->assertSame('tenant_admin', $user->role->codigo);
$this->assertTrue($user->tenant->is($tenant));
$this->assertTrue(
Role::query()
->where('codigo', 'tenant_admin')
->firstOrFail()
->users
->contains($user)
);
}
public function test_only_a_tenant_admin_can_have_a_tenant_code(): void
{
$tenant = $this->createTenant();
$this->expectException(LogicException::class);
User::factory()->create([
'rol_codigo' => 'user',
'tenant_codigo' => $tenant->codigo,
]);
}
public function test_a_tenant_admin_must_have_a_tenant_code(): void
{
$this->expectException(LogicException::class);
User::factory()->create([
'rol_codigo' => 'tenant_admin',
]);
}
private function createTenant(): Tenant
{
$headerLogo = $this->createAttachment('header.png');
$footerLogo = $this->createAttachment('footer.png');
return Tenant::query()->create([
'codigo' => 'acme',
'nombre' => 'Acme',
'dominio' => 'acme.test',
'primary_color' => '#111111',
'secondary_color' => '#222222',
'danger_color' => '#333333',
'success_color' => '#444444',
'header_bg_color' => '#ffffff',
'footer_bg_color' => '#ffffff',
'header_logo_id' => $headerLogo->id,
'footer_logo_id' => $footerLogo->id,
]);
}
private function createAttachment(string $filename): Attachment
{
return Attachment::query()->create([
'path' => "test/{$filename}",
'filename' => $filename,
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);
}
}