feat: implement tenant management system including database schema, CRUD services, and bootstrap functionality

This commit is contained in:
2026-06-26 12:16:50 -03:00
parent 0dae985230
commit 1f9d876bc3
8 changed files with 394 additions and 5 deletions

View File

@@ -4,6 +4,9 @@ namespace Tests\Feature\Tenant;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Tests\TestCase;
class BootstrapTenantControllerTest extends TestCase
@@ -16,6 +19,12 @@ class BootstrapTenantControllerTest extends TestCase
'codigo' => 'acme',
'nombre' => 'Acme',
'dominio' => 'acme.com',
'primary_color' => '#ff0000',
'secondary_color' => '#00ff00',
'danger_color' => '#0000ff',
'header_footer_bg_color' => '#ffffff',
'header_logo' => 'logo_header.png',
'footer_logo' => 'logo_footer.png',
]);
$response = $this->getJson('/api/tenants/bootstrap/acme.com');
@@ -23,7 +32,13 @@ class BootstrapTenantControllerTest extends TestCase
$response
->assertOk()
->assertJsonPath('data.codigo', 'acme')
->assertJsonPath('data.dominio', 'acme.com');
->assertJsonPath('data.dominio', 'acme.com')
->assertJsonPath('data.primary_color', '#ff0000')
->assertJsonPath('data.secondary_color', '#00ff00')
->assertJsonPath('data.danger_color', '#0000ff')
->assertJsonPath('data.header_footer_bg_color', '#ffffff')
->assertJsonPath('data.header_logo', 'logo_header.png')
->assertJsonPath('data.footer_logo', 'logo_footer.png');
$this->assertArrayNotHasKey('props', $response->json('data'));
}
@@ -55,15 +70,43 @@ class BootstrapTenantControllerTest extends TestCase
public function test_it_rejects_duplicate_domains_after_normalization_when_storing(): void
{
$base64Image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
$firstResponse = $this->postJson('/api/tenants', [
'codigo' => 'acme',
'nombre' => 'Acme',
'dominio' => 'https://ACME.com/path',
'primary_color' => '#111111',
'secondary_color' => '#222222',
'danger_color' => '#333333',
'header_footer_bg_color' => '#444444',
'header_logo' => $base64Image,
'footer_logo' => $base64Image,
]);
$firstResponse
->assertCreated()
->assertJsonPath('data.dominio', 'acme.com');
->assertJsonPath('data.dominio', 'acme.com')
->assertJsonPath('data.primary_color', '#111111')
->assertJsonPath('data.secondary_color', '#222222')
->assertJsonPath('data.danger_color', '#333333')
->assertJsonPath('data.header_footer_bg_color', '#444444');
$headerUuid = $firstResponse->json('data.header_logo');
$footerUuid = $firstResponse->json('data.footer_logo');
$this->assertTrue(Str::isUuid($headerUuid));
$this->assertTrue(Str::isUuid($footerUuid));
$this->assertDatabaseHas('tenants', [
'codigo' => 'acme',
'primary_color' => '#111111',
'secondary_color' => '#222222',
'danger_color' => '#333333',
'header_footer_bg_color' => '#444444',
'header_logo' => $headerUuid,
'footer_logo' => $footerUuid,
]);
$secondResponse = $this->postJson('/api/tenants', [
'codigo' => 'globex',
@@ -90,16 +133,41 @@ class BootstrapTenantControllerTest extends TestCase
'dominio' => 'globex.com',
]);
$hdrUuid = (string) Str::uuid();
$ftrUuid = (string) Str::uuid();
$successfulResponse = $this->putJson("/api/tenants/{$tenant->id}", [
'codigo' => 'acme',
'nombre' => 'Acme Updated',
'dominio' => 'https://ACME.com:443/admin',
'primary_color' => '#555555',
'secondary_color' => '#666666',
'danger_color' => '#777777',
'header_footer_bg_color' => '#888888',
'header_logo' => $hdrUuid,
'footer_logo' => $ftrUuid,
]);
$successfulResponse
->assertOk()
->assertJsonPath('data.nombre', 'Acme Updated')
->assertJsonPath('data.dominio', 'acme.com');
->assertJsonPath('data.dominio', 'acme.com')
->assertJsonPath('data.primary_color', '#555555')
->assertJsonPath('data.secondary_color', '#666666')
->assertJsonPath('data.danger_color', '#777777')
->assertJsonPath('data.header_footer_bg_color', '#888888')
->assertJsonPath('data.header_logo', $hdrUuid)
->assertJsonPath('data.footer_logo', $ftrUuid);
$this->assertDatabaseHas('tenants', [
'id' => $tenant->id,
'primary_color' => '#555555',
'secondary_color' => '#666666',
'danger_color' => '#777777',
'header_footer_bg_color' => '#888888',
'header_logo' => $hdrUuid,
'footer_logo' => $ftrUuid,
]);
$failingResponse = $this->putJson("/api/tenants/{$otherTenant->id}", [
'codigo' => 'globex',
@@ -111,4 +179,89 @@ class BootstrapTenantControllerTest extends TestCase
->assertUnprocessable()
->assertJsonValidationErrors(['dominio']);
}
public function test_it_validates_aesthetic_colors(): void
{
$response = $this->postJson('/api/tenants', [
'codigo' => 'acme',
'nombre' => 'Acme',
'primary_color' => 'invalid-color',
]);
$response->assertJsonValidationErrors(['primary_color']);
$response2 = $this->postJson('/api/tenants', [
'codigo' => 'acme',
'nombre' => 'Acme',
'primary_color' => '#12345',
]);
$response2->assertJsonValidationErrors(['primary_color']);
}
public function test_it_validates_logo_must_be_image_or_svg(): void
{
Storage::fake('s3');
$response = $this->postJson('/api/tenants', [
'codigo' => 'acme',
'nombre' => 'Acme',
'header_logo' => UploadedFile::fake()->create('document.pdf', 10, 'application/pdf'),
]);
$response->assertJsonValidationErrors(['header_logo']);
$response2 = $this->postJson('/api/tenants', [
'codigo' => 'acme',
'nombre' => 'Acme',
'header_logo' => 'data:application/pdf;base64,JVBERi0xLjQKJdcfqksKMSAwIG9iagogIDw8IC9UeXBlIC9DYXRhbG9nCiAgICAvUGFnZXMgMiAwIFI...',
]);
$response2->assertJsonValidationErrors(['header_logo']);
}
public function test_it_stores_uploaded_file_logos_in_tenants_directory(): void
{
Storage::fake('s3');
$header = UploadedFile::fake()->image('header.png');
$footer = UploadedFile::fake()->image('footer.svg', 100, 100);
$response = $this->postJson('/api/tenants', [
'codigo' => 'acme',
'nombre' => 'Acme',
'header_logo' => $header,
'footer_logo' => $footer,
]);
$response->assertCreated();
$headerUuid = $response->json('data.header_logo');
$footerUuid = $response->json('data.footer_logo');
$this->assertTrue(Str::isUuid($headerUuid));
$this->assertTrue(Str::isUuid($footerUuid));
$this->assertDatabaseHas('attachments', ['key' => $headerUuid]);
$this->assertDatabaseHas('attachments', ['key' => $footerUuid]);
}
public function test_it_stores_base64_logos_in_tenants_directory(): void
{
Storage::fake('s3');
$base64Image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
$response = $this->postJson('/api/tenants', [
'codigo' => 'acme',
'nombre' => 'Acme',
'header_logo' => $base64Image,
]);
$response->assertCreated();
$headerUuid = $response->json('data.header_logo');
$this->assertTrue(Str::isUuid($headerUuid));
$this->assertDatabaseHas('attachments', ['key' => $headerUuid]);
}
}