feat(authorization): implement WebsiteExtra management with controller, request, resource, service, routes, middleware, and tests
This commit is contained in:
155
tests/Feature/Tenant/AdminAppWebsiteExtraControllerTest.php
Normal file
155
tests/Feature/Tenant/AdminAppWebsiteExtraControllerTest.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?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([
|
||||
'nombre' => 'contactConfig',
|
||||
'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.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_replaces_only_its_tenant_extras(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$otherTenant = $this->createTenant('other');
|
||||
$definition = $this->websiteType->extras()->firstOrFail();
|
||||
|
||||
$tenant->websiteExtras()->create([
|
||||
'website_type_extra_id' => $definition->id,
|
||||
'config' => ['phone' => 'old'],
|
||||
]);
|
||||
$otherTenant->websiteExtras()->create([
|
||||
'website_type_extra_id' => $definition->id,
|
||||
'config' => ['phone' => 'untouched'],
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||
|
||||
$this->putJson('/api/v1/adminapp/tenant/website-extras', [
|
||||
'extras' => [
|
||||
'contactConfig' => [
|
||||
'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
|
||||
);
|
||||
}
|
||||
|
||||
public function test_update_rejects_extras_not_supported_by_the_website_type(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||
|
||||
$this->putJson('/api/v1/adminapp/tenant/website-extras', [
|
||||
'extras' => [
|
||||
'unknown' => ['enabled' => true],
|
||||
],
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors('extras');
|
||||
}
|
||||
|
||||
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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user