feat(authorization): add role and tenant codes to users, implement validation logic, and create tests
This commit is contained in:
106
tests/Feature/Auth/UserAuthorizationRelationsTest.php
Normal file
106
tests/Feature/Auth/UserAuthorizationRelationsTest.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
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 Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use LogicException;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserAuthorizationRelationsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_users_table_has_role_and_tenant_codes(): void
|
||||
{
|
||||
$this->assertTrue(Schema::hasColumns('users', [
|
||||
'rol_codigo',
|
||||
'tenant_codigo',
|
||||
]));
|
||||
}
|
||||
|
||||
public function test_a_new_user_has_the_user_role_and_no_tenant_by_default(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->assertSame('user', $user->rol_codigo);
|
||||
$this->assertNull($user->tenant_codigo);
|
||||
$this->assertSame('user', $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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user