Skip to content

Commit

Permalink
[item] validate api v2 item index input
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislav-chynoransky committed Jun 19, 2024
1 parent 718a810 commit 7cf3870
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
10 changes: 7 additions & 3 deletions app/Http/Controllers/Api/V2/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ public function show($id)

public function index(Request $request)
{
$size = $request->input('size', 15);
$validated = $request->validate([
'ids' => ['array'],
'ids.*' => ['required', 'string'],
'size' => ['integer'],
]);

$items = Item::with(['images', 'authorities'])
->when(
$request->filled('ids'),
fn ($query) => $query->whereIn('id', (array) $request->input('ids'))
fn ($query) => $query->whereIn('id', $validated['ids'])
)
->paginate($size);
->paginate($validated['size'] ?? null);

return ItemResource::collection($items);
}
Expand Down
10 changes: 6 additions & 4 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
);
});

Route::prefix('v2')->group(function () {
Route::get('items/{id}', [V2ItemController::class, 'show']);
Route::get('items', [V2ItemController::class, 'index']);
});
Route::prefix('v2')
->name('api.v2.')
->group(function () {
Route::resource('items', V2ItemController::class)
->only(['index', 'show']);
});
26 changes: 19 additions & 7 deletions tests/Feature/Api/V2/ItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ItemsTest extends TestCase
use RefreshDatabase;
use WithoutSearchIndexing;

public function test_detail()
public function testShow()
{
$authority = Authority::factory()->create(['name' => 'Wouwerman, Philips']);
$item_image = ItemImage::factory()->make(['iipimg_url' => 'test_iipimg_url']);
Expand Down Expand Up @@ -81,8 +81,7 @@ public function testIndex()
$items = Item::factory()->count(3)->create();
$response = $this->getJson('/api/v2/items');

$response->assertStatus(200);

$response->assertOk();
$response->assertJsonCount(3, 'data');
foreach ($items as $item) {
$response->assertJsonFragment([
Expand All @@ -101,11 +100,13 @@ public function testIndexWithIds()
{
$items = Item::factory()->count(3)->create();
$filtered = $items->random(2);
$response = $this->getJson('/api/v2/items?ids[]=' . $filtered->pluck('id')->implode('&ids[]='));

$response->assertStatus(200);
$url = route('api.v2.items.index', [
'ids' => $filtered->pluck('id')->toArray()
]);
$response = $this->getJson($url);

$response->assertJsonCount(2, 'data');
$response->assertOk();
$response->assertJsonCount($filtered->count(), 'data');
foreach ($filtered as $item) {
$response->assertJsonFragment([
'id' => $item->id,
Expand All @@ -118,4 +119,15 @@ public function testIndexWithIds()
]);
}
}

public function testIndexWithInvalidIds()
{
$item = Item::factory()->create();
$url = route('api.v2.items.index', [
'ids' => $item->id,
]);

$response = $this->getJson($url);
$response->assertUnprocessable();
}
}

0 comments on commit 7cf3870

Please sign in to comment.