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

176 lines
5.9 KiB
PHP

<?php
namespace Tests\Feature\Tenant;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteType;
use Database\Seeders\AuthorizationSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class AdminAppWebsiteExtraControllerTest extends TestCase
{
use RefreshDatabase;
private WebsiteType $websiteType;
protected function setUp(): void
{
parent::setUp();
$this->seed(AuthorizationSeeder::class);
$this->websiteType = WebsiteType::query()->create([
'codigo' => 'test-store',
'nombre' => 'Test Store',
]);
$this->websiteType->extras()->create([
'codigo' => 'contactConfig',
'nombre' => 'Configuración de contacto',
'descripcion' => 'Datos de contacto visibles en la tienda.',
'is_required' => false,
'config_schema' => [
'request_rules' => [
'$' => 'required|array',
'phone' => 'required|string|max:30',
],
'transforms' => [],
'database_rules' => [
'$' => 'required|array',
'phone' => 'required|string|max:30',
],
],
]);
}
public function test_authentication_is_required(): void
{
$this->getJson('/api/v1/adminapp/tenant/website-extras')
->assertUnauthorized();
}
public function test_a_customer_cannot_access_adminapp_website_extras(): void
{
$customer = User::factory()->create([
'rol_codigo' => RoleCode::User->value,
'tenant_codigo' => null,
]);
Sanctum::actingAs($customer);
$this->getJson('/api/v1/adminapp/tenant/website-extras')
->assertForbidden();
}
public function test_adminapp_user_can_read_definitions_and_current_values(): void
{
$tenant = $this->createTenant('acme');
$definition = $this->websiteType->extras()->firstOrFail();
$tenant->websiteExtras()->create([
'website_type_extra_id' => $definition->id,
'config' => ['phone' => '+54 341 555 0101'],
]);
Sanctum::actingAs($this->createAdminAppUser($tenant));
$this->getJson('/api/v1/adminapp/tenant/website-extras')
->assertOk()
->assertJsonPath('data.website_type.codigo', 'test-store')
->assertJsonPath('data.definitions.contactConfig.codigo', 'contactConfig')
->assertJsonPath('data.definitions.contactConfig.nombre', 'Configuración de contacto')
->assertJsonPath('data.definitions.contactConfig.is_required', false)
->assertJsonPath('data.extras.contactConfig.phone', '+54 341 555 0101')
->assertJsonPath('data.resolved_extras.contactConfig.phone', '+54 341 555 0101');
}
public function test_adminapp_user_updates_one_extra_without_touching_other_tenants(): void
{
$tenant = $this->createTenant('acme');
$otherTenant = $this->createTenant('other');
$definition = $this->websiteType->extras()->firstOrFail();
$secondaryDefinition = $this->websiteType->extras()->create([
'codigo' => 'footerConfig',
'nombre' => 'Configuración del pie',
'descripcion' => 'Configuración adicional del pie.',
'is_required' => false,
'config_schema' => [
'request_rules' => ['$' => 'required|array'],
'transforms' => [],
'database_rules' => ['$' => 'required|array'],
],
]);
$tenant->websiteExtras()->create([
'website_type_extra_id' => $definition->id,
'config' => ['phone' => 'old'],
]);
$otherTenant->websiteExtras()->create([
'website_type_extra_id' => $definition->id,
'config' => ['phone' => 'untouched'],
]);
$tenant->websiteExtras()->create([
'website_type_extra_id' => $secondaryDefinition->id,
'config' => ['text' => 'also untouched'],
]);
Sanctum::actingAs($this->createAdminAppUser($tenant));
$this->putJson('/api/v1/adminapp/tenant/website-extras/contactConfig', [
'config' => [
'phone' => '+54 341 555 9999',
],
])
->assertOk()
->assertJsonPath('data.extras.contactConfig.phone', '+54 341 555 9999');
$this->assertSame(
['phone' => '+54 341 555 9999'],
$tenant->websiteExtras()->firstOrFail()->config
);
$this->assertSame(
['phone' => 'untouched'],
$otherTenant->websiteExtras()->firstOrFail()->config
);
$this->assertSame(
['text' => 'also untouched'],
$tenant->websiteExtras()
->where('website_type_extra_id', $secondaryDefinition->id)
->firstOrFail()
->config
);
}
public function test_update_returns_not_found_for_an_unsupported_extra_code(): void
{
$tenant = $this->createTenant('acme');
Sanctum::actingAs($this->createAdminAppUser($tenant));
$this->putJson('/api/v1/adminapp/tenant/website-extras/unknown', [
'config' => ['enabled' => true],
])
->assertNotFound();
}
private function createTenant(string $code): Tenant
{
return Tenant::query()->create([
'codigo' => $code,
'nombre' => ucfirst($code),
'dominio' => "{$code}.test",
'website_type_code' => $this->websiteType->codigo,
]);
}
private function createAdminAppUser(Tenant $tenant): User
{
return User::factory()->create([
'rol_codigo' => RoleCode::AdminApp->value,
'tenant_codigo' => $tenant->codigo,
]);
}
}