test(tenant): cover split website types and migration

This commit is contained in:
2026-09-17 14:25:54 -03:00
parent 6bc784519f
commit 486129a92f
36 changed files with 335 additions and 307 deletions

View File

@@ -14,7 +14,8 @@ use App\Domains\Integration\Services\IntegrationAssociationService;
use App\Domains\Integration\Services\IntegrationInstanceService;
use App\Domains\Integration\Services\TelepagosIntegrationService;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteType;
use App\Domains\Tenant\Models\AdminWebsiteType;
use App\Domains\Tenant\Models\StorefrontWebsiteType;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
@@ -40,19 +41,19 @@ class IntegrationInstanceTest extends TestCase
public function test_management_requires_an_authenticated_global_admin(): void
{
Client::create(['code' => 'acme', 'name' => 'Acme']);
WebsiteType::create(['codigo' => 'demo', 'nombre' => 'Demo']);
AdminWebsiteType::create(['codigo' => 'demo', 'nombre' => 'Demo']);
$this->getJson('/api/clients/acme/integrations')->assertUnauthorized();
Sanctum::actingAs(User::factory()->create());
$this->getJson('/api/clients/acme/integrations')->assertForbidden();
$this->getJson('/api/website-types/demo/integrations')->assertForbidden();
$this->getJson('/api/admin-website-types/demo/integrations')->assertForbidden();
}
public function test_only_owner_endpoints_can_manage_configuration(): void
{
$this->admin();
$client = Client::create(['code' => 'acme', 'name' => 'Acme']);
$type = WebsiteType::create(['codigo' => 'demo', 'nombre' => 'Demo']);
$type = AdminWebsiteType::create(['codigo' => 'demo', 'nombre' => 'Demo']);
$this->getJson('/api/integrations')->assertNotFound();
$this->getJson('/api/integration-instances')->assertNotFound();
@@ -60,7 +61,7 @@ class IntegrationInstanceTest extends TestCase
$this->putJson('/api/clients/acme/integrations/test', ['integration_data' => []])
->assertUnprocessable()->assertJsonValidationErrors('integration_data.api_key');
$this->putJson('/api/website-types/demo/integrations/test', ['integration_data' => []])
$this->putJson('/api/admin-website-types/demo/integrations/test', ['integration_data' => []])
->assertUnprocessable()->assertJsonValidationErrors('integration_data.api_key');
$this->putJson('/api/clients/acme/integrations/test', [
@@ -69,22 +70,22 @@ class IntegrationInstanceTest extends TestCase
$clientInstanceId = $client->integrations()->firstOrFail()->integration_instance_id;
self::assertSame('client-secret', IntegrationInstance::findOrFail($clientInstanceId)->integration_data['api_key']);
$this->putJson('/api/website-types/demo/integrations/test', [
$this->putJson('/api/admin-website-types/demo/integrations/test', [
'integration_data' => ['api_key' => 'type-secret'],
])->assertCreated()
->assertJsonPath('data.integration_instance.name', 'Test / Demo')
->assertJsonMissingPath('data.integration_instance.integration_data');
$typeInstanceId = $type->integrations()->firstOrFail()->integration_instance_id;
$this->getJson('/api/website-types/demo/integrations/test')
$this->getJson('/api/admin-website-types/demo/integrations/test')
->assertOk()->assertJsonPath('data.integration_instance_id', $typeInstanceId);
$this->deleteJson('/api/clients/acme/integrations/test')->assertNoContent();
$this->deleteJson('/api/website-types/demo/integrations/test')->assertNoContent();
$this->deleteJson('/api/admin-website-types/demo/integrations/test')->assertNoContent();
}
public function test_tenant_prefers_client_then_type_and_client_context_does_not_inherit(): void
{
$type = WebsiteType::create(['codigo' => 'demo', 'nombre' => 'Demo']);
$type = AdminWebsiteType::create(['codigo' => 'demo', 'nombre' => 'Demo']);
$tenant = $this->tenantForType($type);
$associations = new IntegrationAssociationService;
$associations->associate($type, 'test', $this->makeInstance('type'));
@@ -105,8 +106,10 @@ class IntegrationInstanceTest extends TestCase
public function test_required_configuration_can_come_from_the_website_type(): void
{
$this->integration->update(['requires_configuration' => true]);
$type = WebsiteType::create(['codigo' => 'demo', 'nombre' => 'Demo']);
$type = AdminWebsiteType::create(['codigo' => 'demo', 'nombre' => 'Demo']);
$tenant = $this->tenantForType($type);
$storefront = StorefrontWebsiteType::create(['codigo' => 'different-storefront', 'nombre' => 'Storefront']);
$tenant->update(['storefront_website_type_code' => $storefront->codigo]);
(new IntegrationAssociationService)->associate($type, 'test', $this->makeInstance('type'));
self::assertSame('type', $this->probe()->forTenant($tenant->codigo)->setting());
}
@@ -142,7 +145,7 @@ class IntegrationInstanceTest extends TestCase
public function test_an_instance_is_deleted_only_after_its_last_association_is_removed(): void
{
$client = Client::create(['code' => 'acme', 'name' => 'Acme']);
$type = WebsiteType::create(['codigo' => 'demo', 'nombre' => 'Demo']);
$type = AdminWebsiteType::create(['codigo' => 'demo', 'nombre' => 'Demo']);
$shared = $this->makeInstance('shared');
$associations = new IntegrationAssociationService;
@@ -180,7 +183,7 @@ class IntegrationInstanceTest extends TestCase
Http::assertSentCount(2);
}
private function tenantForType(WebsiteType $type): Tenant
private function tenantForType(AdminWebsiteType $type): Tenant
{
$logo = Attachment::create([
'path' => 'tenants/logo.png', 'filename' => 'logo.png',
@@ -190,7 +193,7 @@ class IntegrationInstanceTest extends TestCase
return Tenant::create([
'codigo' => 'acme', 'nombre' => 'Acme', 'dominio' => 'acme.test',
'website_type_code' => $type->codigo,
'admin_website_type_code' => $type->codigo,
'primary_color' => '#112233', 'secondary_color' => '#445566',
'danger_color' => '#ff0000', 'success_color' => '#00ff00',
'header_bg_color' => '#112233', 'footer_bg_color' => '#112233',