A free open source IT asset/license management system
Improve validation
Marcus Moorefa0e8f6e019a
diff --git a/app/Http/Controllers/Api/LicenseSeatsController.php b/app/Http/Controllers/Api/LicenseSeatsController.phpindex cbb078ed7c..5554b4ee95 100644--- a/app/Http/Controllers/Api/LicenseSeatsController.php+++ b/app/Http/Controllers/Api/LicenseSeatsController.php@@ -102,13 +102,25 @@ class LicenseSeatsController extends Controller'assigned_to' => ['sometimes','int',+ 'prohibits:asset_id',// must be a valid user or null to unassignfunction ($attribute, $value, $fail) {- if (!is_null($value) && !User::find($value)) {+ if (!is_null($value) && !User::where('id', $value)->whereNull('deleted_at')->exists()) {$fail('The selected assigned_to is invalid.');}},],+ 'asset_id' => [+ 'sometimes',+ 'int',+ 'prohibits:assigned_to',+ // must be a valid asset or null to unassign+ function ($attribute, $value, $fail) {+ if (!is_null($value) && !Asset::where('id', $value)->whereNull('deleted_at')->exists()) {+ $fail('The selected asset_id is invalid.');+ }+ }+ ],]);$this->authorize('checkout', License::class);diff --git a/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php b/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.phpindex b6c8729edb..27c3513295 100644--- a/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php+++ b/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php@@ -2,6 +2,7 @@namespace Tests\Feature\LicenseSeats\Api;+use App\Models\Asset;use App\Models\License;use App\Models\LicenseSeat;use App\Models\User;@@ -59,9 +60,39 @@ class LicenseSeatUpdateTest extends TestCase->assertMessagesContains('assigned_to');}+ public function test_asset_id_must_be_a_valid_asset()+ {+ $licenseSeat = LicenseSeat::factory()->create(['assigned_to' => null]);++ $softDeletedAsset = Asset::factory()->trashed()->create();++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->patchJson(+ route('api.licenses.seats.update', [$licenseSeat->license->id, $licenseSeat->id]),+ [+ 'asset_id' => $softDeletedAsset->id,+ 'notes' => '',+ ]+ )+ ->assertStatus(200)+ ->assertStatusMessageIs('error')+ ->assertMessagesContains('asset_id');+ }+public function test_assigned_to_and_asset_id_cannot_be_provided_together(){- $this->markTestIncomplete();+ $licenseSeat = LicenseSeat::factory()->create(['assigned_to' => null]);++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->patchJson($this->route($licenseSeat), [+ 'assigned_to' => User::factory()->create()->id,+ 'asset_id' => Asset::factory()->create()->id,+ 'notes' => '',+ ])+ ->assertStatus(200)+ ->assertStatusMessageIs('error')+ ->assertMessagesContains('assigned_to')+ ->assertMessagesContains('asset_id');}public function test_license_seat_can_be_updated()