From 649772f65f85fa602231c8c24a120663c519b6e5 Mon Sep 17 00:00:00 2001 From: Ovidiu U Date: Sun, 5 Apr 2026 20:32:20 +0100 Subject: [PATCH] fix: add X-Api-Key header to API feature tests and register auth routes --- routes/api.php | 12 ++++++++++++ tests/Feature/Api/AuthControllerTest.php | 4 ++++ tests/Feature/Api/PredictionControllerTest.php | 4 ++++ tests/Feature/Api/StationControllerTest.php | 4 ++++ tests/Feature/Api/StatsControllerTest.php | 4 ++++ 5 files changed, 28 insertions(+) diff --git a/routes/api.php b/routes/api.php index 01fbb93..0052f1c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,13 +1,25 @@ group(function (): void { Route::get('/stations', [StationController::class, 'index']); Route::get('/stats/searches', [StatsController::class, 'searches']); Route::get('/prediction', [PredictionController::class, 'index']); }); + +// Sanctum-authenticated endpoints +Route::middleware('auth:sanctum')->group(function (): void { + Route::get('/auth/me', [AuthController::class, 'me']); + Route::post('/auth/logout', [AuthController::class, 'logout']); +}); diff --git a/tests/Feature/Api/AuthControllerTest.php b/tests/Feature/Api/AuthControllerTest.php index 4095d35..364429c 100644 --- a/tests/Feature/Api/AuthControllerTest.php +++ b/tests/Feature/Api/AuthControllerTest.php @@ -5,6 +5,10 @@ use Illuminate\Foundation\Testing\RefreshDatabase; uses(RefreshDatabase::class); +beforeEach(function () { + $this->withHeaders(['X-Api-Key' => config('app.api_secret_key')]); +}); + it('registers a new user and returns a token', function () { $this->postJson('/api/auth/register', [ 'name' => 'Test User', diff --git a/tests/Feature/Api/PredictionControllerTest.php b/tests/Feature/Api/PredictionControllerTest.php index 95c879d..0e53820 100644 --- a/tests/Feature/Api/PredictionControllerTest.php +++ b/tests/Feature/Api/PredictionControllerTest.php @@ -7,6 +7,10 @@ use Illuminate\Foundation\Testing\RefreshDatabase; uses(RefreshDatabase::class); +beforeEach(function () { + $this->withHeaders(['X-Api-Key' => config('app.api_secret_key')]); +}); + it('returns a prediction response for diesel', function () { $this->getJson('/api/prediction?fuel_type=diesel') ->assertOk() diff --git a/tests/Feature/Api/StationControllerTest.php b/tests/Feature/Api/StationControllerTest.php index f085008..49d945e 100644 --- a/tests/Feature/Api/StationControllerTest.php +++ b/tests/Feature/Api/StationControllerTest.php @@ -8,6 +8,10 @@ use Illuminate\Support\Facades\Http; uses(RefreshDatabase::class); +beforeEach(function () { + $this->withHeaders(['X-Api-Key' => config('app.api_secret_key')]); +}); + it('returns stations near coordinates filtered by fuel type', function () { $station = Station::factory()->create(['lat' => 52.555064, 'lng' => -0.256119]); StationPriceCurrent::factory()->create([ diff --git a/tests/Feature/Api/StatsControllerTest.php b/tests/Feature/Api/StatsControllerTest.php index a11093c..7a86aac 100644 --- a/tests/Feature/Api/StatsControllerTest.php +++ b/tests/Feature/Api/StatsControllerTest.php @@ -5,6 +5,10 @@ use Illuminate\Foundation\Testing\RefreshDatabase; uses(RefreshDatabase::class); +beforeEach(function () { + $this->withHeaders(['X-Api-Key' => config('app.api_secret_key')]); +}); + it('returns search stats for current week', function () { // 10 searches within the rolling 7 days (3 unique IPs) Search::factory()->count(5)->create([