feat(scanner): implement category validation for AdminApp role and update tests

This commit is contained in:
2026-09-04 10:40:46 -03:00
parent 7acef66ee7
commit ffa3f10b18
2 changed files with 49 additions and 3 deletions

View File

@@ -52,7 +52,7 @@ class ScannerTicketControllerTest extends TestCase
$this->getJson('/api/v1/scanner/tickets')->assertUnauthorized();
Sanctum::actingAs(User::factory()->create([
'rol_codigo' => RoleCode::AdminApp->value,
'rol_codigo' => RoleCode::User->value,
'tenant_codigo' => $this->tenant->codigo,
]));
@@ -300,6 +300,45 @@ class ScannerTicketControllerTest extends TestCase
->assertNotFound();
}
public function test_adminapp_can_read_and_scan_all_tenant_categories_without_assignments(): void
{
$this->tenant->update(['scanner_category_validation_enabled' => true]);
$admin = User::factory()->create([
'rol_codigo' => RoleCode::AdminApp->value,
'tenant_codigo' => $this->tenant->codigo,
]);
$this->assertCount(0, $admin->scanCategories);
$otherCategory = Category::query()->create([
'tenant_code' => $this->tenant->codigo, 'nombre' => 'Comidas',
]);
Sanctum::actingAs($admin);
foreach ([$this->category, $otherCategory] as $category) {
$ticket = $this->createTicket((string) Str::uuid(), [], $category);
$this->getJson("/api/v1/scanner/tickets/{$ticket->ticket}")->assertOk();
$this->postJson("/api/v1/scanner/tickets/{$ticket->ticket}/scan")
->assertOk()->assertJsonPath('data.scanner_user_id', $admin->id);
$this->postJson("/api/v1/scanner/tickets/{$ticket->ticket}/scan")
->assertUnprocessable()->assertJsonValidationErrors('ticket');
}
}
public function test_adminapp_cannot_read_or_scan_another_tenants_ticket(): void
{
$admin = User::factory()->create([
'rol_codigo' => RoleCode::AdminApp->value,
'tenant_codigo' => $this->tenant->codigo,
]);
$foreignTenant = $this->createTenant('foreign');
$foreignCategory = Category::query()->create([
'tenant_code' => $foreignTenant->codigo, 'nombre' => 'Externas',
]);
$ticket = $this->createTicket((string) Str::uuid(), [], $foreignCategory, $foreignTenant);
Sanctum::actingAs($admin);
$this->getJson("/api/v1/scanner/tickets/{$ticket->ticket}")->assertNotFound();
$this->postJson("/api/v1/scanner/tickets/{$ticket->ticket}/scan")->assertNotFound();
$this->assertNull($ticket->fresh()->used_at);
}
/** @param array<string, mixed> $attributes */
private function createTicket(
string $uuid,