139 lines
4.6 KiB
PHP
139 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Staff;
|
|
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Authorization\Enums\RoleCode;
|
|
use App\Domains\Catalog\Models\Category;
|
|
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 StaffControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private Tenant $tenant;
|
|
|
|
private User $admin;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->seed(AuthorizationSeeder::class);
|
|
WebsiteType::query()->create(['codigo' => 'onticket', 'nombre' => 'OnTicket']);
|
|
$this->tenant = Tenant::query()->create([
|
|
'codigo' => 'acme',
|
|
'nombre' => 'Acme',
|
|
'dominio' => 'acme.test',
|
|
'website_type_code' => 'onticket',
|
|
]);
|
|
$this->admin = User::factory()->create([
|
|
'rol_codigo' => RoleCode::AdminApp->value,
|
|
'tenant_codigo' => $this->tenant->codigo,
|
|
]);
|
|
}
|
|
|
|
public function test_adminapp_can_create_update_list_and_delete_staff_with_categories(): void
|
|
{
|
|
Sanctum::actingAs($this->admin);
|
|
$firstCategory = $this->createCategory('Bebidas');
|
|
$secondCategory = $this->createCategory('Comidas');
|
|
|
|
$response = $this->postJson('/api/v1/adminapp/tenant/staff', [
|
|
'nombre_apellido' => 'Ada Lovelace',
|
|
'dni' => '12345678',
|
|
'email' => 'ADA@example.test',
|
|
'category_ids' => [$firstCategory->id],
|
|
])->assertSuccessful()
|
|
->assertJsonPath('data.email', 'ada@example.test')
|
|
->assertJsonPath('data.role.codigo', RoleCode::Scanner->value)
|
|
->assertJsonPath('data.categories.0.id', $firstCategory->id);
|
|
|
|
$staffId = $response->json('data.id');
|
|
$this->assertDatabaseHas('category_scanners', [
|
|
'user_id' => $staffId,
|
|
'categoria_id' => $firstCategory->id,
|
|
]);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/staff?search=ada')
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data');
|
|
|
|
$this->putJson("/api/v1/adminapp/tenant/staff/{$staffId}", [
|
|
'nombre_apellido' => 'Ada Byron',
|
|
'dni' => '12345678',
|
|
'email' => 'ada@example.test',
|
|
'category_ids' => [$secondCategory->id],
|
|
])->assertOk()
|
|
->assertJsonPath('data.nombre_apellido', 'Ada Byron')
|
|
->assertJsonPath('data.categories.0.id', $secondCategory->id);
|
|
|
|
$this->assertDatabaseMissing('category_scanners', [
|
|
'user_id' => $staffId,
|
|
'categoria_id' => $firstCategory->id,
|
|
]);
|
|
|
|
$this->deleteJson("/api/v1/adminapp/tenant/staff/{$staffId}")->assertNoContent();
|
|
$this->assertDatabaseMissing('users', ['id' => $staffId]);
|
|
}
|
|
|
|
public function test_admin_cannot_assign_another_tenants_category(): void
|
|
{
|
|
Sanctum::actingAs($this->admin);
|
|
$otherTenant = Tenant::query()->create([
|
|
'codigo' => 'other',
|
|
'nombre' => 'Other',
|
|
'dominio' => 'other.test',
|
|
'website_type_code' => 'onticket',
|
|
]);
|
|
$foreignCategory = Category::query()->create([
|
|
'tenant_code' => $otherTenant->codigo,
|
|
'nombre' => 'Privada',
|
|
]);
|
|
$payload = [
|
|
'nombre_apellido' => 'Grace Hopper',
|
|
'dni' => '87654321',
|
|
'email' => 'grace@example.test',
|
|
'category_ids' => [$foreignCategory->id],
|
|
];
|
|
|
|
$this->postJson('/api/v1/adminapp/tenant/staff', $payload)
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors('category_ids');
|
|
|
|
$parent = $this->createCategory('Local');
|
|
$child = Category::query()->create([
|
|
'tenant_code' => $this->tenant->codigo,
|
|
'categoria_id' => $parent->id,
|
|
'nombre' => 'Subcategoría',
|
|
]);
|
|
$payload['category_ids'] = [$child->id];
|
|
|
|
$this->postJson('/api/v1/adminapp/tenant/staff', $payload)
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors('category_ids');
|
|
}
|
|
|
|
public function test_customer_cannot_manage_staff(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create([
|
|
'rol_codigo' => RoleCode::User->value,
|
|
]));
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/staff')->assertForbidden();
|
|
}
|
|
|
|
private function createCategory(string $name): Category
|
|
{
|
|
return Category::query()->create([
|
|
'tenant_code' => $this->tenant->codigo,
|
|
'nombre' => $name,
|
|
]);
|
|
}
|
|
}
|