Files
portal-api/tests/Feature/Api/Service/ServiceOwnershipTest.php
2026-09-27 11:42:40 +07:00

97 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Service;
use App\Enums\Order\OrderStatus;
use App\Models\Customer;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\Service;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ServiceOwnershipTest extends TestCase
{
use RefreshDatabase;
private function actingAsCustomer(Customer $customer): static
{
return $this->actingAs($customer, 'client')
->withHeaders(['x-access-token' => $this->buildClientAccessToken()]);
}
public function test_client_cannot_fetch_another_customers_service_by_uuid(): void
{
$owner = Customer::factory()->create();
$attacker = Customer::factory()->create();
$product = Product::factory()->create(['product_type' => 'hosting', 'is_active' => true]);
$order = Order::create([
'order_num' => 'ORD-' . uniqid(),
'customer_id' => $owner->id,
'sub_total' => 100000,
'total' => 100000,
'status' => OrderStatus::ACTIVE->value,
]);
$orderItem = OrderItem::create([
'order_id' => $order->id,
'product_id' => $product->id,
'billing_cycle' => 'monthly',
'months' => 1,
'quantity' => 1,
'price' => 100000,
]);
$service = Service::create([
'customer_id' => $owner->id,
'product_id' => $product->id,
'order_item_id' => $orderItem->id,
'status' => 'active',
'server_name' => 'owner-server',
]);
$response = $this->actingAsCustomer($attacker)->getJson("/v1/client/service/{$service->uuid}");
$response->assertStatus(404);
}
public function test_client_can_fetch_own_service_by_uuid(): void
{
$owner = Customer::factory()->create();
$product = Product::factory()->create(['product_type' => 'hosting', 'is_active' => true]);
$order = Order::create([
'order_num' => 'ORD-' . uniqid(),
'customer_id' => $owner->id,
'sub_total' => 100000,
'total' => 100000,
'status' => OrderStatus::ACTIVE->value,
]);
$orderItem = OrderItem::create([
'order_id' => $order->id,
'product_id' => $product->id,
'billing_cycle' => 'monthly',
'months' => 1,
'quantity' => 1,
'price' => 100000,
]);
$service = Service::create([
'customer_id' => $owner->id,
'product_id' => $product->id,
'order_item_id' => $orderItem->id,
'status' => 'active',
'server_name' => 'owner-server',
]);
$response = $this->actingAsCustomer($owner)->getJson("/v1/client/service/{$service->uuid}");
$this->assertNotSame(404, $response->status());
}
}