feat: separate tenant domain and base path, update related models, requests, and tests

This commit is contained in:
2026-08-18 17:23:01 -03:00
parent 8e26611097
commit a8973f6171
14 changed files with 355 additions and 35 deletions

View File

@@ -0,0 +1,92 @@
<?php
namespace Tests\Feature\Migrations;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class SeparateTenantDomainAndBasePathTest extends TestCase
{
private string $originalConnection;
protected function setUp(): void
{
parent::setUp();
$this->originalConnection = DB::getDefaultConnection();
config()->set('database.connections.tenant_path_test', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'foreign_key_constraints' => true,
]);
DB::setDefaultConnection('tenant_path_test');
Schema::create('tenants', function (Blueprint $table): void {
$table->id();
$table->string('dominio');
$table->unique('dominio', 'tenants_dominio_unique');
});
}
protected function tearDown(): void
{
DB::purge('tenant_path_test');
DB::setDefaultConnection($this->originalConnection);
parent::tearDown();
}
public function test_it_splits_existing_tenant_keys_and_enforces_composite_uniqueness(): void
{
DB::table('tenants')->insert([
['dominio' => 'onticket.com.ar'],
['dominio' => 'onticket.com.ar/desfile/'],
['dominio' => 'https://ONTICKET.COM.AR/sonder'],
]);
$migration = require database_path(
'migrations/2026_08_18_060000_separate_tenant_domain_and_base_path.php'
);
$migration->up();
$this->assertDatabaseHas('tenants', [
'dominio' => 'onticket.com.ar',
'base_path' => '/',
]);
$this->assertDatabaseHas('tenants', [
'dominio' => 'onticket.com.ar',
'base_path' => '/desfile',
]);
$this->assertDatabaseHas('tenants', [
'dominio' => 'onticket.com.ar',
'base_path' => '/sonder',
]);
$this->expectException(QueryException::class);
DB::table('tenants')->insert([
'dominio' => 'onticket.com.ar',
'base_path' => '/desfile',
]);
}
public function test_it_recombines_tenant_locations_when_rolled_back(): void
{
DB::table('tenants')->insert(['dominio' => 'onticket.com.ar/desfile']);
$migration = require database_path(
'migrations/2026_08_18_060000_separate_tenant_domain_and_base_path.php'
);
$migration->up();
$migration->down();
$this->assertFalse(Schema::hasColumn('tenants', 'base_path'));
$this->assertDatabaseHas('tenants', [
'dominio' => 'onticket.com.ar/desfile',
]);
}
}

View File

@@ -142,23 +142,27 @@ class BootstrapTenantControllerTest extends TestCase
$this->createTenant([
'codigo' => 'pura-tendencia',
'nombre' => 'Pura Tendencia',
'dominio' => 'qa.onticket.com.ar/puratendencia',
'dominio' => 'qa.onticket.com.ar',
'base_path' => '/puratendencia',
]);
$this->createTenant([
'codigo' => 'pura-tendencia-vip',
'nombre' => 'Pura Tendencia VIP',
'dominio' => 'qa.onticket.com.ar/puratendencia/vip',
'dominio' => 'qa.onticket.com.ar',
'base_path' => '/puratendencia/vip',
]);
$this->getJson('/api/tenants/bootstrap?dominio=qa.onticket.com.ar&path=%2Fpuratendencia%2Fproductos%2F123')
->assertOk()
->assertJsonPath('data.codigo', 'pura-tendencia')
->assertJsonPath('data.dominio', 'qa.onticket.com.ar/puratendencia');
->assertJsonPath('data.dominio', 'qa.onticket.com.ar')
->assertJsonPath('data.base_path', '/puratendencia');
$this->getJson('/api/tenants/bootstrap?dominio=qa.onticket.com.ar&path=%2Fpuratendencia%2Fvip%2Fproductos%2F123')
->assertOk()
->assertJsonPath('data.codigo', 'pura-tendencia-vip')
->assertJsonPath('data.dominio', 'qa.onticket.com.ar/puratendencia/vip');
->assertJsonPath('data.dominio', 'qa.onticket.com.ar')
->assertJsonPath('data.base_path', '/puratendencia/vip');
}
public function test_it_uses_the_root_tenant_for_spa_paths_without_a_tenant_prefix(): void
@@ -171,7 +175,8 @@ class BootstrapTenantControllerTest extends TestCase
$this->createTenant([
'codigo' => 'sonder',
'nombre' => 'Sonder',
'dominio' => 'qa.onticket.com.ar/sonder',
'dominio' => 'qa.onticket.com.ar',
'base_path' => '/sonder',
]);
$this->getJson('/api/tenants/bootstrap?dominio=qa.onticket.com.ar&path=%2Fproducto%2F123')
@@ -574,7 +579,8 @@ class BootstrapTenantControllerTest extends TestCase
$firstResponse
->assertCreated()
->assertJsonPath('data.dominio', 'acme.com/puratendencia')
->assertJsonPath('data.dominio', 'acme.com')
->assertJsonPath('data.base_path', '/puratendencia')
->assertJsonPath('data.primary_color', '#111111')
->assertJsonPath('data.secondary_color', '#222222')
->assertJsonPath('data.danger_color', '#333333')
@@ -613,7 +619,8 @@ class BootstrapTenantControllerTest extends TestCase
$differentPathResponse = $this->postJson('/api/tenants', [
'codigo' => 'pura-tendencia',
'nombre' => 'Pura Tendencia',
'dominio' => 'acme.com/sonder',
'dominio' => 'acme.com',
'base_path' => '/sonder/',
'primary_color' => '#111111',
'secondary_color' => '#222222',
'danger_color' => '#333333',
@@ -626,12 +633,14 @@ class BootstrapTenantControllerTest extends TestCase
$differentPathResponse
->assertCreated()
->assertJsonPath('data.dominio', 'acme.com/sonder');
->assertJsonPath('data.dominio', 'acme.com')
->assertJsonPath('data.base_path', '/sonder');
$secondResponse = $this->postJson('/api/tenants', [
'codigo' => 'globex',
'nombre' => 'Globex',
'dominio' => 'acme.com/puratendencia',
'dominio' => 'acme.com',
'base_path' => '/puratendencia',
'primary_color' => '#111111',
'secondary_color' => '#222222',
'danger_color' => '#333333',

View File

@@ -73,4 +73,14 @@ class TenantDomainNormalizerTest extends TestCase
'/sonder-shop/productos',
));
}
public function test_it_builds_base_path_candidates_from_the_longest_path_to_root(): void
{
$this->assertSame([
'/desfile/productos/123',
'/desfile/productos',
'/desfile',
'/',
], TenantDomainNormalizer::basePathCandidates('/desfile/productos/123?ref=home'));
}
}