Files
shopit-back/tests/Feature/Tenant/BootstrapTenantControllerTest.php

268 lines
9.1 KiB
PHP

<?php
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
{
use RefreshDatabase;
public function test_it_bootstraps_a_tenant_by_domain(): void
{
$tenant = Tenant::create([
'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');
$response
->assertOk()
->assertJsonPath('data.codigo', 'acme')
->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'));
}
public function test_it_bootstraps_a_tenant_from_a_full_url(): void
{
Tenant::create([
'codigo' => 'acme',
'nombre' => 'Acme',
'dominio' => 'acme.com',
]);
$encodedDomain = urlencode('https://ACME.com:8080/path?foo=bar#frag');
$response = $this->getJson("/api/tenants/bootstrap/{$encodedDomain}");
$response
->assertOk()
->assertJsonPath('data.codigo', 'acme')
->assertJsonPath('data.dominio', 'acme.com');
}
public function test_it_returns_not_found_when_the_domain_does_not_exist(): void
{
$response = $this->getJson('/api/tenants/bootstrap/missing.example');
$response->assertNotFound();
}
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.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',
'nombre' => 'Globex',
'dominio' => 'acme.com',
]);
$secondResponse
->assertUnprocessable()
->assertJsonValidationErrors(['dominio']);
}
public function test_it_allows_keeping_the_same_domain_on_update_but_rejects_collisions(): void
{
$tenant = Tenant::create([
'codigo' => 'acme',
'nombre' => 'Acme',
'dominio' => 'acme.com',
]);
$otherTenant = Tenant::create([
'codigo' => 'globex',
'nombre' => 'Globex',
'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.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',
'nombre' => 'Globex',
'dominio' => 'https://ACME.com/storefront',
]);
$failingResponse
->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]);
}
}