Files
Verde-Web/tests/Feature/Api/V1/Geo/FetchBoundaryTest.php

87 lines
2.5 KiB
PHP

<?php
namespace Tests\Feature\Api\V1\Geo;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class FetchBoundaryTest extends TestCase
{
use RefreshDatabase;
private User $admin;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->admin = User::factory()->create([
'role' => User::ROLE_ADMIN,
'status' => User::STATUS_ACTIVE,
]);
}
public function test_authenticated_admin_can_fetch_boundary_via_nominatim_proxy(): void
{
Sanctum::actingAs($this->admin);
// Mock Nominatim API response
Http::fake([
'https://nominatim.openstreetmap.org/search*' => Http::response([
[
'display_name' => 'Baesa, Quezon City, Metro Manila, Philippines',
'geojson' => [
'type' => 'Polygon',
'coordinates' => [
[
[121.015, 14.675],
[121.025, 14.675],
[121.025, 14.685],
[121.015, 14.685],
[121.015, 14.675],
]
]
]
]
], 200)
]);
$response = $this->getJson('/api/v1/geo/fetch-boundary?q=Baesa');
$response->assertStatus(200)
->assertJsonStructure([
'success',
'data' => [
'*' => [
'display_name',
'geojson',
]
]
]);
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Baesa, Quezon City, Metro Manila, Philippines', $response->json('data.0.display_name'));
}
public function test_unauthenticated_user_cannot_fetch_boundary(): void
{
$response = $this->getJson('/api/v1/geo/fetch-boundary?q=Baesa');
$response->assertStatus(401);
}
public function test_fetch_boundary_requires_query_parameter(): void
{
Sanctum::actingAs($this->admin);
$response = $this->getJson('/api/v1/geo/fetch-boundary');
$response->assertStatus(422)
->assertJsonValidationErrors(['q']);
}
}