A free open source IT asset/license management system
Allow query string or parameter for byserial
snipef3a4f5edaa96
diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.phpindex c9983d50ea..fbeb21898a 100755--- a/app/Http/Controllers/Assets/AssetsController.php+++ b/app/Http/Controllers/Assets/AssetsController.php@@ -567,11 +567,12 @@ class AssetsController extends Controller** @since [v3.0]*/- public function getAssetBySerial(Request $request): RedirectResponse+ public function getAssetBySerial(Request $request, $serial = null): RedirectResponse{+ $serial = $serial ?: $request->input('serial');$topsearch = ($request->input('topsearch') == 'true');- if (! $asset = Asset::where('serial', '=', $request->input('serial'))->first()) {+ if (! $asset = Asset::where('serial', '=', $serial)->first()) {return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));}$this->authorize('view', $asset);diff --git a/tests/Feature/Assets/Api/AssetBySerialTest.php b/tests/Feature/Assets/Api/AssetBySerialTest.phpnew file mode 100644index 0000000000..2ecf55cf0f--- /dev/null+++ b/tests/Feature/Assets/Api/AssetBySerialTest.php@@ -0,0 +1,81 @@+<?php++namespace Tests\Feature\Assets\Api;++use App\Models\Asset;+use App\Models\User;+use Tests\TestCase;++class AssetBySerialTest extends TestCase+{+ public function test_returns_asset_by_serial()+ {+ $asset = Asset::factory()->create(['serial' => 'TEST-API-SERIAL-123']);++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.assets.show.byserial', ['any' => 'TEST-API-SERIAL-123']))+ ->assertOk()+ ->assertJsonFragment(['serial' => 'TEST-API-SERIAL-123'])+ ->assertJsonStructure(['total', 'rows']);+ }++ public function test_returns_multiple_assets_with_same_serial()+ {+ Asset::factory()->count(3)->create(['serial' => 'DUPE-SERIAL']);++ $response = $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.assets.show.byserial', ['any' => 'DUPE-SERIAL']))+ ->assertOk();++ $this->assertEquals(3, $response->json('total'));+ }++ public function test_returns_error_when_serial_not_found()+ {+ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.assets.show.byserial', ['any' => 'DOES-NOT-EXIST']))+ ->assertOk()+ ->assertJson(['status' => 'error']);+ }++ public function test_requires_permission()+ {+ Asset::factory()->create(['serial' => 'TEST-API-SERIAL-AUTH']);++ $this->actingAsForApi(User::factory()->create())+ ->getJson(route('api.assets.show.byserial', ['any' => 'TEST-API-SERIAL-AUTH']))+ ->assertForbidden();+ }++ public function test_does_not_return_deleted_assets_by_default()+ {+ $asset = Asset::factory()->create(['serial' => 'DELETED-SERIAL']);+ $asset->delete();++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.assets.show.byserial', ['any' => 'DELETED-SERIAL']))+ ->assertOk()+ ->assertJson(['status' => 'error']);+ }++ public function test_returns_deleted_assets_when_requested()+ {+ $asset = Asset::factory()->create(['serial' => 'DELETED-SERIAL-2']);+ $asset->delete();++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.assets.show.byserial', ['any' => 'DELETED-SERIAL-2']).'?deleted=true')+ ->assertOk()+ ->assertJsonFragment(['serial' => 'DELETED-SERIAL-2']);+ }++ public function test_serial_with_slashes_works_in_path()+ {+ $asset = Asset::factory()->create(['serial' => 'SN/WITH/SLASHES']);++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.assets.show.byserial', ['any' => 'SN/WITH/SLASHES']))+ ->assertOk()+ ->assertJsonFragment(['serial' => 'SN/WITH/SLASHES']);+ }+}diff --git a/tests/Feature/Assets/Ui/AssetBySerialTest.php b/tests/Feature/Assets/Ui/AssetBySerialTest.phpnew file mode 100644index 0000000000..23292df7fb--- /dev/null+++ b/tests/Feature/Assets/Ui/AssetBySerialTest.php@@ -0,0 +1,48 @@+<?php++namespace Tests\Feature\Assets\Ui;++use App\Models\Asset;+use App\Models\User;+use Tests\TestCase;++class AssetBySerialTest extends TestCase+{+ public function test_redirects_to_asset_when_serial_in_path()+ {+ $asset = Asset::factory()->create(['serial' => 'TEST-SERIAL-123']);+ $user = User::factory()->viewAssets()->create();++ $this->actingAs($user)+ ->get(route('findbyserial/hardware', ['any' => 'TEST-SERIAL-123']))+ ->assertRedirectToRoute('hardware.show', $asset->id);+ }++ public function test_redirects_to_asset_when_serial_in_query_string()+ {+ $asset = Asset::factory()->create(['serial' => 'TEST-SERIAL-456']);+ $user = User::factory()->viewAssets()->create();++ $this->actingAs($user)+ ->get(route('findbyserial/hardware').'?serial=TEST-SERIAL-456')+ ->assertRedirectToRoute('hardware.show', $asset->id);+ }++ public function test_redirects_to_index_when_serial_not_found()+ {+ $user = User::factory()->viewAssets()->create();++ $this->actingAs($user)+ ->get(route('findbyserial/hardware', ['any' => 'DOES-NOT-EXIST']))+ ->assertRedirectToRoute('hardware.index');+ }++ public function test_requires_permission()+ {+ Asset::factory()->create(['serial' => 'TEST-SERIAL-789']);++ $this->actingAs(User::factory()->create())+ ->get(route('findbyserial/hardware', ['any' => 'TEST-SERIAL-789']))+ ->assertForbidden();+ }+}